@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.
Files changed (58) hide show
  1. package/dist/error/default.d.mts +8 -0
  2. package/dist/error/default.mjs +107 -0
  3. package/dist/error/index.d.mts +35 -0
  4. package/dist/error/index.mjs +3 -0
  5. package/dist/handler.d.mts +127 -0
  6. package/dist/handler.mjs +272 -0
  7. package/dist/headers.d.mts +8 -0
  8. package/dist/headers.mjs +9 -0
  9. package/dist/index.d.mts +20 -0
  10. package/dist/index.mjs +18 -0
  11. package/dist/options/base.d.mts +28 -0
  12. package/dist/options/binary.d.mts +13 -0
  13. package/dist/options/binary.mjs +7 -0
  14. package/dist/options/error.d.mts +31 -0
  15. package/dist/options/error.mjs +7 -0
  16. package/dist/options/index.d.mts +15 -0
  17. package/dist/options/index.mjs +15 -0
  18. package/dist/options/json.d.mts +19 -0
  19. package/dist/options/json.mjs +7 -0
  20. package/dist/options/meta.d.mts +20 -0
  21. package/dist/options/meta.mjs +7 -0
  22. package/dist/response/base.d.mts +19 -0
  23. package/dist/response/base.mjs +16 -0
  24. package/dist/response/binary/index.d.mts +10 -0
  25. package/dist/response/binary/index.mjs +9 -0
  26. package/dist/response/default.d.mts +11 -0
  27. package/dist/response/error/index.d.mts +28 -0
  28. package/dist/response/error/index.mjs +23 -0
  29. package/dist/response/index.mjs +7 -0
  30. package/dist/response/json/index.d.mts +22 -0
  31. package/dist/response/json/index.mjs +29 -0
  32. package/dist/response/meta/index.d.mts +7 -0
  33. package/dist/response/text/index.d.mts +9 -0
  34. package/dist/response/text/index.mjs +9 -0
  35. package/dist/schema/dist/index.mjs +19 -0
  36. package/dist/schema/dist/zod/dist/index.mjs +111 -0
  37. package/dist/symbol.d.mts +4 -0
  38. package/dist/symbol.mjs +5 -0
  39. package/dist/types.d.mts +6 -0
  40. package/dist/zod/dist/index.mjs +17 -0
  41. package/package.json +3 -3
  42. package/src/error/default.ts +14 -7
  43. package/src/error/index.ts +17 -3
  44. package/src/headers.ts +11 -11
  45. package/src/index.ts +3 -3
  46. package/src/options/base.ts +24 -25
  47. package/src/options/binary.ts +5 -5
  48. package/src/options/index.ts +12 -12
  49. package/src/response/base.ts +16 -13
  50. package/src/response/binary/index.ts +2 -4
  51. package/src/response/default.ts +4 -4
  52. package/src/response/error/index.ts +42 -38
  53. package/src/response/index.ts +1 -1
  54. package/src/response/json/index.ts +44 -40
  55. package/src/response/meta/index.ts +3 -3
  56. package/src/response/text/index.ts +2 -4
  57. package/src/types.ts +3 -5
  58. package/tests/json-symbol.test.ts +7 -7
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
- headers: Headers<any> | undefined,
8
- input: unknown
7
+ headers: Headers<any> | undefined,
8
+ input: unknown
9
9
  ): RawHeaders | null {
10
- const result =
11
- typeof headers === "function"
12
- ? (headers as (arg: unknown) => RawHeaders)(input)
13
- : headers;
10
+ const result =
11
+ typeof headers === "function"
12
+ ? (headers as (arg: unknown) => RawHeaders)(input)
13
+ : headers;
14
14
 
15
- if (!headers) {
16
- return null;
17
- }
15
+ if (!headers) {
16
+ return null;
17
+ }
18
18
 
19
- // @ts-ignore
20
- return result;
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";
@@ -1,36 +1,35 @@
1
- import type { MetaOptions } from "@/options/meta";
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 { BinaryOptions } from "@/options/binary";
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
- 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
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
- headers?: Headers<{
18
- type: ResponseTypes;
19
- data: any;
20
- }>;
21
-
22
- meta?: TMeta;
23
- error?: TError;
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
- binary?: TBinary;
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
- mapResponse?(data: {
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
  }
@@ -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
- export interface Base {
6
- headers?: Headers<Binary>;
5
+ export interface Base {
6
+ headers?: Headers<Binary>;
7
7
 
8
- mapData?: (data: Binary) => Binary;
9
- }
8
+ mapData?: (data: Binary) => Binary;
9
+ }
10
10
  }
11
11
 
12
12
  export function binary<T extends BinaryOptions.Base>(opts: T): T {
13
- return opts;
13
+ return opts;
14
14
  }
@@ -1,17 +1,17 @@
1
1
  export * from "./base";
2
- export * from "./json"
3
- export * from "./meta"
4
- export * from "./error"
5
- export * from "./binary"
2
+ export * from "./binary";
3
+ export * from "./error";
4
+ export * from "./json";
5
+ export * from "./meta";
6
6
 
7
- import { json } from "./json"
8
- import { meta } from "./meta"
9
- import { error } from "./error"
10
- import { binary } from "./binary"
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
- json,
14
- meta,
15
- error,
16
- binary
13
+ json,
14
+ meta,
15
+ error,
16
+ binary,
17
17
  };
