pact-gen-ts
Version:
Generating pact files from typescript definitions
181 lines (180 loc) • 9.58 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.InteractionCreator = void 0;
const ts = __importStar(require("typescript"));
const js_docs_into_interaction_1 = require("./js-docs-into-interaction");
const default_response_status_1 = require("./default-response-status");
const pact_annotations_1 = require("../consts/pact-annotations");
const pact_axios_1 = require("../integrations/pact-axios/pact-axios");
const pact_description_1 = require("./pact-description");
const pact_axios_response_body_1 = require("../integrations/pact-axios/pact-axios-response-body");
const response_body_1 = require("./response-body");
const request_body_1 = require("./request-body");
const pact_axios_request_body_1 = require("../integrations/pact-axios/pact-axios-request-body");
const query_1 = require("./query");
const pact_axios_query_1 = require("../integrations/pact-axios/pact-axios-query");
class InteractionCreator {
constructor(sourceFile, provider) {
this.sourceFile = sourceFile;
this.provider = provider;
this.getPactJsDocsNodes = () => {
const allJsDocsNodes = this.sourceFile.getDescendantsOfKind(ts.SyntaxKind.JSDoc);
return allJsDocsNodes.filter((jsDocNode) => jsDocNode
.getChildrenOfKind(ts.SyntaxKind.JSDocTag)
.find((jsDocTag) => jsDocTag.getFirstChildByKind(ts.SyntaxKind.Identifier)?.getText() === pact_annotations_1.PACT_ANNOTATIONS.PACT));
};
this.getInteractionForPactJsDoc = (pactJsDoc) => {
var _a;
const nodeWithUsagePactJsDoc = pactJsDoc.getParent();
const apiFunctionNode = InteractionCreator.getFunctionNode(nodeWithUsagePactJsDoc);
if (apiFunctionNode) {
const pactDescription = new pact_description_1.PactDescription(pactJsDoc);
const newInteraction = (0, js_docs_into_interaction_1.mapJsDocsIntoInteraction)(pactJsDoc);
newInteraction.description || (newInteraction.description = InteractionCreator.getNameOfFunction(nodeWithUsagePactJsDoc));
newInteraction.request.headers = { ...this.provider.requestHeaders, ...newInteraction.request.headers };
newInteraction.response.headers = { ...this.provider.responseHeaders, ...newInteraction.response.headers };
let responseBody;
let requestBody;
let queryOfRequest;
if (pactDescription.isAxiosTagSet()) {
const pactAxios = new pact_axios_1.PactAxios(apiFunctionNode);
newInteraction.request.method = pactAxios.getRequestMethod();
responseBody = new pact_axios_response_body_1.PactAxiosResponseBody(pactAxios, this.sourceFile);
requestBody = new pact_axios_request_body_1.PactAxiosRequestBody(pactAxios, this.sourceFile);
queryOfRequest = new pact_axios_query_1.PactAxiosQuery(pactAxios, this.sourceFile, this.provider.queryArrayFormat);
}
else {
responseBody = new response_body_1.ResponseBody(apiFunctionNode, this.sourceFile);
requestBody = new request_body_1.RequestBody(apiFunctionNode, this.sourceFile);
queryOfRequest = new query_1.Query(apiFunctionNode, this.sourceFile, this.provider.queryArrayFormat);
}
newInteraction.response.body = responseBody.body;
newInteraction.response.matchingRules = responseBody.matchingRules;
newInteraction.request.body = requestBody.body;
newInteraction.request.query = queryOfRequest.query;
newInteraction.request.matchingRules =
queryOfRequest.matchingRules || requestBody.matchingRules
? { ...queryOfRequest?.matchingRules, ...requestBody?.matchingRules }
: undefined;
(_a = newInteraction.response).status || (_a.status = (0, default_response_status_1.getDefaultResponseStatusForInteraction)(newInteraction));
return newInteraction;
}
throw Error;
};
}
static getAllInteractionsInFile(sourceFile, provider) {
return new InteractionCreator(sourceFile, provider).findAllInteractions();
}
findAllInteractions() {
const pactJsDocNodes = this.getPactJsDocsNodes();
return pactJsDocNodes.map(this.getInteractionForPactJsDoc);
}
}
exports.InteractionCreator = InteractionCreator;
InteractionCreator.getFunctionNode = (node) => {
switch (node.getKind()) {
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.MethodDeclaration:
return node;
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.VariableStatement:
const variableDeclarationNode = node
.getFirstChildByKind(ts.SyntaxKind.VariableDeclarationList)
?.getFirstDescendantByKind(ts.SyntaxKind.VariableDeclaration);
if (variableDeclarationNode) {
return (variableDeclarationNode.getFirstChildByKind(ts.SyntaxKind.FunctionDeclaration) ||
variableDeclarationNode.getFirstChildByKind(ts.SyntaxKind.ArrowFunction));
}
break;
case ts.SyntaxKind.PropertyAssignment:
case ts.SyntaxKind.PropertyDeclaration:
return node.getFirstChildByKind(ts.SyntaxKind.FunctionExpression) || node.getFirstChildByKind(ts.SyntaxKind.ArrowFunction);
}
};
InteractionCreator.getNameOfFunction = (nodeWithFunction) => {
if (nodeWithFunction.getKind() === ts.SyntaxKind.VariableStatement) {
return nodeWithFunction
.getFirstChildByKindOrThrow(ts.SyntaxKind.VariableDeclarationList)
.getFirstDescendantByKindOrThrow(ts.SyntaxKind.VariableDeclaration)
.getFirstChildByKindOrThrow(ts.SyntaxKind.Identifier)
.getText();
}
else {
return nodeWithFunction.getFirstChildByKindOrThrow(ts.SyntaxKind.Identifier)?.getText();
}
};
InteractionCreator.getResponseTypeFromFunctionBody = (bodyOfFunction) => {
const responseBodyJsDoc = bodyOfFunction.getDescendantsOfKind(ts.SyntaxKind.JSDoc).find((jsDocComment) => {
return (jsDocComment.getFirstChildByKind(ts.SyntaxKind.JSDocTag)?.getFirstChildByKind(ts.SyntaxKind.Identifier)?.getText() ===
pact_annotations_1.PACT_ANNOTATIONS.PACT_RESPONSE_BODY);
});
if (responseBodyJsDoc) {
const variableStatementNode = responseBodyJsDoc.getParent();
return variableStatementNode.getFirstDescendantByKind(ts.SyntaxKind.VariableDeclaration)?.getType();
}
};
InteractionCreator.getReturnTypeOfFunction = (functionType) => {
const returnType = functionType.getCallSignatures()[0].getReturnType();
if (returnType.getTargetType()?.getText() === 'Promise<T>') {
return returnType.getTypeArguments()[0];
}
return returnType;
};
InteractionCreator.getParameterWithJsDocFromFunction = (functionDeclaration, jsDoc) => {
const parametersOfFunction = functionDeclaration.getChildrenOfKind(ts.SyntaxKind.Parameter);
for (const parameter of parametersOfFunction) {
const jsDocIdentifierNode = parameter
.getFirstChildByKind(ts.SyntaxKind.JSDoc)
?.getFirstChildByKind(ts.SyntaxKind.JSDocTag)
?.getFirstChildByKind(ts.SyntaxKind.Identifier);
if (jsDocIdentifierNode?.getText() === jsDoc) {
return parameter;
}
}
};
InteractionCreator.getVariableWithJsDocFromFunction = (functionDeclaration, jsDoc) => {
const pactBodyJsDoc = functionDeclaration
.getFirstChildByKind(ts.SyntaxKind.Block)
?.getDescendantsOfKind(ts.SyntaxKind.JSDoc)
.find((jsDocComment) => {
return (jsDocComment.getFirstChildByKind(ts.SyntaxKind.JSDocTag)?.getFirstChildByKind(ts.SyntaxKind.Identifier)?.getText() ===
jsDoc);
});
if (pactBodyJsDoc) {
const variableStatementNode = pactBodyJsDoc.getParent();
return variableStatementNode.getFirstDescendantByKind(ts.SyntaxKind.VariableDeclaration);
}
};