-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} |