UNPKG

@nimbella/postman-api

Version:

Postman Collection to Nimbella Project: Take your APIs seamlessly into Serverless world with this API

138 lines (137 loc) 5.33 kB
"use strict"; /* * Copyright (c) 2019 - present Nimbella Corp. * * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const handlebars_1 = require("handlebars"); const utils_1 = require("../../utils"); const test_gen_1 = require("../test/test-gen"); const TEST_STUB_RES_BODY_MAX_LEN = 300; class BaseGenerator { constructor(ext) { this.language = ext; this.tmplPath = path_1.join(__dirname, 'templates', ext); this.methodStub = fs_1.readFileSync(path_1.join(this.tmplPath, 'method.hbs')); this.gitignoreStub = fs_1.readFileSync(path_1.join(this.tmplPath, 'gitignore.hbs')); this.testStub = fs_1.readFileSync(path_1.join(this.tmplPath, 'test.hbs')); this.methodValidationStub = fs_1.readFileSync(path_1.join(this.tmplPath, 'methodValidation.hbs')); this.globalValidationStub = fs_1.readFileSync(path_1.join(this.tmplPath, 'globalValidation.hbs')); this.includeFile = fs_1.readFileSync(path_1.join(this.tmplPath, 'include.hbs')); this.tmplMethod = handlebars_1.compile(this.methodStub.toString()); this.tmplTest = handlebars_1.compile(this.testStub.toString()); this.tmplGitignore = handlebars_1.compile(this.gitignoreStub.toString()); this.tmplMethodValidation = handlebars_1.compile(this.methodValidationStub.toString()); this.tmplGlobalValidation = handlebars_1.compile(this.globalValidationStub.toString()); this.tmplIncludeFile = handlebars_1.compile(this.includeFile.toString()); } getMethodStub(item) { const path = item.request.url && item.request.url.path ? item.request.url.path.join('/') : ''; const namespace = utils_1.sanitizeName(item.name, ''); let [response] = item.responses.members; if (!response) { response = ''; } const code = response.code || 200; const headers = (response.headers || '').members; const cookies = (response.cookies || '').members; const { status } = response; const body = response.body || ''; const description = (item.request.description || '').content; const params = { path, headers, status, code, cookies, body, description, namespace, }; return this.tmplMethod(Object.assign({}, params)); } async getTestStub(item, location) { let [response] = item.responses.members; if (!response) { response = ''; } const packageName = utils_1.sanitizeFileName(item.parent().name); const namespace = utils_1.sanitizeName(item.name, ''); const testName = utils_1.sanitizeVarName(item.name); const methodName = utils_1.sanitizeFileName(item.name); const args = ''; const expected = ''; const got = ''; let body = response.body || ''; if (body.length > TEST_STUB_RES_BODY_MAX_LEN) { body = undefined; } else { body = JSON.stringify(body); } const testScripts = await test_gen_1.getUnitTests(item, location, packageName, methodName); const isPostRequest = item.request.method.toLowerCase() === 'post'; const headers = response.headers ? response.headers.members : []; const responseTime = 500; const propTypes = []; const propChildKeys = []; const envVars = (item.variables || '').members; const mandatoryProps = utils_1.isJson(response.body) ? Object.keys(JSON.parse(response.body)) : []; const params = { testName, packageName, methodName, args, expected, got, body, testScripts, isPostRequest, headers, responseTime, propTypes, propChildKeys, envVars, mandatoryProps, namespace, }; return this.tmplTest(Object.assign({}, params)); } getMethodValidationStub(item) { return this.tmplMethodValidation(item); } getGlobalValidationStub(item) { return this.tmplGlobalValidation(item); } getIncludeFile(files) { return { location: '.include', content: this.tmplIncludeFile(files), }; } getSupportingItems(collection) { return [ { location: '.gitignore', content: this.tmplGitignore(collection), }, ]; } } exports.default = BaseGenerator;