Skip to content

Commit 6aad6b1

Browse files
committed
chore: use enum for api route type
1 parent daeb493 commit 6aad6b1

File tree

4 files changed

+49
-61
lines changed

4 files changed

+49
-61
lines changed

package-lock.json

Lines changed: 26 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/analysis.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,40 @@ import { extractExportedConstValue, UnsupportedValueError } from 'next/dist/buil
44
import { parseModule } from 'next/dist/build/analysis/parse-module'
55
import { relative } from 'pathe'
66

7+
// I have no idea what eslint is up to here but it gives an error
8+
// eslint-disable-next-line no-shadow
9+
export enum ApiRouteType {
10+
SCHEDULED = 'experimental-scheduled',
11+
BACKGROUND = 'experimental-background',
12+
}
13+
714
export interface ApiStandardConfig {
815
type?: never
916
runtime?: 'nodejs' | 'experimental-edge'
1017
schedule?: never
1118
}
1219

1320
export interface ApiScheduledConfig {
14-
type: 'experimental-scheduled'
21+
type: ApiRouteType.SCHEDULED
1522
runtime?: 'nodejs'
1623
schedule: string
1724
}
1825

1926
export interface ApiBackgroundConfig {
20-
type: 'experimental-background'
27+
type: ApiRouteType.BACKGROUND
2128
runtime?: 'nodejs'
2229
schedule?: never
2330
}
2431

2532
export type ApiConfig = ApiStandardConfig | ApiScheduledConfig | ApiBackgroundConfig
2633

2734
export const validateConfigValue = (config: ApiConfig, apiFilePath: string): config is ApiConfig => {
28-
if (config.type === 'experimental-scheduled') {
35+
if (config.type === ApiRouteType.SCHEDULED) {
2936
if (!config.schedule) {
3037
console.error(
31-
`Invalid config value in ${relative(
32-
process.cwd(),
33-
apiFilePath,
34-
)}: schedule is required when type is "experimental-scheduled"`,
38+
`Invalid config value in ${relative(process.cwd(), apiFilePath)}: schedule is required when type is "${
39+
ApiRouteType.SCHEDULED
40+
}"`,
3541
)
3642
return false
3743
}
@@ -47,13 +53,12 @@ export const validateConfigValue = (config: ApiConfig, apiFilePath: string): con
4753
return true
4854
}
4955

50-
if (!config.type || config.type === 'experimental-background') {
56+
if (!config.type || config.type === ApiRouteType.BACKGROUND) {
5157
if (config.schedule) {
5258
console.error(
53-
`Invalid config value in ${relative(
54-
process.cwd(),
55-
apiFilePath,
56-
)}: schedule is not allowed unless type is "experimental-scheduled"`,
59+
`Invalid config value in ${relative(process.cwd(), apiFilePath)}: schedule is not allowed unless type is "${
60+
ApiRouteType.SCHEDULED
61+
}"`,
5762
)
5863
return false
5964
}

packages/runtime/src/helpers/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { join } from 'pathe'
88

99
import { OPTIONAL_CATCH_ALL_REGEX, CATCH_ALL_REGEX, DYNAMIC_PARAMETER_REGEX, HANDLER_FUNCTION_PATH } from '../constants'
1010

11+
import { ApiRouteType } from './analysis'
1112
import type { ApiRouteConfig } from './functions'
1213
import { I18n } from './types'
1314

@@ -148,26 +149,21 @@ export const getApiRewrites = (basePath: string, apiRoutes: Array<ApiRouteConfig
148149
const [from] = toNetlifyRoute(`${basePath}${apiRoute.route}`)
149150

150151
// Scheduled functions can't be invoked directly, so we 404 them.
151-
if (apiRoute.config.type === 'experimental-scheduled') {
152+
if (apiRoute.config.type === ApiRouteType.SCHEDULED) {
152153
return { from, to: '/404.html', status: 404 }
153154
}
154155
return {
155156
from,
156157
to: `/.netlify/functions/${getFunctionNameForPage(
157158
apiRoute.route,
158-
apiRoute.config.type === 'experimental-background',
159+
apiRoute.config.type === ApiRouteType.BACKGROUND,
159160
)}`,
160161
status: 200,
161162
}
162163
})
163164

164165
return [
165166
...apiRewrites,
166-
{
167-
from: `${basePath}/api`,
168-
to: HANDLER_FUNCTION_PATH,
169-
status: 200,
170-
},
171167
{
172168
from: `${basePath}/api/*`,
173169
to: HANDLER_FUNCTION_PATH,

packages/runtime/src/templates/getApiHandler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Bridge as NodeBridge } from '@vercel/node-bridge/bridge'
33
// Aliasing like this means the editor may be able to syntax-highlight the string
44
import { outdent as javascript } from 'outdent'
55

6-
import { ApiConfig } from '../helpers/analysis'
6+
import { ApiConfig, ApiRouteType } from '../helpers/analysis'
77
import type { NextConfig } from '../helpers/config'
88

99
import type { NextServerType } from './handlerUtils'
@@ -133,7 +133,7 @@ export const getApiHandler = ({
133133
const { Bridge } = require("./bridge");
134134
const { getMaxAge, getMultiValueHeaders, getNextServer } = require('./handlerUtils')
135135
136-
${config.type === 'experimental-scheduled' ? `const { schedule } = require("@netlify/functions")` : ''}
136+
${config.type === ApiRouteType.SCHEDULED ? `const { schedule } = require("@netlify/functions")` : ''}
137137
138138
139139
const { config } = require("${publishDir}/required-server-files.json")
@@ -142,6 +142,6 @@ export const getApiHandler = ({
142142
const pageRoot = path.resolve(path.join(__dirname, "${publishDir}", "serverless", "pages"));
143143
const handler = (${makeHandler.toString()})(config, "${appDir}", pageRoot, ${JSON.stringify(page)})
144144
exports.handler = ${
145-
config.type === 'experimental-scheduled' ? `schedule(${JSON.stringify(config.schedule)}, handler);` : 'handler'
145+
config.type === ApiRouteType.SCHEDULED ? `schedule(${JSON.stringify(config.schedule)}, handler);` : 'handler'
146146
}
147147
`

0 commit comments

Comments
 (0)