@sgohlke/funpara
Version:
Function parameter library for coding and testing
113 lines (111 loc) • 5.09 kB
TypeScript
/**
* Type for a function that returns a Date.
*/
type DateFunction = () => Date;
/**
* Returns a DateFunction that returns the current date.
* Can for example be used to get the current date when logging.
* @returns DateFunction function returning the current data
*/
declare function nowDateFunction(): DateFunction;
/**
* Returns a DateFunction that returns a fixed date.
* Can for example be used to get a fixed date for testing.
* @param dateString fixed date string
* @returns DateFunction function returning the fixed date
*/
declare function fixedDateFunction(dateString: string): DateFunction;
/**
* Test date
* '1001-01-01T00:00:00.000Z'
* as ISO Date string. Can be used to ease and unify testing.
*/
declare const testDateString = "1001-01-01T00:00:00.000Z";
/**
* DateFunction that returns the test date.
* Can for example be used to always get the date when testing.
*/
declare const testDateFunction: DateFunction;
/**
* Type for a fetch function that given an input (url, request, etc.) and request init
* returns a Promise<Response>
*/
type FetchFunction = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
/**
* Returns a FetchFunction that always returns a fixed response.
* Can for example be used to get a fixed Response for testing.
* @param bodyInit body for the response
* @param init response init
* @returns FetchFunction function returning the fixed Response
*/
declare function fixedResponseFetchFunction(bodyInit?: BodyInit, init?: ResponseInit): FetchFunction;
/**
* FetchFunction that returns a fixed Response with and empty body and status 400 (Bad Request).
*/
declare const badRequestFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found).
*/
declare const notFoundFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with and empty body and status 500 (Internal Server Error).
*/
declare const internalServerErrorFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with broken JSON (missing bracket)
*/
declare const brokenJSONFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with an unknown
* Content-Type ('application/unknown')
*/
declare const unknownContentTypeFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response that throws a Timeout error.
*/
declare const timeoutFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with an AggregateError.
*/
declare const aggregateErrorFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response that GraphQL introspection is disabled.
*/
declare const graphQLIntrospectionDisabledFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with an invalid GraphQL schema.
*/
declare const graphQLInvalidSchemaFetchFunction: FetchFunction;
/**
* FetchFunction that returns a fixed Response with an invalid GraphQL body.
*/
declare const graphQLInvalidBodyFetchFunction: FetchFunction;
/**
* Type for a function that given an exit does not return anything.
* @param {number} code The exit code to use
* @returns {never} Does not return anything, may e.g. exit the process or throw an error
*/
type ExitFunction = (code: number) => never;
/**
* Exit function that does not exit the process but throws an error instead
* Can be used in testing to avoid the tests exiting the application.
* @param {number} code The exit code to be thrown in the error
* @returns {never} Does not return anything but throws an error with the message
* "Exit function was called with code CODE" where CODE is the given code.
*/
declare const doNotExitFunction: ExitFunction;
/**
* Type for a function that given a TimerHandler, optional timeout and arguments returns the timeout ID as number.
* @param {TimerHandler} handler The TimerHandler to be called (i.e. in most cases a callback function)
* @param {number} timeout The timeout in milliseconds
* @param {any[]} timeoutArguments The arguments to be passed to the handler
* @returns {number} The timeout ID as number
*/
type TimeoutFunction = (handler: TimerHandler, timeout?: number, ...timeoutArguments: any[]) => number;
/**
* TimeoutFunction that does not call the callback function but returns a fixed timeout ID 1.
* Can be used in testing to avoid the tests waiting for the timeout to finish.
* @returns {number} Fixed timeout ID 1
*/
declare const noCallbackTimeoutFunction: TimeoutFunction;
export { type DateFunction, type ExitFunction, type FetchFunction, type TimeoutFunction, aggregateErrorFetchFunction, badRequestFetchFunction, brokenJSONFetchFunction, doNotExitFunction, fixedDateFunction, fixedResponseFetchFunction, graphQLIntrospectionDisabledFetchFunction, graphQLInvalidBodyFetchFunction, graphQLInvalidSchemaFetchFunction, internalServerErrorFetchFunction, noCallbackTimeoutFunction, notFoundFetchFunction, nowDateFunction, testDateFunction, testDateString, timeoutFetchFunction, unknownContentTypeFetchFunction };