Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: match on http methods #458

Merged
merged 9 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions node/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ test('Loads function paths from the in-source `config` function', async () => {
{
function: 'user-func2',
path: '/user-func2',
method: ['patch'],
},
]
const result = await bundle([internalDirectory, userDirectory], distPath, declarations, {
Expand Down Expand Up @@ -208,6 +209,7 @@ test('Loads function paths from the in-source `config` function', async () => {
pattern: '^/user-func2/?$',
excluded_patterns: [],
path: '/user-func2',
methods: ['PATCH'],
})
expect(routes[2]).toEqual({
function: 'framework-func1',
Expand All @@ -232,6 +234,7 @@ test('Loads function paths from the in-source `config` function', async () => {
pattern: '^/user-func5(?:/(.*))/?$',
excluded_patterns: [],
path: '/user-func5/*',
methods: ['GET'],
})

expect(postCacheRoutes.length).toBe(1)
Expand All @@ -240,6 +243,7 @@ test('Loads function paths from the in-source `config` function', async () => {
pattern: '^/user-func4/?$',
excluded_patterns: [],
path: '/user-func4',
methods: ['POST', 'PUT'],
})

expect(Object.keys(functionConfig)).toHaveLength(1)
Expand Down
3 changes: 3 additions & 0 deletions node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const enum Cache {
Manual = 'manual',
}

export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'

export type Path = `/${string}`

export type OnError = 'fail' | 'bypass' | Path
Expand All @@ -43,6 +45,7 @@ export interface FunctionConfig {
onError?: OnError
name?: string
generator?: string
method?: HTTPMethod | HTTPMethod[]
}

const getConfigExtractor = () => {
Expand Down
7 changes: 4 additions & 3 deletions node/declaration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import regexpAST from 'regexp-tree'

import { FunctionConfig, Path } from './config.js'
import { FunctionConfig, HTTPMethod, Path } from './config.js'
import { FeatureFlags } from './feature_flags.js'

interface BaseDeclaration {
cache?: string
function: string
method?: HTTPMethod | HTTPMethod[]
// todo: remove these two after a while and only support in-source config for non-route related configs
name?: string
generator?: string
Expand Down Expand Up @@ -93,14 +94,14 @@ const createDeclarationsFromFunctionConfigs = (
const declarations: Declaration[] = []

for (const name in functionConfigs) {
const { cache, path } = functionConfigs[name]
const { cache, path, method } = functionConfigs[name]

// If we have a path specified, create a declaration for each path.
if (!functionsVisited.has(name) && path) {
const paths = Array.isArray(path) ? path : [path]

paths.forEach((singlePath) => {
const declaration: Declaration = { function: name, path: singlePath }
const declaration: Declaration = { function: name, path: singlePath, method }
if (cache) {
declaration.cache = cache
}
Expand Down
15 changes: 15 additions & 0 deletions node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface Route {
pattern: string
excluded_patterns: string[]
path?: string
methods?: string[]
}

interface EdgeFunctionConfig {
Expand Down Expand Up @@ -90,6 +91,16 @@ const addExcludedPatterns = (
}
}

/**
* Turns 'get' into ['GET']
*/
const normalizeMethods = (method: undefined | string | string[]) => {
if (!method) return

const methods = Array.isArray(method) ? method : [method]
return methods.map((method) => method.toUpperCase())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We take this from user code that might not even be typed, so if someone uses a non-string value in the method property, we'll throw a bit of a cryptic error when trying to call .toUpperCase()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! added a type check in d9c0008.

}

const generateManifest = ({
bundles = [],
declarations = [],
Expand Down Expand Up @@ -142,6 +153,10 @@ const generateManifest = ({
excluded_patterns: excludedPattern.map(serializePattern),
}

if ('method' in declaration) {
route.methods = normalizeMethods(declaration.method)
}

if ('path' in declaration) {
route.path = declaration.path
}
Expand Down
4 changes: 4 additions & 0 deletions node/validation/manifest/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const routesSchema = {
excluded_patterns: excludedPatternsSchema,
generator: { type: 'string' },
path: { type: 'string' },
methods: {
type: 'array',
items: { type: 'string', enum: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] },
},
},
additionalProperties: false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default async () =>
export const config = {
cache: 'manual',
path: '/user-func4',
method: ['POST', 'PUT'],
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export default async () => new Response('Hello from user function 5.')
export const config = {
path: '/user-func5/*',
excludedPath: '/user-func5/excluded',
method: 'get',
}