aspida-mock
Version:
TypeScript friendly RESTful API mock for aspida
38 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createValues = exports.copyData = exports.httpMethods = exports.createPathRegExp = void 0;
const createPathRegExp = (path) => new RegExp(`^${path.replace(/\/_[^./]+/g, '/[^/]+').replace('.', '\\.')}$`);
exports.createPathRegExp = createPathRegExp;
exports.httpMethods = [
'get',
'post',
'put',
'delete',
'head',
'options',
'patch',
];
const copyData = (res) => {
const { resBody } = res;
return resBody === null ||
typeof resBody !== 'object' ||
(!Array.isArray(resBody) && Object.getPrototypeOf(resBody) !== Object.prototype)
? res
: { ...res, resBody: JSON.parse(JSON.stringify(resBody)) };
};
exports.copyData = copyData;
const createValues = (path, relativePath) => {
const values = {};
const dirList = path.split('/');
const parsedRequestUrl = relativePath.split('/');
parsedRequestUrl.forEach((dir, i) => {
if (dirList[i].startsWith('_')) {
const [valueName, type = 'number'] = dirList[i].slice(1).split('@');
const val = dir.split('.')[0];
values[valueName.split('.')[0]] = isNaN(+val) || type !== 'number' ? val : +val;
}
});
return values;
};
exports.createValues = createValues;
//# sourceMappingURL=utils.js.map