Skip to content

Commit 610d73f

Browse files
committed
refactor: use one-parameter object for makeHandler functions
1 parent ade1a2e commit 610d73f

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

packages/runtime/src/templates/getApiHandler.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ type Mutable<T> = {
2525
-readonly [K in keyof T]: T[K]
2626
}
2727

28+
type MakeApiHandlerParams = {
29+
conf: NextConfig
30+
app: string
31+
pageRoot: string
32+
page: string
33+
NextServer: NextServerType
34+
}
35+
2836
// We return a function and then call `toString()` on it to serialise it as the launcher function
29-
// eslint-disable-next-line max-params
30-
const makeHandler = (conf: NextConfig, app, pageRoot, page, NextServer: NextServerType) => {
37+
const makeApiHandler = ({ conf, app, pageRoot, page, NextServer }: MakeApiHandlerParams) => {
3138
// Change working directory into the site root, unless using Nx, which moves the
3239
// dist directory and handles this itself
3340
const dir = path.resolve(__dirname, app)
@@ -145,7 +152,9 @@ export const getApiHandler = ({
145152
let staticManifest
146153
const path = require("path");
147154
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "server"));
148-
const handler = (${makeHandler.toString()})(config, "${appDir}", pageRoot, ${JSON.stringify(page)}, NextServer)
155+
const handler = (${makeApiHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, page:${JSON.stringify(
156+
page,
157+
)}, NextServer})
149158
exports.handler = ${
150159
config.type === ApiRouteType.SCHEDULED ? `schedule(${JSON.stringify(config.schedule)}, handler);` : 'handler'
151160
}

packages/runtime/src/templates/getHandler.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ type Mutable<T> = {
3131
-readonly [K in keyof T]: T[K]
3232
}
3333

34+
type MakeHandlerParams = {
35+
conf: NextConfig
36+
app: string
37+
pageRoot: string
38+
NextServer: NextServerType
39+
staticManifest: Array<[string, string]>
40+
mode: 'ssr' | 'odb'
41+
}
42+
3443
// We return a function and then call `toString()` on it to serialise it as the launcher function
3544
// eslint-disable-next-line max-lines-per-function
36-
const makeHandler = (
37-
conf: NextConfig,
38-
app: string,
39-
pageRoot,
40-
NextServer: NextServerType,
41-
staticManifest: Array<[string, string]> = [],
42-
mode = 'ssr',
43-
// eslint-disable-next-line max-params
44-
) => {
45+
const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mode = 'ssr' }: MakeHandlerParams) => {
4546
// Change working directory into the site root, unless using Nx, which moves the
4647
// dist directory and handles this itself
4748
const dir = path.resolve(__dirname, app)
@@ -117,7 +118,7 @@ const makeHandler = (
117118
}
118119

119120
return async function handler(event: HandlerEvent, context: HandlerContext) {
120-
let requestMode = mode
121+
let requestMode: string = mode
121122
const prefetchResponse = getPrefetchResponse(event, mode)
122123
if (prefetchResponse) {
123124
return prefetchResponse
@@ -220,7 +221,7 @@ export const getHandler = ({
220221
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "server"));
221222
exports.handler = ${
222223
isODB
223-
? `builder((${makeHandler.toString()})(config, "${appDir}", pageRoot, NextServer, staticManifest, 'odb'));`
224-
: `(${makeHandler.toString()})(config, "${appDir}", pageRoot, NextServer, staticManifest, 'ssr');`
224+
? `builder((${makeHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, NextServer, staticManifest, mode: 'odb' }));`
225+
: `(${makeHandler.toString()})({ conf: config, app: "${appDir}", pageRoot, NextServer, staticManifest, mode: 'ssr' });`
225226
}
226227
`

test/index.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,12 @@ describe('onBuild()', () => {
560560
expect(existsSync(handlerFile)).toBeTruthy()
561561
expect(existsSync(odbHandlerFile)).toBeTruthy()
562562

563-
expect(readFileSync(handlerFile, 'utf8')).toMatch(`(config, "../../..", pageRoot, NextServer, staticManifest, 'ssr')`)
564-
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`(config, "../../..", pageRoot, NextServer, staticManifest, 'odb')`)
563+
expect(readFileSync(handlerFile, 'utf8')).toMatch(
564+
`({ conf: config, app: \"../../..\", pageRoot, NextServer, staticManifest, mode: 'ssr' })`,
565+
)
566+
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(
567+
`({ conf: config, app: \"../../..\", pageRoot, NextServer, staticManifest, mode: 'odb' })`,
568+
)
565569
expect(readFileSync(handlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`)
566570
expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`)
567571
})

0 commit comments

Comments
 (0)