@apisr/response 0.0.1 → 0.0.3
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/error/default.d.mts +8 -0
- package/dist/error/default.mjs +107 -0
- package/dist/error/index.d.mts +35 -0
- package/dist/error/index.mjs +3 -0
- package/dist/handler.d.mts +127 -0
- package/dist/handler.mjs +272 -0
- package/dist/headers.d.mts +8 -0
- package/dist/headers.mjs +9 -0
- package/dist/index.d.mts +20 -0
- package/dist/index.mjs +18 -0
- package/dist/options/base.d.mts +28 -0
- package/dist/options/binary.d.mts +13 -0
- package/dist/options/binary.mjs +7 -0
- package/dist/options/error.d.mts +31 -0
- package/dist/options/error.mjs +7 -0
- package/dist/options/index.d.mts +15 -0
- package/dist/options/index.mjs +15 -0
- package/dist/options/json.d.mts +19 -0
- package/dist/options/json.mjs +7 -0
- package/dist/options/meta.d.mts +20 -0
- package/dist/options/meta.mjs +7 -0
- package/dist/response/base.d.mts +19 -0
- package/dist/response/base.mjs +16 -0
- package/dist/response/binary/index.d.mts +10 -0
- package/dist/response/binary/index.mjs +9 -0
- package/dist/response/default.d.mts +11 -0
- package/dist/response/error/index.d.mts +28 -0
- package/dist/response/error/index.mjs +23 -0
- package/dist/response/index.mjs +7 -0
- package/dist/response/json/index.d.mts +22 -0
- package/dist/response/json/index.mjs +29 -0
- package/dist/response/meta/index.d.mts +7 -0
- package/dist/response/text/index.d.mts +9 -0
- package/dist/response/text/index.mjs +9 -0
- package/dist/schema/dist/index.mjs +19 -0
- package/dist/schema/dist/zod/dist/index.mjs +111 -0
- package/dist/symbol.d.mts +4 -0
- package/dist/symbol.mjs +5 -0
- package/dist/types.d.mts +6 -0
- package/dist/zod/dist/index.mjs +17 -0
- package/package.json +3 -3
- package/src/error/default.ts +14 -7
- package/src/error/index.ts +17 -3
- package/src/headers.ts +11 -11
- package/src/index.ts +3 -3
- package/src/options/base.ts +24 -25
- package/src/options/binary.ts +5 -5
- package/src/options/index.ts +12 -12
- package/src/response/base.ts +16 -13
- package/src/response/binary/index.ts +2 -4
- package/src/response/default.ts +4 -4
- package/src/response/error/index.ts +42 -38
- package/src/response/index.ts +1 -1
- package/src/response/json/index.ts +44 -40
- package/src/response/meta/index.ts +3 -3
- package/src/response/text/index.ts +2 -4
- package/src/types.ts +3 -5
- package/tests/json-symbol.test.ts +7 -7
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { PromiseOr } from "../types.mjs";
|
|
2
|
+
import { Headers } from "../headers.mjs";
|
|
3
|
+
import { Options } from "./base.mjs";
|
|
4
|
+
import { DefaultError } from "../error/index.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/options/error.d.ts
|
|
7
|
+
declare namespace ErrorOptions {
|
|
8
|
+
interface Base<TSchema extends Schema = Schema> {
|
|
9
|
+
headers?: Headers<Infer<TSchema>>;
|
|
10
|
+
mapDefaultError?: (error: DefaultError) => TSchema extends undefined ? DefaultError : Infer<TSchema>;
|
|
11
|
+
mapError?: (ctx: {
|
|
12
|
+
error: Error | null;
|
|
13
|
+
meta: unknown;
|
|
14
|
+
parsedError?: TSchema extends undefined ? DefaultError : Infer<TSchema>;
|
|
15
|
+
}) => PromiseOr<TSchema extends undefined ? DefaultError : Infer<TSchema>>;
|
|
16
|
+
onFailedSchemaValidation?: (ctx: {
|
|
17
|
+
data: unknown;
|
|
18
|
+
}) => PromiseOr<TSchema extends undefined ? DefaultError : Infer<TSchema>>;
|
|
19
|
+
schema?: TSchema;
|
|
20
|
+
validationType?: ValidationType;
|
|
21
|
+
}
|
|
22
|
+
type InferedSchemaFromBase<TError extends Base> = InferOr<ExtractSchema<TError>, DefaultError>;
|
|
23
|
+
type InferedSchema<TOptions extends Options> = TOptions["error"] extends undefined ? DefaultError : InferOr<ExtractSchema<TOptions["error"]>, DefaultError>;
|
|
24
|
+
}
|
|
25
|
+
declare function error<TSchema extends Schema>(opts: ErrorOptions.Base<TSchema> & {
|
|
26
|
+
schema?: TSchema;
|
|
27
|
+
}): ErrorOptions.Base<TSchema> & {
|
|
28
|
+
schema?: TSchema;
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { ErrorOptions, error };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BinaryOptions, binary } from "./binary.mjs";
|
|
2
|
+
import { ErrorOptions, error } from "./error.mjs";
|
|
3
|
+
import { JsonOptions, json } from "./json.mjs";
|
|
4
|
+
import { MetaOptions, meta } from "./meta.mjs";
|
|
5
|
+
import { Options } from "./base.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/options/index.d.ts
|
|
8
|
+
declare const options: {
|
|
9
|
+
json: typeof json;
|
|
10
|
+
meta: typeof meta;
|
|
11
|
+
error: typeof error;
|
|
12
|
+
binary: typeof binary;
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { options };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { binary } from "./binary.mjs";
|
|
2
|
+
import { error } from "./error.mjs";
|
|
3
|
+
import { json } from "./json.mjs";
|
|
4
|
+
import { meta } from "./meta.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/options/index.ts
|
|
7
|
+
const options = {
|
|
8
|
+
json,
|
|
9
|
+
meta,
|
|
10
|
+
error,
|
|
11
|
+
binary
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { options };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PromiseOr } from "../types.mjs";
|
|
2
|
+
import { Headers } from "../headers.mjs";
|
|
3
|
+
import { JsonResponse } from "../response/json/index.mjs";
|
|
4
|
+
import { Options } from "./base.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/options/json.d.ts
|
|
7
|
+
declare namespace JsonOptions {
|
|
8
|
+
interface Base<TSchema extends Schema = Schema> {
|
|
9
|
+
headers?: Headers<Infer<TSchema>>;
|
|
10
|
+
mapData?: (data: Infer<TSchema>) => PromiseOr<Infer<TSchema>>;
|
|
11
|
+
schema?: TSchema;
|
|
12
|
+
validationType?: ValidationType;
|
|
13
|
+
}
|
|
14
|
+
type InferedSchemaFromBase<TJson extends Base> = InferOr<ExtractSchema<TJson>, JsonResponse.DefaultSchema>;
|
|
15
|
+
type InferedSchema<TOptions extends Options> = TOptions["json"] extends undefined ? JsonResponse.DefaultSchema : InferOr<Exclude<ExtractSchema<TOptions["json"]>, undefined>, JsonResponse.DefaultSchema>;
|
|
16
|
+
}
|
|
17
|
+
declare function json<TSchema extends Schema>(opts: JsonOptions.Base<TSchema>): JsonOptions.Base<TSchema>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { JsonOptions, json };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FunctionObject } from "../types.mjs";
|
|
2
|
+
import { DefaultMeta } from "../response/meta/index.mjs";
|
|
3
|
+
import { Options } from "./base.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/options/meta.d.ts
|
|
6
|
+
declare namespace MetaOptions {
|
|
7
|
+
interface Base<TSchema extends Schema = Schema> {
|
|
8
|
+
default?: FunctionObject<Infer<TSchema>, never>;
|
|
9
|
+
schema?: TSchema;
|
|
10
|
+
validationType?: ValidationType;
|
|
11
|
+
}
|
|
12
|
+
type InferedSchema<TOptions extends Options> = TOptions["meta"] extends undefined ? DefaultMeta : InferOr<ExtractSchema<TOptions["meta"]>, DefaultMeta>;
|
|
13
|
+
}
|
|
14
|
+
declare function meta<TSchema extends Schema>(opts: MetaOptions.Base<TSchema> & {
|
|
15
|
+
schema?: TSchema;
|
|
16
|
+
}): MetaOptions.Base<TSchema> & {
|
|
17
|
+
schema?: TSchema;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { MetaOptions, meta };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { RawHeaders } from "../headers.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/response/base.d.ts
|
|
4
|
+
type ResponseTypes = "json" | "binary" | "text" | "error";
|
|
5
|
+
declare namespace BaseResponse {
|
|
6
|
+
interface Options {
|
|
7
|
+
headers?: RawHeaders;
|
|
8
|
+
status?: number;
|
|
9
|
+
statusText?: string;
|
|
10
|
+
}
|
|
11
|
+
class Base<TPayload> extends Response {
|
|
12
|
+
payload: TPayload | undefined;
|
|
13
|
+
constructor(body?: ConstructorParameters<typeof Response>[0], init?: (ResponseInit & {
|
|
14
|
+
payload: TPayload;
|
|
15
|
+
}) | undefined);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { BaseResponse, ResponseTypes };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/response/base.ts
|
|
2
|
+
let BaseResponse;
|
|
3
|
+
(function(_BaseResponse) {
|
|
4
|
+
class Base extends Response {
|
|
5
|
+
payload;
|
|
6
|
+
constructor(body, init) {
|
|
7
|
+
const { payload, ..._init } = init ?? {};
|
|
8
|
+
super(body, _init);
|
|
9
|
+
this.payload = payload;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
_BaseResponse.Base = Base;
|
|
13
|
+
})(BaseResponse || (BaseResponse = {}));
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { BaseResponse };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseResponse } from "../base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/response/binary/index.d.ts
|
|
4
|
+
type Binary = Blob | ArrayBuffer | Uint8Array | ReadableStream;
|
|
5
|
+
declare namespace BinaryResponse {
|
|
6
|
+
class Base extends Response {}
|
|
7
|
+
interface Options extends BaseResponse.Options {}
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { Binary, BinaryResponse };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ErrorResponse } from "./error/index.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/response/default.d.ts
|
|
4
|
+
interface DefaultResponse<TData = unknown> {
|
|
5
|
+
data: TData | null;
|
|
6
|
+
error: ErrorResponse.Base<any, any, any> | null;
|
|
7
|
+
metadata: Record<string, unknown>;
|
|
8
|
+
success: boolean;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { DefaultResponse };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BaseResponse } from "../base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/response/error/index.d.ts
|
|
4
|
+
declare namespace ErrorResponse {
|
|
5
|
+
class Base<TName extends string, TMeta extends Record<string, any>, TOutput extends Record<string, any>> extends Error {
|
|
6
|
+
name: TName;
|
|
7
|
+
meta: TMeta;
|
|
8
|
+
output: TOutput;
|
|
9
|
+
status: number;
|
|
10
|
+
statusText: string;
|
|
11
|
+
constructor({
|
|
12
|
+
meta,
|
|
13
|
+
name,
|
|
14
|
+
output,
|
|
15
|
+
status,
|
|
16
|
+
statusText
|
|
17
|
+
}: {
|
|
18
|
+
name: TName;
|
|
19
|
+
meta: TMeta;
|
|
20
|
+
output: TOutput;
|
|
21
|
+
status: number;
|
|
22
|
+
statusText: string;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
interface Options extends BaseResponse.Options {}
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { ErrorResponse };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/response/error/index.ts
|
|
2
|
+
let ErrorResponse;
|
|
3
|
+
(function(_ErrorResponse) {
|
|
4
|
+
class Base extends Error {
|
|
5
|
+
name;
|
|
6
|
+
meta;
|
|
7
|
+
output;
|
|
8
|
+
status;
|
|
9
|
+
statusText;
|
|
10
|
+
constructor({ meta, name, output, status, statusText }) {
|
|
11
|
+
super();
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.statusText = statusText;
|
|
14
|
+
this.name = name;
|
|
15
|
+
this.meta = meta;
|
|
16
|
+
this.output = output;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
_ErrorResponse.Base = Base;
|
|
20
|
+
})(ErrorResponse || (ErrorResponse = {}));
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ErrorResponse };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseResponse } from "./base.mjs";
|
|
2
|
+
import { BinaryResponse } from "./binary/index.mjs";
|
|
3
|
+
import { ErrorResponse } from "./error/index.mjs";
|
|
4
|
+
import { JsonResponse } from "./json/index.mjs";
|
|
5
|
+
import { TextResponse } from "./text/index.mjs";
|
|
6
|
+
|
|
7
|
+
export { };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BaseResponse } from "../base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/response/json/index.d.ts
|
|
4
|
+
declare namespace JsonResponse {
|
|
5
|
+
class Base<TOutput> extends Response {
|
|
6
|
+
private _output;
|
|
7
|
+
constructor(body: ConstructorParameters<typeof Response>[0], _init?: ConstructorParameters<typeof Response>[1] & {
|
|
8
|
+
output: TOutput;
|
|
9
|
+
});
|
|
10
|
+
get output(): TOutput | undefined;
|
|
11
|
+
json: () => Promise<TOutput>;
|
|
12
|
+
}
|
|
13
|
+
interface Options extends BaseResponse.Options {}
|
|
14
|
+
type DefaultInputSchema = Record<string, any>;
|
|
15
|
+
type DefaultSchema = {
|
|
16
|
+
data: Record<string, any>;
|
|
17
|
+
success: boolean;
|
|
18
|
+
};
|
|
19
|
+
function defaultOnDataOutput(input: DefaultInputSchema): DefaultSchema;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { JsonResponse };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/response/json/index.ts
|
|
2
|
+
let JsonResponse;
|
|
3
|
+
(function(_JsonResponse) {
|
|
4
|
+
class Base extends Response {
|
|
5
|
+
_output;
|
|
6
|
+
constructor(body, _init) {
|
|
7
|
+
const { output, ...init } = _init ?? {};
|
|
8
|
+
super(body, init);
|
|
9
|
+
this._output = output;
|
|
10
|
+
}
|
|
11
|
+
get output() {
|
|
12
|
+
return this._output;
|
|
13
|
+
}
|
|
14
|
+
json = () => {
|
|
15
|
+
return Response.prototype.json.call(this);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
_JsonResponse.Base = Base;
|
|
19
|
+
function defaultOnDataOutput(input) {
|
|
20
|
+
return {
|
|
21
|
+
data: input,
|
|
22
|
+
success: true
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
_JsonResponse.defaultOnDataOutput = defaultOnDataOutput;
|
|
26
|
+
})(JsonResponse || (JsonResponse = {}));
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { JsonResponse };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { resolveZodSchemaFromSources, z } from "./zod/dist/index.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../schema/dist/index.mjs
|
|
4
|
+
function checkSchema(schema, input, options) {
|
|
5
|
+
const validationType = options?.validationType ?? "parse";
|
|
6
|
+
let resolved = input;
|
|
7
|
+
if (options?.sources && schema instanceof z.ZodObject) resolved = {
|
|
8
|
+
...typeof input === "object" && input !== null ? input : {},
|
|
9
|
+
...resolveZodSchemaFromSources(schema, options.sources)
|
|
10
|
+
};
|
|
11
|
+
if (validationType === "parse") return schema.parse(resolved) ?? null;
|
|
12
|
+
if (validationType === "safeParse") {
|
|
13
|
+
const { data } = schema.safeParse(resolved);
|
|
14
|
+
return data ?? null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { checkSchema };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import z, { default as z$1 } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region ../schema/dist/zod/dist/index.mjs
|
|
4
|
+
/**
|
|
5
|
+
* Extracts `from` metadata from each field of a zod object schema.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const schema = z.object({
|
|
10
|
+
* name: z.string().from("body"),
|
|
11
|
+
* id: z.string().from("params"),
|
|
12
|
+
* token: z.string().from("headers", { key: "authorization" }),
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* resolveZodSchemaMeta(schema);
|
|
16
|
+
* // => {
|
|
17
|
+
* // name: { from: "body" },
|
|
18
|
+
* // id: { from: "params" },
|
|
19
|
+
* // token: { from: "headers", key: "authorization" },
|
|
20
|
+
* // }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function resolveZodSchemaMeta(schema) {
|
|
24
|
+
const shape = schema._zod.def.shape;
|
|
25
|
+
const result = {};
|
|
26
|
+
for (const fieldName in shape) {
|
|
27
|
+
const fieldSchema = shape[fieldName];
|
|
28
|
+
const meta = resolveFieldMeta(fieldSchema);
|
|
29
|
+
if (meta) result[fieldName] = meta;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function resolveFieldMeta(schema) {
|
|
34
|
+
const meta = z$1.globalRegistry.get(schema);
|
|
35
|
+
if (meta && typeof meta.from === "string") {
|
|
36
|
+
const key = meta.key ?? meta.fromKey;
|
|
37
|
+
return {
|
|
38
|
+
from: meta.from,
|
|
39
|
+
...key !== void 0 ? { key } : {}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const def = schema._zod?.def;
|
|
43
|
+
if (def?.innerType) return resolveFieldMeta(def.innerType);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Resolves field values from dynamic sources based on the schema's `from` metadata.
|
|
47
|
+
*
|
|
48
|
+
* Sources is a map where keys match possible `from` values
|
|
49
|
+
* (e.g. `"query"`, `"params"`, `"body"`, `"headers"`, `"handler.payload"`)
|
|
50
|
+
* and values are the corresponding data objects.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const schema = z.object({
|
|
55
|
+
* name: z.string().from("body"),
|
|
56
|
+
* id: z.string().from("params"),
|
|
57
|
+
* search: z.string().from("query"),
|
|
58
|
+
* auth: z.string().from("headers", { key: "authorization" }),
|
|
59
|
+
* prev: z.string().from("handler.payload", { key: "name" }),
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* const resolved = resolveZodSchemaFromSources(schema, {
|
|
63
|
+
* body: { name: "John" },
|
|
64
|
+
* params: { id: "42" },
|
|
65
|
+
* query: { search: "test" },
|
|
66
|
+
* headers: { authorization: "Bearer ..." },
|
|
67
|
+
* "handler.payload": { name: "prev-value" },
|
|
68
|
+
* });
|
|
69
|
+
* // => { name: "John", id: "42", search: "test", auth: "Bearer ...", prev: "prev-value" }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function resolveZodSchemaFromSources(schema, sources) {
|
|
73
|
+
const fieldsMeta = resolveZodSchemaMeta(schema);
|
|
74
|
+
const result = {};
|
|
75
|
+
for (const [fieldName, meta] of Object.entries(fieldsMeta)) {
|
|
76
|
+
const source = sources[meta.from];
|
|
77
|
+
if (!source) continue;
|
|
78
|
+
const keys = meta.key ? Array.isArray(meta.key) ? meta.key : [meta.key] : [fieldName];
|
|
79
|
+
for (const k of keys) {
|
|
80
|
+
const value = getNestedValue(source, k);
|
|
81
|
+
if (value !== void 0) {
|
|
82
|
+
result[fieldName] = value;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
function getNestedValue(obj, path) {
|
|
90
|
+
const parts = path.split(".");
|
|
91
|
+
let current = obj;
|
|
92
|
+
for (const part of parts) {
|
|
93
|
+
if (current === null || current === void 0 || typeof current !== "object") return;
|
|
94
|
+
current = current[part];
|
|
95
|
+
}
|
|
96
|
+
return current;
|
|
97
|
+
}
|
|
98
|
+
function extendZod(zod) {
|
|
99
|
+
if (typeof zod.ZodType.prototype.from !== "undefined") return zod;
|
|
100
|
+
zod.ZodType.prototype.from = function(from, options) {
|
|
101
|
+
return this.meta({
|
|
102
|
+
from,
|
|
103
|
+
...options ?? {}
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
return zod;
|
|
107
|
+
}
|
|
108
|
+
extendZod(z);
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
export { resolveZodSchemaFromSources, z };
|
package/dist/symbol.mjs
ADDED
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type AnyObject = Record<string, any>;
|
|
3
|
+
type FunctionObject<TResult, TFnArg = never> = TResult | (TFnArg extends never ? () => TResult : (arg: TFnArg) => TResult);
|
|
4
|
+
type PromiseOr<T> = PromiseLike<T> | T;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { AnyObject, FunctionObject, PromiseOr };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import z, { default as z$1 } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region ../zod/dist/index.mjs
|
|
4
|
+
function extendZod(zod) {
|
|
5
|
+
if (typeof zod.ZodType.prototype.from !== "undefined") return zod;
|
|
6
|
+
zod.ZodType.prototype.from = function(from, options) {
|
|
7
|
+
return this.meta({
|
|
8
|
+
from,
|
|
9
|
+
...options ?? {}
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
return zod;
|
|
13
|
+
}
|
|
14
|
+
const zod = extendZod(z);
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { zod };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apisr/response",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"module": "src/index.ts",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@repo/eslint-config": "*",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"typescript": "5.9.2"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@apisr/schema": "",
|
|
16
|
-
"@apisr/zod": ""
|
|
15
|
+
"@apisr/schema": "^0.0.2",
|
|
16
|
+
"@apisr/zod": "^0.0.2"
|
|
17
17
|
},
|
|
18
18
|
"private": false,
|
|
19
19
|
"scripts": {
|
package/src/error/default.ts
CHANGED
|
@@ -6,6 +6,13 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
6
6
|
_mapDefaultError: ErrorOptions.Base["mapDefaultError"]
|
|
7
7
|
): ErrorRegistry<TOptions> {
|
|
8
8
|
const mapDefaultError = _mapDefaultError ?? ((err) => err);
|
|
9
|
+
const resolveCause = (input: unknown): unknown => {
|
|
10
|
+
if (typeof input !== "object" || input === null || !("cause" in input)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return (input as { cause?: unknown }).cause;
|
|
15
|
+
};
|
|
9
16
|
|
|
10
17
|
const inputSchema = z
|
|
11
18
|
.object({
|
|
@@ -20,7 +27,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
20
27
|
message: "Unauthorized",
|
|
21
28
|
code: "UNAUTHORIZED",
|
|
22
29
|
name: "UnauthorizedError",
|
|
23
|
-
cause: input
|
|
30
|
+
cause: resolveCause(input),
|
|
24
31
|
}),
|
|
25
32
|
options: {
|
|
26
33
|
input: inputSchema,
|
|
@@ -35,7 +42,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
35
42
|
message: "Forbidden",
|
|
36
43
|
code: "FORBIDDEN",
|
|
37
44
|
name: "ForbiddenError",
|
|
38
|
-
cause: input
|
|
45
|
+
cause: resolveCause(input),
|
|
39
46
|
}),
|
|
40
47
|
options: {
|
|
41
48
|
input: inputSchema,
|
|
@@ -50,7 +57,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
50
57
|
message: "Not Found",
|
|
51
58
|
code: "NOT_FOUND",
|
|
52
59
|
name: "NotFoundError",
|
|
53
|
-
cause: input
|
|
60
|
+
cause: resolveCause(input),
|
|
54
61
|
}),
|
|
55
62
|
options: {
|
|
56
63
|
input: inputSchema,
|
|
@@ -65,7 +72,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
65
72
|
message: "Bad Request",
|
|
66
73
|
code: "BAD_REQUEST",
|
|
67
74
|
name: "BadRequestError",
|
|
68
|
-
cause: input
|
|
75
|
+
cause: resolveCause(input),
|
|
69
76
|
}),
|
|
70
77
|
options: {
|
|
71
78
|
input: inputSchema,
|
|
@@ -80,7 +87,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
80
87
|
message: "Conflict",
|
|
81
88
|
code: "CONFLICT",
|
|
82
89
|
name: "ConflictError",
|
|
83
|
-
cause: input
|
|
90
|
+
cause: resolveCause(input),
|
|
84
91
|
}),
|
|
85
92
|
options: {
|
|
86
93
|
input: inputSchema,
|
|
@@ -95,7 +102,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
95
102
|
message: "Too Many Requests",
|
|
96
103
|
code: "TOO_MANY_REQUESTS",
|
|
97
104
|
name: "TooManyRequestsError",
|
|
98
|
-
cause: input
|
|
105
|
+
cause: resolveCause(input),
|
|
99
106
|
}),
|
|
100
107
|
options: {
|
|
101
108
|
input: inputSchema,
|
|
@@ -110,7 +117,7 @@ export function generateDefaultErrors<TOptions extends Options>(
|
|
|
110
117
|
message: "Internal Server Error",
|
|
111
118
|
code: "INTERNAL_SERVER_ERROR",
|
|
112
119
|
name: "InternalServerError",
|
|
113
|
-
cause: input
|
|
120
|
+
cause: resolveCause(input),
|
|
114
121
|
}),
|
|
115
122
|
options: {
|
|
116
123
|
input: inputSchema,
|
package/src/error/index.ts
CHANGED
|
@@ -28,6 +28,22 @@ export type DefaultError = {
|
|
|
28
28
|
stack?: string[];
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
type InferErrorHandlerInput<
|
|
32
|
+
THandlerOptions extends ErrorHandlerOptions | undefined,
|
|
33
|
+
> = THandlerOptions extends {
|
|
34
|
+
input: infer TSchema;
|
|
35
|
+
}
|
|
36
|
+
? TSchema extends Schema
|
|
37
|
+
? Infer<TSchema>
|
|
38
|
+
: never
|
|
39
|
+
: THandlerOptions extends {
|
|
40
|
+
input?: infer TSchema;
|
|
41
|
+
}
|
|
42
|
+
? TSchema extends Schema
|
|
43
|
+
? Infer<TSchema> | undefined
|
|
44
|
+
: undefined
|
|
45
|
+
: never;
|
|
46
|
+
|
|
31
47
|
export type ErrorHandler<
|
|
32
48
|
TOptions extends Options,
|
|
33
49
|
THandlerOptions extends ErrorHandlerOptions | undefined,
|
|
@@ -35,9 +51,7 @@ export type ErrorHandler<
|
|
|
35
51
|
meta: TOptions["meta"] extends undefined
|
|
36
52
|
? never
|
|
37
53
|
: Infer<ExtractSchema<TOptions["meta"]>>;
|
|
38
|
-
input: THandlerOptions
|
|
39
|
-
? never
|
|
40
|
-
: Infer<Exclude<THandlerOptions, undefined>["input"]>;
|
|
54
|
+
input: InferErrorHandlerInput<THandlerOptions>;
|
|
41
55
|
}) => ErrorOptions.InferedSchema<TOptions>;
|
|
42
56
|
|
|
43
57
|
export interface ErrorHandlerOptions<TSchema extends Schema = Schema> {
|