@kaito-http/core 2.4.0 → 2.5.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,8 +1,8 @@
1
1
  export type { HTTPMethod } from 'find-my-way';
2
- export * from './server';
3
- export * from './router';
4
- export * from './route';
5
- export * from './util';
2
+ export * from './error';
6
3
  export * from './req';
7
4
  export * from './res';
8
- export * from './error';
5
+ export * from './route';
6
+ export * from './router';
7
+ export * from './server';
8
+ export * from './util';
@@ -1,14 +1,33 @@
1
1
  /// <reference types="node" />
2
2
  import { HTTPMethod } from 'find-my-way';
3
- import { IncomingMessage } from 'http';
3
+ import { IncomingMessage } from 'node:http';
4
4
  export declare class KaitoRequest {
5
5
  readonly raw: IncomingMessage;
6
6
  private _url;
7
7
  constructor(raw: IncomingMessage);
8
+ /**
9
+ * The full URL of the request, including the protocol, hostname, and path.
10
+ * Note: does not include the query string or hash
11
+ */
8
12
  get fullURL(): string;
13
+ /**
14
+ * A new URL instance for the full URL of the request.
15
+ */
9
16
  get url(): URL;
17
+ /**
18
+ * The HTTP method of the request.
19
+ */
10
20
  get method(): HTTPMethod;
21
+ /**
22
+ * The protocol of the request, either `http` or `https`.
23
+ */
11
24
  get protocol(): 'http' | 'https';
25
+ /**
26
+ * The request headers
27
+ */
12
28
  get headers(): import("http").IncomingHttpHeaders;
29
+ /**
30
+ * The hostname of the request.
31
+ */
13
32
  get hostname(): string;
14
33
  }
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { ServerResponse } from 'http';
2
+ import { ServerResponse } from 'node:http';
3
+ import { CookieSerializeOptions } from 'cookie';
3
4
  export declare type ErroredAPIResponse = {
4
5
  success: false;
5
6
  data: null;
@@ -15,7 +16,31 @@ export declare type AnyResponse = APIResponse<unknown>;
15
16
  export declare class KaitoResponse<T = unknown> {
16
17
  readonly raw: ServerResponse;
17
18
  constructor(raw: ServerResponse);
19
+ /**
20
+ * Send a response
21
+ * @param key The key of the header
22
+ * @param value The value of the header
23
+ * @returns The response object
24
+ */
18
25
  header(key: string, value: string | readonly string[]): this;
26
+ /**
27
+ * Set the status code of the response
28
+ * @param code The status code
29
+ * @returns The response object
30
+ */
19
31
  status(code: number): this;
32
+ /**
33
+ * Set a cookie
34
+ * @param name The name of the cookie
35
+ * @param value The value of the cookie
36
+ * @param options The options for the cookie
37
+ * @returns The response object
38
+ */
39
+ cookie(name: string, value: string, options: CookieSerializeOptions): this;
40
+ /**
41
+ * Send a JSON APIResponse body
42
+ * @param data The data to send
43
+ * @returns The response object
44
+ */
20
45
  json(data: APIResponse<T>): this;
21
46
  }
@@ -1,13 +1,14 @@
1
1
  import { z } from 'zod';
2
2
  import { ExtractRouteParams, KaitoMethod } from './util';
3
- export declare type RouteArgument<Path extends string, Context, Input extends z.ZodSchema> = {
3
+ export declare type RouteArgument<Path extends string, Context, InputOutput> = {
4
4
  ctx: Context;
5
- input: z.infer<Input>;
5
+ input: InputOutput;
6
6
  params: ExtractRouteParams<Path>;
7
7
  };
8
- export declare type Route<Context, Result, Path extends string, Method extends KaitoMethod, Input extends z.ZodSchema> = {
9
- input?: Input;
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>;
10
10
  path: Path;
11
11
  method: Method;
12
- run(args: RouteArgument<Path, Context, Input>): Promise<Result>;
12
+ run(args: RouteArgument<Path, Context, InputOutput>): Promise<Result>;
13
13
  };
14
+ export declare type AnyRoute<Context = any> = Route<Context, any, any, any, any, z.ZodTypeDef, any>;
@@ -1,27 +1,21 @@
1
- import fmw, { HTTPMethod, Instance } from 'find-my-way';
1
+ import fmw from 'find-my-way';
2
2
  import { z } from 'zod';
3
- import { Route } from './route';
3
+ import { AnyRoute, Route } from './route';
4
4
  import { ServerConfig } from './server';
5
5
  import { KaitoMethod } from './util';
6
- export declare type RoutesInit<Context> = Array<Route<Context, unknown, string, KaitoMethod, z.ZodSchema>>;
7
- export declare type MergePaths<Routes extends RoutesInit<unknown>, Prefix extends string> = Routes extends [
8
- infer R,
9
- ...infer Rest
10
- ] ? R extends Route<infer Context, infer Result, infer Path, infer Method, infer Input> ? [Route<Context, Result, `${Prefix}${Path}`, Method, Input>, ...MergePaths<Extract<Rest, RoutesInit<any>>, Prefix>] : MergePaths<Extract<Rest, RoutesInit<any>>, Prefix> : [];
11
- export declare class Router<Context, Routes extends RoutesInit<Context>> {
12
- static create<Context = null>(): Router<Context, []>;
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>[]>>
11
+ ] : [];
12
+ export declare class Router<Context, R extends Routes> {
13
+ readonly routes: R;
14
+ constructor(routes: R);
13
15
  private static handle;
14
- readonly 'routes': Routes;
15
- private constructor();
16
- toFindMyWay(server: ServerConfig<Context>): Instance<fmw.HTTPVersion.V1>;
17
- add<Method extends HTTPMethod, Path extends string, Result, Input extends z.ZodSchema>(route: Route<Context, Result, Path, Method, Input>): Router<Context, [...Routes, Route<Context, Result, Path, Method, Input>]>;
18
- map(): { [Method in Routes[number]["method"]]: { [R in Extract<Routes[number], {
19
- method: Method;
20
- }> as R["path"]]: R; }; };
21
- merge<Prefix extends string, NewRoutes extends RoutesInit<Context>>(prefix: Prefix, router: Router<Context, NewRoutes>): Router<Context, [...Routes, ...MergePaths<NewRoutes, Prefix>]>;
22
- private copyContext;
16
+ 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>]>;
18
+ merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<Context, OtherRoutes>) => Router<Context, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
19
+ toFindMyWay: (server: ServerConfig<Context, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
23
20
  }
24
- /**
25
- * @deprecated Please use Router#create instead
26
- */
27
- export declare const createRouter: typeof Router.create;
21
+ export {};
@@ -1,15 +1,35 @@
1
1
  /// <reference types="node" />
2
- import { Router } from './router';
3
- import * as http from 'http';
2
+ import * as http from 'node:http';
3
+ import { KaitoError } from './error';
4
4
  import { KaitoRequest } from './req';
5
5
  import { KaitoResponse } from './res';
6
- import { KaitoError } from './error';
7
- import { GetContext } from './util';
8
- export declare type Before = (req: http.IncomingMessage, res: http.ServerResponse) => Promise<void>;
9
- export interface ServerConfig<Context> {
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 = {
10
+ success: true;
11
+ data: unknown;
12
+ } | {
13
+ success: false;
14
+ data: {
15
+ status: number;
16
+ message: string;
17
+ };
18
+ };
19
+ export declare type After<BeforeAfterContext> = (ctx: BeforeAfterContext, result: HandlerResult) => Promise<void>;
20
+ export declare type ServerConfigWithBefore<BeforeAfterContext> = {
21
+ before: Before<BeforeAfterContext>;
22
+ after?: After<BeforeAfterContext>;
23
+ } | {
24
+ before?: undefined;
25
+ };
26
+ export declare type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<BeforeAfterContext> & {
10
27
  router: Router<Context, any>;
11
28
  getContext: GetContext<Context>;
12
- before?: Before[];
29
+ rawRoutes?: Partial<Record<KaitoMethod, {
30
+ path: string;
31
+ handler: (request: http.IncomingMessage, response: http.ServerResponse) => unknown;
32
+ }[]>>;
13
33
  onError(arg: {
14
34
  error: Error;
15
35
  req: KaitoRequest;
@@ -18,9 +38,9 @@ export interface ServerConfig<Context> {
18
38
  status: number;
19
39
  message: string;
20
40
  }>;
21
- }
22
- export declare function createFMWServer<Context>(config: ServerConfig<Context>): {
23
- server: http.Server;
24
- fmw: import("find-my-way").Instance<import("find-my-way").HTTPVersion.V1>;
25
41
  };
26
- export declare function createServer<Context>(config: ServerConfig<Context>): http.Server;
42
+ export declare function createFMWServer<Context, BeforeAfterContext = null>(config: ServerConfig<Context, BeforeAfterContext>): {
43
+ readonly server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
44
+ readonly fmw: import("find-my-way").Instance<import("find-my-way").HTTPVersion.V1>;
45
+ };
46
+ export declare function createServer<Context, BeforeAfterContext = null>(config: ServerConfig<Context, BeforeAfterContext>): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;