Skip to content

Commit

Permalink
fix: deep merge in source and toml config (#220)
Browse files Browse the repository at this point in the history
* fix: deep merge in source and toml config
  • Loading branch information
jackiewmacharia committed Nov 23, 2022
1 parent fb8ca47 commit e2789e3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
72 changes: 72 additions & 0 deletions node/declaration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { test, expect } from 'vitest'

import { FunctionConfig } from './config.js'
import { getDeclarationsFromConfig } from './declaration.js'

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' },
json: { path: '/json', cache: 'off' },
} as Record<string, FunctionConfig>

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

const declarations = getDeclarationsFromConfig(tomlConfig, funcConfig)

expect(declarations).toEqual(expectedDeclarations)
})

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' },
json: {},
} as Record<string, FunctionConfig>

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

const declarations = getDeclarationsFromConfig(tomlConfig, funcConfig)

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', () => {
const tomlConfig = [{ function: 'geolocation', path: '/geo', cache: 'off' }]

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

const funcConfigWithoutPath = {
json: { cache: 'off' },
} as Record<string, FunctionConfig>

const expectedDeclarationsWithISCPath = [
{ function: 'geolocation', path: '/geo', cache: 'off' },
{ function: 'json', path: '/json', cache: 'off' },
]

const expectedDeclarationsWithoutISCPath = [{ function: 'geolocation', path: '/geo', cache: 'off' }]

const declarationsWithISCPath = getDeclarationsFromConfig(tomlConfig, funcConfigWithPath)

const declarationsWithoutISCPath = getDeclarationsFromConfig(tomlConfig, funcConfigWithoutPath)

expect(declarationsWithISCPath).toEqual(expectedDeclarationsWithISCPath)
expect(declarationsWithoutISCPath).toEqual(expectedDeclarationsWithoutISCPath)
})
13 changes: 5 additions & 8 deletions node/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ export const getDeclarationsFromConfig = (

// We start by iterating over all the TOML declarations. For any declaration
// for which we also have a function configuration object, we replace the
// path because that object takes precedence.
// defined config (currently path or cache or both) because that object takes
// precedence.
for (const declaration of tomlDeclarations) {
const { path } = functionsConfig[declaration.function] ?? {}
const config = functionsConfig[declaration.function] ?? {}

if (path) {
functionsVisited.add(declaration.function)
functionsVisited.add(declaration.function)

declarations.push({ ...declaration, path })
} else {
declarations.push(declaration)
}
declarations.push({ ...declaration, ...config })
}

// Finally, we must create declarations for functions that are not declared
Expand Down

0 comments on commit e2789e3

Please sign in to comment.