@@ -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
- export interface Options {
7
- status?: number;
8
- statusText?: string;
9
- headers?: RawHeaders;
10
- }
6
+ export interface Options {
7
+ headers?: RawHeaders;
8
+ status?: number;
9
+ statusText?: string;
10
+ }
11
11
 
12
- export class Base<TPayload> extends Response {
13
- public payload: TPayload | undefined;
12
+ export class Base<TPayload> extends Response {
13
+ public payload: TPayload | undefined;
14
14
 
15
- constructor(body?: ConstructorParameters<typeof Response>[0], init?: (ResponseInit & { payload: TPayload }) | undefined) {
16
- const { payload, ..._init } = init ?? {};
15
+ constructor(
16
+ body?: ConstructorParameters<typeof Response>[0],
17
+ init?: (ResponseInit & { payload: TPayload }) | undefined
18
+ ) {
19
+ const { payload, ..._init } = init ?? {};
17
20
 
18
- super(body, _init);
21
+ super(body, _init);
19
22
 
20
- this.payload = payload;
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
- export class Base extends Response {
7
- }
6
+ export class Base extends Response {}
8
7
 
9
- export interface Options extends BaseResponse.Options {
10
- }
8
+ export interface Options extends BaseResponse.Options {}
11
9
  }
@@ -1,8 +1,8 @@
1
1
  import type { ErrorResponse } from "./error";
2
2
 
3
3
  export interface DefaultResponse<TData = unknown> {
4
- success: boolean;
5
- error: ErrorResponse.Base<any, any, any> | null;
6
- data: TData | null;
7
- metadata: Record<string, unknown>;
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
- 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({ meta, name, output, status, statusText }: {
16
- // Name of error in response handler
17
- name: TName;
18
-
19
- // Meta of response
20
- meta: TMeta;
21
-
22
- // Output of handler
23
- output: TOutput;
24
-
25
- status: number;
26
- statusText: string;
27
- }) {
28
- super();
29
-
30
- this.status = status;
31
- this.statusText = statusText;
32
-
33
- this.name = name;
34
- this.meta = meta;
35
- this.output = output;
36
- }
37
- }
38
-
39
-
40
- export interface Options extends BaseResponse.Options {
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
  }
@@ -1,7 +1,7 @@
1
1
  export * from "./base";
2
+ export * from "./binary";
2
3
  export * from "./default";
3
4
  export * from "./error";
4
5
  export * from "./json";
5
- export * from "./binary";
6
6
  export * from "./meta";
7
7
  export * from "./text";
@@ -1,44 +1,48 @@
1
1
  import type { BaseResponse } from "../base";
2
2
 
3
3
  export namespace JsonResponse {
4
- export class Base<TOutput> extends Response {
5
- private _output: TOutput | undefined;
6
-
7
- constructor(body: ConstructorParameters<typeof Response>[0], _init?: ConstructorParameters<typeof Response>[1] & {
8
- output: TOutput;
9
- }) {
10
- const { output, ...init } = _init ?? {};
11
-
12
- super(body, init);
13
-
14
- this._output = output;
15
- }
16
-
17
- get output(): TOutput | undefined {
18
- return this._output;
19
- }
20
-
21
- override json: () => Promise<TOutput> = () => {
22
- return Response.prototype.json.call(this) as Promise<TOutput>;
23
- };
24
- }
25
- // export type Base = Record<string, any> & {
26
- // [responseSymbol]: () => Response;
27
- // }
28
-
29
- export interface Options extends BaseResponse.Options {
30
- }
31
-
32
- export type DefaultInputSchema = Record<string, any>;
33
- export type DefaultSchema = {
34
- data: Record<string, any>;
35
- success: boolean;
36
- };
37
-
38
- export function defaultOnDataOutput(input: DefaultInputSchema): DefaultSchema {
39
- return {
40
- data: input,
41
- success: true
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
- requestId: string;
3
- timestamp: number;
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
- export class Base extends Response {
5
- }
4
+ export class Base extends Response {}
6
5
 
7
- export interface Options extends BaseResponse.Options {
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 extends any, TFnArg = never> = TResult | (
3
- (TFnArg extends never
4
- ? (() => TResult)
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
- const obj = {
7
- id: 123,
8
- [jsonSymbol]: () => new Response()
9
- }
6
+ const obj = {
7
+ id: 123,
8
+ [jsonSymbol]: () => new Response(),
9
+ };
10
10
 
11
- const str = JSON.stringify(obj, null, 2);
11
+ const str = JSON.stringify(obj, null, 2);
12
12
 
13
- console.log(str);
14
- })
13
+ console.log(str);
14
+ });