Skip to content

Commit

Permalink
fix: prevent reach-router dep requirement for v2 (#557)
Browse files Browse the repository at this point in the history
* 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 <shydalys123@gmail.com>

* fix: use JavaScript instead of TypeScript in apiHandler template

---------

Co-authored-by: pieh <misiek.piechowiak@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 23, 2023
1 parent c37721e commit 6d541d1
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions plugin/src/templates/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
Handler,
HandlerContext,
HandlerEvent,
HandlerResponse,
} from '@netlify/functions'
import type { Handler, HandlerEvent } from '@netlify/functions'
import { stripIndent as javascript } from 'common-tags'
import type { GatsbyFunctionRequest } from 'gatsby'
import type {
Expand All @@ -19,8 +14,6 @@ const { join } = require('path')

const etag = require('etag')

const { gatsbyFunction } = require('./api/gatsbyFunction')
const { createRequestObject, createResponseObject } = require('./api/utils')
const {
getPagePathFromPageDataPath,
getGraphQLEngine,
Expand Down Expand Up @@ -142,11 +135,11 @@ const getHandler = (renderMode: RenderMode, appDir: string): Handler => {
* Generate an API handler
*/

const getApiHandler = (appDir: string): Handler =>
const apiHandler = javascript`(appDir) =>
function handler(
event: HandlerEvent,
context: HandlerContext,
): Promise<HandlerResponse> {
event,
context,
) {
// Create a fake Gatsby/Express Request object
const req = createRequestObject({ event, context })
Expand All @@ -158,11 +151,11 @@ const getApiHandler = (appDir: string): Handler =>
// Try to call the actual function
gatsbyFunction(req, res, event, appDir)
} catch (error) {
console.error(`Error executing ${event.path}`, error)
console.error(\`Error executing \${event.path}\`, error)
resolve({ statusCode: 500 })
}
})
}
}`

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

0 comments on commit 6d541d1

Please sign in to comment.