Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: de-duplicate paths
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Oct 5, 2023
1 parent 02c89ff commit 9e2cac9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
10 changes: 9 additions & 1 deletion src/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const isPathLiteral = (path: string) => {
return parts.every((part) => !isExpression(part))
}

/**
* Takes an element from a `path` declaration and returns a Route element that
* represents it.
*/
const getRoute = (path: unknown, functionName: string, methods: string[]): Route | undefined => {
if (typeof path !== 'string') {
throw new FunctionBundlingUserError(`'path' property must be a string, found '${typeof path}'`, {
Expand Down Expand Up @@ -58,12 +62,16 @@ const getRoute = (path: unknown, functionName: string, methods: string[]): Route
}
}

/**
* Takes a `path` declaration, normalizes it into an array, and processes the
* individual elements to obtain an array of `Route` expressions.
*/
export const getRoutes = (input: unknown, functionName: string, methods: string[]): Route[] => {
if (!input) {
return []
}

const paths = Array.isArray(input) ? input : [input]
const paths = [...new Set(Array.isArray(input) ? input : [input])]
const routes = paths.map((path) => getRoute(path, functionName, methods ?? [])).filter(nonNullable)

return routes
Expand Down
67 changes: 45 additions & 22 deletions tests/unit/runtimes/node/in_source_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,30 +238,30 @@ describe('V2 API', () => {
})

describe('`path` property', () => {
test('Missing a leading slash', () => {
expect.assertions(4)
describe('Thows an error when invalid values are supplied', () => {
test('Missing a leading slash', () => {
expect.assertions(4)

try {
const source = `export default async () => {
return new Response("Hello!")
}
export const config = {
path: "missing-slash"
}`

try {
const source = `export default async () => {
return new Response("Hello!")
findISCDeclarations(source, options)
} catch (error) {
const { customErrorInfo, message } = error

expect(message).toBe(`'path' property must start with a '/'`)
expect(customErrorInfo.type).toBe('functionsBundling')
expect(customErrorInfo.location.functionName).toBe('func1')
expect(customErrorInfo.location.runtime).toBe('js')
}
export const config = {
path: "missing-slash"
}`

findISCDeclarations(source, options)
} catch (error) {
const { customErrorInfo, message } = error

expect(message).toBe(`'path' property must start with a '/'`)
expect(customErrorInfo.type).toBe('functionsBundling')
expect(customErrorInfo.location.functionName).toBe('func1')
expect(customErrorInfo.location.runtime).toBe('js')
}
})
})

describe('Thows an error when invalid values are supplied', () => {
test('An invalid pattern', () => {
expect.assertions(4)

Expand Down Expand Up @@ -397,7 +397,11 @@ describe('V2 API', () => {
}
export const config = {
path: ["/store/:category/products/:product-id", "/super-awesome-campaign"]
path: [
"/store/:category/products/:product-id",
"/product/:product-id",
"/super-awesome-campaign"
]
}`

const { routes } = findISCDeclarations(source, options)
Expand All @@ -408,8 +412,27 @@ describe('V2 API', () => {
expression: '^\\/store(?:\\/([^\\/]+?))\\/products(?:\\/([^\\/]+?))-id\\/?$',
methods: [],
},
{
pattern: '/product/:product-id',
expression: '^\\/product(?:\\/([^\\/]+?))-id\\/?$',
methods: [],
},
{ pattern: '/super-awesome-campaign', literal: '/super-awesome-campaign', methods: [] },
])
})

test('De-duplicates paths', () => {
const source = `export default async () => {
return new Response("Hello!")
}
export const config = {
path: ["/products", "/products"]
}`

const { routes } = findISCDeclarations(source, options)

expect(routes).toEqual([{ pattern: '/products', literal: '/products', methods: [] }])
})
})
})

0 comments on commit 9e2cac9

Please sign in to comment.