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: support for multiple paths in in source configuration #230

Merged
merged 20 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a9df376
feat: support for multiple paths in in source configuration
jackiewmacharia Nov 30, 2022
d45f88d
Merge branch 'main' into feat/support-multiple-paths-in-isc
jackiewmacharia Nov 30, 2022
f990697
fix: use array for isc path properties
jackiewmacharia Nov 30, 2022
c20ccf0
fix: support both array and string path properties
jackiewmacharia Nov 30, 2022
0b39add
fix: fix linting
jackiewmacharia Nov 30, 2022
d845c48
fix: have a string usecase in declaration tests by transforming json …
khendrikse Dec 5, 2022
478bc60
fix: remove file-level eslint-disable-max-depth
khendrikse Dec 5, 2022
cbf007d
fix: change conditional for cache config and path length
khendrikse Dec 5, 2022
398a931
Merge branch 'main' into feat/support-multiple-paths-in-isc
khendrikse Dec 5, 2022
fc6a022
fix: change conditional for adding all different types of config
khendrikse Dec 5, 2022
e401ff3
fix: remove fallback to empty object
khendrikse Dec 5, 2022
4ef8af2
fix: change comment
khendrikse Dec 5, 2022
9b08b77
fix: undo changing config fixtures to use an array as path
khendrikse Dec 6, 2022
2d673bb
fix: update comments
khendrikse Dec 6, 2022
c70c4da
Update node/declaration.ts
khendrikse Dec 6, 2022
f7c1a06
Merge branch 'main' into feat/support-multiple-paths-in-isc
khendrikse Dec 6, 2022
1c88b02
Merge branch 'main' into feat/support-multiple-paths-in-isc
khendrikse Dec 6, 2022
2b8ca03
fix: change mutation of config.path
khendrikse Dec 6, 2022
66e2eda
Merge branch 'main' into feat/support-multiple-paths-in-isc
khendrikse Dec 6, 2022
eb129ba
fix: formatting
khendrikse Dec 6, 2022
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
2 changes: 1 addition & 1 deletion node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const enum Cache {

export interface FunctionConfig {
cache?: Cache
path?: string
path?: string | string[]
}

const getConfigExtractor = () => {
Expand Down
64 changes: 58 additions & 6 deletions node/declaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ const deployConfig = {
layers: [],
}

test('In source config takes precedence over netlify.toml config', () => {
test('In-source config takes precedence over netlify.toml config', () => {
const tomlConfig = [
{ function: 'geolocation', path: '/geo', cache: 'off' },
{ function: 'json', path: '/json', cache: 'manual' },
]

const funcConfig = {
geolocation: { path: '/geo-isc', cache: 'manual' },
geolocation: { path: ['/geo-isc', '/*'], cache: 'manual' },
json: { path: '/json', cache: 'off' },
} as Record<string, FunctionConfig>

const expectedDeclarations = [
{ function: 'geolocation', path: '/geo-isc', cache: 'manual' },
{ function: 'geolocation', path: '/*', cache: 'manual' },
{ function: 'json', path: '/json', cache: 'off' },
]

Expand All @@ -30,14 +31,14 @@ test('In source config takes precedence over netlify.toml config', () => {
expect(declarations).toEqual(expectedDeclarations)
})

test("Declarations don't break if no in source config is provided", () => {
test("Declarations don't break if no in-source config is provided", () => {
const tomlConfig = [
{ function: 'geolocation', path: '/geo', cache: 'off' },
{ function: 'json', path: '/json', cache: 'manual' },
]

const funcConfig = {
geolocation: { path: '/geo-isc', cache: 'manual' },
geolocation: { path: ['/geo-isc'], cache: 'manual' },
json: {},
} as Record<string, FunctionConfig>

Expand All @@ -51,11 +52,11 @@ test("Declarations don't break if no in source config is provided", () => {
expect(declarations).toEqual(expectedDeclarations)
})

test('In source config works independent of the netlify.toml file if a path is defined and otherwise if no path is set', () => {
test('In-source config works independent of the netlify.toml file if a path is defined and otherwise if no path is set', () => {
const tomlConfig = [{ function: 'geolocation', path: '/geo', cache: 'off' }]

const funcConfigWithPath = {
json: { path: '/json', cache: 'off' },
json: { path: ['/json', '/json-isc'], cache: 'off' },
} as Record<string, FunctionConfig>

const funcConfigWithoutPath = {
Expand All @@ -65,6 +66,7 @@ test('In source config works independent of the netlify.toml file if a path is d
const expectedDeclarationsWithISCPath = [
{ function: 'geolocation', path: '/geo', cache: 'off' },
{ function: 'json', path: '/json', cache: 'off' },
{ function: 'json', path: '/json-isc', cache: 'off' },
]

const expectedDeclarationsWithoutISCPath = [{ function: 'geolocation', path: '/geo', cache: 'off' }]
Expand All @@ -75,3 +77,53 @@ test('In source config works independent of the netlify.toml file if a path is d
const declarationsWithoutISCPath = getDeclarationsFromConfig(tomlConfig, funcConfigWithoutPath, deployConfig)
expect(declarationsWithoutISCPath).toEqual(expectedDeclarationsWithoutISCPath)
})

test('In-source config works if only the cache config property is set', () => {
const tomlConfig = [{ function: 'geolocation', path: '/geo', cache: 'off' }]

const funcConfig = {
geolocation: { cache: 'manual' },
} as Record<string, FunctionConfig>

const expectedDeclarations = [{ function: 'geolocation', path: '/geo', cache: 'manual' }]

expect(getDeclarationsFromConfig(tomlConfig, funcConfig, deployConfig)).toEqual(expectedDeclarations)
})

test("In-source config path property works if it's not an array", () => {
const tomlConfig = [{ function: 'json', path: '/json-toml', cache: 'off' }]

const funcConfig = {
json: { path: '/json', cache: 'manual' },
} as Record<string, FunctionConfig>

const expectedDeclarations = [{ function: 'json', path: '/json', cache: 'manual' }]

expect(getDeclarationsFromConfig(tomlConfig, funcConfig, deployConfig)).toEqual(expectedDeclarations)
})

test("In-source config path property works if it's not an array and it's not present in toml or deploy config", () => {
const tomlConfig = [{ function: 'geolocation', path: '/geo', cache: 'off' }]
const funcConfig = {
json: { path: '/json-isc', cache: 'manual' },
} as Record<string, FunctionConfig>

const expectedDeclarations = [
{ function: 'geolocation', path: '/geo', cache: 'off' },
{ function: 'json', path: '/json-isc', cache: 'manual' },
]

expect(getDeclarationsFromConfig(tomlConfig, funcConfig, deployConfig)).toEqual(expectedDeclarations)
})

test('In-source config works if path property is an empty array with cache value specified', () => {
const tomlConfig = [{ function: 'json', path: '/json-toml', cache: 'off' }]

const funcConfig = {
json: { path: [], cache: 'manual' },
} as Record<string, FunctionConfig>

const expectedDeclarations = [{ function: 'json', path: '/json-toml', cache: 'manual' }]

expect(getDeclarationsFromConfig(tomlConfig, funcConfig, deployConfig)).toEqual(expectedDeclarations)
})
34 changes: 30 additions & 4 deletions node/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,45 @@ export const getDeclarationsFromConfig = (
// a function configuration object, we replace the path because that object
// takes precedence.
for (const declaration of [...tomlDeclarations, ...deployConfig.declarations]) {
const config = functionsConfig[declaration.function] ?? {}
const config = functionsConfig[declaration.function]

// If no config is found, add the declaration as is
if (!config) {
declarations.push(declaration)

// If we have a path specified as either a string or non-tempty array
// create a declaration for each path
} else if (config.path?.length) {
// eslint-disable-next-line max-depth
if (!Array.isArray(config.path)) config.path = [config.path]

config.path.forEach((path) => {
declarations.push({ ...declaration, ...config, path })
})

// With an in-source config without a path, add the config to the declaration
} else {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { path, ...rest } = config
declarations.push({ ...declaration, ...rest })
}

functionsVisited.add(declaration.function)
declarations.push({ ...declaration, ...config })
}

// Finally, we must create declarations for functions that are not declared
// in the TOML at all.
for (const name in functionsConfig) {
const { path, ...config } = functionsConfig[name]
const { ...config } = functionsConfig[name]
let { path } = functionsConfig[name]

// If we have path specified create a declaration for each path
if (!functionsVisited.has(name) && path) {
declarations.push({ ...config, function: name, path })
if (!Array.isArray(path)) path = [path]

path.forEach((singlePath) => {
declarations.push({ ...config, function: name, path: singlePath })
})
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"test:dev": "run-s test:dev:*",
"test:ci": "run-s test:ci:*",
"test:dev:vitest": "cross-env FORCE_COLOR=0 vitest run",
"test:dev:vitest:watch": "cross-env FORCE_COLOR=0 vitest watch",
"test:dev:deno": "deno test --allow-all deno",
"test:ci:vitest": "cross-env FORCE_COLOR=0 vitest run",
"test:ci:deno": "deno test --allow-all deno",
Expand Down