Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@arpc-packages/schema-gen

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arpc-packages/schema-gen - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

4

dist/index.js

@@ -1,3 +0,3 @@

"use strict";var A=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var H=Object.prototype.hasOwnProperty;var Q=(t,e)=>{for(var i in e)A(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&&A(t,l,{get:()=>e[l],enumerable:!(s=Z(e,l))||s.enumerable});return t};var W=t=>X(A({},"__esModule",{value:!0}),t);var nt={};Q(nt,{generateSchema:()=>J});module.exports=W(nt);var j=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 O(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:O(t.wrapped,e,i,s,l)};if(t.type==="array")return{type:"array",inner:O(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)=>O(S,e,i,s,()=>`${l()}Variant${u}`))}}if(t.type==="map")return{type:"map",key:O(t.key,e,i,s,l),value:O(t.value,e,i,s,l)};if(t.type==="object"){let y={};for(let[d,b]of Object.entries(t.entries))y[d]=O(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 R of h)S.set(R.replaceAll(" ","").replace(/[^a-zA-Z0-9_]/g,"_"),y[R]);let u={type:"string"};if(h.length!==0){let R=["string","number","bigint","boolean"],F=typeof y[h[0]];if(!R.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 R=e.find(F=>F.name===x);if(R&&JSON.stringify(R.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.constructor.name} yet`)}var q=/^(["'`])(.*)\1$/;function C(t){let e=q.exec(t);return e?e[2]:t}function L(t,e){for(let i of Object.values(t)){if(typeof i!="string"){L(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,j.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;L(i.routes,l),L(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=C(c.name.getText());if(!c.initializer||!(0,r.isStringLiteral)(c.initializer))throw new Error(`TokenTypes.${g} must be a string literal`);o[g]=C(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 R(a,o,w){let p=(u||{})[w],c=null,g=null,k,f=0;for(;p;){let n=p[o[f]];if(typeof n.input?.["~standard"]=="number"){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 $=a.forEachChild(n=>{if((0,r.isVariableStatement)(n)){let T=n.declarationList.declarations;if(T.length!==1)return;let z=T[0];if(z.name.getText()==="method"){if(z.initializer&&((0,r.isArrowFunction)(z.initializer)||(0,r.isFunctionExpression)(z.initializer)))return z.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(!$)throw new Error("Failed to find method");let v=$.parameters[0];if(!v)throw new Error("Method must have at least one argument");let P=null;if(v.type){let n=v.type.getText();if(n!=="z.infer<typeof input>"){if(m.get(n)!=="z.infer<typeof input>")throw new Error(`Method argument must be of type or alias a type that is equal to z.infer<typeof input> in the same file, got ${n}`);P=n}}let B={name:v.name.getText(),signature:O(c,d,b,x,()=>P||o.map(n=>n[0].toUpperCase()+n.slice(1)).join("")+"Opts")},Y=O(g,d,b,x,()=>o.map(n=>n[0].toUpperCase()+n.slice(1)).join("")+"Response"),V=null,K=$.getFullStart(),I=$.getStart();if(K!==I){let n=a.getFullText(),T=(0,r.getLeadingCommentRanges)(n,I);T&&(V=n.substring(T[0].pos,T[0].end).trim())}return k===void 0&&(k=!0),{input:B,output:Y,description:V,mutation:k}}let F=Object.keys(i.routes).sort(),_=[];for(let a of F){let c=function(g,k,f){for(let[m,$]of Object.entries(g)){if(typeof $!="string"){let B={};k[m]=B,f.push(m),c($,B,f),f.pop();continue}let v=(0,E.join)(e,$+".ts"),P=M.getSourceFile(v);if(!P)throw new Error(`Failed to parse ${v}`);f.push(m),k[m]=R(P,f,a),f.pop()}};var it=c;let o=null;try{o=await(0,j.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={};_.push({apiVersion:a,methods:p,description:o,defaultProtocol:"",defaultHostname:"",authentication:y}),c(w,p,[])}return{enums:d,objects:b,builtinExceptions:U,customExceptions:h,clients:_}}0&&(module.exports={generateSchema});
"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.constructor.name} 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.0",
"version": "0.3.1",
"module": "dist/index.mjs",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -185,2 +185,3 @@ import { readFile } from "fs/promises";

output: BaseSchema<any, any, any>;
method: () => void;
mutation?: boolean;

@@ -211,5 +212,4 @@ } | RoutesRoutesPartial;

// If this contains a compliant schema, we are at the end of the path.
// @ts-expect-error: This is technically not valid TS, but the worst result will be undefined.
if (typeof nextItem.input?.["~standard"] === "number") {
// If this contains a method, we are at the end of the path.
if (typeof nextItem.method === "function") {
inputSchema = nextItem.input as BaseSchema<any, any, any>;

@@ -296,6 +296,6 @@ outputSchema = nextItem.output as BaseSchema<any, any, any>;

const typeText = arg.type.getText();
if (typeText !== "z.infer<typeof input>") {
if (typeText !== "v.InferOutput<typeof input>") {
const t = typeAliases.get(typeText);
if (t !== "z.infer<typeof input>") {
throw new Error(`Method argument must be of type or alias a type that is equal to z.infer<typeof input> in the same file, got ${typeText}`);
if (t !== "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 ${typeText}`);
}

@@ -302,0 +302,0 @@ inputTypeName = typeText;

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc