Skip to content

Commit 0e16c03

Browse files
authored
add typescript notes
1 parent 732c183 commit 0e16c03

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,46 @@ This will start the normal create-react-app dev server and open your app at `htt
5050

5151
Local in-app requests to the relative path `/.netlify/functions/*` will automatically be proxied to the local functions dev server.
5252

53+
<detail>
54+
<summary>
55+
<b>Typescript</b>
56+
</summary>
57+
You can use Typescript in both your React code (with `react-scripts` v2.1+) and your lambda functions )with `netlify-lambda` v1.1+). Follow these instructions:
58+
59+
1. `yarn add -D typescript @types/node @types/react @types/react-dom @babel/preset-typescript @types/aws-lambda`
60+
2. convert `src/lambda/hello.js` to `src/lambda/hello.ts`
61+
3. use types in your event handler:
62+
63+
```ts
64+
import { Handler, Context, Callback } from 'aws-lambda';
65+
66+
interface HelloResponse {
67+
statusCode: number;
68+
body: string;
69+
}
70+
71+
const handler: Handler = (event: any, context: Context, callback: Callback) => {
72+
const response: HelloResponse = {
73+
statusCode: 200,
74+
body: JSON.stringify({
75+
msg: `Hello world ${Math.floor(Math.random() * 10)}`
76+
})
77+
};
78+
79+
callback(undefined, response);
80+
};
81+
82+
export { handler };
83+
84+
```
85+
86+
rerun and see it work!
87+
88+
You are free to set up your `tsconfig.json` and `tslint` as you see fit.
89+
90+
</detail>
91+
92+
5393
## Service Worker
5494

5595
The service worker does not work with lambda functions out of the box. It prevents calling the function and returns the app itself instead ([Read more](https://github.com/facebook/create-react-app/issues/2237#issuecomment-302693219)). To solve this you have to eject and enhance the service worker configuration in the webpack config. Whitelist the path of your lambda function and you are good to go.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy