Skip to content

Commit c20d3de

Browse files
docs: re-write of the typescript user guide (#489)
This was very out of date. Updated to reflect most recent version of the FF. fixes #472, #469
1 parent da51954 commit c20d3de

File tree

1 file changed

+99
-70
lines changed

1 file changed

+99
-70
lines changed

docs/typescript.md

+99-70
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,102 @@
33
This guide is an example of how to develop a function in Typescript with
44
the Functions Framework.
55

6-
1. Use [gts](https://github.com/google/gts) to configure Typescript.
7-
8-
```sh
9-
npx gts init
10-
```
11-
12-
2. Install the required packages:
13-
14-
```sh
15-
npm install @google-cloud/functions-framework
16-
npm install @types/express concurrently nodemon --save-dev
17-
```
18-
19-
3. Add a `start` script to `package.json`, passing in the `--source` flag to
20-
point to the compiled code directory (configured by `gts` in this example).
21-
Also add a `watch` script to use for development:
22-
23-
```js
24-
"scripts": {
25-
"start": "functions-framework --source=build/src/ --target=helloWorld",
26-
"watch": "concurrently \"tsc -w\" \"nodemon --watch ./build/ --exec npm run start\"",
27-
...
28-
}
29-
```
30-
31-
4. Replace the contents of `src/index.ts` with:
32-
33-
```ts
34-
import type { HttpFunction } from '@google-cloud/functions-framework/build/src/functions';
35-
36-
export const helloWorld: HttpFunction = (req, res) => {
37-
res.send('Hello, World');
38-
};
39-
```
40-
41-
5. Start the built-in local development server in watch mode:
42-
43-
```sh
44-
npm run watch
45-
```
46-
47-
This will continuously watch changes to your TypeScript project and recompile when changes are detected:
48-
49-
```sh
50-
[12:34:56 AM] Starting compilation in watch mode...
51-
[12:34:57 AM] Found 0 errors. Watching for file changes.
52-
...
53-
Serving function...
54-
Function: helloWorld
55-
URL: http://localhost:8080/
56-
```
57-
58-
## Deploying with gcloud CLI
59-
60-
1. Adjust `main` field in `package.json` to point to the compiled javascript source.
61-
62-
```js
63-
"main": "build/src/index.js",
64-
...
65-
```
66-
67-
1. Remove `prepare` script in `package.json` created by [gts](https://github.com/google/gts). This is because the `prepare` script requires typescript to be installed and will cause the function to fail to deploy if not removed.
68-
69-
1. Deploy:
70-
71-
```sh
72-
gcloud functions deploy helloWorld \
73-
--runtime nodejs16 \
74-
--trigger-http
75-
```
6+
1. Create a new Node.js project using the npm CLI.
7+
8+
```sh
9+
mkdir typescript-function && cd typescript-function
10+
npm init -y
11+
```
12+
13+
1. Install the required packages:
14+
15+
```sh
16+
npm install @google-cloud/functions-framework
17+
npm install --save-dev typescript
18+
```
19+
20+
1. Create a `tsconfig.json` in the root directory of your project with the following contents:
21+
22+
```json
23+
{
24+
"compilerOptions": {
25+
"target": "es2016",
26+
"module": "commonjs",
27+
"esModuleInterop": true,
28+
"strict": true,
29+
"outDir": "dist"
30+
},
31+
"include": ["src/**/*"],
32+
"exclude": ["node_modules"]
33+
}
34+
```
35+
36+
1. Update your `package.json` with scripts for building and running your application:
37+
38+
```json
39+
{
40+
"main": "dist/index.js",
41+
"scripts": {
42+
"build": "tsc",
43+
"start": "functions-framework --target=TypescriptFunction",
44+
"prestart": "npm run build",
45+
"gcp-build": "npm run build"
46+
},
47+
...
48+
}
49+
```
50+
51+
- The `main` field must be configured to the compiled source code file that contains your function source.
52+
- The [`gcp-build` script](https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs#executing_custom_build_steps_during_deployment) is run when deploying your application Google Cloud Functions. It allows you to configure build steps that depend on `devDependencies`.
53+
54+
1. Create a `.gitignore` that ensures your `node_modules` and compiled code are not checked into source control and are not uploaded when you deploy to Google Cloud Functions.
55+
56+
```
57+
node_modules/
58+
dist/
59+
```
60+
61+
1. Create a `src/index.ts` file with your function source code.
62+
63+
**Example Typescript HTTP Function**
64+
65+
```typescript
66+
import * as ff from '@google-cloud/functions-framework';
67+
68+
ff.http('TypescriptFunction', (req: ff.Request, res: ff.Response) => {
69+
res.send('OK');
70+
});
71+
```
72+
73+
**Example Typescript CloudEvent Function**
74+
75+
The `cloudEvent` function registration method accepts a template type that can be used to
76+
annotate the event payload type you expect.
77+
78+
```typescript
79+
import ff from '@google-cloud/functions-framework';
80+
81+
interface PubSubData {
82+
subscription: string;
83+
message: {
84+
messageId: string;
85+
publishTime: string;
86+
data: string;
87+
attributes?: {[key: string]: string};
88+
};
89+
}
90+
91+
ff.cloudEvent<PubSubData>('TypescriptFunction', ce => {
92+
console.log(ce.data?.message.messageId);
93+
});
94+
```
95+
96+
## Deploying with the gcloud CLI
97+
98+
If you correctly configured the `main` field and `gcp-build` script in your `package.json` you can deploy to Google Cloud Functions as you would any other Javascript function:
99+
100+
```sh
101+
gcloud functions deploy TypescriptFunction \
102+
--runtime nodejs16 \
103+
--trigger-http
104+
```

0 commit comments

Comments
 (0)