UNPKG

rests

Version:

Easily generate API client's SDK — organize and simplify API Requests.

96 lines (95 loc) 6.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const url = require('url'); const ts = require("typescript"); const os = require("os"); const crypto = require("crypto"); const cp = require("child_process"); const glob = require("glob"); const capitalize = (string) => { return string.substr(0, 1).toUpperCase() + string.substr(1, string.length); }; const formatModulePath = (s) => (/^([a-zA-Z]\:(?:\/|\\))/g.test(s) ? url.pathToFileURL(s) : s); const dent = (str, size = 4) => { return str.replace((new RegExp(`^\t{${size}}`, 'gm')), ''); }; const copyOptions = (o) => (Object.assign(Object.assign({}, o), { headers: Object.assign({}, o.headers), params: Object.assign({}, o.params), values: Object.assign({}, o.values) })); const parseSet = (values) => { var _a, _b; if (((_b = (_a = values === null || values === void 0 ? void 0 : values.__proto__) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) != "Object") { throw new Error("Invalid $options object."); } let saveOptions = copyOptions(values.$options || {}); delete values['$options']; return Object.assign(Object.assign({}, saveOptions), { values: Object.assign(Object.assign({}, saveOptions.values), values) }); }; const mergeOptions = (prevOptions, currentOptions, mutate = false //Mutate the previous options? ) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t; let firstOptions = mutate ? prevOptions || {} : copyOptions(prevOptions || {}); let secondOptions = copyOptions(currentOptions || {}); secondOptions.headers = Object.assign(Object.assign({}, firstOptions.headers), secondOptions.headers); secondOptions.params = Object.assign(Object.assign({}, firstOptions.params), secondOptions.params); secondOptions.values = Object.assign(Object.assign({}, firstOptions.values), secondOptions.values); secondOptions.$other = Object.assign(Object.assign(Object.assign({}, firstOptions === null || firstOptions === void 0 ? void 0 : firstOptions.$other), secondOptions === null || secondOptions === void 0 ? void 0 : secondOptions.$other), { openapi: Object.assign(Object.assign(Object.assign({}, (_a = firstOptions === null || firstOptions === void 0 ? void 0 : firstOptions.$other) === null || _a === void 0 ? void 0 : _a.openapi), (_b = secondOptions === null || secondOptions === void 0 ? void 0 : secondOptions.$other) === null || _b === void 0 ? void 0 : _b.openapi), { fields: Object.assign(Object.assign(Object.assign({}, (_d = (_c = firstOptions === null || firstOptions === void 0 ? void 0 : firstOptions.$other) === null || _c === void 0 ? void 0 : _c.openapi) === null || _d === void 0 ? void 0 : _d.fields), (_f = (_e = secondOptions === null || secondOptions === void 0 ? void 0 : secondOptions.$other) === null || _e === void 0 ? void 0 : _e.openapi) === null || _f === void 0 ? void 0 : _f.fields), { parameters: [ ...(((_j = (_h = (_g = firstOptions === null || firstOptions === void 0 ? void 0 : firstOptions.$other) === null || _g === void 0 ? void 0 : _g.openapi) === null || _h === void 0 ? void 0 : _h.fields) === null || _j === void 0 ? void 0 : _j.parameters) || []), ...(((_m = (_l = (_k = secondOptions === null || secondOptions === void 0 ? void 0 : secondOptions.$other) === null || _k === void 0 ? void 0 : _k.openapi) === null || _l === void 0 ? void 0 : _l.fields) === null || _m === void 0 ? void 0 : _m.parameters) || []), ], responses: Object.assign(Object.assign({}, (_q = (_p = (_o = firstOptions === null || firstOptions === void 0 ? void 0 : firstOptions.$other) === null || _o === void 0 ? void 0 : _o.openapi) === null || _p === void 0 ? void 0 : _p.fields) === null || _q === void 0 ? void 0 : _q.responses), (_t = (_s = (_r = secondOptions === null || secondOptions === void 0 ? void 0 : secondOptions.$other) === null || _r === void 0 ? void 0 : _r.openapi) === null || _s === void 0 ? void 0 : _s.fields) === null || _t === void 0 ? void 0 : _t.responses) }) }) }); Object.assign(firstOptions, secondOptions); return firstOptions; }; const get = (t, path) => (path.split(".").reduce((r, k) => r === null || r === void 0 ? void 0 : r[k], t)); const escapeRegExp = (string) => { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }; const tsCompile = function (source, options = null) { // Default options -- you could also perform a merge, or use the project tsconfig.json if (null === options) { options = { compilerOptions: { module: 'CommonJS', target: 'es6', esModuleInterop: true } }; } return ts.transpileModule(source, options).outputText; }; const tsToJs = function (file, watch = false) { let fileName = path.basename(file); let fileDir = path.dirname(file); let outFileName = fileName.replace(/\.tsx?$/, '.js'); let outPath = path.join(fileDir, "/.restscache/"); cp[watch ? 'exec' : 'execSync'](`npx tsc ${fileName} --outDir .restscache` + (watch ? ' --watch' : ''), { cwd: fileDir }); if (fs.existsSync(path.join(fileDir, "package.json"))) { let outFile = path.join(outPath, outFileName); return outFile; } let filePattern = `**${path.basename(fileDir)}/${outFileName}`; let outFiles = glob.sync(filePattern, { cwd: outPath }); let outFile = outFiles.find((v) => (!v.includes("node_modules"))); if (!outFile.includes(".restscache")) { outFile = path.join(outPath, outFile); } if (!outFile) { let checkOutRoot = path.join(outPath, outFileName); if (fs.existsSync(checkOutRoot)) { return checkOutRoot; } if (watch) { return false; } throw new Error(`Couldn't find the compiled file path for ${fileName}`); } return outFile; }; const isInitializable = (category) => { var _a; return (((_a = category === null || category === void 0 ? void 0 : category.$options) === null || _a === void 0 ? void 0 : _a.params) && Object.keys(category.$options.params).find((p) => category.$options.params[p].$initsOnly)); }; module.exports = { capitalize, dent, copyOptions, parseSet, mergeOptions, get, escapeRegExp, isInitializable, formatModulePath, tsCompile, tsToJs };