UNPKG

@nimbella/postman-api

Version:

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

103 lines (102 loc) 3.59 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 tslib_1 = require("tslib"); const base_gen_1 = tslib_1.__importDefault(require("./base-gen")); const utils_1 = require("../../utils"); const TEST_STUB_RES_BODY_MAX_LEN = 300; class PyGenerator extends base_gen_1.default { constructor() { super('py'); } 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 = utils_1.escapeSpecialChars(JSON.stringify(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) { 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 = []; 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)); } } exports.default = PyGenerator;