@apisr/response 0.0.1 → 0.0.2
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 +103 -0
- package/dist/error/index.d.mts +30 -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 +124 -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/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 { MetaOptions, meta } from "./meta.mjs";
|
|
2
|
+
import { ErrorOptions, error } from "./error.mjs";
|
|
3
|
+
import { JsonOptions, json } from "./json.mjs";
|
|
4
|
+
import { BinaryOptions, binary } from "./binary.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 { json } from "./json.mjs";
|
|
2
|
+
import { meta } from "./meta.mjs";
|
|
3
|
+
import { error } from "./error.mjs";
|
|
4
|
+
import { binary } from "./binary.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 { DefaultMeta } from "../response/meta/index.mjs";
|
|
2
|
+
import { FunctionObject } from "../types.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
|
+
status?: number;
|
|
8
|
+
statusText?: string;
|
|
9
|
+
headers?: RawHeaders;
|
|
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
|
+
success: boolean;
|
|
6
|
+
error: ErrorResponse.Base<any, any, any> | null;
|
|
7
|
+
data: TData | null;
|
|
8
|
+
metadata: Record<string, unknown>;
|
|
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,124 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
|
|
3
|
+
//#region ../schema/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.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
|
+
function checkSchema(schema, input, options) {
|
|
110
|
+
const validationType = options?.validationType ?? "parse";
|
|
111
|
+
let resolved = input;
|
|
112
|
+
if (options?.sources && schema instanceof z.ZodObject) resolved = {
|
|
113
|
+
...typeof input === "object" && input !== null ? input : {},
|
|
114
|
+
...resolveZodSchemaFromSources(schema, options.sources)
|
|
115
|
+
};
|
|
116
|
+
if (validationType === "parse") return schema.parse(resolved) ?? null;
|
|
117
|
+
else if (validationType === "safeParse") {
|
|
118
|
+
const { data } = schema.safeParse(resolved);
|
|
119
|
+
return data ?? null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
export { checkSchema };
|
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 extends any, 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$1);
|
|
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.2",
|
|
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/headers.ts
CHANGED
|
@@ -4,18 +4,18 @@ export type RawHeaders = Record<string, string>;
|
|
|
4
4
|
export type Headers<T> = FunctionObject<RawHeaders, T>;
|
|
5
5
|
|
|
6
6
|
export function resolveHeaders(
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
headers: Headers<any> | undefined,
|
|
8
|
+
input: unknown
|
|
9
9
|
): RawHeaders | null {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
const result =
|
|
11
|
+
typeof headers === "function"
|
|
12
|
+
? (headers as (arg: unknown) => RawHeaders)(input)
|
|
13
|
+
: headers;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
if (!headers) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
return result;
|
|
21
21
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
export * from "./error";
|
|
1
2
|
export * from "./handler";
|
|
2
3
|
export * from "./headers";
|
|
4
|
+
export * from "./options";
|
|
5
|
+
export * from "./response";
|
|
3
6
|
export * from "./symbol";
|
|
4
7
|
export * from "./types";
|
|
5
|
-
export * from "./response";
|
|
6
|
-
export * from "./options";
|
|
7
|
-
export * from "./error";
|
package/src/options/base.ts
CHANGED
|
@@ -1,36 +1,35 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Headers } from "@/headers";
|
|
2
|
+
import type { BinaryOptions } from "@/options/binary";
|
|
2
3
|
import type { ErrorOptions } from "@/options/error";
|
|
3
4
|
import type { JsonOptions } from "@/options/json";
|
|
4
|
-
import type {
|
|
5
|
-
import type { Headers } from "@/headers";
|
|
5
|
+
import type { MetaOptions } from "@/options/meta";
|
|
6
6
|
import type { BaseResponse, ResponseTypes } from "@/response/base";
|
|
7
7
|
import type { Binary } from "@/response/binary";
|
|
8
|
-
import type { PromiseOr } from "@/types";
|
|
9
8
|
import type { DefaultResponse } from "@/response/default";
|
|
9
|
+
import type { PromiseOr } from "@/types";
|
|
10
10
|
|
|
11
11
|
export interface Options<
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
TMeta extends MetaOptions.Base = MetaOptions.Base,
|
|
13
|
+
TError extends ErrorOptions.Base = ErrorOptions.Base,
|
|
14
|
+
TJson extends JsonOptions.Base = JsonOptions.Base,
|
|
15
|
+
TBinary extends BinaryOptions.Base = BinaryOptions.Base,
|
|
16
16
|
> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
json?: TJson;
|
|
17
|
+
binary?: TBinary;
|
|
18
|
+
error?: TError;
|
|
19
|
+
headers?: Headers<{
|
|
20
|
+
type: ResponseTypes;
|
|
21
|
+
data: any;
|
|
22
|
+
}>;
|
|
23
|
+
json?: TJson;
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
mapResponse?(data: {
|
|
26
|
+
data: JsonOptions.InferedSchemaFromBase<TJson> | Binary | string;
|
|
27
|
+
error: ErrorOptions.InferedSchemaFromBase<TError>;
|
|
28
|
+
// headers: RawHeaders;
|
|
29
|
+
// status: number | undefined;
|
|
30
|
+
// statusText: string | undefined;
|
|
31
|
+
response: BaseResponse.Base<DefaultResponse>;
|
|
32
|
+
}): PromiseOr<Response>;
|
|
27
33
|
|
|
28
|
-
|
|
29
|
-
data: JsonOptions.InferedSchemaFromBase<TJson> | Binary | string;
|
|
30
|
-
error: ErrorOptions.InferedSchemaFromBase<TError>;
|
|
31
|
-
// headers: RawHeaders;
|
|
32
|
-
// status: number | undefined;
|
|
33
|
-
// statusText: string | undefined;
|
|
34
|
-
response: BaseResponse.Base<DefaultResponse>;
|
|
35
|
-
}): PromiseOr<Response>;
|
|
34
|
+
meta?: TMeta;
|
|
36
35
|
}
|
package/src/options/binary.ts
CHANGED
|
@@ -2,13 +2,13 @@ import type { Headers } from "@/headers";
|
|
|
2
2
|
import type { Binary } from "@/response/binary";
|
|
3
3
|
|
|
4
4
|
export namespace BinaryOptions {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
export interface Base {
|
|
6
|
+
headers?: Headers<Binary>;
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
mapData?: (data: Binary) => Binary;
|
|
9
|
+
}
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export function binary<T extends BinaryOptions.Base>(opts: T): T {
|
|
13
|
-
|
|
13
|
+
return opts;
|
|
14
14
|
}
|