pact-gen-ts
Version:
Generating pact files from typescript definitions
81 lines (80 loc) • 3.73 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapJsDocsIntoInteraction = mapJsDocsIntoInteraction;
const ts = __importStar(require("typescript"));
const pact_annotations_1 = require("../consts/pact-annotations");
function mapJsDocsIntoInteraction(jsDocNode) {
const newInteraction = { request: {}, response: {} };
for (const jsDocTag of jsDocNode.getChildrenOfKind(ts.SyntaxKind.JSDocTag)) {
switch (jsDocTag.getFirstChildByKind(ts.SyntaxKind.Identifier)?.getText()) {
case pact_annotations_1.PACT_ANNOTATIONS.PACT_DESCRIPTION: {
newInteraction.description = jsDocTag.getCommentText();
break;
}
case pact_annotations_1.PACT_ANNOTATIONS.PACT_METHOD: {
newInteraction.request.method = jsDocTag.getCommentText();
break;
}
case pact_annotations_1.PACT_ANNOTATIONS.PACT_RESPONSE_STATUS: {
newInteraction.response.status = Number(jsDocTag.getCommentText());
break;
}
case pact_annotations_1.PACT_ANNOTATIONS.PACT_PATH: {
newInteraction.request.path = jsDocTag.getCommentText();
break;
}
case pact_annotations_1.PACT_ANNOTATIONS.PACT_RESPONSE_HEADER: {
const { nameOfHeader, valueOfHeader } = getHeaderPair(jsDocTag.getCommentText());
if (nameOfHeader && valueOfHeader) {
newInteraction.response.headers = { ...newInteraction.response.headers, [nameOfHeader]: valueOfHeader };
}
break;
}
case pact_annotations_1.PACT_ANNOTATIONS.PACT_REQUEST_HEADER: {
const { nameOfHeader, valueOfHeader } = getHeaderPair(jsDocTag.getCommentText());
if (nameOfHeader && valueOfHeader) {
newInteraction.request.headers = { ...newInteraction.request.headers, [nameOfHeader]: valueOfHeader };
}
break;
}
}
}
return newInteraction;
}
const getHeaderPair = (jsDocComment) => {
const headerPair = jsDocComment?.match(/"(?<nameOfHeader>.*?)"\s+"(?<valueOfHeader>.*?)"$/);
return { nameOfHeader: headerPair?.groups?.nameOfHeader, valueOfHeader: headerPair?.groups?.valueOfHeader };
};