@casual-simulation/aux-common
Version:
Common library for AUX projects
203 lines • 6.63 kB
JavaScript
import * as _z from 'zod/v4/core';
/**
* Constructs a new procedure.
*/
export function procedure() {
return new ProcBuilder();
}
class ProcBuilder {
origins(allowedOrigins) {
this._allowedOrigins = allowedOrigins;
return this;
}
http(method, path, scope) {
this._http = {
method: method,
path: path,
scope: scope,
};
return this;
}
view(scope, path) {
this._view = {
scope,
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,
view: this._view,
};
}
}
/**
* 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).sort((a, b) => a.localeCompare(b))) {
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) {
if (schema instanceof _z.$ZodPipe) {
return getSchemaMetadata(schema._zod.def.out);
}
else if (schema instanceof _z.$ZodString ||
schema instanceof _z.$ZodBigInt) {
return { type: 'string', description: getDescription(schema) };
}
else if (schema instanceof _z.$ZodBoolean) {
return { type: 'boolean', description: getDescription(schema) };
}
else if (schema instanceof _z.$ZodNumber) {
return { type: 'number', description: getDescription(schema) };
}
else if (schema instanceof _z.$ZodAny ||
schema instanceof _z.$ZodUnknown) {
return { type: 'any', description: getDescription(schema) };
}
else if (schema instanceof _z.$ZodNull) {
return { type: 'null', description: getDescription(schema) };
}
else if (schema instanceof _z.$ZodObject) {
const schemaMetadata = {};
for (let key in schema._zod.def.shape) {
schemaMetadata[key] = getSchemaMetadata(schema._zod.def.shape[key]);
}
return {
type: 'object',
schema: schemaMetadata,
catchall: schema._zod.def.catchall
? getSchemaMetadata(schema._zod.def.catchall)
: undefined,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodArray) {
return {
type: 'array',
schema: getSchemaMetadata(schema._zod.def.element),
maxLength: schema._zod.def.checks?.find((c) => c instanceof _z.$ZodCheckMaxLength)?._zod.def.maximum,
minLength: schema._zod.def.checks?.find((c) => c instanceof _z.$ZodCheckMinLength)?._zod.def.minimum,
exactLength: schema._zod.def.checks?.find((c) => c instanceof _z.$ZodCheckLengthEquals)?._zod.def.length,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodEnum) {
return {
type: 'enum',
values: [...schema._zod.values],
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodDate) {
return { type: 'date', description: getDescription(schema) };
}
else if (schema instanceof _z.$ZodLiteral) {
return {
type: 'literal',
value: schema._zod.def.values[0],
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodOptional) {
return {
...getSchemaMetadata(schema._zod.def.innerType),
optional: true,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodNonOptional) {
return {
...getSchemaMetadata(schema._zod.def.innerType),
optional: false,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodNullable) {
return {
...getSchemaMetadata(schema._zod.def.innerType),
nullable: true,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodPrefault ||
schema instanceof _z.$ZodDefault) {
return {
...getSchemaMetadata(schema._zod.def.innerType),
hasDefault: true,
defaultValue: schema._zod.def.defaultValue,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodNever) {
return undefined;
}
else if (schema instanceof _z.$ZodDiscriminatedUnion) {
return {
type: 'union',
options: schema._zod.def.options.map((o) => getSchemaMetadata(o)),
discriminator: schema._zod.def.discriminator,
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodUnion) {
return {
type: 'union',
options: schema._zod.def.options.map((o) => getSchemaMetadata(o)),
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodRecord) {
return {
type: 'record',
valueSchema: getSchemaMetadata(schema._zod.def.valueType),
description: getDescription(schema),
};
}
else if (schema instanceof _z.$ZodTuple) {
return {
type: 'tuple',
items: schema._zod.def.items.map((o) => getSchemaMetadata(o)),
description: getDescription(schema),
};
}
else {
console.error('Unsupported schema type', schema);
throw new Error(`Unsupported schema type: ${schema}`);
}
}
function getDescription(schema) {
return _z.globalRegistry.get(schema)?.description;
}
//# sourceMappingURL=GenericRPCInterface.js.map