Skip to content

Commit

Permalink
fix: improve telefunction's human readable name
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 3, 2022
1 parent c1a2ee9 commit c8dd213
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 26 deletions.
3 changes: 3 additions & 0 deletions telefunc/client/callTelefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { __internal_fetchTelefunc }

import { makeHttpRequest } from './callTelefunc/makeHttpRequest'
import { serializeTelefunctionArguments } from './callTelefunc/serializeTelefunctionArguments'
import { getTelefunctionName } from './callTelefunc/getTelefunctionName'
import { telefuncConfig } from './telefuncConfig'
import { objectAssign, assertUsage, isBrowser, assert } from './utils'

Expand All @@ -17,7 +18,9 @@ async function __internal_fetchTelefunc(

const callContext = {}
{
const telefunctionName = getTelefunctionName({ telefunctionFileExport, telefunctionFilePath })
objectAssign(callContext, {
telefunctionName,
telefunctionFilePath,
telefunctionFileExport,
telefunctionArgs,
Expand Down
11 changes: 11 additions & 0 deletions telefunc/client/callTelefunc/getTelefunctionName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { getTelefunctionName }

function getTelefunctionName({
telefunctionFilePath,
telefunctionFileExport,
}: {
telefunctionFilePath: string
telefunctionFileExport: string
}) {
return `\`${telefunctionFileExport}()\` (${telefunctionFilePath})`
}
7 changes: 2 additions & 5 deletions telefunc/client/callTelefunc/makeHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type { TelefunctionError } from '../TelefunctionError'
async function makeHttpRequest(callContext: {
telefuncUrl: string
httpRequestBody: string
telefunctionFilePath: string
telefunctionFileExport: string
telefunctionName: string
}): Promise<{ telefunctionReturn: unknown } | { telefunctionCallError: TelefunctionError }> {
const method = 'POST'

Expand Down Expand Up @@ -74,9 +73,7 @@ async function makeHttpRequest(callContext: {
} else {
assert('abort' in responseValue)
const abortValue = responseValue.ret
const telefunctionCallError = new Error(
`Abort. (Telefunction \`${callContext.telefunctionFileExport}\` of ${callContext.telefunctionFilePath}.)`,
)
const telefunctionCallError = new Error(`Abort. (Telefunction ${callContext.telefunctionName}.)`)
objectAssign(telefunctionCallError, { isAbort: true as const, abortValue })
executeCallErrorListeners(telefunctionCallError)
return { telefunctionCallError }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function serializeTelefunctionArguments(callContext: {
telefunctionFileExport: string
telefunctionArgs: unknown[]
telefuncUrl: string
telefunctionName: string
}) {
const bodyParsed = {
file: callContext.telefunctionFilePath,
Expand All @@ -25,7 +26,7 @@ function serializeTelefunctionArguments(callContext: {
assertUsage(
false,
[
`Cannot serialize arguments for telefunction \`${callContext.telefunctionFileExport}\` (${callContext.telefunctionFilePath}).`,
`Cannot serialize arguments for telefunction ${callContext.telefunctionName}.`,
'Make sure that the arguments pass to telefunction calls are always serializable.',
`Serialization error: ${lowercaseFirstLetter(err.message)}`,
].join(' '),
Expand Down
17 changes: 9 additions & 8 deletions telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ async function runTelefunc_(runContext: {
if (parsed.isMalformed) {
return malformedRequest
}
const { telefunctionFilePath, telefunctionFileExport, telefunctionKey, telefunctionArgs } = parsed
const { telefunctionName, telefunctionKey, telefunctionArgs } = parsed
objectAssign(runContext, {
telefunctionFilePath,
telefunctionFileExport,
telefunctionName,
telefunctionKey,
telefunctionArgs,
})
Expand All @@ -88,11 +87,13 @@ async function runTelefunc_(runContext: {
{
assertUsage(
runContext.telefunctionKey in runContext.telefunctions,
`Could not find telefunction \`${runContext.telefunctionFileExport}\` (${
runContext.telefunctionFilePath
}). 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(', ')}]`,
[
`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]
objectAssign(runContext, { telefunction })
Expand Down
5 changes: 2 additions & 3 deletions telefunc/node/server/runTelefunc/applyShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import type { Telefunction } from '../types'

async function applyShield(runContext: {
telefunction: Telefunction
telefunctionFilePath: string
telefunctionFileExport: string
telefunctionName: string
telefunctionArgs: unknown[]
}) {
const { telefunction } = runContext
const hasShield = !shieldIsMissing(telefunction)
assertWarning(
hasShield || telefunction.length === 0,
`The telefunction ${runContext.telefunctionFileExport} (${runContext.telefunctionFilePath}) accepts arguments yet is missing a \`shield()\`, see https://telefunc.com/shield`,
`The telefunction ${runContext.telefunctionName} accepts arguments yet is missing a \`shield()\`, see https://telefunc.com/shield`,
)
if (hasShield) {
shieldApply(telefunction, runContext.telefunctionArgs)
Expand Down
9 changes: 4 additions & 5 deletions telefunc/node/server/runTelefunc/executeTelefunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { assertUsage, isPromise } from '../../utils'

async function executeTelefunction(runContext: {
telefunction: Telefunction
telefunctionFilePath: string
telefunctionFileExport: string
telefunctionName: string
telefunctionArgs: unknown[]
telefunctions: Record<string, Telefunction>
providedContext: Telefunc.Context | null
Expand All @@ -25,11 +24,11 @@ async function executeTelefunction(runContext: {
const onError = (err: unknown) => {
assertUsage(
typeof err === 'object' && err !== null,
`The telefunction ${runContext.telefunctionFileExport} (${runContext.telefunctionFilePath}) threw a non-object error: \`${err}\`. Make sure the telefunction does \`throw new Error(${err})\` instead.`,
`The telefunction ${runContext.telefunctionName} threw a non-object error: \`${err}\`. Make sure the telefunction does \`throw new Error(${err})\` instead.`,
)
assertUsage(
err !== Abort,
`Missing parentheses \`()\` in \`throw Abort\`: it should be \`throw Abort()\`. Telefunction: ${runContext.telefunctionFileExport} (${runContext.telefunctionFilePath}).`,
`Missing parentheses \`()\` in \`throw Abort\`: it should be \`throw Abort()\`. Telefunction: ${runContext.telefunctionName}.`,
)
if (isAbort(err)) {
telefunctionAborted = true
Expand All @@ -51,7 +50,7 @@ async function executeTelefunction(runContext: {
if (!telefunctionHasErrored && !telefunctionAborted) {
assertUsage(
isPromise(resultSync),
`The telefunction ${runContext.telefunctionFileExport} (${runContext.telefunctionFilePath}) did not return a promise. A telefunction should always return a promise (e.g. define it as a \`async function\`).`,
`The telefunction ${runContext.telefunctionName} did not return a promise. A telefunction should always return a promise (e.g. define it as a \`async function\`).`,
)
try {
telefunctionReturn = await resultSync
Expand Down
1 change: 1 addition & 0 deletions telefunc/node/server/runTelefunc/getTelefunctionName.ts
6 changes: 5 additions & 1 deletion telefunc/node/server/runTelefunc/getTelefunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { getTelefunctions }
import { assertUsage, isCallable } from '../../utils'
import type { Telefunction, TelefuncFiles } from '../types'
import { getTelefunctionKey } from './getTelefunctionKey'
import { getTelefunctionName } from './getTelefunctionName'

async function getTelefunctions(runContext: { telefuncFiles: TelefuncFiles }): Promise<{
telefunctions: Record<string, Telefunction>
Expand Down Expand Up @@ -34,6 +35,9 @@ function assertTelefunction(
): asserts telefunction is Telefunction {
assertUsage(
isCallable(telefunction),
`The telefunction \`${telefunctionFileExport}\` (${telefunctionFilePath}) is not a function. Make sure the \`export { ${telefunctionFileExport} }\` of ${telefunctionFilePath} to be a function.`,
`The telefunction ${getTelefunctionName({
telefunctionFileExport,
telefunctionFilePath,
})} is not a function. Make sure the \`export { ${telefunctionFileExport} }\` of ${telefunctionFilePath} to be a function.`,
)
}
4 changes: 4 additions & 0 deletions telefunc/node/server/runTelefunc/parseHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { parseHttpRequest }
import { parse } from '@brillout/json-s/parse'
import { getTelefunctionKey } from './getTelefunctionKey'
import { assertUsage, hasProp, getPluginError, getUrlPathname } from '../../utils'
import { getTelefunctionName } from './getTelefunctionName'

const devErrMsgPrefix =
'Malformed request in development. This is unexpected since, in development, all requests are expected to originate from the Telefunc Client. If this error is happening in production, then either the environment variable `NODE_ENV="production"` or `telefunc({ isProduction: true })` is missing.'
Expand All @@ -15,6 +16,7 @@ function parseHttpRequest(runContext: {
| {
telefunctionFilePath: string
telefunctionFileExport: string
telefunctionName: string
telefunctionKey: string
telefunctionArgs: unknown[]
isMalformed: false
Expand Down Expand Up @@ -83,10 +85,12 @@ function parseHttpRequest(runContext: {
const telefunctionFileExport = bodyParsed.name
const telefunctionArgs = bodyParsed.args
const telefunctionKey = getTelefunctionKey({ telefunctionFilePath, telefunctionFileExport })
const telefunctionName = getTelefunctionName({ telefunctionFilePath, telefunctionFileExport })

return {
telefunctionFilePath,
telefunctionFileExport,
telefunctionName,
telefunctionKey,
telefunctionArgs,
isMalformed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { assert, assertUsage, hasProp, lowercaseFirstLetter } from '../../utils'

function serializeTelefunctionResult(runContext: {
telefunctionReturn: unknown
telefunctionFileExport: string
telefunctionFilePath: string
telefunctionName: string
telefunctionAborted: boolean
}) {
const bodyValue: Record<string, unknown> = {
Expand All @@ -23,7 +22,7 @@ function serializeTelefunctionResult(runContext: {
assertUsage(
false,
[
`Cannot serialize value returned by telefunction \`${runContext.telefunctionFileExport}\` (${runContext.telefunctionFilePath}).`,
`Cannot serialize value returned by telefunction ${runContext.telefunctionName}.`,
'Make sure that telefunctions always return a serializable value.',
`Serialization error: ${lowercaseFirstLetter(err.message)}`,
].join(' '),
Expand Down

0 comments on commit c8dd213

Please sign in to comment.