Skip to content

Commit

Permalink
fix: improve DX around not found telefunction
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 5, 2022
1 parent 26b4233 commit c40a378
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
26 changes: 10 additions & 16 deletions telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export { runTelefunc }

import type { TelefuncFiles, Telefunction } from './types'
import type { TelefuncFiles } from './types'
import type { ViteDevServer } from 'vite'
import { assert, assertUsage, checkType, objectAssign } from '../utils'
import { assert, objectAssign } from '../utils'
import { getContextOptional } from './getContext'
import { loadTelefuncFiles } from './runTelefunc/loadTelefuncFiles'
import { parseHttpRequest } from './runTelefunc/parseHttpRequest'
Expand All @@ -13,6 +13,7 @@ import { serializeTelefunctionResult } from './runTelefunc/serializeTelefunction
import { handleError } from './runTelefunc/handleError'
import { executeServerErrorListeners } from './runTelefunc/onTelefuncServerError'
import { applyShield } from './runTelefunc/applyShield'
import { findTelefunction } from './runTelefunc/findTelefunction'

type HttpResponse = {
body: string
Expand Down Expand Up @@ -62,40 +63,33 @@ async function runTelefunc_(runContext: {
if (parsed.isMalformed) {
return malformedRequest
}
const { telefunctionName, telefunctionKey, telefunctionArgs } = parsed
const { telefunctionName, telefunctionKey, telefunctionArgs, telefunctionFilePath, telefunctionFileExport } = parsed
objectAssign(runContext, {
telefunctionName,
telefunctionKey,
telefunctionArgs,
telefunctionFilePath,
telefunctionFileExport,
})
}

{
const telefuncFiles = runContext.telefuncFiles || (await loadTelefuncFiles(runContext))
assert(telefuncFiles, 'No `.telefunc.js` file found')
checkType<TelefuncFiles>(telefuncFiles)
objectAssign(runContext, { telefuncFiles })
runContext.telefuncFiles
}

{
const { telefunctions } = await getTelefunctions(runContext)
checkType<Record<string, Telefunction>>(telefunctions)
objectAssign(runContext, { telefunctions })
}

{
assertUsage(
runContext.telefunctionKey in runContext.telefunctions,
[
`Could not find telefunction ${
runContext.telefunctionName
}. The client is likely out-of-sync with the server, see https://telefunc.com/out-of-sync. Try reloading the client and/or server. Loaded telefunctions: [${Object.keys(
runContext.telefunctions,
).join(', ')}]`,
].join(' '),
)
const telefunction = runContext.telefunctions[runContext.telefunctionKey]
const telefunction = findTelefunction(runContext)
if (!telefunction) {
return malformedRequest
}
objectAssign(runContext, { telefunction })
}

Expand Down
54 changes: 54 additions & 0 deletions telefunc/node/server/runTelefunc/findTelefunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export { findTelefunction }

import { assert, assertUsage, getPluginError } from '../../utils'
import type { Telefunction } from '../types'

function findTelefunction(runContext: {
telefunctionKey: string
telefunctionName: string
telefunctionFilePath: string
telefuncFiles: Record<string, Record<string, unknown>>
telefunctionFileExport: string
telefunctions: Record<string, Telefunction>
isProduction: boolean
}) {
const telefunctionsFound = Object.keys(runContext.telefunctions)
assertUsage(
telefunctionsFound.length > 0,
[
`Telefunction ${runContext.telefunctionName} not found.`,
"Your app doesn't seem to have any `.telefunc.{js|ts|...}` file.",
].join(' '),
)

const telefunction = runContext.telefunctions[runContext.telefunctionKey]
if (!telefunction) {
if (!runContext.isProduction) {
const errMsg = getNotFoundErrMsg()
console.error(getPluginError(errMsg))
}
return null
}

return telefunction

function getNotFoundErrMsg() {
let errMsg = `Telefunction ${runContext.telefunctionName} not found.`
const { telefuncFiles, telefunctionFilePath, telefunctionFileExport } = runContext
const telefuncFile = telefuncFiles[telefunctionFilePath]
if (!telefuncFile) {
errMsg += ` The file \`${runContext.telefunctionFilePath}\` doens't seem to exist.`
} else {
assert(!telefuncFile[telefunctionFileExport])
errMsg += ` The file \`${runContext.telefunctionFilePath}\` doens't seem to have an export \`${telefunctionFileExport}\`.`
}
errMsg += [runContext.telefunctionKey, ...telefunctionsFound]
.sort()
.map(
(telefunctionKey) =>
`\n${telefunctionKey} ${telefunctionsFound.includes(telefunctionKey) ? '[✅ Found]' : '[❌ Not Found]'}`,
)
.join('')
return errMsg
}
}

0 comments on commit c40a378

Please sign in to comment.