Skip to content

Commit 6d541d1

Browse files
taty2010piehkodiakhq[bot]
authored
fix: prevent reach-router dep requirement for v2 (#557)
* fix: skip writeFunctions if there are no functions * fix: removed top level writeFunctions import * fix: prettier reformat * fix: moved deleteFunction under dynamic import * fix: use javascript tagged template for api function handler implementation Co-authored-by: taty2010 <[email protected]> * fix: use JavaScript instead of TypeScript in apiHandler template --------- Co-authored-by: pieh <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent c37721e commit 6d541d1

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

plugin/src/templates/handlers.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import type {
2-
Handler,
3-
HandlerContext,
4-
HandlerEvent,
5-
HandlerResponse,
6-
} from '@netlify/functions'
1+
import type { Handler, HandlerEvent } from '@netlify/functions'
72
import { stripIndent as javascript } from 'common-tags'
83
import type { GatsbyFunctionRequest } from 'gatsby'
94
import type {
@@ -19,8 +14,6 @@ const { join } = require('path')
1914

2015
const etag = require('etag')
2116

22-
const { gatsbyFunction } = require('./api/gatsbyFunction')
23-
const { createRequestObject, createResponseObject } = require('./api/utils')
2417
const {
2518
getPagePathFromPageDataPath,
2619
getGraphQLEngine,
@@ -142,11 +135,11 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => {
142135
* Generate an API handler
143136
*/
144137

145-
const getApiHandler = (appDir: string): Handler =>
138+
const apiHandler = javascript`(appDir) =>
146139
function handler(
147-
event: HandlerEvent,
148-
context: HandlerContext,
149-
): Promise<HandlerResponse> {
140+
event,
141+
context,
142+
) {
150143
// Create a fake Gatsby/Express Request object
151144
const req = createRequestObject({ event, context })
152145
@@ -158,11 +151,11 @@ const getApiHandler = (appDir: string): Handler =>
158151
// Try to call the actual function
159152
gatsbyFunction(req, res, event, appDir)
160153
} catch (error) {
161-
console.error(`Error executing ${event.path}`, error)
154+
console.error(\`Error executing \${event.path}\`, error)
162155
resolve({ statusCode: 500 })
163156
}
164157
})
165-
}
158+
}`
166159

167160
export const makeHandler = (appDir: string, renderMode: RenderMode): string =>
168161
// This is a string, but if you have the right editor plugin it should format as js
@@ -188,5 +181,5 @@ export const makeApiHandler = (appDir: string): string =>
188181
const { resolve } = require("path");
189182
190183
const pageRoot = resolve(__dirname, "${appDir}");
191-
exports.handler = ((${getApiHandler.toString()})(pageRoot))
184+
exports.handler = ((${apiHandler})(pageRoot))
192185
`

0 commit comments

Comments
 (0)