@casual-simulation/aux-common
Version:
Common library for AUX projects
171 lines • 5.51 kB
JavaScript
import z from 'zod';
/**
* Constructs a new procedure.
*/
export function procedure() {
return new ProcBuilder();
}
class ProcBuilder {
origins(allowedOrigins) {
this._allowedOrigins = allowedOrigins;
return this;
}
http(method, path) {
this._http = {
method: method,
path: path,
};
return this;
}
inputs(schema, query) {
this._schema = schema;
this._querySchema = query;
return this;
}
handler(handler, mapToResponse) {
return {
schema: this._schema,
querySchema: this._querySchema,
handler: handler,
mapToResponse,
allowedOrigins: this._allowedOrigins,
http: this._http,
};
}
}
/**
* Gets the metadata for the given procedures.
* @param procedures The procedures to get metadata for.
*/
export function getProcedureMetadata(procedures) {
let metadatas = [];
for (let procedure of Object.keys(procedures)) {
const proc = procedures[procedure];
metadatas.push({
name: procedure,
inputs: proc.schema ? getSchemaMetadata(proc.schema) : undefined,
query: proc.querySchema
? getSchemaMetadata(proc.querySchema)
: undefined,
origins: proc.allowedOrigins,
http: proc.http,
});
}
return {
procedures: metadatas,
};
}
/**
* Gets a serializable version of the schema metdata.
* @param schema The schema to get metadata for.
*/
export function getSchemaMetadata(schema) {
var _a, _b, _c;
if (schema instanceof z.ZodString) {
return { type: 'string', description: schema._def.description };
}
else if (schema instanceof z.ZodBoolean) {
return { type: 'boolean', description: schema._def.description };
}
else if (schema instanceof z.ZodNumber) {
return { type: 'number', description: schema._def.description };
}
else if (schema instanceof z.ZodAny) {
return { type: 'any', description: schema._def.description };
}
else if (schema instanceof z.ZodNull) {
return { type: 'null', description: schema._def.description };
}
else if (schema instanceof z.ZodObject) {
const schemaMetadata = {};
for (let key in schema.shape) {
schemaMetadata[key] = getSchemaMetadata(schema.shape[key]);
}
return {
type: 'object',
schema: schemaMetadata,
catchall: schema._def.catchall
? getSchemaMetadata(schema._def.catchall)
: undefined,
description: schema._def.description,
};
}
else if (schema instanceof z.ZodArray) {
return {
type: 'array',
schema: getSchemaMetadata(schema._def.type),
maxLength: (_a = schema._def.maxLength) === null || _a === void 0 ? void 0 : _a.value,
minLength: (_b = schema._def.minLength) === null || _b === void 0 ? void 0 : _b.value,
exactLength: (_c = schema._def.exactLength) === null || _c === void 0 ? void 0 : _c.value,
description: schema._def.description,
};
}
else if (schema instanceof z.ZodEnum) {
return {
type: 'enum',
values: [...schema._def.values],
description: schema._def.description,
};
}
else if (schema instanceof z.ZodDate) {
return { type: 'date', description: schema._def.description };
}
else if (schema instanceof z.ZodLiteral) {
return {
type: 'literal',
value: schema.value,
description: schema._def.description,
};
}
else if (schema instanceof z.ZodOptional) {
return { ...getSchemaMetadata(schema._def.innerType), optional: true };
}
else if (schema instanceof z.ZodNullable) {
return { ...getSchemaMetadata(schema._def.innerType), nullable: true };
}
else if (schema instanceof z.ZodDefault) {
return {
...getSchemaMetadata(schema._def.innerType),
hasDefault: true,
defaultValue: schema._def.defaultValue(),
};
}
else if (schema instanceof z.ZodNever) {
return undefined;
}
else if (schema instanceof z.ZodEffects) {
return { ...getSchemaMetadata(schema._def.schema) };
}
else if (schema instanceof z.ZodUnion) {
return {
type: 'union',
options: schema._def.options.map((o) => getSchemaMetadata(o)),
description: schema._def.description,
};
}
else if (schema instanceof z.ZodDiscriminatedUnion) {
return {
type: 'union',
options: schema._def.options.map((o) => getSchemaMetadata(o)),
discriminator: schema._def.discriminator,
description: schema._def.description,
};
}
else if (schema instanceof z.ZodRecord) {
return {
type: 'record',
valueSchema: getSchemaMetadata(schema._def.valueType),
};
}
else if (schema instanceof z.ZodTuple) {
return {
type: 'tuple',
items: schema._def.items.map((o) => getSchemaMetadata(o)),
};
}
else {
console.error('Unsupported schema type', schema);
throw new Error(`Unsupported schema type: ${schema}`);
}
}
//# sourceMappingURL=GenericRPCInterface.js.map