openapi-sync 3.0.2 ā 4.0.0
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/README.md +46 -1343
- package/dist/index.d.mts +43 -1
- package/dist/index.d.ts +43 -1
- package/dist/index.js +72 -54
- package/dist/index.mjs +72 -54
- package/package.json +28 -13
- package/db.json +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,66 +1,84 @@
|
|
|
1
|
-
import
|
|
2
|
-
`+" ".repeat(
|
|
3
|
-
`).filter(
|
|
4
|
-
${" ".repeat(
|
|
5
|
-
${" ".repeat(
|
|
1
|
+
import U from'fs';import M from'path';import*as ae from'js-yaml';import Kt from'lodash.isequal';import xe from'lodash.get';import Wt from'axios';import Dt from'axios-retry';import Qt from'@apidevtools/swagger-parser';import {CurlGenerator}from'curl-generator';var re=(u=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(u,{get:(d,e)=>(typeof require!="undefined"?require:d)[e]}):u)(function(u){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+u+'" is not supported')});var ne=(u,d,e)=>new Promise((j,b)=>{var v=O=>{try{y(e.next(O));}catch(E){b(E);}},m=O=>{try{y(e.throw(O));}catch(E){b(E);}},y=O=>O.done?j(O.value):Promise.resolve(O.value).then(v,m);y((e=e.apply(u,d)).next());});var Xt=/^[A-Za-z_$][A-Za-z0-9_$]*$/,Et=/[A-Za-z0-9_$]/;var ct=u=>["object"].includes(typeof u)&&!(u instanceof Blob),Gt=u=>{try{return ae.load(u),!0}catch(d){let e=d;if(e instanceof ae.YAMLException)return false;throw e}},Tt=u=>{if(Gt(u)){let d=ae.load(u),e=JSON.stringify(d,null,2);return JSON.parse(e)}},K=u=>u.substring(0,1).toUpperCase()+u.substring(1),St=(u,d)=>{let e=u.split("/"),j=`${K(d)}`,b=[];return e.forEach(v=>{if(v[0]==="{"&&v[v.length-1]==="}"){let y=v.replace(/{/,"").replace(/}/,"");b.push(y),v=`$${y}`;}else if(v[0]==="<"&&v[v.length-1]===">"){let y=v.replace(/</,"").replace(/>/,"");b.push(y),v=`$${y}`;}else if(v[0]===":"){let y=v.replace(/:/,"");b.push(y),v=`$${y}`;}let m="";v.split("").forEach(y=>{let O=y;Et.test(y)||(O="/"),m+=O;}),m.split("/").forEach(y=>{j+=K(y);});}),{name:j,variables:b,pathParts:e}},se=(u,d=1)=>{let e="{",j=Object.keys(u);for(let b=0;b<j.length;b++){let v=j[b],m=u[v];if(e+=`
|
|
2
|
+
`+" ".repeat(d)+v+": ",Array.isArray(m)){e+="[";for(let y=0;y<m.length;y++){let O=m[y];typeof O=="object"&&O!==null?e+=se(O,d+1):e+=typeof O=="string"?`"${O}"`:O,y<m.length-1&&(e+=", ");}e+="]";}else typeof m=="object"&&m!==null?e+=""+se(m,d+1):e+=m.split(`
|
|
3
|
+
`).filter(y=>y.trim()!=="").join(`
|
|
4
|
+
${" ".repeat(d)}`);b<j.length-1&&(e+=", ");}return e+=`
|
|
5
|
+
${" ".repeat(d-1)}}`,e},ue=(u,d=1)=>`
|
|
6
6
|
\`\`\`typescript
|
|
7
|
-
${" ".repeat(
|
|
7
|
+
${" ".repeat(d)} ${u.split(`
|
|
8
8
|
`).filter(e=>e.trim()!=="").join(`
|
|
9
|
-
${" ".repeat(
|
|
10
|
-
\`\`\``;function
|
|
11
|
-
`,
|
|
12
|
-
`).filter(
|
|
9
|
+
${" ".repeat(d)} `)}
|
|
10
|
+
\`\`\``;function er(u,d){return d.split(".").reduce((j,b)=>j&&j[b]!==void 0?j[b]:void 0,u)}var Ut=(u,d="CUSTOM CODE")=>{let e=`// \u{1F512} ${d} START`,j=`// \u{1F512} ${d} END`,b={beforeGenerated:"",afterGenerated:""},v=0,m=[];for(;v<u.length;){let y=u.indexOf(e,v);if(y===-1)break;let O=u.indexOf(j,y);if(O===-1)break;let E=O+j.length,P=u.substring(E,E+100),B=P.match(/^\s*\n\s*(\/\/ =+)/);if(B){let ie=P.indexOf(`
|
|
11
|
+
`,B.index+1);ie!==-1?E+=ie+1:E+=B[0].length;}let _=y,L=u.substring(Math.max(0,y-200),y).lastIndexOf("// ==========");L!==-1&&(_=Math.max(0,y-200)+L);let V=u.substring(_,E);m.push({start:_,end:E,content:V}),v=E;}return m.length>0&&(u.substring(0,m[0].start).split(`
|
|
12
|
+
`).filter(E=>{let P=E.trim();return P.length>0&&!P.startsWith("//")}).join("").length===0?(b.beforeGenerated=m[0].content,m.length>1&&(b.afterGenerated=m[1].content)):(b.afterGenerated=m[0].content,m.length>1&&!b.beforeGenerated&&(b.beforeGenerated=m[1].content))),b},At=(u,d="CUSTOM CODE",e=true)=>{let j=e?`// ${u==="top"?"Add your custom code below this line":"Add your custom code above this line"}
|
|
13
13
|
// This section will be preserved during regeneration
|
|
14
14
|
`:"";return `// ${"=".repeat(60)}
|
|
15
|
-
// \u{1F512} ${
|
|
16
|
-
${
|
|
15
|
+
// \u{1F512} ${d} START
|
|
16
|
+
${j}// ${"=".repeat(60)}
|
|
17
17
|
|
|
18
|
-
// \u{1F512} ${
|
|
19
|
-
// ${"=".repeat(60)}`},
|
|
20
|
-
`)};var
|
|
21
|
-
`).filter(
|
|
22
|
-
*${" ".repeat(1)}`))),
|
|
23
|
-
${
|
|
18
|
+
// \u{1F512} ${d} END
|
|
19
|
+
// ${"=".repeat(60)}`},wt=(u,d,e={})=>{let{position:j="bottom",markerText:b="CUSTOM CODE",includeInstructions:v=true}=e,m={beforeGenerated:"",afterGenerated:""};d&&(m=Ut(d,b)),!m.beforeGenerated&&!m.afterGenerated&&((j==="top"||j==="both")&&(m.beforeGenerated=At("top",b,v)),(j==="bottom"||j==="both")&&(m.afterGenerated=At("bottom",b,v)));let y=[];return m.beforeGenerated&&(y.push(m.beforeGenerated),y.push("")),y.push(u),m.afterGenerated&&(y.push(""),y.push(m.afterGenerated)),y.join(`
|
|
20
|
+
`)};var me=M.join(__dirname,"../","../db.json");U.existsSync(me)||U.writeFileSync(me,"{}");var $e={};try{$e=re(me);}catch(u){$e={};}var oe=$e||{},zt=u=>{U.writeFileSync(me,JSON.stringify(u));},Nt=(u,d)=>{oe[u]=d,zt(oe);},kt=u=>oe[u],Rt=()=>{oe={},zt(oe);};var Y=process.cwd(),Oe={},Jt=Wt.create({timeout:6e4});Dt(Jt,{retries:20,retryCondition:u=>u.code==="ECONNABORTED"||u.message.includes("Network Error"),retryDelay:u=>u*1e3});var Z=(u,d,e)=>ne(null,null,function*(){var m,y,O,E;if(!(((m=e==null?void 0:e.customCode)==null?void 0:m.enabled)!==false)){yield U.promises.writeFile(u,d);return}let b=null;try{b=yield U.promises.readFile(u,"utf-8");}catch(P){}let v=wt(d,b,{position:((y=e==null?void 0:e.customCode)==null?void 0:y.position)||"bottom",markerText:(O=e==null?void 0:e.customCode)==null?void 0:O.markerText,includeInstructions:(E=e==null?void 0:e.customCode)==null?void 0:E.includeInstructions});yield U.promises.writeFile(u,v);}),Mt=(u,d,e,j)=>ne(null,null,function*(){var je,ve,Ce,Ee,Ae,ce,Te,Se,we,ze;let b=yield Jt.get(u),v=ct(b.data)?b.data:Tt(b.data),m;try{m=yield Qt.parse(v);}catch(t){let r=t instanceof Error?t.message:String(t);throw new Error(`Failed to parse OpenAPI spec for ${d}: ${r}`)}let y=M.join((e==null?void 0:e.folder)||"",d),O={},E=t=>{var r,f;if((r=e==null?void 0:e.folderSplit)!=null&&r.customFolder){let l=e.folderSplit.customFolder(t);if(l)return l}return (f=e==null?void 0:e.folderSplit)!=null&&f.byTags&&t.tags&&t.tags.length>0?t.tags[0].toLowerCase().replace(/\s+/g,"-"):"default"},P=typeof(e==null?void 0:e.server)=="string"?e==null?void 0:e.server:((ve=(je=m==null?void 0:m.servers)==null?void 0:je[(e==null?void 0:e.server)||0])==null?void 0:ve.url)||"",B=typeof((Ee=(Ce=e==null?void 0:e.types)==null?void 0:Ce.name)==null?void 0:Ee.prefix)=="string"?e==null?void 0:e.types.name.prefix:"I",_=typeof((ce=(Ae=e==null?void 0:e.endpoints)==null?void 0:Ae.name)==null?void 0:ce.prefix)=="string"?e==null?void 0:e.endpoints.name.prefix:"",de=(t,r)=>{var l,a;let f=K(t);if((a=(l=e==null?void 0:e.types)==null?void 0:l.name)!=null&&a.format){let o=e==null?void 0:e.types.name.format("shared",{name:t},f);if(o)return `${B}${o}`}return `${B}${f}`},L=(t,r,f,l,a,o=0)=>{let s="",p="",i="";if(r)if(r.$ref)if(r.$ref[0]==="#"){let $=(r.$ref||"").split("/");$.shift(),[...$].pop();let k=xe(t,$,null);if(k){k!=null&&k.name&&(s=k.name),p=$[$.length-1];let R=de(p);R.includes(".")&&(R=R.split(".").map((W,Q)=>Q===0?W:`["${W}"]`).join("")),i+=`${a!=null&&a.noSharedImport?"":"Shared."}${R}`;}}else i+="";else if(r.anyOf)i+=`(${r.anyOf.map($=>L(t,$,"",l,a)).filter($=>!!$).join("|")})`;else if(r.oneOf)i+=`(${r.oneOf.map($=>L(t,$,"",l,a)).filter($=>!!$).join("|")})`;else if(r.allOf)i+=`(${r.allOf.map($=>L(t,$,"",l,a)).filter($=>!!$).join("&")})`;else if(r.items)i+=`${L(t,r.items,"",false,a)}[]`;else if(r.properties){let $=Object.keys(r.properties),C=r.required||[],N="";$.forEach(k=>{var ee,W,Q,X,le,te;let R="";!((W=(ee=e==null?void 0:e.types)==null?void 0:ee.doc)!=null&&W.disable)&&((X=(Q=r.properties)==null?void 0:Q[k])!=null&&X.description)&&(R=" * "+((le=r.properties)==null?void 0:le[k].description.split(`
|
|
21
|
+
`).filter(pe=>pe.trim()!=="").join(`
|
|
22
|
+
*${" ".repeat(1)}`))),N+=(R?`/**
|
|
23
|
+
${R}
|
|
24
24
|
*/
|
|
25
|
-
`:"")+`${
|
|
26
|
-
${" ".repeat(
|
|
27
|
-
`:""}`:""},
|
|
28
|
-
`);
|
|
29
|
-
${
|
|
30
|
-
}`:a+="{}";}else if(r.enum&&r.enum.length>0)r.enum.length>1&&(a+=r.enum[0]);else if(r.type)if(r.example)a+=JSON.stringify(r.example);else {let
|
|
31
|
-
`).filter(
|
|
32
|
-
*${" ".repeat(1)}`)),
|
|
33
|
-
${
|
|
25
|
+
`:"")+`${L(t,(te=r.properties)==null?void 0:te[k],k,C.includes(k),a,o+1)}`;}),N.length>0?i+=`{
|
|
26
|
+
${" ".repeat(o)}${N}${" ".repeat(o)}}`:i+="{[k: string]: any}";}else if(r.enum&&r.enum.length>0)r.enum.length>1&&(i+="("),r.enum.map($=>JSON.stringify($)).filter($=>!!$).forEach(($,C)=>{i+=`${C===0?"":"|"}${$}`;}),r.enum.length>1&&(i+=")");else if(r.type){let $=C=>{let N="";if(typeof C=="string")["string","integer","number","array","boolean","null"].includes(C)?["integer","number"].includes(C)?N+="number":C==="array"?N+="any[]":N+=C:C==="object"&&(r.additionalProperties?N+=`{[k: string]: ${L(t,r.additionalProperties,"",true,a)||"any"}}`:N+="{[k: string]: any}");else if(Array.isArray(C)){let k=C.map(R=>$(R));k.filter(R=>R!==""),k.length>1&&(N+="("+k.join("|")+")");}else N+="any";return N};i=$(r.type);}else i+="any";else i="string";let h=s||f;a!=null&&a.useComponentName&&!h&&(h=p);let z=h?` "${h}"${l?"":"?"}: `:"",n=r!=null&&r.nullable?" | null":"";return i.length>0?`${z}${i}${n}${h?`;
|
|
27
|
+
`:""}`:""},V=(t,r)=>{let a="";if(r)if(r.$ref)if(r.$ref[0]==="#"){let o=(r.$ref||"").split("/");o.shift();let p=xe(t,o,null);p&&(p!=null&&p.name&&(p.name),o[o.length-1],a+=V(t,p));}else a+="";else if(r.anyOf)a+=V(t,r.anyOf[0]);else if(r.oneOf)a+=V(t,r.oneOf[0]);else if(r.allOf)a+=`{${r.allOf.map(o=>`...(${V(t,o)})`).join(",")}}`;else if(r.items)a+=`[${V(t,r.items)}]`;else if(r.properties){let p=Object.keys(r.properties).map(i=>{var h;return ` "${i}": ${V(t,(h=r.properties)==null?void 0:h[i])}`}).join(`,
|
|
28
|
+
`);p.length>0?a+=`{
|
|
29
|
+
${p}
|
|
30
|
+
}`:a+="{}";}else if(r.enum&&r.enum.length>0)r.enum.length>1&&(a+=r.enum[0]);else if(r.type)if(r.example)a+=JSON.stringify(r.example);else {let o=s=>{let p="";if(typeof s=="string")["string","integer","number","array","boolean","null"].includes(s)?["integer","number"].includes(s)?p+="123":s==="array"?p+="[]":s==="boolean"?p+="true":s==="null"?p+="null":p+=`"${s}"`:s==="object"&&(p+="{}");else if(Array.isArray(s)){let i=s.map(h=>o(h));i.filter(h=>h!==""),i.length>1&&(p+=i.join("|"));}else p+="any";return p};a=o(r.type);}else a+="any";else a="string";return a};j&&!isNaN(j)&&j>0&&(process.env.NODE_ENV&&["production","prod","test","staging"].includes(process.env.NODE_ENV)||(Oe[d]&&clearTimeout(Oe[d]),Oe[d]=setTimeout(()=>Mt(u,d,e,j),j)));let ie=kt(d);if(Kt(ie,m))return;Nt(d,m);let ye="",g="",H={};m.components&&Object.keys(m.components).forEach(t=>{if(["schemas","responses","parameters","examples","requestBodies","headers","links","callbacks"].includes(t)){let r=m.components[t],f={},l={};Object.keys(r).forEach(o=>{var i;let s=(i=r[o])!=null&&i.schema?r[o].schema:r[o],p=`${L(m,s,"",true,{noSharedImport:true,useComponentName:["parameters"].includes(t)})}`;if(p){let h=o.split("."),z=f,n=l;for(let $=0;$<h.length;$++){let C=h[$];$<h.length-1?(C in z||(z[C]={},n[C]={}),z=z[C],n=n[C]):(z[C]=p,n[C]=s);}}}),Object.keys(f).forEach(o=>{var h,z,n,$;let s=de(o),p=f[o],i="";!((z=(h=e==null?void 0:e.types)==null?void 0:h.doc)!=null&&z.disable)&&o in r&&((n=r[o])!=null&&n.description)&&(i=" * "+r[o].description.split(`
|
|
31
|
+
`).filter(C=>C.trim()!=="").join(`
|
|
32
|
+
*${" ".repeat(1)}`)),H[o]=(($=H[o])!=null?$:"")+(i?`/**
|
|
33
|
+
${i}
|
|
34
34
|
*/
|
|
35
|
-
`:"")+"export type "+
|
|
36
|
-
`;});}});let
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
`),
|
|
44
|
-
|
|
35
|
+
`:"")+"export type "+s+" = "+(typeof p=="string"?p:se(p))+`;
|
|
36
|
+
`;});}});let be=t=>{let r="";if(t.content){let f=Object.keys(t.content);f[0]&&t.content[f[0]].schema&&(r+=`${L(m,t.content[f[0]].schema,"")}`);}return r},G=(t,r,f=0)=>{if(!t)return r==="joi"?"Joi.any()":r==="yup"?"yup.mixed()":"z.any()";if(t.$ref){if(t.$ref[0]==="#"){let l=t.$ref.split("/");l.shift();let a=l.join("."),o=xe(m,a);if(o)return G(o,r,f)}return r==="joi"?"Joi.any()":r==="yup"?"yup.mixed()":"z.any()"}if(t.anyOf||t.oneOf){let l=t.anyOf||t.oneOf;if(l.every(o=>o.enum!==void 0&&Array.isArray(o.enum))){let o=[],s=false;if(l.forEach(p=>{p.enum&&p.enum.forEach(i=>{i===null?s=true:o.includes(i)||o.push(i);});}),o.length>0){let p=o.map(i=>JSON.stringify(i)).join(", ");if(r==="zod"){let i=`z.enum([${p}])`;return s&&(i+=".nullable()"),i}else if(r==="yup"){let i=`yup.mixed().oneOf([${p}])`;return s&&(i+=".nullable()"),i}else return s?`Joi.valid(${p}, null)`:`Joi.valid(${p})`}else if(s)return r==="zod"?"z.null()":r==="yup"?"yup.mixed().nullable()":"Joi.valid(null)"}return r==="zod"?`z.union([${l.map(p=>G(p,r,f+1)).join(", ")}])`:r==="yup"?`yup.mixed().oneOf([${l.map(p=>G(p,r,f+1)).join(", ")}])`:`Joi.alternatives().try(${l.map(s=>G(s,r,f+1)).join(", ")})`}if(t.allOf)if(r==="zod"){let l=t.allOf.map(o=>G(o,r,f+1)),a=l[0];for(let o=1;o<l.length;o++)a=`${a}.merge(${l[o]})`;return a}else return G(t.allOf[0],r,f+1);if(t.items){let l=G(t.items,r,f+1);if(r==="zod"){let a=`z.array(${l})`;return t.minItems!==void 0&&(a+=`.min(${t.minItems})`),t.maxItems!==void 0&&(a+=`.max(${t.maxItems})`),a}else if(r==="yup"){let a=`yup.array().of(${l})`;return t.minItems!==void 0&&(a+=`.min(${t.minItems})`),t.maxItems!==void 0&&(a+=`.max(${t.maxItems})`),a}else {let a=`Joi.array().items(${l})`;return t.minItems!==void 0&&(a+=`.min(${t.minItems})`),t.maxItems!==void 0&&(a+=`.max(${t.maxItems})`),a}}if(t.properties){let l=t.required||[],a=" ".repeat(f+1),o=Object.entries(t.properties).map(([s,p])=>{let i=l.includes(s),h=G(p,r,f+1);return i||(h+=".optional()"),`${a}${s}: ${h}`}).join(`,
|
|
37
|
+
`);return r==="zod"?`z.object({
|
|
38
|
+
${o}
|
|
39
|
+
${" ".repeat(f)}})`:r==="yup"?`yup.object({
|
|
40
|
+
${o}
|
|
41
|
+
${" ".repeat(f)}})`:`Joi.object({
|
|
42
|
+
${o}
|
|
43
|
+
${" ".repeat(f)}})`}if(t.enum&&t.enum.length>0){let l=[],a=false;t.enum.forEach(s=>{s===null?a=true:l.includes(s)||l.push(s);});let o=l.map(s=>JSON.stringify(s)).join(", ");if(console.log("zod enum 3 here",{schema:t,enumValues:o}),r==="zod"){let s=`z.enum([${o}])`;return a&&(s+=".nullable()"),s}else if(r==="yup"){let s=`yup.mixed().oneOf([${o}])`;return a&&(s+=".nullable()"),s}else {let s=`Joi.valid(${o})`;return a&&(s+=".allow(null)"),s}}if(t.type){let a=(o=>{switch(o){case "string":if(r==="zod"){let s="z.string()";return t.format==="email"?s+=".email()":t.format==="uuid"?s+=".uuid()":t.format==="url"||t.format==="uri"?s+=".url()":t.format==="date-time"?s+=".datetime()":t.format==="date"&&(s+=".date()"),t.minLength&&(s+=`.min(${t.minLength})`),t.maxLength&&(s+=`.max(${t.maxLength})`),t.pattern&&(s+=`.regex(/${t.pattern}/)`),s}else if(r==="yup"){let s="yup.string()";return t.format==="email"?s+=".email()":(t.format==="url"||t.format==="uri")&&(s+=".url()"),t.minLength&&(s+=`.min(${t.minLength})`),t.maxLength&&(s+=`.max(${t.maxLength})`),t.pattern&&(s+=`.matches(/${t.pattern}/)`),s}else {let s="Joi.string()";return t.format==="email"?s+=".email()":t.format==="url"||t.format==="uri"?s+=".uri()":t.format==="uuid"?s+=".guid({ version: 'uuidv4' })":t.format==="date-time"&&(s+=".isoDate()"),t.minLength&&(s+=`.min(${t.minLength})`),t.maxLength&&(s+=`.max(${t.maxLength})`),t.pattern&&(s+=`.pattern(/${t.pattern}/)`),s}case "integer":case "number":if(r==="zod"){let s="z.number()";return o==="integer"&&(s+=".int()"),t.minimum!==void 0&&(t.exclusiveMinimum?s+=`.gt(${t.minimum})`:s+=`.min(${t.minimum})`),t.maximum!==void 0&&(t.exclusiveMaximum?s+=`.lt(${t.maximum})`:s+=`.max(${t.maximum})`),s}else if(r==="yup"){let s="yup.number()";return o==="integer"&&(s+=".integer()"),t.minimum!==void 0&&(s+=`.min(${t.minimum})`),t.maximum!==void 0&&(s+=`.max(${t.maximum})`),s}else {let s="Joi.number()";return o==="integer"&&(s+=".integer()"),t.minimum!==void 0&&(t.exclusiveMinimum?s+=`.greater(${t.minimum})`:s+=`.min(${t.minimum})`),t.maximum!==void 0&&(t.exclusiveMaximum?s+=`.less(${t.maximum})`:s+=`.max(${t.maximum})`),s}case "boolean":return r==="joi"?"Joi.boolean()":r==="yup"?"yup.boolean()":"z.boolean()";case "null":return r==="joi"?"Joi.any().allow(null)":r==="yup"?"yup.mixed().nullable()":"z.null()";default:return r==="joi"?"Joi.any()":r==="yup"?"yup.mixed()":"z.any()"}})(typeof t.type=="string"?t.type:t.type[0]);return t.nullable&&r!=="joi"?a+=".nullable()":t.nullable&&r==="joi"&&(a+=".allow(null)"),a}return r==="joi"?"Joi.any()":r==="yup"?"yup.mixed()":"z.any()"},Bt=t=>{var r,f,l,a,o;if((f=(r=e==null?void 0:e.endpoints)==null?void 0:r.value)!=null&&f.replaceWords&&Array.isArray(e==null?void 0:e.endpoints.value.replaceWords)){let s=t;return (o=(a=(l=e==null?void 0:e.endpoints)==null?void 0:l.value)==null?void 0:a.replaceWords)==null||o.forEach((p,i)=>{let h=new RegExp(p.replace,"g");s=s.replace(h,p.with||"");}),s}else return t},Pt=(t,r,f=[])=>{var o,s;let l=(o=e==null?void 0:e.endpoints)==null?void 0:o.exclude,a=(s=e==null?void 0:e.endpoints)==null?void 0:s.include;if(a){let p=a.tags&&a.tags.length>0?f.some(h=>a.tags.includes(h)):true,i=a.endpoints&&a.endpoints.length>0?a.endpoints.some(h=>{let z=!h.method||h.method.toLowerCase()===r.toLowerCase();return h.path?t===h.path&&z:h.regex?new RegExp(h.regex).test(t)&&z:false}):true;if(!p||!i)return true}return !!(l&&(l.tags&&l.tags.length>0&&f.some(i=>l.tags.includes(i))||l.endpoints&&l.endpoints.length>0&&l.endpoints.some(i=>{let h=!i.method||i.method.toLowerCase()===r.toLowerCase();return i.path?t===i.path&&h:i.regex?new RegExp(i.regex).test(t)&&h:false})))};if(Object.keys(m.paths||{}).forEach(t=>{let r=m.paths[t];Object.keys(r).forEach(l=>{var pe,Ne,ke,Re,Je,Me,qe,Be,Pe,Le,Fe,Ge,Ue,Ve,Ke,We,De,Qe,Ye,Ze,He,Xe,_e,ge,et,tt,rt,nt,st,at,ot,it,lt,pt,ut,mt,dt,yt,ft,$t,xt,Ot,It,ht,bt,jt,vt,Ct;let a=l,o=St(t,a),s=((pe=r[a])==null?void 0:pe.tags)||[];if(Pt(t,a,s))return;let p=r[a],i=E({method:a,path:t,summary:p==null?void 0:p.summary,operationId:p==null?void 0:p.operationId,tags:s,parameters:p==null?void 0:p.parameters,requestBody:p==null?void 0:p.requestBody,responses:p==null?void 0:p.responses});O[i]||(O[i]={endpoints:"",types:"",validation:""});let h=((ke=(Ne=e==null?void 0:e.endpoints)==null?void 0:Ne.value)!=null&&ke.includeServer?P:"")+o.pathParts.map(x=>(x[0]==="{"&&x[x.length-1]==="}"?x=`\${${x.replace(/{/,"").replace(/}/,"")}}`:x[0]==="<"&&x[x.length-1]===">"?x=`\${${x.replace(/</,"").replace(/>/,"")}}`:x[0]===":"&&(x=`\${${x.replace(/:/,"")}}`),x)).join("/"),z=`"${h}"`;o.variables.length>0&&(z=`(${o.variables.map(I=>`${I}:string`).join(",")})=> \`${h}\``),z=Bt(z);let n=r[a],$="";if(n!=null&&n.parameters&&((n==null?void 0:n.parameters).forEach((I,A)=>{(I.$ref||I.in==="query"&&I.name)&&($+=`${L(m,I.$ref?I:I.schema,I.name||"",I.required)}`);}),$)){$=`{
|
|
44
|
+
${$}}`;let I=`${o.name}Query`;if((Je=(Re=e==null?void 0:e.types)==null?void 0:Re.name)!=null&&Je.useOperationId&&(n!=null&&n.operationId)&&(I=`${n.operationId}Query`),I=K(`${B}${I}`),(qe=(Me=e==null?void 0:e.types)==null?void 0:Me.name)!=null&&qe.format){let S=e==null?void 0:e.types.name.format("endpoint",{code:"",type:"query",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},I);S&&(I=`${B}${S}`);}let A=`export type ${I} = ${$};
|
|
45
|
+
`;e!=null&&e.folderSplit?O[i].types+=A:g+=A;}if(((Be=e==null?void 0:e.validations)==null?void 0:Be.disable)!==true&&((Le=(Pe=e==null?void 0:e.validations)==null?void 0:Pe.generate)==null?void 0:Le.query)!==false&&(n!=null&&n.parameters)){let x=((Fe=e.validations)==null?void 0:Fe.library)||"zod",A=n.parameters.filter(S=>!S.$ref&&S.in==="query"&&S.name);if(A.length>0){let S=((Ge=e==null?void 0:e.validations)==null?void 0:Ge.name)||((Ue=e==null?void 0:e.types)==null?void 0:Ue.name),J=typeof(S==null?void 0:S.prefix)=="string"?S.prefix:"I",c=typeof((Ke=(Ve=e==null?void 0:e.validations)==null?void 0:Ve.name)==null?void 0:Ke.suffix)=="string"?e.validations.name.suffix:"Schema",w=`${o.name}Query`;if(S!=null&&S.useOperationId&&(n!=null&&n.operationId)&&(w=`${n.operationId}Query`),w=K(`${J}${w}${c}`),(De=(We=e==null?void 0:e.validations)==null?void 0:We.name)!=null&&De.format){let F=e.validations.name.format({code:"",type:"query",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},w);F&&(w=`${J}${F}${c}`);}else if((Ye=(Qe=e==null?void 0:e.types)==null?void 0:Qe.name)!=null&&Ye.format){let F=e.types.name.format("endpoint",{code:"",type:"query",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},w);F&&(w=`${J}${F}${c}`);}let T=A.map(F=>{let Lt=F!=null&&F.schema?G(F.schema,x):x==="joi"?"Joi.string()":x==="yup"?"yup.string()":"z.string()",Ft=F.required?"":".optional()";return ` ${F.name}: ${Lt}${Ft}`}).join(`,
|
|
46
|
+
`),D=`export const ${w} = ${x==="joi"?"Joi.object":x==="yup"?"yup.object":"z.object"}({
|
|
47
|
+
${T}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
`;e!=null&&e.folderSplit||O[i]||(O[i]={endpoints:"",types:"",validation:""}),O[i].validation+=D;}}let C=n==null?void 0:n.requestBody,N="";if(C&&(N=be(C),N)){let x=`${o.name}DTO`;if((He=(Ze=e==null?void 0:e.types)==null?void 0:Ze.name)!=null&&He.useOperationId&&(n!=null&&n.operationId)&&(x=`${n.operationId}DTO`),x=K(`${B}${x}`),(_e=(Xe=e==null?void 0:e.types)==null?void 0:Xe.name)!=null&&_e.format){let A=e==null?void 0:e.types.name.format("endpoint",{code:"",type:"dto",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},x);A&&(x=`${B}${A}`);}let I=`export type ${x} = ${N};
|
|
51
|
+
`;e!=null&&e.folderSplit?O[i].types+=I:g+=I;}if(((ge=e==null?void 0:e.validations)==null?void 0:ge.disable)!==true&&((tt=(et=e==null?void 0:e.validations)==null?void 0:et.generate)==null?void 0:tt.dto)!==false&&C){let x=((rt=e.validations)==null?void 0:rt.library)||"zod";if(C.content){let I=Object.keys(C.content);if(I[0]&&C.content[I[0]].schema){let A=((nt=e==null?void 0:e.validations)==null?void 0:nt.name)||((st=e==null?void 0:e.types)==null?void 0:st.name),S=typeof(A==null?void 0:A.prefix)=="string"?A.prefix:"I",J=typeof((ot=(at=e==null?void 0:e.validations)==null?void 0:at.name)==null?void 0:ot.suffix)=="string"?e.validations.name.suffix:"Schema",c=`${o.name}DTO`;if(A!=null&&A.useOperationId&&(n!=null&&n.operationId)&&(c=`${n.operationId}DTO`),c=K(`${S}${c}${J}`),(lt=(it=e==null?void 0:e.validations)==null?void 0:it.name)!=null&<.format){let q=e.validations.name.format({code:"",type:"dto",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},c);q&&(c=`${S}${q}${J}`);}else if((ut=(pt=e==null?void 0:e.types)==null?void 0:pt.name)!=null&&ut.format){let q=e.types.name.format("endpoint",{code:"",type:"dto",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},c);q&&(c=`${S}${q}${J}`);}let w=G(C.content[I[0]].schema,x),T=`export const ${c} = ${w};
|
|
52
|
+
|
|
53
|
+
`;e!=null&&e.folderSplit||O[i]||(O[i]={endpoints:"",types:"",validation:""}),O[i].validation+=T;}}}let k={},R="";if(n!=null&&n.responses){let x=n==null?void 0:n.responses;Object.keys(x).forEach(A=>{var S,J,c,w;if(R=be(x[A]),k[A]=R,R){let T=`${o.name}${A}Response`;if((J=(S=e==null?void 0:e.types)==null?void 0:S.name)!=null&&J.useOperationId&&(n!=null&&n.operationId)&&(T=`${n.operationId}${A}Response`),T=K(`${B}${T}`),(w=(c=e==null?void 0:e.types)==null?void 0:c.name)!=null&&w.format){let D=e==null?void 0:e.types.name.format("endpoint",{code:A,type:"response",method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},T);D&&(T=`${B}${D}`);}let q=`export type ${T} = ${R};
|
|
54
|
+
`;e!=null&&e.folderSplit?O[i].types+=q:g+=q;}});}let ee=x=>!x||!x.length?"":x.map(I=>Object.entries(I).map(([S,J])=>{let c=S,w="";return Array.isArray(J)&&J.length&&(w=`
|
|
55
|
+
- Scopes: [\`${J.join("`, `")}\`]`,c=`**${c}**`),`
|
|
56
|
+
- ${c}${w}`}).join("")).join(`
|
|
57
|
+
`),W=n!=null&&n.security?ee(n.security):"",Q="";if(!((dt=(mt=e==null?void 0:e.endpoints)==null?void 0:mt.doc)!=null&&dt.disable)){let x="";if((ft=(yt=e==null?void 0:e.endpoints)==null?void 0:yt.doc)!=null&&ft.showCurl){let I={},A="",S="";($t=n.requestBody)!=null&&$t.content&&Object.keys(n.requestBody.content).forEach(w=>{let T=n.requestBody.content[w].schema;if(T){Array.isArray(I["Content-type"])?I["Content-type"].push(w):I["Content-type"]=[w];let q=V(m,T);q&&(A=q);}}),n!=null&&n.security&&n.security.forEach(c=>{Object.keys(c).forEach(w=>{var q,D;let T=(D=(q=m.components)==null?void 0:q.securitySchemes)==null?void 0:D[w];T&&(T.type==="mutualTLS"?S+=`
|
|
58
|
+
--cert client-certificate.crt --key client-private-key.key --cacert ca-certificate.crt`:T.type==="apiKey"?I[(T==null?void 0:T.name)||"X-API-KEY"]="{API_KEY_VALUE}":I.Authorization=`${(T==null?void 0:T.scheme)==="basic"?"Basic":"Bearer"} {${(T==null?void 0:T.scheme)==="basic"?"VALUE":"TOKEN"}}`);});});let J={};Object.keys(I).forEach(c=>{Array.isArray(I[c])?J[c]=I[c].join("; "):J[c]=I[c];}),x=`
|
|
45
59
|
\`\`\`bash
|
|
46
|
-
${CurlGenerator({url:
|
|
47
|
-
\`\`\``;}
|
|
48
|
-
* ${
|
|
60
|
+
${CurlGenerator({url:P+t,method:a.toUpperCase(),headers:J,body:A})}${S}
|
|
61
|
+
\`\`\``;}Q=`/**${n!=null&&n.description?`
|
|
62
|
+
* ${n==null?void 0:n.description} `:""}
|
|
49
63
|
* **Method**: \`${a.toUpperCase()}\`
|
|
50
|
-
* **Summary**: ${(
|
|
51
|
-
* **Tags**: [${((
|
|
52
|
-
* **OperationId**: ${(
|
|
53
|
-
* **Query**: ${
|
|
54
|
-
* **DTO**: ${
|
|
55
|
-
* **Response**: ${Object.entries(
|
|
56
|
-
- **${
|
|
57
|
-
* **Security**: ${
|
|
58
|
-
`:""}${
|
|
64
|
+
* **Summary**: ${(n==null?void 0:n.summary)||""}
|
|
65
|
+
* **Tags**: [${((xt=n==null?void 0:n.tags)==null?void 0:xt.join(", "))||""}]
|
|
66
|
+
* **OperationId**: ${(n==null?void 0:n.operationId)||""} ${$?`
|
|
67
|
+
* **Query**: ${ue($)} `:""}${N?`
|
|
68
|
+
* **DTO**: ${ue(N)} `:""}${R?`
|
|
69
|
+
* **Response**: ${Object.entries(k).map(([I,A])=>`
|
|
70
|
+
- **${I}**: ${ue(A,2)} `).join("")}`:""}${W?`
|
|
71
|
+
* **Security**: ${W}
|
|
72
|
+
`:""}${x}
|
|
59
73
|
*/
|
|
60
|
-
`;}let
|
|
61
|
-
`;e!=null&&e.folderSplit?
|
|
74
|
+
`;}let X=(It=(Ot=e==null?void 0:e.endpoints)==null?void 0:Ot.name)!=null&&It.useOperationId&&((ht=n==null?void 0:n.operationId)==null?void 0:ht.length)>0?n.operationId:`${o.name}`;if((jt=(bt=e==null?void 0:e.endpoints)==null?void 0:bt.name)!=null&&jt.format){let x=e==null?void 0:e.endpoints.name.format({method:a,path:t,summary:n==null?void 0:n.summary,operationId:n==null?void 0:n.operationId},X);x&&(X=x);}let le={method:`"${a}"`,operationId:`"${n==null?void 0:n.operationId}"`,url:z,tags:(n==null?void 0:n.tags)||[]},te=`${Q}export const ${_}${X} = ${((Ct=(vt=e==null?void 0:e.endpoints)==null?void 0:vt.value)==null?void 0:Ct.type)==="object"?se(le):z};
|
|
75
|
+
`;e!=null&&e.folderSplit?O[i].endpoints+=te:ye+=te;});}),e!=null&&e.folderSplit){for(let[t,r]of Object.entries(O))if(r.endpoints||r.types){let f=M.join(y,t);if(r.endpoints){let l=M.join(Y,f,"endpoints.ts");yield U.promises.mkdir(M.dirname(l),{recursive:true}),yield Z(l,r.endpoints,e);}if(r.types){let l=M.join(Y,f,"types.ts");yield U.promises.mkdir(M.dirname(l),{recursive:true});let a=Object.values(H).length>0?`import * as Shared from "../shared";
|
|
76
|
+
|
|
77
|
+
${r.types}`:r.types;yield Z(l,a,e);}if(((Te=e==null?void 0:e.validations)==null?void 0:Te.disable)!==true&&r.validation){let l=((Se=e.validations)==null?void 0:Se.library)||"zod",a=l==="joi"?'import Joi from "joi";':l==="yup"?'import * as yup from "yup";':'import { z } from "zod";',o=M.join(Y,f,"validations.ts");yield U.promises.mkdir(M.dirname(o),{recursive:true}),yield Z(o,`${a}
|
|
78
|
+
|
|
79
|
+
${r.validation}`,e);}}}if(ye.length>0){let t=M.join(Y,y,"endpoints.ts");yield U.promises.mkdir(M.dirname(t),{recursive:true}),yield Z(t,ye,e);}if(Object.values(H).length>0){let t=M.join(Y,y,e!=null&&e.folderSplit?"":"types","shared.ts");yield U.promises.mkdir(M.dirname(t),{recursive:true}),yield Z(t,Object.values(H).join(`
|
|
80
|
+
`),e);}if(g.length>0){let t=M.join(Y,y,"types","index.ts");yield U.promises.mkdir(M.dirname(t),{recursive:true}),yield Z(t,`${Object.values(H).length>0?`import * as Shared from "./shared";
|
|
62
81
|
|
|
63
|
-
${
|
|
64
|
-
`),e);}if(Z.length>0){let s=F.join(se,p,"types","index.ts");yield z.promises.mkdir(F.dirname(s),{recursive:true}),yield ne(s,`${Object.values(V).length>0?`import * as Shared from "./shared";
|
|
82
|
+
`:""}${g}`,e);}if(((we=e==null?void 0:e.validations)==null?void 0:we.disable)!==true){let t=((ze=e.validations)==null?void 0:ze.library)||"zod",r=t==="joi"?'import Joi from "joi";':t==="yup"?'import * as yup from "yup";':'import { z } from "zod";',f=Object.values(O).map(l=>l.validation).filter(l=>l.length>0).join("");if(f){let l=M.join(Y,y,"validations.ts");yield U.promises.mkdir(M.dirname(l),{recursive:true}),yield Z(l,`${r}
|
|
65
83
|
|
|
66
|
-
|
|
84
|
+
${f}`,e);}}}),qt=Mt;var he=process.cwd(),Cr=u=>ne(null,null,function*(){let d;try{re("esbuild-register");}catch(E){throw E}let e=M.join(he,"openapi.sync.js"),j=M.join(he,"openapi.sync.ts"),b=M.join(he,"openapi.sync.json"),v=[e,j,b];try{for(let E of v)U.existsSync(E)&&(d=re(E),Object.keys(d).length===1&&d.default&&(d=d.default));}catch(E){console.log(E);}typeof d=="function"&&(d=d());let m=d;if(!m)throw new Error("No config found");let y=Object.keys(m.api),O=u&&"refetchInterval"in u&&!isNaN(u==null?void 0:u.refetchInterval)?u.refetchInterval:m.refetchInterval;Rt();for(let E=0;E<y.length;E+=1){let P=y[E],B=m.api[P];qt(B,P,m,O);}});export{Cr as Init,se as JSONStringify,K as capitalize,At as createCustomCodeMarker,Ut as extractCustomCode,St as getEndpointDetails,er as getNestedValue,ct as isJson,Gt as isYamlString,wt as mergeCustomCode,ue as renderTypeRefMD,Xt as variableName,Et as variableNameChar,Tt as yamlStringToJson};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-sync",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A developer
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "A powerful developer tool that automates API documentation synchronization with your codebase using OpenAPI specifications. Generates TypeScript types, endpoint definitions, runtime validation schemas (Zod, Yup, Joi), and comprehensive documentation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|
|
@@ -15,6 +15,12 @@
|
|
|
15
15
|
"typescript",
|
|
16
16
|
"ts",
|
|
17
17
|
"dts",
|
|
18
|
+
"zod",
|
|
19
|
+
"yup",
|
|
20
|
+
"joi",
|
|
21
|
+
"validation schema",
|
|
22
|
+
"validation schema generation",
|
|
23
|
+
"validation schema generator",
|
|
18
24
|
"node",
|
|
19
25
|
"codegen",
|
|
20
26
|
"generation",
|
|
@@ -28,31 +34,24 @@
|
|
|
28
34
|
"dist/**/*.d.ts",
|
|
29
35
|
"dist/**/*.d.mts",
|
|
30
36
|
"!dist/**/*.map",
|
|
31
|
-
"db.json",
|
|
32
37
|
"LICENSE",
|
|
33
38
|
"README.md",
|
|
34
|
-
"package.json"
|
|
39
|
+
"package.json",
|
|
40
|
+
"!website/**/*"
|
|
35
41
|
],
|
|
36
42
|
"repository": {
|
|
37
43
|
"type": "git",
|
|
38
44
|
"url": "https://github.com/akintomiwa-fisayo/openapi-sync"
|
|
39
45
|
},
|
|
40
|
-
"homepage": "https://
|
|
46
|
+
"homepage": "https://openapi-sync.com",
|
|
41
47
|
"bugs": {
|
|
42
48
|
"url": "https://github.com/akintomiwa-fisayo/openapi-sync/issues"
|
|
43
49
|
},
|
|
44
50
|
"scripts": {
|
|
45
|
-
"build-tsc": "rm -rf dist db.json && tsc && echo '{}' > db.json",
|
|
46
51
|
"build": "rm -rf dist db.json && tsup && echo '{}' > db.json",
|
|
47
52
|
"publish-package": "npm run build && npm publish",
|
|
48
53
|
"test": "jest",
|
|
49
|
-
"test:
|
|
50
|
-
"test:coverage": "jest --coverage",
|
|
51
|
-
"test:ci": "jest --ci --coverage --watchAll=false",
|
|
52
|
-
"analyze:size": "npm run build && du -sh dist/* | sort -h",
|
|
53
|
-
"analyze:detailed": "npm run build && du -h dist/* && echo '\nš Total dist size:' && du -sh dist",
|
|
54
|
-
"analyze:deps": "npm list --depth=0 --production",
|
|
55
|
-
"analyze:bundle": "npm run build && node scripts/analyze-bundle.js"
|
|
54
|
+
"test:coverage": "jest --coverage"
|
|
56
55
|
},
|
|
57
56
|
"author": "P-Technologies",
|
|
58
57
|
"license": "ISC",
|
|
@@ -79,5 +78,21 @@
|
|
|
79
78
|
"lodash.get": "^4.4.2",
|
|
80
79
|
"lodash.isequal": "^4.5.0",
|
|
81
80
|
"yargs": "^17.7.2"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"zod": "^3.22.0",
|
|
84
|
+
"yup": "^1.3.0",
|
|
85
|
+
"joi": "^17.11.0"
|
|
86
|
+
},
|
|
87
|
+
"peerDependenciesMeta": {
|
|
88
|
+
"zod": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"yup": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"joi": {
|
|
95
|
+
"optional": true
|
|
96
|
+
}
|
|
82
97
|
}
|
|
83
98
|
}
|
package/db.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|