@discordkit/core 1.1.0-next.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addParams.d.ts +1 -1
- package/dist/addParams.js.map +1 -1
- package/dist/buildURL.d.ts +2 -1
- package/dist/buildURL.js.map +1 -1
- package/dist/isNonNullable.d.ts +3 -1
- package/dist/methods.d.ts +6 -2
- package/dist/methods.js.map +1 -1
- package/dist/snowflake.d.ts +1 -1
- package/dist/snowflake.js +3 -5
- package/dist/snowflake.js.map +1 -1
- package/dist/toProcedure.d.ts +23 -28
- package/dist/toProcedure.js +4 -5
- package/dist/toProcedure.js.map +1 -1
- package/dist/toQuery.d.ts +3 -3
- package/dist/toValidated.d.ts +4 -3
- package/dist/toValidated.js +14 -14
- package/dist/toValidated.js.map +1 -1
- package/package.json +5 -4
- package/dist/wrap.d.ts +0 -18
- package/dist/wrap.js +0 -34
- package/dist/wrap.js.map +0 -1
package/dist/addParams.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export type RequestParams = Partial<Record<string, number[] | string[] | boolean | number | string | null | undefined>>;
|
|
2
|
-
export declare const addParams:
|
|
2
|
+
export declare const addParams: (url: URL, params: RequestParams) => URL;
|
package/dist/addParams.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addParams.js","sourceRoot":"","sources":["../src/addParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAS/C,MAAM,CAAC,MAAM,SAAS,GAAG,
|
|
1
|
+
{"version":3,"file":"addParams.js","sourceRoot":"","sources":["../src/addParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAS/C,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAE,MAAqB,EAAO,EAAE;IAChE,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC3D,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
|
package/dist/buildURL.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { type RequestParams } from "./addParams.js";
|
|
2
|
+
export declare const buildURL: (resource: string, params?: RequestParams, base?: string) => URL;
|
package/dist/buildURL.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildURL.js","sourceRoot":"","sources":["../src/buildURL.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAsB,MAAM,gBAAgB,CAAC;AAE/D,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,QAAgB,EAChB,
|
|
1
|
+
{"version":3,"file":"buildURL.js","sourceRoot":"","sources":["../src/buildURL.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAsB,MAAM,gBAAgB,CAAC;AAE/D,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,QAAgB,EAChB,MAAsB,EACtB,IAAa,EACR,EAAE,CACP,SAAS,CACP,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,IAAI,IAAI,QAAQ,CAAC,EACtD,MAAM,IAAI,EAAE,CACb,CAAC"}
|
package/dist/isNonNullable.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
type Maybe<T> = T | null | undefined;
|
|
1
2
|
/**
|
|
2
3
|
* Used to test whether a `Maybe` typed value is `null` or `undefined`.
|
|
3
4
|
*
|
|
@@ -16,4 +17,5 @@
|
|
|
16
17
|
* }
|
|
17
18
|
* ```
|
|
18
19
|
*/
|
|
19
|
-
export declare const isNonNullable: <T extends unknown
|
|
20
|
+
export declare const isNonNullable: <T extends Maybe<unknown>>(val?: T) => val is NonNullable<T>;
|
|
21
|
+
export {};
|
package/dist/methods.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { GenericSchema, GenericSchemaAsync, InferOutput } from "valibot";
|
|
2
2
|
import type { RequestParams } from "./addParams.js";
|
|
3
3
|
import type { RequestBody } from "./request.js";
|
|
4
|
-
export type Fetcher<
|
|
4
|
+
export type Fetcher<
|
|
5
|
+
/** A schema to validate the input arguments of a fetch call */
|
|
6
|
+
S extends GenericSchema | GenericSchemaAsync | null = null,
|
|
7
|
+
/** The return value expected from the fetch call */
|
|
8
|
+
R = void> = S extends null ? () => Promise<R> : (config: InferOutput<NonNullable<S>>) => Promise<R>;
|
|
5
9
|
export declare const get: <T>(url: string, params?: RequestParams) => Promise<T>;
|
|
6
10
|
export declare const post: <T>(url: string, body?: RequestBody) => Promise<T>;
|
|
7
11
|
export declare const put: <T>(url: string, body?: RequestBody) => Promise<T>;
|
package/dist/methods.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"methods.js","sourceRoot":"","sources":["../src/methods.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"methods.js","sourceRoot":"","sources":["../src/methods.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAWvC,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,EAAK,GAAW,EAAE,MAAsB,EAAc,EAAE,CAC9E,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC;AAEjC,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,EAAK,GAAW,EAAE,IAAkB,EAAc,EAAE,CAC3E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;AAEvC,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,EAAK,GAAW,EAAE,IAAkB,EAAc,EAAE,CAC1E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;AAEtC,MAAM,CAAC,MAAM,KAAK,GAAG,KAAK,EAAK,GAAW,EAAE,IAAkB,EAAc,EAAE,CAC5E,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AAExC,MAAM,CAAC,MAAM,MAAM,GAAG,KAAK,EAAY,GAAW,EAAc,EAAE,CAChE,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC"}
|
package/dist/snowflake.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const snowflake: import("valibot").
|
|
1
|
+
export declare const snowflake: import("valibot").CustomSchema<string, import("valibot").ErrorMessage<import("valibot").CustomIssue> | undefined>;
|
package/dist/snowflake.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { custom, pipe, transform, union, string, number, is } from "valibot";
|
|
2
2
|
import { isNonNullable } from "./isNonNullable.js";
|
|
3
|
-
export const snowflake =
|
|
3
|
+
export const snowflake = custom((val) => isNonNullable(val) &&
|
|
4
4
|
(typeof val === `bigint` ||
|
|
5
5
|
typeof val === `number` ||
|
|
6
6
|
typeof val === `string`)
|
|
7
|
-
? is(
|
|
8
|
-
// eslint-disable-next-line no-bitwise
|
|
9
|
-
Number((BigInt(val) >> 22n) + 1420070400000n))
|
|
7
|
+
? is(pipe(union([string(), number()]), transform((input) => new Date(input))), Number((BigInt(val) >> 22n) + 1420070400000n))
|
|
10
8
|
: false, `Invalid Snowflake ID`);
|
|
11
9
|
//# sourceMappingURL=snowflake.js.map
|
package/dist/snowflake.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snowflake.js","sourceRoot":"","sources":["../src/snowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"snowflake.js","sourceRoot":"","sources":["../src/snowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAC7B,CAAC,GAAG,EAAE,EAAE,CACN,aAAa,CAAC,GAAG,CAAC;IAClB,CAAC,OAAO,GAAG,KAAK,QAAQ;QACtB,OAAO,GAAG,KAAK,QAAQ;QACvB,OAAO,GAAG,KAAK,QAAQ,CAAC;IACxB,CAAC,CAAC,EAAE,CACA,IAAI,CACF,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,EAC3B,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CACtC,EACD,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,GAAG,cAAc,CAAC,CAC9C;IACH,CAAC,CAAC,KAAK,EACX,sBAAsB,CACvB,CAAC"}
|
package/dist/toProcedure.d.ts
CHANGED
|
@@ -1,36 +1,31 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import { type Wrap } from "./wrap.js";
|
|
1
|
+
import type { GenericSchema, GenericSchemaAsync, InferInput, InferOutput } from "valibot";
|
|
2
|
+
import type { AnyProcedureBuilder, MutationProcedure, ProcedureType, QueryProcedure, SubscriptionProcedure } from "@trpc/server/unstable-core-do-not-import";
|
|
4
3
|
import type { Fetcher } from "./methods.js";
|
|
5
|
-
type Result<T = void> = T extends
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
4
|
+
type Result<T = void> = T extends GenericSchema | GenericSchemaAsync ? InferOutput<T> : T;
|
|
5
|
+
type Schema = GenericSchema | GenericSchemaAsync;
|
|
6
|
+
type ProvedureDef<I extends Schema | null = null, O extends Schema | null = null> = I extends Schema ? O extends Schema ? {
|
|
7
|
+
input: InferInput<I>;
|
|
8
|
+
output: InferOutput<O>;
|
|
9
|
+
meta: unknown;
|
|
10
|
+
} : {
|
|
11
|
+
input: InferInput<I>;
|
|
12
|
+
output: undefined;
|
|
13
|
+
meta: unknown;
|
|
14
|
+
} : O extends Schema ? {
|
|
15
|
+
input: undefined;
|
|
16
|
+
output: InferOutput<O>;
|
|
17
|
+
meta: unknown;
|
|
18
|
+
} : {
|
|
19
|
+
input: undefined;
|
|
20
|
+
output: undefined;
|
|
21
|
+
meta: unknown;
|
|
22
|
+
};
|
|
23
|
+
type BaseProcedure<T extends ProcedureType, I extends Schema | null = null, O extends Schema | null = null> = T extends `query` ? QueryProcedure<ProvedureDef<I, O>> : T extends `mutation` ? MutationProcedure<ProvedureDef<I, O>> : SubscriptionProcedure<ProvedureDef<I, O>>;
|
|
16
24
|
/**
|
|
17
25
|
* Given a {@link Fetcher | Fetcher} function and it's associated input and
|
|
18
26
|
* output Zod schemas, this produces a function which accepts a tRPC procedure
|
|
19
27
|
* builder of the given procedure type. This can then be used in a tRPC router
|
|
20
28
|
* to scaffold an API route to forward a request to Discord's API.
|
|
21
29
|
*/
|
|
22
|
-
export declare const toProcedure: <T extends
|
|
23
|
-
_config: import("@trpc/server").RootConfig<{
|
|
24
|
-
ctx: object;
|
|
25
|
-
meta: object;
|
|
26
|
-
errorShape: import("@trpc/server").DefaultErrorShape;
|
|
27
|
-
transformer: import("@trpc/server").DefaultDataTransformer;
|
|
28
|
-
}>;
|
|
29
|
-
_ctx_out: object;
|
|
30
|
-
_input_in: typeof unsetMarker;
|
|
31
|
-
_input_out: typeof unsetMarker;
|
|
32
|
-
_output_in: typeof unsetMarker;
|
|
33
|
-
_output_out: typeof unsetMarker;
|
|
34
|
-
_meta: object;
|
|
35
|
-
}>) => BaseProcedure<T, I, O>;
|
|
30
|
+
export declare const toProcedure: <T extends ProcedureType, I extends GenericSchema | GenericSchemaAsync | null = null, O extends GenericSchema | GenericSchemaAsync | null = null>(type: T, fn: Fetcher<I extends GenericSchema | GenericSchemaAsync ? I : null, Result<O>>, input?: I, output?: O) => (procedure: AnyProcedureBuilder) => BaseProcedure<T, I, O>;
|
|
36
31
|
export {};
|
package/dist/toProcedure.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { wrap } from "./wrap.js";
|
|
2
1
|
import { isNonNullable } from "./isNonNullable.js";
|
|
3
2
|
/**
|
|
4
3
|
* Given a {@link Fetcher | Fetcher} function and it's associated input and
|
|
@@ -10,16 +9,16 @@ export const toProcedure = (type, fn, input, output) => (procedure) => {
|
|
|
10
9
|
if (isNonNullable(input) && isNonNullable(output)) {
|
|
11
10
|
// @ts-expect-error
|
|
12
11
|
return procedure
|
|
13
|
-
.input(
|
|
14
|
-
.output(
|
|
12
|
+
.input(input)
|
|
13
|
+
.output(output)[type](async (opts) => fn(opts.input));
|
|
15
14
|
}
|
|
16
15
|
if (isNonNullable(input) && !isNonNullable(output)) {
|
|
17
16
|
// @ts-expect-error
|
|
18
|
-
return procedure.input(
|
|
17
|
+
return procedure.input(input)[type](async (opts) => fn(opts.input));
|
|
19
18
|
}
|
|
20
19
|
if (!isNonNullable(input) && isNonNullable(output)) {
|
|
21
20
|
// @ts-expect-error
|
|
22
|
-
return procedure.output(
|
|
21
|
+
return procedure.output(output)[type](async () => fn());
|
|
23
22
|
}
|
|
24
23
|
// @ts-expect-error
|
|
25
24
|
return procedure[type](async () => fn());
|
package/dist/toProcedure.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toProcedure.js","sourceRoot":"","sources":["../src/toProcedure.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"toProcedure.js","sourceRoot":"","sources":["../src/toProcedure.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAsCnD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,CAKE,IAAO,EACP,EAGC,EACD,KAAS,EACT,MAAU,EACV,EAAE,CACJ,CAAC,SAA8B,EAA0B,EAAE;IACzD,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,mBAAmB;QACnB,OAAO,SAAS;aACb,KAAK,CAAC,KAAK,CAAC;aACZ,MAAM,CAAC,MAAM,CAAC,CACd,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QACnD,mBAAmB;QACnB,OAAO,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QACnD,mBAAmB;QACnB,OAAO,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAC1D,CAAC;IACD,mBAAmB;IACnB,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;AAC3C,CAAC,CAAC"}
|
package/dist/toQuery.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { GenericSchema } from "valibot";
|
|
2
2
|
import type { Fetcher } from "./methods.js";
|
|
3
3
|
interface Register {
|
|
4
4
|
}
|
|
@@ -6,7 +6,7 @@ type QueryKey = readonly unknown[];
|
|
|
6
6
|
type QueryMeta = Register extends {
|
|
7
7
|
queryMeta: infer TQueryMeta;
|
|
8
8
|
} ? TQueryMeta extends Record<string, unknown> ? TQueryMeta : Record<string, unknown> : Record<string, unknown>;
|
|
9
|
-
type FetchDirection =
|
|
9
|
+
type FetchDirection = `backward` | `forward`;
|
|
10
10
|
type QueryFunctionContext<TQueryKey extends QueryKey = QueryKey, TPageParam = never> = [TPageParam] extends [never] ? {
|
|
11
11
|
queryKey: TQueryKey;
|
|
12
12
|
signal: AbortSignal;
|
|
@@ -24,5 +24,5 @@ export type QueryFunction<T = unknown, TQueryKey extends QueryKey = QueryKey, TP
|
|
|
24
24
|
* which can then be used with React-Query as a query function without
|
|
25
25
|
* the need for any additional boilerplate.
|
|
26
26
|
*/
|
|
27
|
-
export declare const toQuery: <S extends
|
|
27
|
+
export declare const toQuery: <S extends GenericSchema | null, R, T extends Fetcher<S, R>>(fn: T) => Parameters<T>[`length`] extends 0 ? () => QueryFunction<Awaited<ReturnType<T>>> : (config: Parameters<T>[0]) => QueryFunction<Awaited<ReturnType<T>>>;
|
|
28
28
|
export {};
|
package/dist/toValidated.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type GenericSchema, type GenericSchemaAsync, type InferOutput } from "valibot";
|
|
2
2
|
import type { Fetcher } from "./methods.js";
|
|
3
|
-
type
|
|
3
|
+
type InferFetcherSchema<F> = F extends Fetcher<infer S, unknown> ? S : never;
|
|
4
|
+
type InferFetcherReturn<F> = F extends Fetcher<GenericSchema | GenericSchemaAsync | null, infer R> ? R : never;
|
|
4
5
|
/**
|
|
5
6
|
* Given a {@link Fetcher | Fetcher} function and it's associated input
|
|
6
7
|
* and output Zod schemas, this returns a new validated {@link Fetcher | Fetcher} function which will validate it's input and result at runtime.
|
|
@@ -8,5 +9,5 @@ type ToValidated = <F extends Fetcher<BaseSchema, Output<BaseSchema>> | Fetcher<
|
|
|
8
9
|
* type-safety when dealing with raw user input in a framework agnostic
|
|
9
10
|
* environment.
|
|
10
11
|
*/
|
|
11
|
-
export declare const toValidated:
|
|
12
|
+
export declare const toValidated: <F extends Fetcher<GenericSchema | GenericSchemaAsync, InferOutput<GenericSchema | GenericSchemaAsync>> | Fetcher<GenericSchema | GenericSchemaAsync> | Fetcher<null, null> | Fetcher<null, InferOutput<GenericSchema | GenericSchemaAsync>>>(fn: F, input?: InferFetcherSchema<F> | null, output?: GenericSchema<unknown, InferFetcherReturn<F>> | GenericSchemaAsync<unknown, InferFetcherReturn<F>>) => F;
|
|
12
13
|
export {};
|
package/dist/toValidated.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isObject } from "./isObject.js";
|
|
3
|
-
const isSchema = (val) => isNonNullable(val) && isObject(val) && `_parse` in val;
|
|
1
|
+
import { parseAsync, isOfKind } from "valibot";
|
|
4
2
|
/**
|
|
5
3
|
* Given a {@link Fetcher | Fetcher} function and it's associated input
|
|
6
4
|
* and output Zod schemas, this returns a new validated {@link Fetcher | Fetcher} function which will validate it's input and result at runtime.
|
|
@@ -8,16 +6,18 @@ const isSchema = (val) => isNonNullable(val) && isObject(val) && `_parse` in val
|
|
|
8
6
|
* type-safety when dealing with raw user input in a framework agnostic
|
|
9
7
|
* environment.
|
|
10
8
|
*/
|
|
11
|
-
export const toValidated = (fn, input, output) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
export const toValidated = (fn, input, output) => new Proxy(fn, {
|
|
10
|
+
async apply(target, _, [config]) {
|
|
11
|
+
// Validate the fetcher args before fetching
|
|
12
|
+
if (input && isOfKind(`schema`, input)) {
|
|
13
|
+
await parseAsync(input, config);
|
|
14
|
+
}
|
|
15
|
+
const result = await target(config);
|
|
16
|
+
// Validate the result of the fetch call before returning
|
|
17
|
+
if (output && isOfKind(`schema`, output)) {
|
|
18
|
+
await parseAsync(output, result);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
16
21
|
}
|
|
17
|
-
|
|
18
|
-
if (isSchema(output)) {
|
|
19
|
-
output._parse(result);
|
|
20
|
-
}
|
|
21
|
-
return result;
|
|
22
|
-
};
|
|
22
|
+
});
|
|
23
23
|
//# sourceMappingURL=toValidated.js.map
|
package/dist/toValidated.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toValidated.js","sourceRoot":"","sources":["../src/toValidated.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"toValidated.js","sourceRoot":"","sources":["../src/toValidated.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,UAAU,EACV,QAAQ,EACT,MAAM,SAAS,CAAC;AASjB;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAUzB,EAAK,EACL,KAAoC,EACpC,MAEsD,EACnD,EAAE,CACL,IAAI,KAAK,CAAI,EAAE,EAAE;IACf,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC;QAC7B,4CAA4C;QAC5C,IAAI,KAAK,IAAI,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC;YACvC,MAAM,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAClC,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,CAAC;QAEpC,yDAAyD;QACzD,IAAI,MAAM,IAAI,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAC;YACzC,MAAM,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,MAAuB,CAAC;IACjC,CAAC;CACF,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discordkit/core",
|
|
3
3
|
"description": "Core utility functions for Discordkit",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Drake Costa <drake@saeris.gg> (https://saeris.gg)",
|
|
7
7
|
"keywords": [
|
|
@@ -43,12 +43,13 @@
|
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"scripts": {
|
|
45
45
|
"dev": "yarn build --watch --declarationmap",
|
|
46
|
-
"build": "tsc
|
|
46
|
+
"build": "tsc",
|
|
47
|
+
"lint": "eslint --cache"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
49
|
-
"valibot": ">= 0.
|
|
50
|
+
"valibot": ">= 1.0.0"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"typescript": "^5.3
|
|
53
|
+
"typescript": "^5.8.3"
|
|
53
54
|
}
|
|
54
55
|
}
|
package/dist/wrap.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { type BaseSchema, type BaseSchemaAsync, type Input, type Output } from "valibot";
|
|
2
|
-
export interface ValidationIssue {
|
|
3
|
-
message: string;
|
|
4
|
-
path?: unknown[];
|
|
5
|
-
}
|
|
6
|
-
export interface TypeSchema<TOutput, TInput = TOutput> {
|
|
7
|
-
_input: TInput;
|
|
8
|
-
_output: TOutput;
|
|
9
|
-
assert(data: unknown): Promise<TOutput>;
|
|
10
|
-
parse(data: unknown): Promise<TOutput>;
|
|
11
|
-
validate(data: unknown): Promise<{
|
|
12
|
-
data: TOutput;
|
|
13
|
-
} | {
|
|
14
|
-
issues: ValidationIssue[];
|
|
15
|
-
}>;
|
|
16
|
-
}
|
|
17
|
-
export type Wrap<T extends BaseSchema | BaseSchemaAsync> = TypeSchema<Output<T>, Input<T>>;
|
|
18
|
-
export declare function wrap<TSchema extends BaseSchema | BaseSchemaAsync>(schema: TSchema): TypeSchema<Output<TSchema>, Input<TSchema>>;
|
package/dist/wrap.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { safeParseAsync } from "valibot";
|
|
2
|
-
// Adapted from https://github.com/decs/typeschema
|
|
3
|
-
export function wrap(schema) {
|
|
4
|
-
const validate = async (data) => {
|
|
5
|
-
const result = await safeParseAsync(schema, data);
|
|
6
|
-
if (result.success) {
|
|
7
|
-
return {
|
|
8
|
-
data: result.output
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
return {
|
|
12
|
-
issues: result.issues.map(({ message, path }) => ({
|
|
13
|
-
message,
|
|
14
|
-
path: path?.map(({ key }) => key) ?? []
|
|
15
|
-
}))
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
const assert = async (data) => {
|
|
19
|
-
const result = await validate(data);
|
|
20
|
-
if (`data` in result)
|
|
21
|
-
return result.data;
|
|
22
|
-
throw new AggregateError(result.issues, `Assertion failed`);
|
|
23
|
-
};
|
|
24
|
-
return {
|
|
25
|
-
// eslint-disable-next-line no-undefined
|
|
26
|
-
_input: undefined,
|
|
27
|
-
// eslint-disable-next-line no-undefined
|
|
28
|
-
_output: undefined,
|
|
29
|
-
assert,
|
|
30
|
-
parse: assert,
|
|
31
|
-
validate
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
//# sourceMappingURL=wrap.js.map
|
package/dist/wrap.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wrap.js","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAKf,MAAM,SAAS,CAAC;AAsBjB,kDAAkD;AAClD,MAAM,UAAU,IAAI,CAClB,MAAe;IAEf,MAAM,QAAQ,GAAG,KAAK,EACpB,IAAa,EACuD,EAAE;QACtE,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAClD,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,OAAO;gBACL,IAAI,EAAE,MAAM,CAAC,MAAM;aACpB,CAAC;QACJ,CAAC;QACD,OAAO;YACL,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;gBAChD,OAAO;gBACP,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE;aACxC,CAAC,CAAC;SACJ,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,MAAM,GAAG,KAAK,EAAE,IAAa,EAA4B,EAAE;QAC/D,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,MAAM,IAAI,MAAM;YAAE,OAAO,MAAM,CAAC,IAAI,CAAC;QACzC,MAAM,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC9D,CAAC,CAAC;IAEF,OAAO;QACL,wCAAwC;QACxC,MAAM,EAAE,SAAiC;QACzC,wCAAwC;QACxC,OAAO,EAAE,SAAkC;QAC3C,MAAM;QACN,KAAK,EAAE,MAAM;QACb,QAAQ;KACT,CAAC;AACJ,CAAC"}
|