Skip to content

Commit

Permalink
feat!: replace mode property with cache (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Nov 10, 2022
1 parent 8529a59 commit 5ca08c2
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'max-lines': 'off',
'max-statements': 'off',
'node/no-missing-import': 'off',
'no-shadow': 'off',
'unicorn/prefer-json-parse-buffer': 'off',
},
overrides: [
Expand Down
10 changes: 4 additions & 6 deletions node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ImportMap } from './import_map.js'
import { Logger } from './logger.js'
import { getPackagePath } from './package_json.js'

// eslint-disable-next-line no-shadow
enum ConfigExitCode {
Success = 0,
UnhandledError = 1,
Expand All @@ -21,14 +20,13 @@ enum ConfigExitCode {
SerializationError,
}

// eslint-disable-next-line no-shadow
export const enum Mode {
BeforeCache = 'before-cache',
AfterCache = 'after-cache',
export const enum Cache {
Off = 'off',
Manual = 'manual',
}

export interface FunctionConfig {
mode?: Mode
cache?: Cache
path?: string
}

Expand Down
2 changes: 1 addition & 1 deletion node/declaration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FunctionConfig } from './config.js'

interface BaseDeclaration {
cache?: string
function: string
mode?: string
name?: string
}

Expand Down
4 changes: 2 additions & 2 deletions node/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ test('Generates a manifest with pre and post-cache routes', () => {
]
const declarations = [
{ function: 'func-1', path: '/f1' },
{ function: 'func-2', mode: 'not_a_supported_mode', path: '/f2' },
{ function: 'func-3', mode: 'after-cache', path: '/f3' },
{ function: 'func-2', cache: 'not_a_supported_value', path: '/f2' },
{ function: 'func-3', cache: 'manual', path: '/f3' },
]
const manifest = generateManifest({ bundles: [bundle1, bundle2], declarations, functions })

Expand Down
4 changes: 2 additions & 2 deletions node/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path'
import globToRegExp from 'glob-to-regexp'

import type { Bundle } from './bundle.js'
import { Mode } from './config.js'
import { Cache } from './config.js'
import type { Declaration } from './declaration.js'
import { EdgeFunction } from './edge_function.js'
import { getPackageVersion } from './package_json.js'
Expand Down Expand Up @@ -50,7 +50,7 @@ const generateManifest = ({ bundles = [], declarations = [], functions }: Genera
pattern: serializablePattern,
}

if (declaration.mode === Mode.AfterCache) {
if (declaration.cache === Cache.Manual) {
postCacheRoutes.push(route)
} else {
preCacheRoutes.push(route)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default async () => new Response('Hello from user function 3')

export const config = () => ({
mode: 'not_a_supported_mode',
cache: 'not_a_supported_value',
path: '/user-func3',
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export default async () => new Response('Hello from user function 4. I should run after the cache!')
export default async () =>
new Response('Hello from user function 4. I will be cached!', {
headers: {
'cache-control': 'public, s-maxage=60',
},
})

export const config = () => ({
mode: 'after-cache',
cache: 'manual',
path: '/user-func4',
})

0 comments on commit 5ca08c2

Please sign in to comment.