Skip to content

Commit 4534141

Browse files
JoostVoskuilcmendible
authored andcommitted
Add Typescript exercise (copy of node)
1 parent 1ea1239 commit 4534141

File tree

11 files changed

+931
-2
lines changed

11 files changed

+931
-2
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"ms-dotnettools.csdevkit",
3535
"vscjava.vscode-java-pack",
3636
"ms-toolsai.jupyter",
37-
"ms-python.python"
37+
"ms-python.python",
38+
"ms-vscode.vscode-typescript-next"
3839
]
3940
}
4041
},

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Run:
8888
## Labs instructions
8989

9090
- [Node Server](./exercisefiles/node/README.md)
91+
- [Node Server Typescript](./exercisefiles/node_typescript/README.md)
9192
- [.NET Web API](./exercisefiles/dotnet/README.md)
9293
- [Java Spring Boot](./exercisefiles/springboot/README.md)
9394
- [Java Quarkus](./exercisefiles/quarkus/README.md)

exercisefiles/dotnet/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ With the previous exercises you have gone through some common activities that de
159159
- Create documentation
160160
- Create tests
161161

162-
However, there are many other things that Copilot can helkp you with. Feel free to explore other slash command in the Copilot chat like:
162+
However, there are many other things that Copilot can help you with. Feel free to explore other slash command in the Copilot chat like:
163163
- `/fix`: to fix the problems in your code
164164
- `/explain`: for Copilot to explain you what the code does
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Activate GitHub Copilot using Nodejs and Typescript
2+
3+
Demo project for running labs to evaluate Copilot viability
4+
5+
> Make sure GitHub Copilot is configure and enabled for the current language, just check the status bar on the bottom right corner of VS Code.
6+
7+
## Instructions
8+
9+
- Go to the exercisefile folder
10+
- Open `nodeserver.ts` and begin by writing a Nodejs server, check the first suggestions based on the initial text
11+
- Open `test.ts` file and analyze the current test
12+
- Use `tsc` to compile
13+
- Open a command prompt and run the test (`mocha test.js`)
14+
- See the result, it should display something like:
15+
16+
``` bash
17+
mocha test.js
18+
server is listening on port 3000
19+
20+
Node Server
21+
22+
√ should return "key not passed" if key is not passed
23+
24+
1 passing (34ms)
25+
26+
```
27+
28+
### Exercise 1: Introduction
29+
30+
31+
- In the NodeServer.ts develop the rest of the methods described in the Exercise described in the section below (do not forget to open color.json file in Visual Studio Code, so CoPilot get all the context to make better recommendations)
32+
- In the Test.ts file add the methods to test the functionality
33+
- Run the tests to verify that all is working
34+
- Open the dockerfile file, and fill it, in order to create a docker container with a node image that can run the web server
35+
- Create command to run docker in port 4000
36+
- Test that the application is working in port 4000
37+
- In the **nodeserver.ts** file, you can type a new line like //run a curl command to test the server
38+
39+
So we can see how Copilot based on the current file produces a curl command, to be executed in command line
40+
- Also you can be more specific like: //run a curl command to test the daysBetweenDates method
41+
42+
So it generates a test for a specific method
43+
44+
### Exercise 2: Building new functionlities
45+
46+
The exercise consist of building a web server using Nodejs that serves the request of various functionality.
47+
48+
The requests that the server must attend are the following:
49+
50+
- **/Get** :
51+
52+
* Return a hello world message
53+
54+
55+
- **/DaysBetweenDates**:
56+
57+
* Calculate days between two dates
58+
* receive by query string 2 parameters date1 and date 2, and calculate the days between those two dates.
59+
60+
> **_NOTE:_** Use above information inside the Copilot inline feature in the `nodeserver.js` file. Press enter and wait for Copilot to suggest you the code.
61+
62+
63+
- **/Validatephonenumber**:
64+
65+
* Receive by querystring a parameter called phoneNumber
66+
* validate phoneNumber with Spanish format, for example +34666777888
67+
* if phoneNumber is valid return "valid"
68+
* if phoneNumber is not valid return "invalid"
69+
70+
> **_NOTE:_** Use above information inside the Copilot inline feature in the `nodeserver.js` file. Press enter and wait for Copilot to suggest you the code.
71+
72+
73+
- **/ValidateSpanishDNI**:
74+
75+
* Receive by querystring a parameter called dni
76+
* calculate DNI letter
77+
* if DNI is valid return "valid"
78+
* if DNI is not valid return "invalid"
79+
80+
> NOTE: Use above information inside a comment in the `nodeserver.js` file. In this case, you may want to see multiple solutions from Copilot to pick the one that best fits the way to calculate the letter. In order to see the firs 10 suggestions from Copilot press ctrl + enter.
81+
82+
83+
- **/ReturnColorCode**:
84+
85+
* Receive by querystring a parameter called color
86+
* read colors.json file and return the rgba field
87+
* get color var from querystring
88+
* iterate for each color in colors.json to find the color
89+
* return the code.hex field
90+
91+
> NOTE: Lets try Copilot chat now. Paste the above information and make it as detailed as possible in the Copilot chat text box. Copilot will use by default the open file as context in order to generate the suggestion.
92+
93+
- **/TellMeAJoke**:
94+
95+
* Make a call to the joke api and return a random joke using axios (https://official-joke-api.appspot.com/random_joke)
96+
97+
98+
- **/MoviesByDirector**:
99+
100+
* Receive by querystring a parameter called director
101+
* Make a call to the movie api and return a list of movies of that director using axios
102+
* Return the full list of movies
103+
104+
> **_NOTE:_** This will require to browse to https://www.omdbapi.com/apikey.aspx and request a FREE API Key
105+
106+
107+
- **/ParseUrl**:
108+
109+
* Retrieves a parameter from querystring called someurl
110+
* Parse the url and return the protocol, host, port, path, querystring and hash
111+
* Return the parsed host
112+
113+
- **/ListFiles**:
114+
115+
* Get the current directory
116+
* Get the list of files in the current directory
117+
* Return the list of files
118+
119+
> **_NOTE:_** Copilot can also help with these kind of commands locally. The feature is called Copilot in the CLI. You can learn more information about this feature [here](https://docs.github.com/en/copilot/github-copilot-in-the-cli/about-github-copilot-in-the-cli).
120+
121+
122+
- **/GetFullTextFile**:
123+
124+
* Read `sample.txt`` and return lines that contains the word "Fusce"
125+
126+
> **_NOTE:_** Becareful with this implementation, since this normally reads the full content of the file before analizing it, so memory usage is high and may fail when files are too big.
127+
>
128+
> You can use Copilot Code completion or inline chat. Once done you can also use Copilot Inline Chat to refactor the code to put this logic in a function.
129+
130+
- **/GetLineByLinefromtTextFile**:
131+
132+
* Read `sample.txt` line by line
133+
* Create a promise to read the file line by line, and return a list of lines that contains the word "Fusce"
134+
* Return the list of lines
135+
136+
> **_NOTE:_** You can use Copilot Code completion or inline chat. Once done you can also use Copilot Inline Chat to refactor the code to put this logic in a function.
137+
138+
- **/CalculateMemoryConsumption**:
139+
140+
* Return the memory consumption of the process in GB, rounded to 2 decimals
141+
142+
143+
- **/RandomEuropeanCountry**:
144+
145+
* Make an array of european countries and its iso codes
146+
* Return a random country from the array
147+
* Return the country and its iso code
148+
149+
> **_NOTE:_** Copilot can help you to generate data sets.
150+
151+
### Excercise 2: Document the code
152+
153+
Documenting code is always a boring and painful task. However, we can use Copilot to document it for us. In the chat, ask Copilot to document the `nodeserver.js` file.
154+
155+
### Exercise 4: Building tests
156+
157+
We will create automated tests to check that the functionality of the previous endpoints is correctly implemented. The tests should be together in the `test.ts` file.
158+
159+
You can leverage Copilot to run the tests. There is a `/tests` command that you can directly run from Copilot Chat or by selecting the piece of code you want to create tests for and using the Copilot inline feature.
160+
161+
### Exercise 5: Create a Dockerfile
162+
163+
Now that we have the new functionality added and tests covering it, lets create a Dockerfile for the Node JS Application.
164+
165+
- Build the image using Copilot and expose the port 3000.
166+
167+
## Summary
168+
169+
With the previous exercises you have gone through some common activities that developers usually run:
170+
- Create new features in the code
171+
- Work with external APIs
172+
- Create documentation
173+
- Create tests
174+
175+
However, there are many other things that Copilot can helkp you with. Feel free to explore other slash command in the Copilot chat like:
176+
- `/fix`: to fix the problems in your code
177+
- `/explain`: for Copilot to explain you what the code does
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[
2+
{
3+
"color": "black",
4+
"category": "hue",
5+
"type": "primary",
6+
"code": {
7+
"rgba": [0,0,0,1],
8+
"hex": "#000000"
9+
}
10+
},
11+
{
12+
"color": "white",
13+
"category": "value",
14+
"code": {
15+
"rgba": [255,255,255,1],
16+
"hex": "#FFFFFF"
17+
}
18+
},
19+
{
20+
"color": "red",
21+
"category": "hue",
22+
"type": "primary",
23+
"code": {
24+
"rgba": [255,0,0,1],
25+
"hex": "#FF0000"
26+
}
27+
},
28+
{
29+
"color": "blue",
30+
"category": "hue",
31+
"type": "primary",
32+
"code": {
33+
"rgba": [0,0,255,1],
34+
"hex": "#0000FF"
35+
}
36+
},
37+
{
38+
"color": "yellow",
39+
"category": "hue",
40+
"type": "primary",
41+
"code": {
42+
"rgba": [255,255,0,1],
43+
"hex": "#FFFF00"
44+
}
45+
},
46+
{
47+
"color": "green",
48+
"category": "hue",
49+
"type": "secondary",
50+
"code": {
51+
"rgba": [0,255,0,1],
52+
"hex": "#00FF00"
53+
}
54+
}
55+
]
56+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Create a dockerfile with node image
2+
3+
# Create a directory to hold the application code inside the image, this will be the working directory for your application
4+
5+
# Set the working directory to /usr/src/app
6+
7+
# Copy package.json and package-lock.json to the working directory
8+
9+
# Install npm
10+
11+
# Copy the current directory contents into the container at /usr/src/app
12+
13+
# Make port 3000 available to the world outside this container
14+
15+
# Run Nodeserver.js when the container launches
16+
17+
# Write a docker comand to build the image and tag it as mynodeapp
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// write a nodejs server that will expose a method call "get" that will return the value of the key passed in the query string
2+
// example: http://localhost:3000/get?key=hello
3+
// if the key is not passed, return "key not passed"
4+
// if the key is passed, return "hello" + key
5+
// if the url has other methods, return "method not supported"
6+
// when server is listening, log "server is listening on port 3000"
7+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "nodeserver",
3+
"version": "1.0.0",
4+
"main": "nodeserver.js",
5+
"scripts": {
6+
"build": "tsc",
7+
"start": "node nodeserver.js",
8+
"test": "mocha test.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"description": "",
14+
"dependencies": {
15+
"typescript": "^5.6.3"
16+
},
17+
"devDependencies": {
18+
"@types/assert": "^1.5.11",
19+
"@types/node": "^22.8.6"
20+
}
21+
}

0 commit comments

Comments
 (0)