UNPKG

oazapfts

Version:

OpenApi TypeScript client generator

305 lines (304 loc) 10.9 kB
import e from "typescript"; import t from "lodash"; //#region \0rolldown/runtime.js var n = Object.defineProperty, r = (e, t) => { let r = {}; for (var i in e) n(r, i, { get: e[i], enumerable: !0 }); return t || n(r, Symbol.toStringTag, { value: "Module" }), r; }, i = { readOnly: "Read", writeOnly: "Write" }; function a(e) { return e ? i[e] : ""; } //#endregion //#region src/helpers/toIdentifier.ts function o(e, n = !1, r) { let i = t.camelCase(e) + a(r); return n && (i = t.upperFirst(i)), J(i) ? i : "$" + i; } //#endregion //#region src/generate/tscodegen.ts var s = /* @__PURE__ */ r({ addComment: () => U, appendNodes: () => H, block: () => C, changePropertyValue: () => z, createArrowFunction: () => w, createCall: () => y, createClassDeclaration: () => E, createConstructor: () => D, createEnumTypeNode: () => h, createFunctionDeclaration: () => T, createIndexSignature: () => M, createInterfaceAliasDeclaration: () => _, createKeywordType: () => f, createLiteral: () => m, createMethod: () => O, createMethodCall: () => b, createObjectBinding: () => N, createObjectLiteral: () => x, createParameter: () => k, createPropertyAssignment: () => S, createPropertySignature: () => j, createQuestionToken: () => u, createTemplateString: () => P, createTypeAliasDeclaration: () => g, findFirstVariableDeclaration: () => R, findNode: () => F, getFirstDeclarationName: () => L, getName: () => I, isValidIdentifier: () => J, keywordType: () => d, modifier: () => p, printFile: () => q, printNode: () => G, printNodes: () => K, questionToken: () => l, toExpression: () => v, transform: () => V, updateFunctionDeclaration: () => X, updateVariableDeclaration: () => Y }), c = e.factory, l = c.createToken(e.SyntaxKind.QuestionToken); function u(e) { if (e) return e === !0 ? l : e; } var d = { any: c.createKeywordTypeNode(e.SyntaxKind.AnyKeyword), number: c.createKeywordTypeNode(e.SyntaxKind.NumberKeyword), integer: c.createKeywordTypeNode(e.SyntaxKind.NumberKeyword), object: c.createKeywordTypeNode(e.SyntaxKind.ObjectKeyword), string: c.createKeywordTypeNode(e.SyntaxKind.StringKeyword), boolean: c.createKeywordTypeNode(e.SyntaxKind.BooleanKeyword), undefined: c.createKeywordTypeNode(e.SyntaxKind.UndefinedKeyword), void: c.createKeywordTypeNode(e.SyntaxKind.VoidKeyword), never: c.createKeywordTypeNode(e.SyntaxKind.NeverKeyword), null: c.createLiteralTypeNode(c.createNull()), unknown: c.createKeywordTypeNode(e.SyntaxKind.UnknownKeyword) }; function f(e) { return d[e]; } var p = { async: c.createModifier(e.SyntaxKind.AsyncKeyword), export: c.createModifier(e.SyntaxKind.ExportKeyword) }; function m(t) { switch (typeof t) { case "string": return c.createStringLiteral(t); case "boolean": return t ? c.createTrue() : c.createFalse(); case "number": return String(t).charAt(0) === "-" ? c.createPrefixUnaryExpression(e.SyntaxKind.MinusToken, c.createNumericLiteral(String(-t))) : c.createNumericLiteral(String(t)); } } function h(e) { let t = e.map((e) => e === null ? d.null : c.createLiteralTypeNode(m(e))); return t.length > 1 ? c.createUnionTypeNode(t) : t[0]; } function g({ modifiers: e, name: t, typeParameters: n, type: r }) { return c.createTypeAliasDeclaration(e, t, n, r); } function _({ modifiers: t, name: n, typeParameters: r, type: i, inheritedNodeNames: a }) { let s = a ? [c.createHeritageClause(e.SyntaxKind.ExtendsKeyword, a.map((e) => { let t = typeof e == "string" ? e : e.escapedText.toString(); return c.createExpressionWithTypeArguments(c.createIdentifier(o(t, !0)), void 0); }))] : []; return c.createInterfaceDeclaration(t, n, r, s, i.members); } function v(e) { return typeof e == "string" ? c.createIdentifier(e) : e; } function y(e, { typeArgs: t, args: n } = {}) { return c.createCallExpression(v(e), t, n); } function b(e, t) { return y(c.createPropertyAccessExpression(c.createThis(), e), t); } function x(e) { return c.createObjectLiteralExpression(e.map(([e, t]) => S(e, v(t))), !0); } function S(t, n) { return e.isIdentifier(n) && n.text === t ? c.createShorthandPropertyAssignment(t) : c.createPropertyAssignment(A(t), n); } function C(...e) { return c.createBlock(e, !0); } function w(e, t, { modifiers: n, typeParameters: r, type: i, equalsGreaterThanToken: a } = {}) { return c.createArrowFunction(n, r, e, i, a, t); } function T(e, { modifiers: t, asteriskToken: n, typeParameters: r, type: i }, a, o) { return c.createFunctionDeclaration(t, n, e, r, a, i, o); } function E({ modifiers: e, name: t, typeParameters: n, heritageClauses: r, members: i }) { return c.createClassDeclaration(e, t, n, r, i); } function D({ modifiers: e, parameters: t, body: n }) { return c.createConstructorDeclaration(e, t, n); } function O(e, { modifiers: t, asteriskToken: n, questionToken: r, typeParameters: i, type: a } = {}, o = [], s) { return c.createMethodDeclaration(t, n, e, u(r), i, o, a, s); } function k(e, { modifiers: t, dotDotDotToken: n, questionToken: r, type: i, initializer: a }) { return c.createParameterDeclaration(t, n, e, u(r), i, a); } function A(e) { return typeof e == "string" ? J(e) ? c.createIdentifier(e) : c.createStringLiteral(e) : e; } function j({ modifiers: e, name: t, questionToken: n, type: r }) { return c.createPropertySignature(e, A(t), u(n), r); } function M(e, { modifiers: t, indexName: n = "key", indexType: r = d.string } = {}) { return c.createIndexSignature(t, [k(n, { type: r })], e); } function N(e) { return c.createObjectBindingPattern(e.map(({ dotDotDotToken: e, propertyName: t, name: n, initializer: r }) => c.createBindingElement(e, t, n, r))); } function P(e, t) { return t.length ? c.createTemplateExpression(c.createTemplateHead(e), t.map(({ expression: e, literal: n }, r) => c.createTemplateSpan(e, r === t.length - 1 ? c.createTemplateTail(n) : c.createTemplateMiddle(n)))) : c.createStringLiteral(e); } function F(e, t, n) { let r = e.find((e) => e.kind === t && (!n || n(e))); if (!r) throw Error(`Node not found: ${t}`); return r; } function I(t) { return e.isIdentifier(t) ? t.escapedText : e.isLiteralExpression(t) ? t.text : ""; } function L(t) { let n = e.getNameOfDeclaration(t.declarationList.declarations[0]); return n ? I(n) : ""; } function R(t, n) { let [r] = F(t, e.SyntaxKind.VariableStatement, (e) => L(e) === n).declarationList.declarations; if (!r) throw Error(`Missing ${n} declaration`); return r; } function z(t, n, r) { let i = t.properties.find((t) => e.isPropertyAssignment(t) && I(t.name) === n); if (i && e.isPropertyAssignment(i)) Object.assign(i, { initializer: r }); else throw Error(`No such property: ${n}`); } function B(t) { return (n) => (r) => { function i(r) { return t(r, n) || e.visitEachChild(r, i, n); } return e.visitNode(r, i); }; } function V(t, ...n) { return e.transform(t, n.map(B)).transformed[0]; } function H(e, ...t) { return c.createNodeArray([...e, ...t]); } function U(t, n) { return n ? e.addSyntheticLeadingComment(t, e.SyntaxKind.MultiLineCommentTrivia, `*\n * ${n.replace(/\n/g, "\n * ")}\n `, !0) : t; } var W = e.createPrinter({ newLine: e.NewLineKind.LineFeed }); function G(t) { let n = e.createSourceFile("someFileName.ts", "", e.ScriptTarget.Latest, !1, e.ScriptKind.TS); return W.printNode(e.EmitHint.Unspecified, t, n); } function K(t) { let n = e.createSourceFile("someFileName.ts", "", e.ScriptTarget.Latest, !1, e.ScriptKind.TS); return t.map((t) => W.printNode(e.EmitHint.Unspecified, t, n)).join("\n"); } function q(e) { return W.printFile(e); } function J(t) { if (!t.length || t.trim() !== t) return !1; let n = e.parseIsolatedEntityName(t, e.ScriptTarget.Latest); return !!n && n.kind === e.SyntaxKind.Identifier && e.identifierToKeywordKind(n) === void 0; } function Y(t, n) { return e.factory.updateVariableDeclaration(t, n.name || t.name, n.exclamationToken || t.exclamationToken, n.type || t.type, n.initializer || t.initializer); } function X(t, n) { return e.factory.updateFunctionDeclaration(t, n.modifiers || t.modifiers, n.asteriskToken || t.asteriskToken, n.name || t.name, n.typeParameters || t.typeParameters, n.parameters || t.parameters, n.type || t.type, n.body || t.body); } //#endregion //#region src/generate/generateServers.ts var Z = e.factory; function Q(e) { let n = e.split(/{([\s\S]+?)}/g), r = t.chunk(n.slice(1), 2); return P(n[0], r.map(([e, t]) => ({ expression: Z.createIdentifier(e), literal: t }))); } function $(e, t) { return w([k(N(Object.entries(t || {}).map(([e, t]) => ({ name: e, initializer: m(t.default) }))), { type: Z.createTypeLiteralNode(Object.entries(t || {}).map(([e, t]) => j({ name: e, type: t.enum ? h(t.enum) : Z.createUnionTypeNode([ d.string, d.number, d.boolean ]) }))) })], Q(e)); } function ee(e) { return e.variables ? $(e.url, e.variables) : Z.createStringLiteral(e.url); } function te(e) { if (!e) return "/"; let { url: t, variables: n } = e; return n ? t.replace(/\{(.+?)\}/g, (e, t) => n[t] ? String(n[t].default) : e) : t; } function ne(e) { return te(e?.[0]); } function re(e, n) { return e.description ? t.camelCase(e.description.replace(/\W+/, " ")) : `server${n + 1}`; } function ie(e) { return x(e.map((e, t) => [re(e, t), ee(e)])); } function ae(t) { return e.factory.createVariableStatement([e.factory.createModifier(e.SyntaxKind.ExportKeyword)], e.factory.createVariableDeclarationList([e.factory.createVariableDeclaration("servers", void 0, void 0, ie(t))], e.NodeFlags.Const)); } //#endregion //#region src/context.ts function oe(e, n = {}) { let r = t.cloneDeep(e); return { opts: n, spec: r, mode: void 0, banner: "DO NOT MODIFY - This file has been generated using oazapfts.\nSee https://www.npmjs.com/package/oazapfts", imports: [[{ namespace: "Oazapfts" }, { from: "@oazapfts/runtime" }], [{ namespace: "QS" }, { from: "@oazapfts/runtime/query" }]], defaults: { baseUrl: ne(r.servers), headers: {} }, servers: r.servers || [], init: ce(), discriminatingSchemas: /* @__PURE__ */ new Set(), aliases: [], enumAliases: [], enumRefs: {}, refs: {}, refsOnlyMode: /* @__PURE__ */ new Map(), typeAliases: {}, operationNames: /* @__PURE__ */ new Map() }; } function se(e, t) { return { ...e, mode: t }; } function ce() { return [e.factory.createVariableStatement(void 0, e.factory.createVariableDeclarationList([e.factory.createVariableDeclaration("oazapfts", void 0, void 0, e.factory.createCallExpression(e.factory.createPropertyAccessExpression(e.factory.createIdentifier("Oazapfts"), "runtime"), void 0, [e.factory.createIdentifier("defaults")]))], e.NodeFlags.Const))]; } //#endregion export { o as C, s as S, g as _, C as a, p as b, T as c, N as d, x as f, P as g, j as h, U as i, M as l, S as m, se as n, y as o, k as p, ae as r, h as s, oe as t, m as u, J as v, q as x, d as y }; //# sourceMappingURL=context-BZ7CId2F.js.map