@bitstack/ng-query-codegen-openapi 0.0.33 → 0.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bin/cli.mjs +6 -5
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1 -0
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/common-types-generator.ts +1 -0
package/lib/bin/cli.mjs
CHANGED
|
@@ -9,6 +9,7 @@ export interface RequestOptions {
|
|
|
9
9
|
reportProgress?: boolean;
|
|
10
10
|
withCredentials?: boolean;
|
|
11
11
|
timeout?: number;
|
|
12
|
+
ignoreGlobalError?: boolean;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export interface QueryConfig {
|
|
@@ -51,7 +52,7 @@ ${n.join(`
|
|
|
51
52
|
\u9019\u4E9B\u6A94\u6848\u662F\u7531 @bitstack/ng-query-codegen-openapi \u7522\u751F\u5668\u6240\u5EFA\u7ACB\u3002
|
|
52
53
|
|
|
53
54
|
\u5982\u6709\u7591\u554F\uFF0C\u8ACB\u53C3\u8003\u5C08\u6848\u6587\u4EF6\u6216\u806F\u7E6B\u958B\u767C\u5718\u968A\u3002
|
|
54
|
-
`}c();import me from"typescript";c();K();import{supportDeepObjects as Le}from"oazapfts/generate";var S=class{constructor(t){this.options=t}extractEndpointInfos(t){return t.map(e=>this.extractSingleEndpointInfo(e))}extractSingleEndpointInfo(t){let{verb:e,path:n,operation:o}=t,{operationNameSuffix:s="",argSuffix:a="Req",responseSuffix:p="Res",queryMatch:u,endpointOverrides:i}=this.options,m=E({verb:e,path:n}),y=s?P(m+s):m,d=P(m+s+a),T=P(m+s+p),v=Z(e,n,D(t,i),u),C=`${m.replace(/([A-Z])/g,"_$1").toUpperCase()}`,
|
|
55
|
+
`}c();import me from"typescript";c();K();import{supportDeepObjects as Le}from"oazapfts/generate";var S=class{constructor(t){this.options=t}extractEndpointInfos(t){return t.map(e=>this.extractSingleEndpointInfo(e))}extractSingleEndpointInfo(t){let{verb:e,path:n,operation:o}=t,{operationNameSuffix:s="",argSuffix:a="Req",responseSuffix:p="Res",queryMatch:u,endpointOverrides:i}=this.options,m=E({verb:e,path:n}),y=s?P(m+s):m,d=P(m+s+a),T=P(m+s+p),v=Z(e,n,D(t,i),u),C=`${m.replace(/([A-Z])/g,"_$1").toUpperCase()}`,O=o.summary||`${e.toUpperCase()} ${n}`,{queryParams:ve,pathParams:be,isVoidArg:Oe,hasRequestBody:xe}=this.extractParameters(t),Te=this.extractContentType(o),Ce=Array.isArray(o.tags)?o.tags:[];return{operationName:y,argTypeName:d,responseTypeName:T,isQuery:v,verb:e.toUpperCase(),path:n,queryKeyName:C,queryParams:ve,pathParams:be,isVoidArg:Oe,summary:O,contentType:Te,hasRequestBody:xe,tags:Ce}}extractParameters(t){let{operation:e,pathItem:n}=t,o=this.resolveArray(e.parameters),s=this.resolveArray(n.parameters).filter(y=>!o.some(d=>d.name===y.name&&d.in===y.in)),a=Le([...s,...o]).filter(y=>y.in!=="header"),p=a.filter(y=>y.in==="query"),u=a.filter(y=>y.in==="path"),i=!!e.requestBody,m=p.length===0&&u.length===0&&!e.requestBody;return{queryParams:p,pathParams:u,isVoidArg:m,hasRequestBody:i}}resolveArray(t){return t?Array.isArray(t)?t:[t]:[]}extractContentType(t){if(!t.requestBody)return"application/json";let e=t.requestBody.content;if(!e||typeof e!="object")return"application/json";let n=Object.keys(e);return n.length===0?"application/json":n[0]}};c();var ze=r=>r.charAt(0).toUpperCase()+r.slice(1);function pe(r,t,e,n){let o={};e&&Object.keys(e).forEach(i=>{if(o[i]=i,i.endsWith("Vo")){let m=i.slice(0,-2)+"VO";o[m]=i}if(i.endsWith("Dto")){let m=i.slice(0,-3)+"DTO";o[m]=i}});let s=`/* eslint-disable */
|
|
55
56
|
// [Warning] Generated automatically - do not edit manually
|
|
56
57
|
|
|
57
58
|
`;e&&Object.keys(e).length>0&&(s+=`import * as Schema from "../schema";
|
|
@@ -59,12 +60,12 @@ ${n.join(`
|
|
|
59
60
|
`;let p=[],u=[];return r.forEach(i=>{let m=i.argTypeName,y=i.responseTypeName;if(m){let d=He(i,n,o);d.trim()===""?u.push(`export type ${m} = void;`,""):u.push(`export type ${m} = {`,d,"};","")}if(y){let d=Ze(i,n,o);d.trim()===""?u.push(`export type ${y} = void;`,""):u.push(`export type ${y} = {`,d,"};","")}}),u.length>0&&p.push(u.join(`
|
|
60
61
|
`)),p.length===0&&p.push("// \u6B64\u6A94\u6848\u7528\u65BC\u5B9A\u7FA9 API \u76F8\u95DC\u7684\u985E\u578B","// \u985E\u578B\u5B9A\u7FA9\u6703\u6839\u64DA OpenAPI Schema \u81EA\u52D5\u751F\u6210",""),s+p.join(`
|
|
61
62
|
|
|
62
|
-
`)}function He(r,t,e={}){let n=[];r.queryParams&&r.queryParams.length>0&&r.queryParams.forEach(s=>{let a=s.required?"":"?",p=ae(s,e);n.push(` ${s.name}${a}: ${p};`)}),r.pathParams&&r.pathParams.length>0&&r.pathParams.forEach(s=>{let a=s.required?"":"?",p=ae(s,e);n.push(` ${s.name}${a}: ${p};`)});let o=t?.find(s=>s.operation?.operationId===r.operationName||s.operation?.operationId===r.operationName.toLowerCase()||s.verb===r.verb.toLowerCase()&&s.path===r.path);if(o?.operation?.requestBody){let a=o.operation.requestBody.content,p=a["application/json"]||a["*/*"],u=a["multipart/form-data"]||a["application/x-www-form-urlencoded"];if(p?.schema){let i=
|
|
63
|
+
`)}function He(r,t,e={}){let n=[];r.queryParams&&r.queryParams.length>0&&r.queryParams.forEach(s=>{let a=s.required?"":"?",p=ae(s,e);n.push(` ${s.name}${a}: ${p};`)}),r.pathParams&&r.pathParams.length>0&&r.pathParams.forEach(s=>{let a=s.required?"":"?",p=ae(s,e);n.push(` ${s.name}${a}: ${p};`)});let o=t?.find(s=>s.operation?.operationId===r.operationName||s.operation?.operationId===r.operationName.toLowerCase()||s.verb===r.verb.toLowerCase()&&s.path===r.path);if(o?.operation?.requestBody){let a=o.operation.requestBody.content,p=a["application/json"]||a["*/*"],u=a["multipart/form-data"]||a["application/x-www-form-urlencoded"];if(p?.schema){let i=b(p.schema,e,1);n.push(` body: ${i};`)}else if(u?.schema){let i=b(u.schema,e,1);n.push(` body: ${i};`)}else{let i=Object.values(a)[0];if(i?.schema){let m=b(i.schema,e,1);n.push(` body: ${m};`)}else n.push(" body?: any; // Request body from OpenAPI")}}return n.length===0?"":n.join(`
|
|
63
64
|
`)}function Ze(r,t,e={}){let n=[],o=t?.find(s=>s.operation?.operationId===r.operationName||s.operation?.operationId===r.operationName.toLowerCase()||s.verb===r.verb.toLowerCase()&&s.path===r.path);if(o?.operation?.responses){let s=o.operation.responses[200]||o.operation.responses[201];if(s?.content){let a=s.content["application/json"]||s.content["*/*"]||Object.values(s.content)[0];if(a?.schema){let p=Ye(a.schema,e);n.push(...p)}else n.push(" // Success response from OpenAPI"),n.push(" data?: any;")}}return n.length===0?"":n.join(`
|
|
64
|
-
`)}function Ye(r,t={}){let e=[];if(r.type==="object"&&r.properties){let n=r.required||[];Object.entries(r.properties).forEach(([o,s])=>{let p=n.includes(o)?"":"?",u=
|
|
65
|
+
`)}function Ye(r,t={}){let e=[];if(r.type==="object"&&r.properties){let n=r.required||[];Object.entries(r.properties).forEach(([o,s])=>{let p=n.includes(o)?"":"?",u=b(s,t,1),m=/[^a-zA-Z0-9_$]/.test(o)?`"${o}"`:o;s.description&&e.push(` /** ${s.description} */`),e.push(` ${m}${p}: ${u};`)})}return e}function b(r,t={},e=0){if(!r)return"any";if(r.$ref){let o=r.$ref;if(o.startsWith("#/components/schemas/")){let s=o.replace("#/components/schemas/",""),a=t[s]||s,u=`Schema.${ze(a)}`;return r.nullable?`${u} | null`:u}}let n;switch(r.type){case"string":r.enum?n=r.enum.map(a=>`"${a}"`).join(" | "):r.format==="binary"?n="Blob":n="string";break;case"number":case"integer":n="number";break;case"boolean":n="boolean";break;case"array":let o=r.items?b(r.items,t,e):"any";n=o.includes("|")?`(${o})[]`:`${o}[]`;break;case"object":if(r.properties){let a=Object.entries(r.properties);if(a.length===0)r.additionalProperties?n=`Record<string, ${r.additionalProperties===!0?"any":b(r.additionalProperties,t,e)}>`:n="{}";else{let p=" ".repeat(e+1),u=" ".repeat(e),i=[];a.forEach(([m,y])=>{let T=(r.required||[]).includes(m)?"":"?",v=b(y,t,e+1),O=/[^a-zA-Z0-9_$]/.test(m)?`"${m}"`:m;y.description&&i.push(`${p}/** ${y.description} */`),i.push(`${p}${O}${T}: ${v};`)}),n=`{
|
|
65
66
|
${i.join(`
|
|
66
67
|
`)}
|
|
67
|
-
${u}}`}}else r.additionalProperties?n=`Record<string, ${r.additionalProperties===!0?"any":
|
|
68
|
+
${u}}`}}else r.additionalProperties?n=`Record<string, ${r.additionalProperties===!0?"any":b(r.additionalProperties,t,e)}>`:n="any";break;default:n="any";break}return r.nullable?`${n} | null`:n}function ae(r,t={}){return r.schema?b(r.schema,t):"any"}c();function ce(r,t){let{groupKey:e,refetchOnMountOrArgChange:n=60}=t,o=e?`${e.charAt(0).toUpperCase()+e.slice(1)}QueryService`:"QueryService",s=e?`${e.charAt(0).toUpperCase()+e.slice(1)}ApiService`:"ApiService",a=r.filter(i=>i.isQuery).map(i=>`
|
|
68
69
|
/**
|
|
69
70
|
* ${i.summary}
|
|
70
71
|
*/
|
|
@@ -148,7 +149,7 @@ export class ${s} {
|
|
|
148
149
|
private config = inject(${e.importName});
|
|
149
150
|
private http = inject(${n.importReturnTypeName});
|
|
150
151
|
|
|
151
|
-
${r.map(a=>{let p=a.verb.toUpperCase()==="GET",u=!a.isVoidArg,i=a.hasRequestBody,m=v=>v.includes("{")&&u?`\`\${this.config.rootUrl}${v.replace(/\{([^}]+)\}/g,"${args.variables.$1}")}\``:`\`\${this.config.rootUrl}${v}\``,y=a.queryParams.length>0,d=!p&&u,T=()=>{let v="";return a.queryParams&&a.queryParams.length>0&&(v=`params: {${a.queryParams.map(
|
|
152
|
+
${r.map(a=>{let p=a.verb.toUpperCase()==="GET",u=!a.isVoidArg,i=a.hasRequestBody,m=v=>v.includes("{")&&u?`\`\${this.config.rootUrl}${v.replace(/\{([^}]+)\}/g,"${args.variables.$1}")}\``:`\`\${this.config.rootUrl}${v}\``,y=a.queryParams.length>0,d=!p&&u,T=()=>{let v="";return a.queryParams&&a.queryParams.length>0&&(v=`params: {${a.queryParams.map(O=>`${O.name}: args.variables.${O.name},`).join(`
|
|
152
153
|
`)}},`),`{
|
|
153
154
|
...args?.fetchOptions,
|
|
154
155
|
headers: { 'Content-Type': '${a.contentType}', ...args?.fetchOptions?.headers },
|