Skip to content

Commit

Permalink
fix: implement context argument
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Nov 14, 2022
1 parent c8c3e36 commit d3a6d63
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
17 changes: 14 additions & 3 deletions telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export { runTelefunc }
export type { HttpResponse }

import { assert, objectAssign, isProduction } from '../utils'
import { getContextOptional, isAsyncMode } from './getContext'
import { Telefunc } from './getContext'
import { loadTelefuncFiles } from './runTelefunc/loadTelefuncFiles'
import { parseHttpRequest } from './runTelefunc/parseHttpRequest'
// import { getEtag } from './runTelefunc/getEtag'
Expand All @@ -14,10 +15,15 @@ import { findTelefunction } from './runTelefunc/findTelefunction'
import { globalContext } from './globalContext'
import { resolveServerConfig } from './serverConfig'

/** The HTTP Response of a telefunction remote call HTTP Request */
type HttpResponse = {
/** HTTP Response Status Code */
statusCode: 200 | 403 | 500 | 400
/** HTTP Response Body */
body: string
/** HTTP Response Header `Content-Type` */
contentType: 'text/plain'
/** HTTP Response Header `ETag` */
etag: string | null
}

Expand Down Expand Up @@ -54,7 +60,12 @@ async function runTelefunc(runContext: Parameters<typeof runTelefunc_>[0]) {
}
}

async function runTelefunc_(httpRequest: { url: string; method: string; body: unknown }): Promise<HttpResponse> {
async function runTelefunc_(httpRequest: {
url: string
method: string
body: unknown
context?: Telefunc.Context
}): Promise<HttpResponse> {
const runContext = {}
{
const serverConfig = resolveServerConfig()
Expand All @@ -76,7 +87,7 @@ async function runTelefunc_(httpRequest: { url: string; method: string; body: un
}

objectAssign(runContext, {
providedContext: isAsyncMode() ? null : getContextOptional() || null
providedContext: httpRequest.context || null
})
{
const parsed = parseHttpRequest(runContext)
Expand Down
34 changes: 23 additions & 11 deletions telefunc/node/server/telefunc.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
export { telefunc }

import { runTelefunc } from './runTelefunc'
import { runTelefunc, HttpResponse } from './runTelefunc'
import { Telefunc } from './getContext'
import { assertUsage, hasProp, isObject } from '../utils'

/**
* Get the HTTP response of a telefunction call.
* @param httpRequest.url HTTP request URL
* @param httpRequest.method HTTP request method
* @param httpRequest.body HTTP request body
* @returns HTTP response
/** Get HTTP Response for a telefunction remote call HTTP Request.
* @returns HTTP Response
*/
async function telefunc(httpRequest: { url: string; body: string; method: string }) {
async function telefunc(httpRequest: {
/** The URL of the HTTP Request */
url: string
/** The method of the HTTP Request ('GET', 'POST', ...) */
method: string
/** The body of HTTP Request */
body: string
/** The context object, see https://telefunc.com/getContext */
context?: Telefunc.Context
}): Promise<HttpResponse> {
assertHttpRequest(httpRequest, arguments.length)
const httpResponse = await runTelefunc(httpRequest)
return httpResponse
}

function assertHttpRequest(httpRequest: unknown, numberOfArguments: number) {
assertUsage(httpRequest, '`telefunc(httpRequest)`: argument `httpRequest` is missing.')
assertUsage(numberOfArguments === 1, '`telefunc()`: all arguments should be passed as a single argument object.')
assertUsage(isObject(httpRequest), '`telefunc(httpRequest)`: argument `httpRequest` should be an object.')
assertUsage(httpRequest, '`telefunc(httpRequest)`: argument `httpRequest` is missing.')
assertUsage(isObject(httpRequest), '`telefunc(httpRequest)`: argument `httpRequest` should be an object.')
assertUsage(hasProp(httpRequest, 'url'), '`telefunc({ url })`: argument `url` is missing.')
assertUsage(hasProp(httpRequest, 'url', 'string'), '`telefunc({ url })`: argument `url` should be a string.')
assertUsage(hasProp(httpRequest, 'method'), '`telefunc({ method })`: argument `method` is missing.')
assertUsage(hasProp(httpRequest, 'method', 'string'), '`telefunc({ method })`: argument `method` should be a string.')
assertUsage(hasProp(httpRequest, 'body'), '`telefunc({ body })`: argument `body` is missing.')
assertUsage(
!('context' in httpRequest) || hasProp(httpRequest, 'context', 'object'),
'`telefunc({ context })`: argument `context` should be an object.'
)
Object.keys(httpRequest).forEach((key) => {
assertUsage(['url', 'method', 'body'].includes(key), '`telefunc({ ' + key + ' })`: Unknown argument `' + key + '`.')
assertUsage(
['url', 'method', 'body', 'context'].includes(key),
'`telefunc({ ' + key + ' })`: Unknown argument `' + key + '`.'
)
})
// We further assert the `httpRequest` in `./runTelefunc/parseHttpRequest.ts`
}

0 comments on commit d3a6d63

Please sign in to comment.