UNPKG

@openapi-generator-plus/typescript-fetch-client-generator

Version:
102 lines (101 loc) 6.6 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("@openapi-generator-plus/types"); const path_1 = __importDefault(require("path")); const handlebars_templates_1 = require("@openapi-generator-plus/handlebars-templates"); const typescript_generator_common_1 = __importStar(require("@openapi-generator-plus/typescript-generator-common")); const idx = __importStar(require("@openapi-generator-plus/indexed-type")); const createGenerator = (config, context) => { const myContext = (0, typescript_generator_common_1.chainTypeScriptGeneratorContext)(context, { loadAdditionalTemplates: async (hbs) => { await (0, handlebars_templates_1.loadTemplates)(path_1.default.resolve(__dirname, '../templates'), hbs); }, additionalWatchPaths: () => { return [path_1.default.resolve(__dirname, '../templates')]; }, defaultNpmOptions: () => ({ name: 'typescript-fetch-api', version: '0.0.1', private: true, repository: null, }), defaultTypeScriptOptions: () => ({ target: 'ES5', libs: ['$target', 'DOM', 'ES2021.String'], }), }); const generatorOptions = Object.assign(Object.assign({}, (0, typescript_generator_common_1.options)(config, myContext)), { legacyUnnamespacedModelSupport: !!config.legacyUnnamespacedModelSupport, withInterfaces: !!config.withInterfaces, includePolyfills: config.includePolyfills !== undefined ? !!config.includePolyfills : true }); myContext.additionalExportTemplates = async (outputPath, doc, hbs, rootContext) => { const relativeSourceOutputPath = generatorOptions.relativeSourceOutputPath; await (0, handlebars_templates_1.emit)('api', path_1.default.join(outputPath, relativeSourceOutputPath, 'api.ts'), Object.assign(Object.assign({}, rootContext), doc), true, hbs); await (0, handlebars_templates_1.emit)('models', path_1.default.join(outputPath, relativeSourceOutputPath, 'models.ts'), Object.assign(Object.assign(Object.assign({}, rootContext), doc), { schemas: idx.filter(doc.schemas, schema => (0, types_1.isCodegenObjectSchema)(schema) || (0, types_1.isCodegenEnumSchema)(schema) || (0, types_1.isCodegenOneOfSchema)(schema) || (0, types_1.isCodegenAnyOfSchema)(schema) || (0, types_1.isCodegenInterfaceSchema)(schema)) }), true, hbs); await (0, handlebars_templates_1.emit)('runtime', path_1.default.join(outputPath, relativeSourceOutputPath, 'runtime.ts'), Object.assign(Object.assign({}, rootContext), doc), true, hbs); await (0, handlebars_templates_1.emit)('configuration', path_1.default.join(outputPath, relativeSourceOutputPath, 'configuration.ts'), Object.assign(Object.assign({}, rootContext), doc), true, hbs); await (0, handlebars_templates_1.emit)('index', path_1.default.join(outputPath, relativeSourceOutputPath, 'index.ts'), Object.assign(Object.assign({}, rootContext), doc), true, hbs); await (0, handlebars_templates_1.emit)('README', path_1.default.join(outputPath, 'README.md'), Object.assign(Object.assign({}, rootContext), doc), false, hbs); }; const base = (0, typescript_generator_common_1.default)(config, myContext); return Object.assign(Object.assign({}, base), { templateRootContext: () => { return Object.assign(Object.assign(Object.assign({}, base.templateRootContext()), generatorOptions), { generatorClass: '@openapi-generator-plus/typescript-fetch-client-generator' }); }, generatorType: () => types_1.CodegenGeneratorType.CLIENT, toNativeType: function (options) { const { schemaType } = options; if (schemaType === types_1.CodegenSchemaType.BINARY || schemaType === types_1.CodegenSchemaType.FILE) { /* We support string and Blob, which is what FormData supports. It also enables us to handle literal binary values created from strings... which is hopefully a good idea. */ return new context.NativeType('string | Blob'); } else { return base.toNativeType(options); } }, postProcessDocument: (doc) => { for (const group of doc.groups) { for (const op of group.operations) { if (op.parameters) { op.parameters = idx.filter(op.parameters, param => param.in !== 'header' || !isForbiddenHeaderName(param.name)); } if (op.headerParams) { op.headerParams = idx.filter(op.headerParams, param => !isForbiddenHeaderName(param.name)); } } } } }); }; /** See https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name */ function isForbiddenHeaderName(name) { if (name.toLowerCase().startsWith('proxy-') || name.toLowerCase().startsWith('sec-')) { return true; } return [ 'Accept-Charset', 'Accept-Encoding', 'Access-Control-Request-Headers', 'Access-Control-Request-Method', 'Connection', 'Content-Length', 'Cookie', 'Cookie2', 'Date', 'DNT', 'Expect', 'Feature-Policy', 'Host', 'Keep-Alive', 'Origin', 'Referer', 'TE', 'Trailer', 'Transfer-Encoding', 'Upgrade', 'Via', ].map(h => h.toLowerCase()).includes(name.toLowerCase()); } exports.default = createGenerator;