unmock-core
Version:
[][npmjs] [](https://circleci.com/gh/unmock/unmock-js) [](h
109 lines • 4.39 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const io = require("io-ts");
const json_schema_poet_1 = require("json-schema-poet");
const json_schema_strictly_typed_1 = require("json-schema-strictly-typed");
const DynamicJSONSymbol = Symbol();
const isDynamic = (unk) => typeof unk === "object" && unk.dynamic === DynamicJSONSymbol;
const DynamicJSONValue = new io.Type("DynamicJSONValueType", isDynamic, (input, context) => isDynamic(input) ? io.success(input) : io.failure(input, context), io.identity);
const JSO = json_schema_strictly_typed_1.JSONSchemaObject(json_schema_strictly_typed_1.JSSTEmpty(DynamicJSONValue), DynamicJSONValue);
const ExtendedPrimitive = io.union([json_schema_strictly_typed_1.JSONPrimitive, JSO]);
const ExtendedValue = io.recursion("ExtendedValue", () => io.union([
ExtendedPrimitive,
json_schema_strictly_typed_1.JSONArray,
json_schema_strictly_typed_1.JSONObject,
ExtendedObject,
ExtendedArray,
]));
const ExtendedObject = io.recursion("ExtendedObject", () => io.record(io.string, ExtendedValue));
const ExtendedArray = io.recursion("ExtendedArray", () => io.array(ExtendedValue));
const removeDynamicSymbol = (schema) => {
const { dynamic } = schema, rest = __rest(schema, ["dynamic"]);
return rest;
};
const JSONSchemify = (e) => JSO.is(e)
? removeDynamicSymbol(e)
: ExtendedArray.is(e) || json_schema_strictly_typed_1.JSONArray.is(e)
? json_schema_poet_1.tuple_({})(e.map((i) => JSONSchemify(i)))
: ExtendedObject.is(e) || json_schema_strictly_typed_1.JSONObject.is(e)
? json_schema_poet_1.type_({})(Object.entries(e).reduce((a, b) => (Object.assign(Object.assign({}, a), { [b[0]]: JSONSchemify(b[1]) })), {}), {})
: json_schema_poet_1.cnst_({})(e);
const jspt = json_schema_poet_1.extendT({
dynamic: DynamicJSONSymbol,
});
exports.u = {
str() {
return jspt.string();
},
number() {
return jspt.number();
},
int() {
return jspt.integer();
},
city() {
return json_schema_poet_1.string_({ dynamic: DynamicJSONSymbol })("address.city");
},
};
class DynamicServiceSpec {
constructor(updater, statusCode = 200, baseUrl, name) {
this.updater = updater;
this.statusCode = statusCode;
this.baseUrl = baseUrl;
this.name = name;
this.data = {};
}
reply(maybeStatusCode, maybeData) {
if (maybeData !== undefined) {
this.data = JSONSchemify(maybeData);
this.statusCode = maybeStatusCode;
}
else if (typeof maybeStatusCode === "number" &&
maybeStatusCode >= 100 &&
maybeStatusCode < 599) {
this.statusCode = maybeStatusCode;
}
else {
this.data = JSONSchemify(maybeStatusCode);
}
const store = this.updater({
data: this.data,
statusCode: this.statusCode,
});
return buildFluentNock(store, this.baseUrl, this.name);
}
}
exports.DynamicServiceSpec = DynamicServiceSpec;
const buildFluentNock = (store, baseUrl, name) => {
const dynFn = (method, endpoint) => ({ statusCode, data, }) => store.updateOrAdd({
baseUrl,
method,
endpoint: endpoint.startsWith("/") ? endpoint : `/${endpoint}`,
statusCode,
response: data,
name,
});
return Object.entries({
get: 200,
head: 200,
post: 201,
put: 204,
patch: 204,
delete: 200,
options: 200,
trace: 200,
}).reduce((o, [method, code]) => (Object.assign(Object.assign({}, o), { [method]: (endpoint) => new DynamicServiceSpec(dynFn(method, endpoint), code, baseUrl, name) })), {});
};
exports.nockify = ({ backend, baseUrl, name, }) => buildFluentNock(backend.serviceStore, baseUrl, name);
//# sourceMappingURL=nock.js.map