Skip to content

Commit c863343

Browse files
committed
add netlify handler
1 parent b51b2c2 commit c863343

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
119119
"@babel/preset-env": "^7.22.4",
120120
"@babel/preset-typescript": "^7.21.5",
121+
"@netlify/functions": "^1.6.0",
121122
"@rollup/plugin-terser": "^0.4.3",
122123
"@rollup/plugin-typescript": "^11.1.1",
123124
"@semantic-release/changelog": "^6.0.3",

src/use/netlify.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
createHandler as createRawHandler,
3+
HandlerOptions as RawHandlerOptions,
4+
OperationContext,
5+
} from '../handler';
6+
7+
import type {
8+
Handler,
9+
HandlerEvent,
10+
HandlerContext
11+
} from '@netlify/functions';
12+
13+
export interface RequestContext {
14+
req: HandlerEvent;
15+
ctx: HandlerContext
16+
}
17+
/**
18+
* Handler options when using the netlify adapter
19+
*
20+
* @category Server/fetch
21+
*/
22+
export type HandlerOptions<Context extends OperationContext = undefined> =
23+
RawHandlerOptions<HandlerEvent, RequestContext, Context>;
24+
25+
/**
26+
* Create a GraphQL over HTTP spec compliant request handler for netlify functions
27+
*
28+
* @category Server/fetch
29+
*/
30+
export function createHandler<Context extends OperationContext = undefined>(
31+
options: HandlerOptions<Context>,
32+
): Handler {
33+
const handler = createRawHandler(options);
34+
return async function handleRequest(req, ctx) {
35+
try {
36+
const [body, init] = await handler({
37+
method: req.httpMethod,
38+
url: req.path,
39+
headers: req.headers,
40+
body: req.body,
41+
raw: req,
42+
context: { req, ctx },
43+
});
44+
return {
45+
// if body is null, return undefined
46+
body: body ?? undefined,
47+
statusCode: init.status,
48+
...init,
49+
};
50+
} catch (err) {
51+
// The handler shouldnt throw errors.
52+
// If you wish to handle them differently, consider implementing your own request handler.
53+
console.error(
54+
'Internal error occurred during request handling. ' +
55+
'Please check your implementation.',
56+
err,
57+
);
58+
return { statusCode: 500 };
59+
}
60+
};
61+
}

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,6 +3406,15 @@ __metadata:
34063406
languageName: node
34073407
linkType: hard
34083408

3409+
"@netlify/functions@npm:^1.6.0":
3410+
version: 1.6.0
3411+
resolution: "@netlify/functions@npm:1.6.0"
3412+
dependencies:
3413+
is-promise: ^4.0.0
3414+
checksum: ecff9a1161b2df94d139431e7a2b42d9790cf4ec250ad3aec57935cc1b1e78beccfe855c6e5522311baefce84b91ed690904aedf02b2511eaaafdc8f6daab75e
3415+
languageName: node
3416+
linkType: hard
3417+
34093418
"@nodelib/fs.scandir@npm:2.1.5":
34103419
version: 2.1.5
34113420
resolution: "@nodelib/fs.scandir@npm:2.1.5"
@@ -7701,6 +7710,7 @@ __metadata:
77017710
"@babel/plugin-proposal-optional-chaining": ^7.21.0
77027711
"@babel/preset-env": ^7.22.4
77037712
"@babel/preset-typescript": ^7.21.5
7713+
"@netlify/functions": ^1.6.0
77047714
"@rollup/plugin-terser": ^0.4.3
77057715
"@rollup/plugin-typescript": ^11.1.1
77067716
"@semantic-release/changelog": ^6.0.3
@@ -8501,6 +8511,13 @@ __metadata:
85018511
languageName: node
85028512
linkType: hard
85038513

8514+
"is-promise@npm:^4.0.0":
8515+
version: 4.0.0
8516+
resolution: "is-promise@npm:4.0.0"
8517+
checksum: 0b46517ad47b00b6358fd6553c83ec1f6ba9acd7ffb3d30a0bf519c5c69e7147c132430452351b8a9fc198f8dd6c4f76f8e6f5a7f100f8c77d57d9e0f4261a8a
8518+
languageName: node
8519+
linkType: hard
8520+
85048521
"is-property@npm:^1.0.2":
85058522
version: 1.0.2
85068523
resolution: "is-property@npm:1.0.2"

0 commit comments

Comments
 (0)