UNPKG

@sgohlke/funpara

Version:

Function parameter library for coding and testing

1 lines 8.07 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/naming-convention */\n\n// Date related types and functions\n\n/**\n * Type for a function that returns a Date.\n */\ntype DateFunction = () => Date\n\n/**\n * Returns a DateFunction that returns the current date.\n * Can for example be used to get the current date when logging.\n * @returns DateFunction function returning the current data\n */\nfunction nowDateFunction(): DateFunction {\n return (): Date => new Date()\n}\n\n/**\n * Returns a DateFunction that returns a fixed date.\n * Can for example be used to get a fixed date for testing.\n * @param dateString fixed date string\n * @returns DateFunction function returning the fixed date\n */\nfunction fixedDateFunction(dateString: string): DateFunction {\n return (): Date => new Date(dateString)\n}\n\n/**\n * Test date\n * '1001-01-01T00:00:00.000Z'\n * as ISO Date string. Can be used to ease and unify testing.\n */\nconst testDateString = '1001-01-01T00:00:00.000Z'\n\n/**\n * DateFunction that returns the test date.\n * Can for example be used to always get the date when testing.\n */\nconst testDateFunction: DateFunction = fixedDateFunction(testDateString)\n\n// Fetch related types and functions\n\n/**\n * Type for a fetch function that given an input (url, request, etc.) and request init\n * returns a Promise<Response>\n */\ntype FetchFunction = (input: string | URL | Request, init?: RequestInit) => Promise<Response>\n\n/**\n * Returns a FetchFunction that always returns a fixed response.\n * Can for example be used to get a fixed Response for testing.\n * @param bodyInit body for the response\n * @param init response init\n * @returns FetchFunction function returning the fixed Response\n */\nfunction fixedResponseFetchFunction(bodyInit?: BodyInit, init?: ResponseInit): FetchFunction {\n return (): Promise<Response> => Promise.resolve(new Response(bodyInit, init))\n}\n\n/**\n * FetchFunction that returns a fixed Response with and empty body and status 400 (Bad Request).\n */\nconst badRequestFetchFunction = fixedResponseFetchFunction(undefined, {\n status: 400,\n})\n\n/**\n * FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found).\n */\nconst notFoundFetchFunction: FetchFunction = fixedResponseFetchFunction(undefined, { status: 404 })\n\n/**\n * FetchFunction that returns a fixed Response with and empty body and status 500 (Internal Server Error).\n */\nconst internalServerErrorFetchFunction = fixedResponseFetchFunction(undefined, {\n status: 500,\n})\n\n/**\n * FetchFunction that returns a fixed Response with broken JSON (missing bracket)\n */\nconst brokenJSONFetchFunction: FetchFunction = fixedResponseFetchFunction(\n '{\"data\": {\"message\": \"Missing bracket\"}',\n {\n headers: { 'Content-Type': 'application/json' },\n status: 200,\n },\n)\n\n/**\n * FetchFunction that returns a fixed Response with an unknown\n * Content-Type ('application/unknown')\n */\nconst unknownContentTypeFetchFunction: FetchFunction = fixedResponseFetchFunction(undefined, {\n headers: { 'Content-Type': 'application/unknown' },\n status: 200,\n})\n\n/**\n * FetchFunction that returns a fixed Response that throws a Timeout error.\n */\nconst timeoutFetchFunction: FetchFunction = (): Promise<Response> =>\n new Promise<Response>(() => {\n throw new Error('Connection failed ETIMEDOUT')\n })\n\n/**\n * FetchFunction that returns a fixed Response with an AggregateError.\n */\nconst aggregateErrorFetchFunction = fixedResponseFetchFunction(\n '{\"errors\":[{\"message\":\"aaa The first error!, The second error!\", \"originalError\": {\"errors\": [{\"message\":\"The first error!\"}, {\"message\":\"The second error!\"} ] } }]}',\n {\n headers: { 'Content-Type': 'application/json' },\n status: 200,\n },\n)\n\n/**\n * FetchFunction that returns a fixed Response that GraphQL introspection is disabled.\n */\nconst graphQLIntrospectionDisabledFetchFunction = fixedResponseFetchFunction(\n '{\"errors\": [ { \"message\": \"Introspection is disabled\"}],\"data\": null}',\n { status: 200 },\n)\n\n/**\n * FetchFunction that returns a fixed Response with an invalid GraphQL schema.\n */\nconst graphQLInvalidSchemaFetchFunction = fixedResponseFetchFunction(\n '{\"data\": {\"__schema\":\"NotAGraphQLSchema\", \"_service\": {\"sdl\":\"NotAGraphQLSchema\"}}}',\n { status: 200 },\n)\n\n/**\n * FetchFunction that returns a fixed Response with an invalid GraphQL body.\n */\nconst graphQLInvalidBodyFetchFunction = fixedResponseFetchFunction(\n '{\"message\": \"I am not GraphQL!\"}',\n { status: 200 },\n)\n\n// Exit function related types and functions\n\n/**\n * Type for a function that given an exit does not return anything.\n * @param {number} code The exit code to use\n * @returns {never} Does not return anything, may e.g. exit the process or throw an error\n */\ntype ExitFunction = (code: number) => never\n\n/**\n * Exit function that does not exit the process but throws an error instead\n * Can be used in testing to avoid the tests exiting the application.\n * @param {number} code The exit code to be thrown in the error\n * @returns {never} Does not return anything but throws an error with the message\n * \"Exit function was called with code CODE\" where CODE is the given code.\n */\nconst doNotExitFunction: ExitFunction = (code: number): never => {\n throw new Error(`Exit function was called with code ${code}`)\n}\n\n// Timeout function related types and functions\n\n/**\n * Type for a function that given a TimerHandler, optional timeout and arguments returns the timeout ID as number.\n * @param {TimerHandler} handler The TimerHandler to be called (i.e. in most cases a callback function)\n * @param {number} timeout The timeout in milliseconds\n * @param {any[]} timeoutArguments The arguments to be passed to the handler\n * @returns {number} The timeout ID as number\n */\ntype TimeoutFunction = (\n handler: TimerHandler,\n timeout?: number,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...timeoutArguments: any[]\n) => number\n\n/**\n * TimeoutFunction that does not call the callback function but returns a fixed timeout ID 1.\n * Can be used in testing to avoid the tests waiting for the timeout to finish.\n * @returns {number} Fixed timeout ID 1\n */\nconst noCallbackTimeoutFunction: TimeoutFunction = (): number => 1\n\nexport {\n aggregateErrorFetchFunction,\n badRequestFetchFunction,\n brokenJSONFetchFunction,\n doNotExitFunction,\n fixedDateFunction,\n fixedResponseFetchFunction,\n graphQLIntrospectionDisabledFetchFunction,\n graphQLInvalidBodyFetchFunction,\n graphQLInvalidSchemaFetchFunction,\n internalServerErrorFetchFunction,\n noCallbackTimeoutFunction,\n notFoundFetchFunction,\n nowDateFunction,\n testDateFunction,\n testDateString,\n timeoutFetchFunction,\n unknownContentTypeFetchFunction,\n}\n\nexport type { DateFunction, ExitFunction, FetchFunction, TimeoutFunction }\n"],"mappings":";;;;;;AAcA,SAAS,kBAAgC;CACvC,6BAAmB,IAAI,KAAK;AAC9B;;;;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