Skip to content

Typescript hello world #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ Table of Contents:
| **[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] |
| **[Image Transform](functions/image-transform-node/README.md)** <br/> 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)** <br/> A function to upload file from form-data to S3. | node19 | [Serverless Framework] |
| **[PostgeSQL Node](functions/postgre-sql-node/README.md)** <br/> A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] |
| **[Python ChatBot](functions/python-dependencies/README.md)** <br/> A chatbot example with ChatterBot. | python310 | [Serverless Framework] |
| **[Python Dependencies](functions/python-dependencies/README.md)** <br/> Example showing how to use Python requirements with Serverless Framework. | python310 | [Serverless Framework] |
| **[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] |
| **[Redis TLS](functions/redis-tls/README.md)** <br/> How to connect a function to a Scaleway Redis cluster with TLS enabled. | python310 | [Terraform] |
| **[Rust MNIST](functions/rust-mnist/README.md)** <br/> A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] |
| **[PostgreSQL Python](functions/postgre-sql-python/README.md)** <br/> A Python function to perform a query on a PostgreSQL managed database. | python310 | [Serverless Framework] |
| **[Terraform Python](functions/terraform-python-example/README.md)** <br/> A Python function deployed with Terraform. | python310 | [Terraform] |
| **[PostgeSQL Node](functions/postgre-sql-node/README.md)** <br/> A Node function to connect and interact with PostgreSQL database. | node18 | [Serverless Framework] |
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |

### 📦 Containers

Expand Down
3 changes: 3 additions & 0 deletions functions/typescript-with-node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
package-lock.json
.serverless/
76 changes: 76 additions & 0 deletions functions/typescript-with-node/README.md
Original file line number Diff line number Diff line change
@@ -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 <function URL>
```

The output should be "Hello world!".
9 changes: 9 additions & 0 deletions functions/typescript-with-node/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export {handle};

function handle (event: Record<string, unknown>, context: Record<string, unknown>, cb: unknown) {
return {
body: "Hello world!",
headers: { "Content-Type": ["application/json"] },
statusCode: 200,
};
};
16 changes: 16 additions & 0 deletions functions/typescript-with-node/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
18 changes: 18 additions & 0 deletions functions/typescript-with-node/serverless.yml
Original file line number Diff line number Diff line change
@@ -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