@kaito-http/core 2.5.0 → 2.7.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.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { HTTPMethod } from 'find-my-way';
3
- import { IncomingMessage } from 'node:http';
2
+ import type { HTTPMethod } from 'find-my-way';
3
+ import type { IncomingMessage } from 'node:http';
4
4
  export declare class KaitoRequest {
5
5
  readonly raw: IncomingMessage;
6
6
  private _url;
@@ -1,18 +1,18 @@
1
1
  /// <reference types="node" />
2
- import { ServerResponse } from 'node:http';
3
- import { CookieSerializeOptions } from 'cookie';
4
- export declare type ErroredAPIResponse = {
2
+ import type { ServerResponse } from 'node:http';
3
+ import type { CookieSerializeOptions } from 'cookie';
4
+ export type ErroredAPIResponse = {
5
5
  success: false;
6
6
  data: null;
7
7
  message: string;
8
8
  };
9
- export declare type SuccessfulAPIResponse<T> = {
9
+ export type SuccessfulAPIResponse<T> = {
10
10
  success: true;
11
11
  data: T;
12
12
  message: 'OK';
13
13
  };
14
- export declare type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
15
- export declare type AnyResponse = APIResponse<unknown>;
14
+ export type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
15
+ export type AnyResponse = APIResponse<unknown>;
16
16
  export declare class KaitoResponse<T = unknown> {
17
17
  readonly raw: ServerResponse;
18
18
  constructor(raw: ServerResponse);
@@ -1,14 +1,17 @@
1
- import { z } from 'zod';
2
- import { ExtractRouteParams, KaitoMethod } from './util';
3
- export declare type RouteArgument<Path extends string, Context, InputOutput> = {
1
+ import type { z } from 'zod';
2
+ import type { ExtractRouteParams, KaitoMethod } from './util';
3
+ export type RouteArgument<Path extends string, Context, QueryOutput, BodyOutput> = {
4
4
  ctx: Context;
5
- input: InputOutput;
5
+ body: BodyOutput;
6
+ query: QueryOutput;
6
7
  params: ExtractRouteParams<Path>;
7
8
  };
8
- export declare type Route<Context, Result, Path extends string, Method extends KaitoMethod, InputOutput = never, InputDef extends z.ZodTypeDef = z.ZodTypeDef, InputInput = InputOutput> = {
9
- input?: z.ZodType<InputOutput, InputDef, InputInput>;
9
+ export type AnyQueryDefinition = Record<string, z.ZodTypeAny>;
10
+ export type Route<Context, Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition, BodyOutput, BodyDef extends z.ZodTypeDef, BodyInput> = {
11
+ body?: z.ZodType<BodyOutput, BodyDef, BodyInput>;
12
+ query?: Query;
10
13
  path: Path;
11
14
  method: Method;
12
- run(args: RouteArgument<Path, Context, InputOutput>): Promise<Result>;
15
+ run(args: RouteArgument<Path, Context, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result> | Result;
13
16
  };
14
- export declare type AnyRoute<Context = any> = Route<Context, any, any, any, any, z.ZodTypeDef, any>;
17
+ export type AnyRoute<Context = any> = Route<Context, any, any, any, AnyQueryDefinition, any, any, any>;
@@ -1,20 +1,34 @@
1
1
  import fmw from 'find-my-way';
2
2
  import { z } from 'zod';
3
- import { AnyRoute, Route } from './route';
4
- import { ServerConfig } from './server';
5
- import { KaitoMethod } from './util';
6
- declare type Routes = readonly AnyRoute[];
7
- declare type RemapRoutePrefix<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer Context, infer Result, infer Path, infer Method, infer InputOutput, infer InputDef, infer InputInput> ? Route<Context, Result, `${Prefix}${Path}`, Method, InputOutput, InputDef, InputInput> : never;
8
- declare type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends [infer First, ...infer Rest] ? [
9
- RemapRoutePrefix<Extract<First, AnyRoute<any>>, Prefix>,
10
- ...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute<any>[]>>
3
+ import type { AnyQueryDefinition, AnyRoute, Route } from './route';
4
+ import type { ServerConfig } from './server';
5
+ import type { KaitoMethod } from './util';
6
+ type Routes = readonly AnyRoute[];
7
+ type RemapRoutePrefix<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer Context, infer Result, infer Path, infer Method, infer Query, infer BodyOutput, infer BodyDef, infer BodyInput> ? Route<Context, Result, `${Prefix}${Path}`, Method, Query, BodyOutput, BodyDef, BodyInput> : never;
8
+ type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends [infer First, ...infer Rest] ? [
9
+ RemapRoutePrefix<Extract<First, AnyRoute>, Prefix>,
10
+ ...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute[]>>
11
11
  ] : [];
12
12
  export declare class Router<Context, R extends Routes> {
13
13
  readonly routes: R;
14
- constructor(routes: R);
15
- private static handle;
16
14
  static create: <Context_1>() => Router<Context_1, []>;
17
- add: <Result, Path extends string, Method extends KaitoMethod, InputOutput = never, InputDef extends z.ZodTypeDef = z.ZodTypeDef, InputInput = InputOutput>(route: Route<Context, Result, Path, Method, InputOutput, InputDef, InputInput>) => Router<Context, [...R, Route<Context, Result, Path, Method, InputOutput, InputDef, InputInput>]>;
15
+ private static handle;
16
+ constructor(routes: R);
17
+ /**
18
+ * Adds a new route to the router
19
+ * @param route The route specification to add to this router
20
+ * @returns A new router with this route added
21
+ * @deprecated Use `Router#add` instead
22
+ */
23
+ old_add: <Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(route: Method extends "GET" ? Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "body"> : Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>) => Router<Context, [...R, Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>]>;
24
+ /**
25
+ * Adds a new route to the router
26
+ * @param method The HTTP method to add a route for
27
+ * @param path The path to add a route for
28
+ * @param route The route specification to add to this router
29
+ * @returns A new router with this route added
30
+ */
31
+ add: <Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(method: Method, path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Result | Promise<Result>) | (Method extends "GET" ? Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method"> : Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">)) => Router<Context, [...R, Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>]>;
18
32
  merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<Context, OtherRoutes>) => Router<Context, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
19
33
  toFindMyWay: (server: ServerConfig<Context, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
20
34
  }
@@ -1,12 +1,12 @@
1
1
  /// <reference types="node" />
2
2
  import * as http from 'node:http';
3
- import { KaitoError } from './error';
4
- import { KaitoRequest } from './req';
5
- import { KaitoResponse } from './res';
6
- import { Router } from './router';
7
- import { GetContext, KaitoMethod } from './util';
8
- export declare type Before<BeforeAfterContext> = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<BeforeAfterContext>;
9
- export declare type HandlerResult = {
3
+ import type { KaitoError } from './error';
4
+ import type { KaitoRequest } from './req';
5
+ import type { KaitoResponse } from './res';
6
+ import type { Router } from './router';
7
+ import type { GetContext, KaitoMethod } from './util';
8
+ export type Before<BeforeAfterContext> = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<BeforeAfterContext>;
9
+ export type HandlerResult = {
10
10
  success: true;
11
11
  data: unknown;
12
12
  } | {
@@ -16,20 +16,20 @@ export declare type HandlerResult = {
16
16
  message: string;
17
17
  };
18
18
  };
19
- export declare type After<BeforeAfterContext> = (ctx: BeforeAfterContext, result: HandlerResult) => Promise<void>;
20
- export declare type ServerConfigWithBefore<BeforeAfterContext> = {
19
+ export type After<BeforeAfterContext> = (ctx: BeforeAfterContext, result: HandlerResult) => Promise<void>;
20
+ export type ServerConfigWithBefore<BeforeAfterContext> = {
21
21
  before: Before<BeforeAfterContext>;
22
22
  after?: After<BeforeAfterContext>;
23
23
  } | {
24
24
  before?: undefined;
25
25
  };
26
- export declare type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<BeforeAfterContext> & {
26
+ export type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<BeforeAfterContext> & {
27
27
  router: Router<Context, any>;
28
28
  getContext: GetContext<Context>;
29
- rawRoutes?: Partial<Record<KaitoMethod, {
29
+ rawRoutes?: Partial<Record<KaitoMethod, Array<{
30
30
  path: string;
31
31
  handler: (request: http.IncomingMessage, response: http.ServerResponse) => unknown;
32
- }[]>>;
32
+ }>>>;
33
33
  onError(arg: {
34
34
  error: Error;
35
35
  req: KaitoRequest;
@@ -1,20 +1,20 @@
1
- import { HTTPMethod } from 'find-my-way';
2
- import { KaitoRequest } from './req';
3
- import { KaitoResponse } from './res';
4
- export declare type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${string}:${infer Param}/${infer Rest}` ? {
1
+ import type { HTTPMethod } from 'find-my-way';
2
+ import type { KaitoRequest } from './req';
3
+ import type { KaitoResponse } from './res';
4
+ export type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${string}:${infer Param}/${infer Rest}` ? {
5
5
  [k in Param | keyof ExtractRouteParams<Rest>]: string;
6
6
  } : T extends `${string}:${infer Param}` ? {
7
7
  [k in Param]: string;
8
8
  } : {};
9
- export declare type KaitoMethod = HTTPMethod | '*';
10
- export declare type GetContext<Result> = (req: KaitoRequest, res: KaitoResponse) => Promise<Result>;
9
+ export type KaitoMethod = HTTPMethod | '*';
10
+ export type GetContext<Result> = (req: KaitoRequest, res: KaitoResponse) => Promise<Result>;
11
11
  export declare function createGetContext<Context>(callback: GetContext<Context>): GetContext<Context>;
12
- export declare type InferContext<T> = T extends (req: KaitoRequest, res: KaitoResponse) => Promise<infer U> ? U : never;
12
+ export type InferContext<T> = T extends (req: KaitoRequest, res: KaitoResponse) => Promise<infer U> ? U : never;
13
13
  export declare function getLastEntryInMultiHeaderValue(headerValue: string | string[]): string;
14
- declare type RemoveEndSlashes<T extends string> = T extends `${infer U}/` ? U : T;
15
- declare type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U}` : `/${T}`;
16
- export declare type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
17
- export declare type Values<T> = T[keyof T];
18
- export declare type NoEmpty<T> = [keyof T] extends [never] ? never : T;
19
- export declare function getInput(req: KaitoRequest): Promise<unknown>;
14
+ type RemoveEndSlashes<T extends string> = T extends `${infer U}/` ? U : T;
15
+ type AddStartSlashes<T extends string> = T extends `/${infer U}` ? `/${U}` : `/${T}`;
16
+ export type NormalizePath<T extends string> = AddStartSlashes<RemoveEndSlashes<T>>;
17
+ export type Values<T> = T[keyof T];
18
+ export type NoEmpty<T> = [keyof T] extends [never] ? never : T;
19
+ export declare function getBody(req: KaitoRequest): Promise<unknown>;
20
20
  export {};