@arpc-packages/schema-gen
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -1,3 +0,3 @@ | ||
"use strict";var C=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var H=Object.prototype.hasOwnProperty;var Q=(t,e)=>{for(var i in e)C(t,i,{get:e[i],enumerable:!0})},X=(t,e,i,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of G(e))!H.call(t,l)&&l!==i&&C(t,l,{get:()=>e[l],enumerable:!(s=Z(e,l))||s.enumerable});return t};var W=t=>X(C({},"__esModule",{value:!0}),t);var nt={};Q(nt,{generateSchema:()=>J});module.exports=W(nt);var z=require("fs/promises"),E=require("path"),D=require("@arpc-packages/lockfile"),r=require("typescript");var U=[{name:"BadRequest",description:"Thrown when a request is invalid."},{name:"Ratelimited",description:"Thrown when a user is ratelimited."},{name:"Unauthorized",description:"Thrown when a user is not authorized."},{name:"InternalServerError",description:"Thrown when the server encounters an internal error."}];var N=["string","number","bigint","boolean"];function R(t,e,i,s,l){if(N.indexOf(t.type)!==-1)return{type:t.type};if(t.type==="nullable"||t.type==="optional")return{type:"nullable",inner:R(t.wrapped,e,i,s,l)};if(t.type==="array")return{type:"array",inner:R(t.item,e,i,s,l)};if(t.type==="union"){let h=function(S){for(let u of S.options)u.type==="union"?h(u):y.push(u)};var M=h;let y=[];return h(t),{type:"union",inner:y.map((S,u)=>R(S,e,i,s,()=>`${l()}Variant${u}`))}}if(t.type==="map")return{type:"map",key:R(t.key,e,i,s,l),value:R(t.value,e,i,s,l)};if(t.type==="object"){let y={};for(let[d,b]of Object.entries(t.entries))y[d]=R(b,e,i,s,()=>`${l()}${d[0].toUpperCase()}${d.slice(1)}`);let h=l(),S=1,u=h;for(;s.has(u);){let d=i.find(b=>b.name===u);if(d&&JSON.stringify(d.fields)===JSON.stringify(y))return{type:"object",key:u};S++,u=`${h}V${S}`}return s.add(u),i.push({name:u,fields:y}),{type:"object",key:u}}if(t.type==="enum"){let y=t.enum,h=Object.keys(y),S=new Map;for(let O of h)S.set(O.replaceAll(" ","").replace(/[^a-zA-Z0-9_]/g,"_"),y[O]);let u={type:"string"};if(h.length!==0){let O=["string","number","bigint","boolean"],F=typeof y[h[0]];if(!O.includes(F))throw new Error("Enums can only have string, number, bigint, or boolean values");u={type:F}}let d=l(),b=1,x=d;for(;s.has(x);){let O=e.find(F=>F.name===x);if(O&&JSON.stringify(O.data)===JSON.stringify(S))return{type:"enum_value",enum:x};b++,x=`${d}V${b}`}return s.add(x),e.push({name:x,valueType:u,data:S}),{type:"enum_value",enum:x}}if(t.type==="literal")return{type:"literal",value:t.literal};throw new Error(`arpc doesn't support the type ${t.schema} yet`)}var q=/^(["'`])(.*)\1$/;function j(t){let e=q.exec(t);return e?e[2]:t}function I(t,e){for(let i of Object.values(t)){if(typeof i!="string"){I(i,e);continue}e.has(i)||e.add(i)}}var tt=/^TokenTypes\.([A-Za-z0-9_]+)$/;function et(t){if(t==="undefined")return;let e=tt.exec(t||"");if(!e)throw new Error(`Invalid token type: ${t}`);return e[1]}async function J(t){let e=(0,E.join)(process.cwd(),"rpc"),i=(0,D.parse)(await(0,z.readFile)((0,E.join)(e,"index.ts"),"utf-8")),s=[(0,E.join)(e,"index.ts")];i.hasAuthentication&&s.push((0,E.join)(e,"authentication.ts")),i.hasRatelimiting&&s.push((0,E.join)(e,"ratelimiting.ts"));let l=new Set;I(i.routes,l),I(i.exceptions,l);for(let a of l)s.push((0,E.join)(e,a));let M=(0,r.createProgram)(s,{allowJs:!0,alwaysStrict:!1});M.getTypeChecker();let y=null;if(i.hasAuthentication){let a=M.getSourceFile((0,E.join)(e,"authentication.ts"));if(!a)throw new Error("Failed to parse rpc/authentication.ts");let o=null,w=null;if(y=a.forEachChild(p=>{if((0,r.isEnumDeclaration)(p)&&p.name.text==="TokenTypes"){o={};for(let c of p.members){let g=j(c.name.getText());if(!c.initializer||!(0,r.isStringLiteral)(c.initializer))throw new Error(`TokenTypes.${g} must be a string literal`);o[g]=j(c.initializer.getText())}return w!==null?{tokenTypes:o,defaultTokenType:w}:void 0}if((0,r.isVariableStatement)(p)){let c=p.declarationList.declarations;if(c.length===1){let g=c[0];if(g.name.getText()==="defaultTokenType"&&(w=et(g.initializer?.getText()),o))return{tokenTypes:o,defaultTokenType:w}}}})||null,o)y||(y={tokenTypes:o});else throw new Error("Failed to find TokenTypes enum in rpc/authentication.ts")}let h=[],S=Object.keys(i.exceptions).sort();for(let a of S){let o=(0,E.join)(e,i.exceptions[a]+".ts"),w=M.getSourceFile(o);if(!w)throw new Error(`Failed to parse ${o}`);let p=w.forEachChild(f=>{if((0,r.isClassDeclaration)(f)&&f.name?.text===a)return f});if(!p)throw new Error(`Failed to find class ${a} in ${o}`);let c=null,g=p.getFullStart(),k=p.getStart();if(g!==k){let f=w.getFullText(),m=(0,r.getLeadingCommentRanges)(f,k);m&&(c=f.substring(m[0].pos,m[0].end).trim())}h.push({name:a,description:c||`A ${a} custom exception`})}let u=t._routes,d=[],b=[],x=new Set;function O(a,o,w){let p=(u||{})[w],c=null,g=null,k,f=0;for(;p;){let n=p[o[f]];if(typeof n.method=="function"){c=n.input,g=n.output,k=n.mutation;break}p=n,f++}if(!c||!g)throw new Error("Lockfile and router routes are out of sync");let m=new Map;a.forEachChild(n=>{(0,r.isTypeAliasDeclaration)(n)&&m.set(n.name.text,n.type.getText())});for(let[n,T]of m){for(;m.has(T);)T=m.get(T);m.set(n,T)}let v=a.forEachChild(n=>{if((0,r.isVariableStatement)(n)){let T=n.declarationList.declarations;if(T.length!==1)return;let B=T[0];if(B.name.getText()==="method"){if(B.initializer&&((0,r.isArrowFunction)(B.initializer)||(0,r.isFunctionExpression)(B.initializer)))return B.initializer;throw new Error("method must be an arrow function or function expression")}}if(((0,r.isMethodDeclaration)(n)||(0,r.isFunctionDeclaration)(n))&&n.name?.getText()==="method")return n});if(!v)throw new Error("Failed to find method");let $=v.parameters[0];if(!$)throw new Error("Method must have at least one argument");let P=null;if($.type){let n=$.type.getText();if(n!=="v.InferOutput<typeof input>"){if(m.get(n)!=="v.InferOutput<typeof input>")throw new Error(`Method argument must be of type or alias a type that is equal to v.InferOutput<typeof input> in the same file, got ${n}`);P=n}}let A={name:$.name.getText(),signature:R(c,d,b,x,()=>P||o.map(n=>n[0].toUpperCase()+n.slice(1)).join("")+"Opts")},Y=R(g,d,b,x,()=>o.map(n=>n[0].toUpperCase()+n.slice(1)).join("")+"Response"),_=null,K=v.getFullStart(),V=v.getStart();if(K!==V){let n=a.getFullText(),T=(0,r.getLeadingCommentRanges)(n,V);T&&(_=n.substring(T[0].pos,T[0].end).trim())}return k===void 0&&(k=!0),{input:A,output:Y,description:_,mutation:k}}let F=Object.keys(i.routes).sort(),L=[];for(let a of F){let c=function(g,k,f){for(let[m,v]of Object.entries(g)){if(typeof v!="string"){let A={};k[m]=A,f.push(m),c(v,A,f),f.pop();continue}let $=(0,E.join)(e,v+".ts"),P=M.getSourceFile($);if(!P)throw new Error(`Failed to parse ${$}`);f.push(m),k[m]=O(P,f,a),f.pop()}};var it=c;let o=null;try{o=await(0,z.readFile)((0,E.join)(e,"descriptions",`${a}.md`),"utf-8")}catch{}o===`You can write markdown here to describe your API version. | ||
"use strict";var C=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var H=Object.prototype.hasOwnProperty;var Q=(t,e)=>{for(var i in e)C(t,i,{get:e[i],enumerable:!0})},X=(t,e,i,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of G(e))!H.call(t,l)&&l!==i&&C(t,l,{get:()=>e[l],enumerable:!(s=Z(e,l))||s.enumerable});return t};var W=t=>X(C({},"__esModule",{value:!0}),t);var nt={};Q(nt,{generateSchema:()=>J});module.exports=W(nt);var z=require("fs/promises"),E=require("path"),D=require("@arpc-packages/lockfile"),r=require("typescript");var U=[{name:"BadRequest",description:"Thrown when a request is invalid."},{name:"Ratelimited",description:"Thrown when a user is ratelimited."},{name:"Unauthorized",description:"Thrown when a user is not authorized."},{name:"InternalServerError",description:"Thrown when the server encounters an internal error."}];var N=["string","number","bigint","boolean"];function R(t,e,i,s,l){if(N.indexOf(t.type)!==-1)return{type:t.type};if(t.type==="nullable"||t.type==="optional")return{type:"nullable",inner:R(t.wrapped,e,i,s,l)};if(t.type==="array")return{type:"array",inner:R(t.item,e,i,s,l)};if(t.type==="union"){let h=function(S){for(let u of S.options)u.type==="union"?h(u):y.push(u)};var M=h;let y=[];return h(t),{type:"union",inner:y.map((S,u)=>R(S,e,i,s,()=>`${l()}Variant${u}`))}}if(t.type==="map")return{type:"map",key:R(t.key,e,i,s,l),value:R(t.value,e,i,s,l)};if(t.type==="object"){let y={};for(let[d,b]of Object.entries(t.entries))y[d]=R(b,e,i,s,()=>`${l()}${d[0].toUpperCase()}${d.slice(1)}`);let h=l(),S=1,u=h;for(;s.has(u);){let d=i.find(b=>b.name===u);if(d&&JSON.stringify(d.fields)===JSON.stringify(y))return{type:"object",key:u};S++,u=`${h}V${S}`}return s.add(u),i.push({name:u,fields:y}),{type:"object",key:u}}if(t.type==="enum"){let y=t.enum,h=Object.keys(y),S=new Map;for(let O of h)S.set(O.replaceAll(" ","").replace(/[^a-zA-Z0-9_]/g,"_"),y[O]);let u={type:"string"};if(h.length!==0){let O=["string","number","bigint","boolean"],F=typeof y[h[0]];if(!O.includes(F))throw new Error("Enums can only have string, number, bigint, or boolean values");u={type:F}}let d=l(),b=1,x=d;for(;s.has(x);){let O=e.find(F=>F.name===x);if(O&&JSON.stringify(O.data)===JSON.stringify(S))return{type:"enum_value",enum:x};b++,x=`${d}V${b}`}return s.add(x),e.push({name:x,valueType:u,data:S}),{type:"enum_value",enum:x}}if(t.type==="literal")return{type:"literal",value:t.literal};throw new Error(`arpc doesn't support the type ${t.type} yet`)}var q=/^(["'`])(.*)\1$/;function j(t){let e=q.exec(t);return e?e[2]:t}function I(t,e){for(let i of Object.values(t)){if(typeof i!="string"){I(i,e);continue}e.has(i)||e.add(i)}}var tt=/^TokenTypes\.([A-Za-z0-9_]+)$/;function et(t){if(t==="undefined")return;let e=tt.exec(t||"");if(!e)throw new Error(`Invalid token type: ${t}`);return e[1]}async function J(t){let e=(0,E.join)(process.cwd(),"rpc"),i=(0,D.parse)(await(0,z.readFile)((0,E.join)(e,"index.ts"),"utf-8")),s=[(0,E.join)(e,"index.ts")];i.hasAuthentication&&s.push((0,E.join)(e,"authentication.ts")),i.hasRatelimiting&&s.push((0,E.join)(e,"ratelimiting.ts"));let l=new Set;I(i.routes,l),I(i.exceptions,l);for(let a of l)s.push((0,E.join)(e,a));let M=(0,r.createProgram)(s,{allowJs:!0,alwaysStrict:!1});M.getTypeChecker();let y=null;if(i.hasAuthentication){let a=M.getSourceFile((0,E.join)(e,"authentication.ts"));if(!a)throw new Error("Failed to parse rpc/authentication.ts");let o=null,w=null;if(y=a.forEachChild(p=>{if((0,r.isEnumDeclaration)(p)&&p.name.text==="TokenTypes"){o={};for(let c of p.members){let g=j(c.name.getText());if(!c.initializer||!(0,r.isStringLiteral)(c.initializer))throw new Error(`TokenTypes.${g} must be a string literal`);o[g]=j(c.initializer.getText())}return w!==null?{tokenTypes:o,defaultTokenType:w}:void 0}if((0,r.isVariableStatement)(p)){let c=p.declarationList.declarations;if(c.length===1){let g=c[0];if(g.name.getText()==="defaultTokenType"&&(w=et(g.initializer?.getText()),o))return{tokenTypes:o,defaultTokenType:w}}}})||null,o)y||(y={tokenTypes:o});else throw new Error("Failed to find TokenTypes enum in rpc/authentication.ts")}let h=[],S=Object.keys(i.exceptions).sort();for(let a of S){let o=(0,E.join)(e,i.exceptions[a]+".ts"),w=M.getSourceFile(o);if(!w)throw new Error(`Failed to parse ${o}`);let p=w.forEachChild(f=>{if((0,r.isClassDeclaration)(f)&&f.name?.text===a)return f});if(!p)throw new Error(`Failed to find class ${a} in ${o}`);let c=null,g=p.getFullStart(),k=p.getStart();if(g!==k){let f=w.getFullText(),m=(0,r.getLeadingCommentRanges)(f,k);m&&(c=f.substring(m[0].pos,m[0].end).trim())}h.push({name:a,description:c||`A ${a} custom exception`})}let u=t._routes,d=[],b=[],x=new Set;function O(a,o,w){let p=(u||{})[w],c=null,g=null,k,f=0;for(;p;){let n=p[o[f]];if(typeof n.method=="function"){c=n.input,g=n.output,k=n.mutation;break}p=n,f++}if(!c||!g)throw new Error("Lockfile and router routes are out of sync");let m=new Map;a.forEachChild(n=>{(0,r.isTypeAliasDeclaration)(n)&&m.set(n.name.text,n.type.getText())});for(let[n,T]of m){for(;m.has(T);)T=m.get(T);m.set(n,T)}let v=a.forEachChild(n=>{if((0,r.isVariableStatement)(n)){let T=n.declarationList.declarations;if(T.length!==1)return;let B=T[0];if(B.name.getText()==="method"){if(B.initializer&&((0,r.isArrowFunction)(B.initializer)||(0,r.isFunctionExpression)(B.initializer)))return B.initializer;throw new Error("method must be an arrow function or function expression")}}if(((0,r.isMethodDeclaration)(n)||(0,r.isFunctionDeclaration)(n))&&n.name?.getText()==="method")return n});if(!v)throw new Error("Failed to find method");let $=v.parameters[0];if(!$)throw new Error("Method must have at least one argument");let P=null;if($.type){let n=$.type.getText();if(n!=="v.InferOutput<typeof input>"){if(m.get(n)!=="v.InferOutput<typeof input>")throw new Error(`Method argument must be of type or alias a type that is equal to v.InferOutput<typeof input> in the same file, got ${n}`);P=n}}let A={name:$.name.getText(),signature:R(c,d,b,x,()=>P||o.map(n=>n[0].toUpperCase()+n.slice(1)).join("")+"Opts")},Y=R(g,d,b,x,()=>o.map(n=>n[0].toUpperCase()+n.slice(1)).join("")+"Response"),_=null,K=v.getFullStart(),V=v.getStart();if(K!==V){let n=a.getFullText(),T=(0,r.getLeadingCommentRanges)(n,V);T&&(_=n.substring(T[0].pos,T[0].end).trim())}return k===void 0&&(k=!0),{input:A,output:Y,description:_,mutation:k}}let F=Object.keys(i.routes).sort(),L=[];for(let a of F){let c=function(g,k,f){for(let[m,v]of Object.entries(g)){if(typeof v!="string"){let A={};k[m]=A,f.push(m),c(v,A,f),f.pop();continue}let $=(0,E.join)(e,v+".ts"),P=M.getSourceFile($);if(!P)throw new Error(`Failed to parse ${$}`);f.push(m),k[m]=O(P,f,a),f.pop()}};var it=c;let o=null;try{o=await(0,z.readFile)((0,E.join)(e,"descriptions",`${a}.md`),"utf-8")}catch{}o===`You can write markdown here to describe your API version. | ||
`&&(o=null);let w=i.routes[a],p={};L.push({apiVersion:a,methods:p,description:o,defaultProtocol:"",defaultHostname:"",authentication:y}),c(w,p,[])}return{enums:d,objects:b,builtinExceptions:U,customExceptions:h,clients:L}}0&&(module.exports={generateSchema}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@arpc-packages/schema-gen", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"module": "dist/index.mjs", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -164,3 +164,3 @@ import type { Enum, Object, Signature } from "@arpc-packages/client-gen"; | ||
// Throw an error if we don't know how to handle the type. | ||
throw new Error(`arpc doesn't support the type ${schema.schema} yet`); | ||
throw new Error(`arpc doesn't support the type ${schema.type} yet`); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
102735