pact-gen-ts
Version:
Generating pact files from typescript definitions
48 lines (47 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.changeObjectRepresentationIntoExample = void 0;
const mapper_1 = require("../utils/mapper");
const object_type_1 = require("../utils/object-type");
const exampleRepresentationOfType = (0, mapper_1.createMapper)([
['boolean', true],
['number', 10],
['string', 'text'],
['email', 'email@example.com'],
['iso-date', '2013-02-01'],
['iso-datetime', '2015-08-06T16:53:10+01:00'],
['iso-datetime-with-millis', '2015-08-06T16:53:10.123+01:00'],
['iso-time', 'T22:44:30.652Z'],
['timestamp', 'Mon, 31 Oct 2016 15:21:14 -0400'],
['uuid', 'ce118b6e-d8e1-11e7-9296-cec278b6b50a'],
['ipv4', '127.0.0.13'],
['ipv6', '::ffff:192.0.2.128'],
['hex', '3F'],
]);
const changeObjectRepresentationIntoExample = (objectRepresentation) => {
if (objectRepresentation.isArray) {
return [
(0, exports.changeObjectRepresentationIntoExample)({
...objectRepresentation,
isArray: false,
}),
];
}
if (objectRepresentation.isEnum) {
return objectRepresentation.enumValues[0];
}
if (objectRepresentation.exampleValue) {
if (objectRepresentation.type === 'number') {
return Number(objectRepresentation.exampleValue);
}
if (objectRepresentation.exampleValue.startsWith('"') && objectRepresentation.exampleValue.endsWith('"')) {
return objectRepresentation.exampleValue.slice(1, -1);
}
return objectRepresentation.exampleValue;
}
if ((0, object_type_1.isLiteralObject)(objectRepresentation.type)) {
return Object.fromEntries(Object.entries(objectRepresentation.type).map(([key, value]) => [key, (0, exports.changeObjectRepresentationIntoExample)(value)]));
}
return exampleRepresentationOfType(objectRepresentation.type);
};
exports.changeObjectRepresentationIntoExample = changeObjectRepresentationIntoExample;