Skip to content

Commit

Permalink
fix: warn user when telefunction has no shield()
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 3, 2022
1 parent f8751f0 commit dc08539
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { executeTelefunction } from './runTelefunc/executeTelefunction'
import { serializeTelefunctionResult } from './runTelefunc/serializeTelefunctionResult'
import { handleError } from './runTelefunc/handleError'
import { executeServerErrorListeners } from './runTelefunc/onTelefuncServerError'
import { applyShield } from './runTelefunc/applyShield'

type HttpResponse = {
body: string
Expand Down Expand Up @@ -104,6 +105,8 @@ async function runTelefunc_(runContext: {
objectAssign(runContext, { telefunction })
}

applyShield(runContext)

const { telefunctionReturn, telefunctionAborted, telefunctionHasErrored, telefunctionError } =
await executeTelefunction(runContext)
objectAssign(runContext, {
Expand Down
22 changes: 22 additions & 0 deletions telefunc/node/server/runTelefunc/applyShield.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export { applyShield }

import { shield, shieldApply, shieldIsMissing, shieldToHumandReadable } from '../shield'
import { assertWarning } from '../../utils'
import type { Telefunction } from '../types'

async function applyShield(runContext: {
telefunction: Telefunction
telefunctionFilePath: string
telefunctionExportName: string
telefunctionArgs: unknown[]
}) {
const { telefunction } = runContext
const hasShield = !shieldIsMissing(telefunction)
assertWarning(
hasShield || telefunction.length === 0,
`The telefunction ${runContext.telefunctionExportName} (${runContext.telefunctionFilePath}) accepts arguments yet is missing a \`shield()\`, see https://telefunc.com/shield`,
)
if (hasShield) {
shieldApply(telefunction, runContext.telefunctionArgs)
}
}

0 comments on commit dc08539

Please sign in to comment.