UNPKG

aspida-mock

Version:

TypeScript friendly RESTful API mock for aspida

94 lines 4.34 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasMockHandler = exports.callMockHandler = exports.mockClient = exports.printLog = exports.mockMiddleware = exports.mockMethods = void 0; const aspida_1 = require("aspida"); const callMockHandler_1 = __importStar(require("./callMockHandler")); exports.callMockHandler = callMockHandler_1.default; Object.defineProperty(exports, "hasMockHandler", { enumerable: true, get: function () { return callMockHandler_1.hasMockHandler; } }); const mockMethods = (methods) => methods; exports.mockMethods = mockMethods; const mockMiddleware = (middleware) => middleware; exports.mockMiddleware = mockMiddleware; const printLog = (config, status) => { const searchString = (0, aspida_1.dataToURLString)(config.query || {}); console.log(`[mock] ${config.method}: ${config.path}${searchString ? `?${searchString}` : ''} => ${status}`); }; exports.printLog = printLog; const mockClient = (aspidaClient) => { let mockRoutes = []; let mockConfig; return { baseURL: aspidaClient.baseURL, fetch(baseURL, url, method, params, type) { if (!(0, callMockHandler_1.hasMockHandler)(url, method, mockRoutes)) { return aspidaClient.fetch(baseURL, url, method, params, type); } const send = async () => { var _a; const customConfig = { path: url !== null && url !== void 0 ? url : '', method, query: params === null || params === void 0 ? void 0 : params.query, reqBody: params === null || params === void 0 ? void 0 : params.body, reqHeaders: params === null || params === void 0 ? void 0 : params.headers, }; const result = (_a = (await (0, callMockHandler_1.default)(customConfig, mockRoutes, mockConfig === null || mockConfig === void 0 ? void 0 : mockConfig.middleware))) !== null && _a !== void 0 ? _a : { status: 404 }; if (mockConfig === null || mockConfig === void 0 ? void 0 : mockConfig.log) (0, exports.printLog)(customConfig, result.status); await new Promise(resolve => setTimeout(resolve, mockConfig === null || mockConfig === void 0 ? void 0 : mockConfig.delayMSec)); const res = { status: result.status, body: result.resBody, headers: result.resHeaders, }; if (result.status >= 400) { const err = new Error(`Request failed with status code ${result.status}`); err.response = res; throw err; } return res; }; return { send, json: send, text: send, arrayBuffer: send, blob: send, formData: send, }; }, attachRoutes(routes, config) { mockRoutes = routes; mockConfig = config; }, detachRoutes() { mockRoutes = []; mockConfig = undefined; }, }; }; exports.mockClient = mockClient; //# sourceMappingURL=index.js.map