Skip to content

Commit e6bb955

Browse files
authored
Typescript hello world (#30)
* docs: add typescript example * docs: updated the project README to link typescript-with-node example * fix: add type in handler --------- Co-authored-by: Emilie BOUIN <ebouin@scaleway.com>
1 parent de587e4 commit e6bb955

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ Table of Contents:
3737
| **[Go MultiPart Upload to S3](functions/go-upload-file-s3-multipart)** <br/> A function to upload file from form-data to S3. | go119 | [Serverless Framework] |
3838
| **[Image Transform](functions/image-transform-node/README.md)** <br/> A function that resizes images from an S3 bucket. | node16 | [Serverless Framework] |
3939
| **[Node MultiPart Upload to S3](functions/node-upload-file-s3-multipart/README.md)** <br/> A function to upload file from form-data to S3. | node19 | [Serverless Framework] |
40+
| **[PostgeSQL Node](functions/postgre-sql-node/README.md)** <br/> A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] |
4041
| **[Python ChatBot](functions/python-dependencies/README.md)** <br/> A chatbot example with ChatterBot. | python310 | [Serverless Framework] |
4142
| **[Python Dependencies](functions/python-dependencies/README.md)** <br/> Example showing how to use Python requirements with Serverless Framework. | python310 | [Serverless Framework] |
4243
| **[Python MultiPart Upload to S3](functions/python-upload-file-s3-multipart/README.md)** <br/> A function to upload file from form-data to S3. | python311 | [Python API Framework] |
4344
| **[Redis TLS](functions/redis-tls/README.md)** <br/> How to connect a function to a Scaleway Redis cluster with TLS enabled. | python310 | [Terraform] |
4445
| **[Rust MNIST](functions/rust-mnist/README.md)** <br/> A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] |
4546
| **[PostgreSQL Python](functions/postgre-sql-python/README.md)** <br/> A Python function to perform a query on a PostgreSQL managed database. | python310 | [Serverless Framework] |
4647
| **[Terraform Python](functions/terraform-python-example/README.md)** <br/> A Python function deployed with Terraform. | python310 | [Terraform] |
47-
| **[PostgeSQL Node](functions/postgre-sql-node/README.md)** <br/> A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] |
48+
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |
4849

4950
### 📦 Containers
5051

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
package-lock.json
3+
.serverless/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Use typescript with Node runtime
2+
3+
## Requirements
4+
5+
This example assumes you are familiar with how serverless functions work. If needed, you can check [Scaleway's official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/)
6+
7+
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.
8+
9+
Finally, you will need Node.js installed in your computer to run this example.
10+
11+
## Context
12+
13+
By default, Node runtime treats files with the .js suffix. If you wish to use Typescript language with Node runtime, you can do so by following this example.
14+
15+
## Description
16+
17+
This example aims to show how to use Typescript language with Node runtime (node 18 runtime in this example). Used packages are specified in `package.json`.
18+
19+
The function in this example returns a simple "Hello world!" with a status code 200.
20+
21+
## Setup
22+
23+
### Install npm modules
24+
25+
Once your environment is set up (see [Requirements](#requirements)), you can install `npm` dependencies from `package.json` file using:
26+
27+
```shell
28+
npm install
29+
```
30+
31+
### Install a Typescript compiler
32+
33+
First, it is necessary to install the [Typescript compiler package](https://www.npmjs.com/package/typescript) globally.
34+
35+
```shell
36+
npm install -g typescript
37+
```
38+
39+
You can run `tsc --version` to ensure the compiler is correctly installed.
40+
41+
### Create a Typescript configuration file
42+
43+
When this is done, you can initialize the Typescript project with Node.js. For that, you can run:
44+
45+
```shell
46+
tsc --init
47+
```
48+
49+
This will create a `tsconfig.json` file in the project root directory.
50+
51+
### Transpile your code
52+
53+
Before deploying your function, you need to transpile your Typescript code into brower readable JavaScript.
54+
55+
```shell
56+
tsc
57+
```
58+
59+
### Deploy your function
60+
61+
Finally, you can deploy your function with:
62+
63+
```shell
64+
serverless deploy
65+
```
66+
67+
## Running
68+
69+
Once your function is deployed, you can check the result in a browser or by running the following command:
70+
71+
```console
72+
# Get request
73+
curl -i -X GET <function URL>
74+
```
75+
76+
The output should be "Hello world!".
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export {handle};
2+
3+
function handle (event: Record<string, unknown>, context: Record<string, unknown>, cb: unknown) {
4+
return {
5+
body: "Hello world!",
6+
headers: { "Content-Type": ["application/json"] },
7+
statusCode: 200,
8+
};
9+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "typescript-with-node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"serverless-scaleway-functions": "^0.4.5",
14+
"typescript": "^5.0.2"
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
service: typescript-hello-world
2+
configValidationMode: off
3+
provider:
4+
name: scaleway
5+
runtime: node18
6+
7+
plugins:
8+
- serverless-scaleway-functions
9+
10+
package:
11+
patterns:
12+
- "!node_modules/**"
13+
- "!.gitignore"
14+
- "!.git/**"
15+
16+
functions:
17+
hello-world:
18+
handler: handler.handle

0 commit comments

Comments
 (0)