mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-06-17 11:25:03 +00:00
chore: format
This commit is contained in:
parent
6c55b98eee
commit
aaf4e02c5f
56 changed files with 282 additions and 255 deletions
|
@ -1,11 +1,4 @@
|
|||
import {
|
||||
Client,
|
||||
Message,
|
||||
MessageReaction,
|
||||
PartialMessageReaction,
|
||||
PartialUser,
|
||||
User
|
||||
} from "discord.js";
|
||||
import { Client, Message, MessageReaction, PartialMessageReaction, PartialUser, User } from "discord.js";
|
||||
import { ContextResponseOptions, fetchContextChannel, GenericCommandSource } from "../pluginUtils.js";
|
||||
import { MINUTES, noop } from "../utils.js";
|
||||
import { Awaitable } from "./typeUtils.js";
|
||||
|
|
|
@ -4,7 +4,7 @@ import {
|
|||
ButtonStyle,
|
||||
MessageActionRowComponentBuilder,
|
||||
MessageComponentInteraction,
|
||||
MessageCreateOptions
|
||||
MessageCreateOptions,
|
||||
} from "discord.js";
|
||||
import moment from "moment";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
|
|
@ -23,143 +23,143 @@ import { $ZodRecordKey, $ZodType } from "zod/v4/core";
|
|||
const RESOLVING = Symbol("mapOnSchema/resolving");
|
||||
|
||||
export function mapOnSchema<T extends $ZodType, TResult extends $ZodType>(
|
||||
schema: T,
|
||||
fn: (schema: $ZodType) => TResult,
|
||||
schema: T,
|
||||
fn: (schema: $ZodType) => TResult,
|
||||
): TResult;
|
||||
|
||||
/**
|
||||
/**
|
||||
* Applies {@link fn} to each element of the schema recursively, replacing every schema with its return value.
|
||||
* The rewriting is applied bottom-up (ie. {@link fn} will get called on "children" first).
|
||||
*/
|
||||
export function mapOnSchema(schema: $ZodType, fn: (schema: $ZodType) => $ZodType): $ZodType {
|
||||
// Cache results to support recursive schemas
|
||||
const results = new Map<$ZodType, $ZodType | typeof RESOLVING>();
|
||||
// Cache results to support recursive schemas
|
||||
const results = new Map<$ZodType, $ZodType | typeof RESOLVING>();
|
||||
|
||||
function mapElement(s: $ZodType) {
|
||||
const value = results.get(s);
|
||||
if (value === RESOLVING) {
|
||||
throw new Error("Recursive schema access detected");
|
||||
} else if (value !== undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
results.set(s, RESOLVING);
|
||||
const result = mapOnSchema(s, fn);
|
||||
results.set(s, result);
|
||||
return result;
|
||||
function mapElement(s: $ZodType) {
|
||||
const value = results.get(s);
|
||||
if (value === RESOLVING) {
|
||||
throw new Error("Recursive schema access detected");
|
||||
} else if (value !== undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function mapInner() {
|
||||
if (schema instanceof z.ZodObject) {
|
||||
const newShape: Record<string, $ZodType> = {};
|
||||
for (const [key, value] of Object.entries(schema.shape)) {
|
||||
newShape[key] = mapElement(value);
|
||||
}
|
||||
results.set(s, RESOLVING);
|
||||
const result = mapOnSchema(s, fn);
|
||||
results.set(s, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
return new z.ZodObject({
|
||||
...schema.def,
|
||||
shape: newShape,
|
||||
});
|
||||
} else if (schema instanceof z.ZodArray) {
|
||||
return new z.ZodArray({
|
||||
...schema.def,
|
||||
type: "array",
|
||||
element: mapElement(schema.def.element),
|
||||
});
|
||||
} else if (schema instanceof z.ZodMap) {
|
||||
return new z.ZodMap({
|
||||
...schema.def,
|
||||
keyType: mapElement(schema.def.keyType),
|
||||
valueType: mapElement(schema.def.valueType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodSet) {
|
||||
return new z.ZodSet({
|
||||
...schema.def,
|
||||
valueType: mapElement(schema.def.valueType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodOptional) {
|
||||
return new z.ZodOptional({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodNullable) {
|
||||
return new z.ZodNullable({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodDefault) {
|
||||
return new z.ZodDefault({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodReadonly) {
|
||||
return new z.ZodReadonly({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodLazy) {
|
||||
return new z.ZodLazy({
|
||||
...schema.def,
|
||||
// NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas
|
||||
getter: () => mapElement(schema._def.getter()),
|
||||
});
|
||||
} else if (schema instanceof z.ZodPromise) {
|
||||
return new z.ZodPromise({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodCatch) {
|
||||
return new z.ZodCatch({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema._def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodTuple) {
|
||||
return new z.ZodTuple({
|
||||
...schema.def,
|
||||
items: schema.def.items.map((item: $ZodType) => mapElement(item)),
|
||||
rest: schema.def.rest && mapElement(schema.def.rest),
|
||||
});
|
||||
} else if (schema instanceof z.ZodDiscriminatedUnion) {
|
||||
return new z.ZodDiscriminatedUnion({
|
||||
...schema.def,
|
||||
options: schema.options.map((option) => mapOnSchema(option, fn)),
|
||||
});
|
||||
} else if (schema instanceof z.ZodUnion) {
|
||||
return new z.ZodUnion({
|
||||
...schema.def,
|
||||
options: schema.options.map((option) => mapOnSchema(option, fn)),
|
||||
});
|
||||
} else if (schema instanceof z.ZodIntersection) {
|
||||
return new z.ZodIntersection({
|
||||
...schema.def,
|
||||
right: mapElement(schema.def.right),
|
||||
left: mapElement(schema.def.left),
|
||||
});
|
||||
} else if (schema instanceof z.ZodRecord) {
|
||||
return new z.ZodRecord({
|
||||
...schema.def,
|
||||
keyType: mapElement(schema.def.keyType) as $ZodRecordKey,
|
||||
valueType: mapElement(schema.def.valueType),
|
||||
});
|
||||
} else {
|
||||
return schema;
|
||||
}
|
||||
function mapInner() {
|
||||
if (schema instanceof z.ZodObject) {
|
||||
const newShape: Record<string, $ZodType> = {};
|
||||
for (const [key, value] of Object.entries(schema.shape)) {
|
||||
newShape[key] = mapElement(value);
|
||||
}
|
||||
|
||||
return new z.ZodObject({
|
||||
...schema.def,
|
||||
shape: newShape,
|
||||
});
|
||||
} else if (schema instanceof z.ZodArray) {
|
||||
return new z.ZodArray({
|
||||
...schema.def,
|
||||
type: "array",
|
||||
element: mapElement(schema.def.element),
|
||||
});
|
||||
} else if (schema instanceof z.ZodMap) {
|
||||
return new z.ZodMap({
|
||||
...schema.def,
|
||||
keyType: mapElement(schema.def.keyType),
|
||||
valueType: mapElement(schema.def.valueType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodSet) {
|
||||
return new z.ZodSet({
|
||||
...schema.def,
|
||||
valueType: mapElement(schema.def.valueType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodOptional) {
|
||||
return new z.ZodOptional({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodNullable) {
|
||||
return new z.ZodNullable({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodDefault) {
|
||||
return new z.ZodDefault({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodReadonly) {
|
||||
return new z.ZodReadonly({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodLazy) {
|
||||
return new z.ZodLazy({
|
||||
...schema.def,
|
||||
// NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas
|
||||
getter: () => mapElement(schema._def.getter()),
|
||||
});
|
||||
} else if (schema instanceof z.ZodPromise) {
|
||||
return new z.ZodPromise({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema.def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodCatch) {
|
||||
return new z.ZodCatch({
|
||||
...schema.def,
|
||||
innerType: mapElement(schema._def.innerType),
|
||||
});
|
||||
} else if (schema instanceof z.ZodTuple) {
|
||||
return new z.ZodTuple({
|
||||
...schema.def,
|
||||
items: schema.def.items.map((item: $ZodType) => mapElement(item)),
|
||||
rest: schema.def.rest && mapElement(schema.def.rest),
|
||||
});
|
||||
} else if (schema instanceof z.ZodDiscriminatedUnion) {
|
||||
return new z.ZodDiscriminatedUnion({
|
||||
...schema.def,
|
||||
options: schema.options.map((option) => mapOnSchema(option, fn)),
|
||||
});
|
||||
} else if (schema instanceof z.ZodUnion) {
|
||||
return new z.ZodUnion({
|
||||
...schema.def,
|
||||
options: schema.options.map((option) => mapOnSchema(option, fn)),
|
||||
});
|
||||
} else if (schema instanceof z.ZodIntersection) {
|
||||
return new z.ZodIntersection({
|
||||
...schema.def,
|
||||
right: mapElement(schema.def.right),
|
||||
left: mapElement(schema.def.left),
|
||||
});
|
||||
} else if (schema instanceof z.ZodRecord) {
|
||||
return new z.ZodRecord({
|
||||
...schema.def,
|
||||
keyType: mapElement(schema.def.keyType) as $ZodRecordKey,
|
||||
valueType: mapElement(schema.def.valueType),
|
||||
});
|
||||
} else {
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
return fn(mapInner());
|
||||
return fn(mapInner());
|
||||
}
|
||||
|
||||
export function deepPartial<T extends z.ZodType>(schema: T): T {
|
||||
return mapOnSchema(schema, (s) => (s instanceof z.ZodObject ? s.partial() : s)) as T;
|
||||
return mapOnSchema(schema, (s) => (s instanceof z.ZodObject ? s.partial() : s)) as T;
|
||||
}
|
||||
|
||||
/** Make all object schemas "strict" (ie. fail on unknown keys), except if they are marked as `.passthrough()` */
|
||||
export function deepStrict<T extends z.ZodType>(schema: T): T {
|
||||
return mapOnSchema(schema, (s) =>
|
||||
s instanceof z.ZodObject /* && s.def.unknownKeys !== "passthrough" */ ? s.strict() : s,
|
||||
) as T;
|
||||
return mapOnSchema(schema, (s) =>
|
||||
s instanceof z.ZodObject /* && s.def.unknownKeys !== "passthrough" */ ? s.strict() : s,
|
||||
) as T;
|
||||
}
|
||||
|
||||
export function deepStrictAll<T extends z.ZodType>(schema: T): T {
|
||||
return mapOnSchema(schema, (s) => (s instanceof z.ZodObject ? s.strict() : s)) as T;
|
||||
return mapOnSchema(schema, (s) => (s instanceof z.ZodObject ? s.strict() : s)) as T;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue