Skip to content

Commit 391ebfa

Browse files
authored
docs: add ts example (GoogleCloudPlatform#192)
* docs: add ts example Signed-off-by: Naseem <naseem@transit.app> * docs: simplify instructions, leverage tsc-watch for development Signed-off-by: Naseem <naseem@transit.app> * fix: use Title Casing for title Signed-off-by: Naseem <naseem@transit.app> * fix: improve readbility and copy pasteability Signed-off-by: Naseem <naseem@transit.app> * adapt watch script to use nodemon and concurrently Signed-off-by: Naseem <naseem@transit.app>
1 parent e64c3b8 commit 391ebfa

File tree

2 files changed

+74
-17
lines changed

2 files changed

+74
-17
lines changed

docs/README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This directory contains advanced docs around the Functions Framework.
55
- [Testing events and Pub/Sub](events.md)
66
- [Debugging Functions](debugging.md)
77
- [Running and Deploying Docker Containers](docker.md)
8+
- [Writing a Function in Typescript](typescript.md)
89

910
## TODO Docs
1011

@@ -16,28 +17,28 @@ The Functions Framework works with standard tooling that you might use when writ
1617

1718
1. Write an `index.js` file containing your Node.js function:
1819

19-
```js
20-
exports.helloWorld = (req, res) => {
21-
res.send('Hello, World');
22-
};
23-
```
20+
```js
21+
exports.helloWorld = (req, res) => {
22+
res.send('Hello, World');
23+
};
24+
```
2425

2526
2. Install the Functions Framework:
2627

27-
```sh
28-
npm install @google-cloud/functions-framework
29-
```
28+
```sh
29+
npm install @google-cloud/functions-framework
30+
```
3031

3132
3. Run `node`, enable the inspector and run the Functions Framework:
3233

33-
```sh
34-
node --inspect node_modules/@google-cloud/functions-framework --target=helloWorld
35-
...
36-
Debugger listening on ws://127.0.0.1:9229/5f57f5e9-ea4b-43ce-be1d-6e9b838ade4a
37-
For help see https://nodejs.org/en/docs/inspector
38-
Serving function...
39-
Function: helloWorld
40-
URL: http://localhost:8080/
41-
```
34+
```sh
35+
node --inspect node_modules/@google-cloud/functions-framework --target=helloWorld
36+
...
37+
Debugger listening on ws://127.0.0.1:9229/5f57f5e9-ea4b-43ce-be1d-6e9b838ade4a
38+
For help see https://nodejs.org/en/docs/inspector
39+
Serving function...
40+
Function: helloWorld
41+
URL: http://localhost:8080/
42+
```
4243

4344
You can now use an IDE or other tooling to add breakpoints, step through your code and debug your function.

docs/typescript.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Typescript
2+
3+
This guide is an example of how to develop a function in Typescript with
4+
the Functions Framework.
5+
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+
```

0 commit comments

Comments
 (0)