UNPKG

@sgohlke/funpara

Version:

Function parameter library for coding and testing

111 lines (110 loc) 4.97 kB
//#region src/index.ts /** * 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 */ function nowDateFunction() { return () => /* @__PURE__ */ new Date(); } /** * 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 */ function fixedDateFunction(dateString) { return () => new Date(dateString); } /** * Test date * '1001-01-01T00:00:00.000Z' * as ISO Date string. Can be used to ease and unify testing. */ 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. */ const testDateFunction = fixedDateFunction(testDateString); /** * 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 */ function fixedResponseFetchFunction(bodyInit, init) { return () => Promise.resolve(new Response(bodyInit, init)); } /** * FetchFunction that returns a fixed Response with and empty body and status 400 (Bad Request). */ const badRequestFetchFunction = fixedResponseFetchFunction(void 0, { status: 400 }); /** * FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found). */ const notFoundFetchFunction = fixedResponseFetchFunction(void 0, { status: 404 }); /** * FetchFunction that returns a fixed Response with and empty body and status 500 (Internal Server Error). */ const internalServerErrorFetchFunction = fixedResponseFetchFunction(void 0, { status: 500 }); /** * FetchFunction that returns a fixed Response with broken JSON (missing bracket) */ const brokenJSONFetchFunction = fixedResponseFetchFunction("{\"data\": {\"message\": \"Missing bracket\"}", { headers: { "Content-Type": "application/json" }, status: 200 }); /** * FetchFunction that returns a fixed Response with an unknown * Content-Type ('application/unknown') */ const unknownContentTypeFetchFunction = fixedResponseFetchFunction(void 0, { headers: { "Content-Type": "application/unknown" }, status: 200 }); /** * FetchFunction that returns a fixed Response that throws a Timeout error. */ const timeoutFetchFunction = () => new Promise(() => { throw new Error("Connection failed ETIMEDOUT"); }); /** * FetchFunction that returns a fixed Response with an AggregateError. */ const aggregateErrorFetchFunction = fixedResponseFetchFunction("{\"errors\":[{\"message\":\"aaa The first error!, The second error!\", \"originalError\": {\"errors\": [{\"message\":\"The first error!\"}, {\"message\":\"The second error!\"} ] } }]}", { headers: { "Content-Type": "application/json" }, status: 200 }); /** * FetchFunction that returns a fixed Response that GraphQL introspection is disabled. */ const graphQLIntrospectionDisabledFetchFunction = fixedResponseFetchFunction("{\"errors\": [ { \"message\": \"Introspection is disabled\"}],\"data\": null}", { status: 200 }); /** * FetchFunction that returns a fixed Response with an invalid GraphQL schema. */ const graphQLInvalidSchemaFetchFunction = fixedResponseFetchFunction("{\"data\": {\"__schema\":\"NotAGraphQLSchema\", \"_service\": {\"sdl\":\"NotAGraphQLSchema\"}}}", { status: 200 }); /** * FetchFunction that returns a fixed Response with an invalid GraphQL body. */ const graphQLInvalidBodyFetchFunction = fixedResponseFetchFunction("{\"message\": \"I am not GraphQL!\"}", { status: 200 }); /** * 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. */ const doNotExitFunction = (code) => { throw new Error(`Exit function was called with code ${code}`); }; /** * 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 */ const noCallbackTimeoutFunction = () => 1; //#endregion export { aggregateErrorFetchFunction, badRequestFetchFunction, brokenJSONFetchFunction, doNotExitFunction, fixedDateFunction, fixedResponseFetchFunction, graphQLIntrospectionDisabledFetchFunction, graphQLInvalidBodyFetchFunction, graphQLInvalidSchemaFetchFunction, internalServerErrorFetchFunction, noCallbackTimeoutFunction, notFoundFetchFunction, nowDateFunction, testDateFunction, testDateString, timeoutFetchFunction, unknownContentTypeFetchFunction }; //# sourceMappingURL=index.mjs.map