UNPKG

@nimbella/postman-api

Version:

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

107 lines (106 loc) 3.62 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 }); exports.isPublicApi = exports.canBeUpdated = exports.fileChecksum = exports.stringChecksum = exports.escapeSpecialChars = exports.isGuid = exports.isJson = exports.sanitizeVarName = exports.sanitizeFileName = exports.sanitizeName = void 0; const tslib_1 = require("tslib"); const crypto_1 = require("crypto"); const fs_1 = require("fs"); const public_apis_1 = require("./public-apis"); const logger_1 = tslib_1.__importDefault(require("./logger")); function sanitizeName(name, joiner) { let newName = ''; newName = name .replace(/[^a-zA-Z0-9]/g, joiner) .replace(/-{2,}/g, joiner) .toLowerCase(); if (joiner && newName.endsWith(joiner)) newName = newName.slice(0, -1); return newName; } exports.sanitizeName = sanitizeName; function sanitizeFileName(name) { return sanitizeName(name, '-'); } exports.sanitizeFileName = sanitizeFileName; function sanitizeVarName(name) { return sanitizeName(name, '_'); } exports.sanitizeVarName = sanitizeVarName; function isJson(item) { item = typeof item === 'string' ? item : JSON.stringify(item); try { item = JSON.parse(item); } catch (error) { return false; } if (typeof item === 'object' && item !== null) { return true; } return false; } exports.isJson = isJson; function isGuid(uid) { if (uid[0] === '{') { uid = uid.substring(1, uid.length - 1); } const regexGuid = /^(\{){0,1}[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(\}){0,1}$/gi; return regexGuid.test(uid); } exports.isGuid = isGuid; function escapeSpecialChars(data) { return data .replace(/\\n/g, '\\n') .replace(/\\'/g, "\\'") .replace(/\\"/g, '\\"') .replace(/\\&/g, '\\&') .replace(/\\r/g, '\\r') .replace(/\\t/g, '\\t') .replace(/\\b/g, '\\b') .replace(/\\f/g, '\\f'); } exports.escapeSpecialChars = escapeSpecialChars; function stringChecksum(str, algorithm = 'md5', encoding = 'hex') { const length = str.length; const hash = crypto_1.createHash(algorithm).update(str, 'utf8').digest(encoding); return { length, hash }; } exports.stringChecksum = stringChecksum; function fileChecksum(location) { let data = ''; try { data = fs_1.readFileSync(location); } catch (error) { logger_1.default.error(error); } return exports.stringChecksum(data); } exports.fileChecksum = fileChecksum; function canBeUpdated(fileLocation, generatedText) { try { const fileText = fileChecksum(fileLocation); const genText = stringChecksum(generatedText); return fileText.length < genText.length; } catch (error) { console.log(error); return false; } } exports.canBeUpdated = canBeUpdated; function isPublicApi(url) { return url in public_apis_1.urls; } exports.isPublicApi = isPublicApi;