Skip to content

Commit

Permalink
feat: allow user to manually provide the telefunc files with `telefun…
Browse files Browse the repository at this point in the history
…cConfig.telefuncFiles`
  • Loading branch information
brillout committed Mar 3, 2022
1 parent 74dd7df commit b47f0d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assert, objectAssign } from '../utils'
import { getContextOptional } from './getContext'
import { loadTelefuncFiles } from './runTelefunc/loadTelefuncFiles'
import { parseHttpRequest } from './runTelefunc/parseHttpRequest'
import { getEtag } from './runTelefunc/getEtag'
// import { getEtag } from './runTelefunc/getEtag'
import { getTelefunctions } from './runTelefunc/getTelefunctions'
import { executeTelefunction } from './runTelefunc/executeTelefunction'
import { serializeTelefunctionResult } from './runTelefunc/serializeTelefunctionResult'
Expand Down
9 changes: 9 additions & 0 deletions telefunc/node/server/runTelefunc/loadTelefuncFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import type { TelefuncFiles } from '../types'
import { assertUsage } from '../../utils'
import { loadTelefuncFilesWithVite } from '../../vite/loadTelefuncFilesWithVite'
import { loadTelefuncFilesWithInternalMechanism } from './loadTelefuncFilesWithInternalMechanism'
import { loadTelefuncFilesFromConfig } from './loadTelefuncFilesFromConfig'

async function loadTelefuncFiles(runContext: {
root: string | null
viteDevServer: ViteDevServer | null
isProduction: boolean
telefuncFiles: string[] | null
}): Promise<TelefuncFiles | null> {
// Handles:
// - When the user provides the telefunc file paths with `telefuncConfig.telefuncFiles`
if( runContext.telefuncFiles) {
const telefuncFilesLoaded = loadTelefuncFilesFromConfig(runContext.telefuncFiles, runContext.root)
return telefuncFilesLoaded
}

// Handles:
// - Next.js
// - Nuxt
Expand Down
23 changes: 23 additions & 0 deletions telefunc/node/server/runTelefunc/loadTelefuncFilesFromConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export { loadTelefuncFilesFromConfig }

import { assert, assertPosixPath, assertUsage, toPosixPath } from '../../utils'
import { posix } from 'path'
import type { TelefuncFiles } from '../types'

async function loadTelefuncFilesFromConfig(telefuncFiles: string[], root: string | null): Promise<TelefuncFiles> {
assertUsage(root, 'You need to set `telefuncConfig.root`.')
const telefuncFilesLoaded: TelefuncFiles = {}
await Promise.all(
telefuncFiles.map(async (telefuncFilePath) => {
const path = posix.relative(toPosixPath(root), toPosixPath(telefuncFilePath))
assertPosixPath(path)
assertUsage(
!path.startsWith('../'),
`The telefunc file \`${telefuncFilePath}\` is not inlcuded in your project root \`${root}\`.`,
)
assert(!path.startsWith('/') && !path.startsWith('.'))
telefuncFilesLoaded['/' + path] = await import(telefuncFilePath)
}),
)
return telefuncFilesLoaded
}
9 changes: 6 additions & 3 deletions telefunc/node/server/telefuncConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export const telefuncConfig = getTelefuncConfigObject()

import type { ViteDevServer } from 'vite'
import type { Telefunction } from './types'
import { isAbsolute } from 'path'
import { assert, assertInfo, assertUsage, assertWarning, hasProp } from '../utils'
import { globalContext } from './globalContext'
Expand All @@ -14,7 +13,7 @@ type TelefuncServerConfig = {
isProduction: boolean
viteDevServer: ViteDevServer | null
disableEtag: boolean
telefuncFiles: Record<string, Record<string, Telefunction>> | null
telefuncFiles: string[] | null
debug: boolean
}

Expand Down Expand Up @@ -67,8 +66,12 @@ const configSpec = {
},
},
telefuncFiles: {
validate(_val: unknown) {
validate(val: unknown) {
assertWarning(false, '`telefuncConfig.telefuncFiles` is experimental')
assertUsage(
Array.isArray(val) && val.every((v) => typeof v === 'string' && isAbsolute(v)),
'`telefuncConfig.telefuncFiles` should be a list of absolute paths',
)
},
getDefault() {
return null
Expand Down

0 comments on commit b47f0d9

Please sign in to comment.