@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
package/src/options/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
export * from "./base";
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
2
|
+
export * from "./binary";
|
|
3
|
+
export * from "./error";
|
|
4
|
+
export * from "./json";
|
|
5
|
+
export * from "./meta";
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
7
|
+
import { binary } from "./binary";
|
|
8
|
+
import { error } from "./error";
|
|
9
|
+
import { json } from "./json";
|
|
10
|
+
import { meta } from "./meta";
|
|
11
11
|
|
|
12
12
|
export const options = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
json,
|
|
14
|
+
meta,
|
|
15
|
+
error,
|
|
16
|
+
binary,
|
|
17
17
|
};
|
package/src/response/base.ts
CHANGED
|
@@ -3,21 +3,24 @@ import type { RawHeaders } from "@/headers";
|
|
|
3
3
|
export type ResponseTypes = "json" | "binary" | "text" | "error";
|
|
4
4
|
|
|
5
5
|
export namespace BaseResponse {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
export interface Options {
|
|
7
|
+
headers?: RawHeaders;
|
|
8
|
+
status?: number;
|
|
9
|
+
statusText?: string;
|
|
10
|
+
}
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
export class Base<TPayload> extends Response {
|
|
13
|
+
public payload: TPayload | undefined;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
constructor(
|
|
16
|
+
body?: ConstructorParameters<typeof Response>[0],
|
|
17
|
+
init?: (ResponseInit & { payload: TPayload }) | undefined
|
|
18
|
+
) {
|
|
19
|
+
const { payload, ..._init } = init ?? {};
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
super(body, _init);
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
this.payload = payload;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
23
26
|
}
|
|
@@ -3,9 +3,7 @@ import type { BaseResponse } from "../base";
|
|
|
3
3
|
export type Binary = Blob | ArrayBuffer | Uint8Array | ReadableStream;
|
|
4
4
|
|
|
5
5
|
export namespace BinaryResponse {
|
|
6
|
-
|
|
7
|
-
}
|
|
6
|
+
export class Base extends Response {}
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
}
|
|
8
|
+
export interface Options extends BaseResponse.Options {}
|
|
11
9
|
}
|
package/src/response/default.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ErrorResponse } from "./error";
|
|
2
2
|
|
|
3
3
|
export interface DefaultResponse<TData = unknown> {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
data: TData | null;
|
|
5
|
+
error: ErrorResponse.Base<any, any, any> | null;
|
|
6
|
+
metadata: Record<string, unknown>;
|
|
7
|
+
success: boolean;
|
|
8
8
|
}
|
|
@@ -1,42 +1,46 @@
|
|
|
1
1
|
import type { BaseResponse } from "../base";
|
|
2
2
|
|
|
3
3
|
export namespace ErrorResponse {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
export class Base<
|
|
5
|
+
TName extends string,
|
|
6
|
+
TMeta extends Record<string, any>,
|
|
7
|
+
TOutput extends Record<string, any>,
|
|
8
|
+
> extends Error {
|
|
9
|
+
public override name: TName;
|
|
10
|
+
public meta: TMeta;
|
|
11
|
+
public output: TOutput;
|
|
12
|
+
public status: number;
|
|
13
|
+
public statusText: string;
|
|
14
|
+
|
|
15
|
+
constructor({
|
|
16
|
+
meta,
|
|
17
|
+
name,
|
|
18
|
+
output,
|
|
19
|
+
status,
|
|
20
|
+
statusText,
|
|
21
|
+
}: {
|
|
22
|
+
// Name of error in response handler
|
|
23
|
+
name: TName;
|
|
24
|
+
|
|
25
|
+
// Meta of response
|
|
26
|
+
meta: TMeta;
|
|
27
|
+
|
|
28
|
+
// Output of handler
|
|
29
|
+
output: TOutput;
|
|
30
|
+
|
|
31
|
+
status: number;
|
|
32
|
+
statusText: string;
|
|
33
|
+
}) {
|
|
34
|
+
super();
|
|
35
|
+
|
|
36
|
+
this.status = status;
|
|
37
|
+
this.statusText = statusText;
|
|
38
|
+
|
|
39
|
+
this.name = name;
|
|
40
|
+
this.meta = meta;
|
|
41
|
+
this.output = output;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Options extends BaseResponse.Options {}
|
|
42
46
|
}
|
package/src/response/index.ts
CHANGED
|
@@ -1,44 +1,48 @@
|
|
|
1
1
|
import type { BaseResponse } from "../base";
|
|
2
2
|
|
|
3
3
|
export namespace JsonResponse {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
4
|
+
export class Base<TOutput> extends Response {
|
|
5
|
+
private _output: TOutput | undefined;
|
|
6
|
+
|
|
7
|
+
constructor(
|
|
8
|
+
body: ConstructorParameters<typeof Response>[0],
|
|
9
|
+
_init?: ConstructorParameters<typeof Response>[1] & {
|
|
10
|
+
output: TOutput;
|
|
11
|
+
}
|
|
12
|
+
) {
|
|
13
|
+
const { output, ...init } = _init ?? {};
|
|
14
|
+
|
|
15
|
+
super(body, init);
|
|
16
|
+
|
|
17
|
+
this._output = output;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get output(): TOutput | undefined {
|
|
21
|
+
return this._output;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override json: () => Promise<TOutput> = () => {
|
|
25
|
+
return Response.prototype.json.call(this) as Promise<TOutput>;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// export type Base = Record<string, any> & {
|
|
29
|
+
// [responseSymbol]: () => Response;
|
|
30
|
+
// }
|
|
31
|
+
|
|
32
|
+
export interface Options extends BaseResponse.Options {}
|
|
33
|
+
|
|
34
|
+
export type DefaultInputSchema = Record<string, any>;
|
|
35
|
+
export type DefaultSchema = {
|
|
36
|
+
data: Record<string, any>;
|
|
37
|
+
success: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export function defaultOnDataOutput(
|
|
41
|
+
input: DefaultInputSchema
|
|
42
|
+
): DefaultSchema {
|
|
43
|
+
return {
|
|
44
|
+
data: input,
|
|
45
|
+
success: true,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
44
48
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type DefaultMeta = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
2
|
+
requestId: string;
|
|
3
|
+
timestamp: number;
|
|
4
|
+
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type { BaseResponse } from "../base";
|
|
2
2
|
|
|
3
3
|
export namespace TextResponse {
|
|
4
|
-
|
|
5
|
-
}
|
|
4
|
+
export class Base extends Response {}
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
}
|
|
6
|
+
export interface Options extends BaseResponse.Options {}
|
|
9
7
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
export type AnyObject = Record<string, any>;
|
|
2
|
-
export type FunctionObject<TResult
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
: ((arg: TFnArg) => TResult))
|
|
6
|
-
);
|
|
2
|
+
export type FunctionObject<TResult, TFnArg = never> =
|
|
3
|
+
| TResult
|
|
4
|
+
| (TFnArg extends never ? () => TResult : (arg: TFnArg) => TResult);
|
|
7
5
|
|
|
8
6
|
export type PromiseOr<T> = PromiseLike<T> | T;
|
|
9
7
|
// export type IfUndefined<> =
|
|
@@ -3,12 +3,12 @@ import { test } from "bun:test";
|
|
|
3
3
|
const jsonSymbol = Symbol("_json");
|
|
4
4
|
|
|
5
5
|
test("json symbol stringify", () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const obj = {
|
|
7
|
+
id: 123,
|
|
8
|
+
[jsonSymbol]: () => new Response(),
|
|
9
|
+
};
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const str = JSON.stringify(obj, null, 2);
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
})
|
|
13
|
+
console.log(str);
|
|
14
|
+
});
|