From bad5fae2a53bcb3e75c2b8ff97c02a0bdb4d2cd3 Mon Sep 17 00:00:00 2001 From: Emilie BOUIN Date: Fri, 24 Mar 2023 17:02:06 +0100 Subject: [PATCH 1/3] docs: add typescript example --- functions/typescript-hello-world/.gitignore | 3 + functions/typescript-hello-world/README.md | 76 +++++++++++++++++++ functions/typescript-hello-world/handler.ts | 9 +++ functions/typescript-hello-world/package.json | 16 ++++ .../typescript-hello-world/serverless.yml | 18 +++++ 5 files changed, 122 insertions(+) create mode 100644 functions/typescript-hello-world/.gitignore create mode 100644 functions/typescript-hello-world/README.md create mode 100644 functions/typescript-hello-world/handler.ts create mode 100644 functions/typescript-hello-world/package.json create mode 100644 functions/typescript-hello-world/serverless.yml diff --git a/functions/typescript-hello-world/.gitignore b/functions/typescript-hello-world/.gitignore new file mode 100644 index 0000000..2226b57 --- /dev/null +++ b/functions/typescript-hello-world/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +package-lock.json +.serverless/ \ No newline at end of file diff --git a/functions/typescript-hello-world/README.md b/functions/typescript-hello-world/README.md new file mode 100644 index 0000000..5da6779 --- /dev/null +++ b/functions/typescript-hello-world/README.md @@ -0,0 +1,76 @@ +# Use typescript with Node runtime + +## Requirements + +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/) + +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. + +Finally, you will need Node.js installed in your computer to run this example. + +## Context + +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. + +## Description + +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`. + +The function in this example returns a simple "Hello world!" with a status code 200. + +## Setup + +### Install npm modules + +Once your environment is set up (see [Requirements](#requirements)), you can install `npm` dependencies from `package.json` file using: + +```shell +npm install +``` + +### Install a Typescript compiler + +First, it is necessary to install the [Typescript compiler package](https://www.npmjs.com/package/typescript) globally. + +```shell +npm install -g typescript +``` + +You can run `tsc --version` to ensure the compiler is correctly installed. + +### Create a Typescript configuration file + +When this is done, you can initialize the Typescript project with Node.js. For that, you can run: + +```shell +tsc --init +``` + +This will create a `tsconfig.json` file in the project root directory. + +### Transpile your code + +Before deploying your function, you need to transpile your Typescript code into brower readable JavaScript. + +```shell +tsc +``` + +### Deploy your function + +Finally, you can deploy your function with: + +```shell +serverless deploy +``` + +## Running + +Once your function is deployed, you can check the result in a browser or by running the following command: + +```console +# Get request +curl -i -X GET +``` + +The output should be "Hello world!". diff --git a/functions/typescript-hello-world/handler.ts b/functions/typescript-hello-world/handler.ts new file mode 100644 index 0000000..eeb12aa --- /dev/null +++ b/functions/typescript-hello-world/handler.ts @@ -0,0 +1,9 @@ +export {handle}; + +function handle (event, context, cb) { + return { + body: "Hello world!", + headers: { "Content-Type": ["application/json"] }, + statusCode: 200, + }; +}; \ No newline at end of file diff --git a/functions/typescript-hello-world/package.json b/functions/typescript-hello-world/package.json new file mode 100644 index 0000000..373c9ba --- /dev/null +++ b/functions/typescript-hello-world/package.json @@ -0,0 +1,16 @@ +{ + "name": "typescript-with-node", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "serverless-scaleway-functions": "^0.4.5", + "typescript": "^5.0.2" + } +} diff --git a/functions/typescript-hello-world/serverless.yml b/functions/typescript-hello-world/serverless.yml new file mode 100644 index 0000000..62116f3 --- /dev/null +++ b/functions/typescript-hello-world/serverless.yml @@ -0,0 +1,18 @@ +service: typescript-hello-world +configValidationMode: off +provider: + name: scaleway + runtime: node18 + +plugins: + - serverless-scaleway-functions + +package: + patterns: + - "!node_modules/**" + - "!.gitignore" + - "!.git/**" + +functions: + hello-world: + handler: handler.handle \ No newline at end of file From 22831697b8e7de223932d34e1526da987096a208 Mon Sep 17 00:00:00 2001 From: Emilie BOUIN Date: Fri, 24 Mar 2023 17:11:22 +0100 Subject: [PATCH 2/3] docs: updated the project README to link typescript-with-node example --- README.md | 4 +++- .../.gitignore | 0 .../README.md | 0 .../handler.ts | 0 .../package.json | 0 .../serverless.yml | 0 6 files changed, 3 insertions(+), 1 deletion(-) rename functions/{typescript-hello-world => typescript-with-node}/.gitignore (100%) rename functions/{typescript-hello-world => typescript-with-node}/README.md (100%) rename functions/{typescript-hello-world => typescript-with-node}/handler.ts (100%) rename functions/{typescript-hello-world => typescript-with-node}/package.json (100%) rename functions/{typescript-hello-world => typescript-with-node}/serverless.yml (100%) diff --git a/README.md b/README.md index e14d64e..3420f54 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,14 @@ Table of Contents: | **[Go MultiPart Upload to S3](functions/go-upload-file-s3-multipart)**
A function to upload file from form-data to S3. | go119 | [Serverless Framework] | | **[Image Transform](functions/image-transform-node/README.md)**
A function that resizes images from an S3 bucket. | node16 | [Serverless Framework] | | **[Node MultiPart Upload to S3](functions/node-upload-file-s3-multipart/README.md)**
A function to upload file from form-data to S3. | node19 | [Serverless Framework] | +| **[PostgeSQL Node](functions/postgre-sql-node/README.md)**
A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] | | **[Python ChatBot](functions/python-dependencies/README.md)**
A chatbot example with ChatterBot. | python310 | [Serverless Framework] | | **[Python Dependencies](functions/python-dependencies/README.md)**
Example showing how to use Python requirements with Serverless Framework. | python310 | [Serverless Framework] | | **[Redis TLS](functions/redis-tls/README.md)**
How to connect a function to a Scaleway Redis cluster with TLS enabled. | python310 | [Terraform] | | **[Rust MNIST](functions/rust-mnist/README.md)**
A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] | | **[Terraform Python](functions/terraform-python-example/README.md)**
A Python function deployed with Terraform. | python310 | [Terraform] | -| **[PostgeSQL Node](functions/postgre-sql-node/README.md)**
A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] | +| **[Typescript with Node runtime](functions/typescript-with-node/README.md)**
A Typescript function using Node runtime. | node18 | [Serverless Framework] | + ### 📦 Containers diff --git a/functions/typescript-hello-world/.gitignore b/functions/typescript-with-node/.gitignore similarity index 100% rename from functions/typescript-hello-world/.gitignore rename to functions/typescript-with-node/.gitignore diff --git a/functions/typescript-hello-world/README.md b/functions/typescript-with-node/README.md similarity index 100% rename from functions/typescript-hello-world/README.md rename to functions/typescript-with-node/README.md diff --git a/functions/typescript-hello-world/handler.ts b/functions/typescript-with-node/handler.ts similarity index 100% rename from functions/typescript-hello-world/handler.ts rename to functions/typescript-with-node/handler.ts diff --git a/functions/typescript-hello-world/package.json b/functions/typescript-with-node/package.json similarity index 100% rename from functions/typescript-hello-world/package.json rename to functions/typescript-with-node/package.json diff --git a/functions/typescript-hello-world/serverless.yml b/functions/typescript-with-node/serverless.yml similarity index 100% rename from functions/typescript-hello-world/serverless.yml rename to functions/typescript-with-node/serverless.yml From baa2d0fa30405716d7cc098e9330de1c536d231e Mon Sep 17 00:00:00 2001 From: Emilie BOUIN Date: Mon, 27 Mar 2023 15:02:11 +0200 Subject: [PATCH 3/3] fix: add type in handler --- functions/typescript-with-node/handler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/typescript-with-node/handler.ts b/functions/typescript-with-node/handler.ts index eeb12aa..53092a7 100644 --- a/functions/typescript-with-node/handler.ts +++ b/functions/typescript-with-node/handler.ts @@ -1,6 +1,6 @@ export {handle}; -function handle (event, context, cb) { +function handle (event: Record, context: Record, cb: unknown) { return { body: "Hello world!", headers: { "Content-Type": ["application/json"] },