UNPKG

pact-gen-ts

Version:

Generating pact files from typescript definitions

100 lines (99 loc) 4.53 kB
"use strict"; 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.PactAxios = void 0; const ts = __importStar(require("typescript")); class PactAxios { constructor(currentFunctionNode) { const axiosExpression = currentFunctionNode.getDescendantsOfKind(ts.SyntaxKind.CallExpression).find((callExpression) => { const propertyAccess = callExpression.getChildrenOfKind(ts.SyntaxKind.PropertyAccessExpression)[0]; let currentExpression; /** Handle .then on axios call */ if (propertyAccess) { currentExpression = propertyAccess; } else { currentExpression = callExpression; } /** Find which of the expression identifiers are coming from axios and node_modules */ const axiosMethodIdentifier = currentExpression.getChildrenOfKind(ts.SyntaxKind.Identifier).find((identifier) => { const filePath = identifier.getImplementations()[0]?.getSourceFile().getFilePath(); return filePath?.includes('axios') && filePath.includes('node_modules'); }); return Boolean(axiosMethodIdentifier); }); if (!axiosExpression) { throw Error('Axios expression was not found.'); } this.axiosCallExpression = axiosExpression; } getRequestMethod() { return this.axiosCallExpression.getDescendantsOfKind(ts.SyntaxKind.Identifier)[1]?.getText().toUpperCase(); } getResponseBodyType() { const responseBodyType = this.axiosCallExpression.getTypeArguments()[0]?.getType(); if (!responseBodyType) { throw Error('Axios response body type not found. Make sure you have set Response type properly on axios request. See https://github.com/HLTech/pact-gen-ts#axios---pact-axios for more details.'); } return responseBodyType; } getQueryType() { return this.getAxiosConfigProperty('params'); } getRequestBodyType() { const requestMethod = this.getRequestMethod(); const secondAxiosCallArgument = this.axiosCallExpression.getArguments()[1]; if (!secondAxiosCallArgument) { return; } const isRequestBodyNeeded = requestMethod && ['POST', 'PUT', 'PATCH'].includes(requestMethod); /** POST,PUT,PATCH in axios call have data as a second argument */ if (isRequestBodyNeeded) { return secondAxiosCallArgument.getType(); } /** Other methods than POST,PUT,PATCH can pass data using axios config object and 'data' property */ if (secondAxiosCallArgument) { return this.getAxiosConfigProperty('data'); } } /** Find within config object in axios call type of property */ getAxiosConfigProperty(property) { const axiosConfig = this.axiosCallExpression .getArguments() .find((argument) => argument.getType().getProperties()[0]?.getEscapedName() === property); return axiosConfig?.getType().getProperty(property)?.getValueDeclaration()?.getType(); } } exports.PactAxios = PactAxios;