@kaito-http/core 3.0.1 → 3.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.
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/cors/cors.ts
21
+ var cors_exports = {};
22
+ __export(cors_exports, {
23
+ experimental_createCORSTransform: () => experimental_createCORSTransform,
24
+ experimental_createOriginMatcher: () => experimental_createOriginMatcher
25
+ });
26
+ module.exports = __toCommonJS(cors_exports);
27
+ function experimental_createOriginMatcher(origins) {
28
+ if (origins.length === 0) {
29
+ return () => false;
30
+ }
31
+ const source = origins.map((origin) => {
32
+ if (origin.startsWith("*.")) {
33
+ const escapedDomain = origin.slice(2).replace(/[.+?^${}()|[\]\\]/g, "\\$&");
34
+ return `^(?:https?://)[^.]+\\.${escapedDomain}$`;
35
+ } else {
36
+ const escapedOrigin = origin.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
37
+ return `^${escapedOrigin}$`;
38
+ }
39
+ }).join("|");
40
+ const regex = new RegExp(source);
41
+ return (origin) => regex.test(origin);
42
+ }
43
+ function experimental_createCORSTransform(origins) {
44
+ const matcher = experimental_createOriginMatcher(origins);
45
+ return (request, response) => {
46
+ const origin = request.headers.get("Origin");
47
+ if (origin && matcher(origin)) {
48
+ response.headers.set("Access-Control-Allow-Origin", origin);
49
+ response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
50
+ response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
51
+ response.headers.set("Access-Control-Max-Age", "86400");
52
+ response.headers.set("Access-Control-Allow-Credentials", "true");
53
+ }
54
+ };
55
+ }
56
+ // Annotate the CommonJS export names for ESM import in node:
57
+ 0 && (module.exports = {
58
+ experimental_createCORSTransform,
59
+ experimental_createOriginMatcher
60
+ });
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Creates a function that matches origins against a predefined set of patterns, supporting wildcards.
3
+ * The matcher handles both exact matches and wildcard subdomain patterns (e.g., '*.example.com').
4
+ *
5
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
6
+ *
7
+ * @param origins Array of origin patterns to match against.
8
+ * Patterns can be exact origins (e.g., 'https://example.com') or wildcard patterns (e.g., '*.example.com') that match subdomains.
9
+ * @returns A function that tests if an origin matches any of the patterns
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const allowedOrigins = [
14
+ * 'https://example.com',
15
+ * '*.trusted-domain.com' // Won't match https://evil-domain.com, only subdomains
16
+ * ];
17
+ *
18
+ * const matcher = createOriginMatcher(allowedOrigins);
19
+ *
20
+ * // Exact match
21
+ * console.log(matcher('https://example.com')); // true
22
+ * console.log(matcher('http://example.com')); // false
23
+ *
24
+ * // Wildcard subdomain matches
25
+ * console.log(matcher('https://app.trusted-domain.com')); // true
26
+ * console.log(matcher('https://staging.trusted-domain.com')); // true
27
+ * console.log(matcher('https://trusted-domain.com')); // false, because it's not a subdomain
28
+ * console.log(matcher('https://evil-domain.com')); // false
29
+ * ```
30
+ */
31
+ declare function experimental_createOriginMatcher(origins: string[]): (origin: string) => boolean;
32
+ /**
33
+ * Create a function to apply CORS headers with sane defaults for most apps.
34
+ *
35
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
36
+ *
37
+ * @param options Options object
38
+ * @returns A function that will mutate the Response object by applying the CORS headers
39
+ * @example
40
+ * ```ts
41
+ * const cors = createCORSHandler({
42
+ * origins: ['https://example.com', "*.allows-subdomains.com", "http://localhost:3000"],
43
+ * });
44
+ *
45
+ * const handler = createKaitoHandler({
46
+ * // ...
47
+ * transform: async (request, response) => {
48
+ * cors(request, response);
49
+ * }
50
+ * });
51
+ * ```
52
+ */
53
+ declare function experimental_createCORSTransform(origins: string[]): (request: Request, response: Response) => void;
54
+
55
+ export { experimental_createCORSTransform, experimental_createOriginMatcher };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Creates a function that matches origins against a predefined set of patterns, supporting wildcards.
3
+ * The matcher handles both exact matches and wildcard subdomain patterns (e.g., '*.example.com').
4
+ *
5
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
6
+ *
7
+ * @param origins Array of origin patterns to match against.
8
+ * Patterns can be exact origins (e.g., 'https://example.com') or wildcard patterns (e.g., '*.example.com') that match subdomains.
9
+ * @returns A function that tests if an origin matches any of the patterns
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const allowedOrigins = [
14
+ * 'https://example.com',
15
+ * '*.trusted-domain.com' // Won't match https://evil-domain.com, only subdomains
16
+ * ];
17
+ *
18
+ * const matcher = createOriginMatcher(allowedOrigins);
19
+ *
20
+ * // Exact match
21
+ * console.log(matcher('https://example.com')); // true
22
+ * console.log(matcher('http://example.com')); // false
23
+ *
24
+ * // Wildcard subdomain matches
25
+ * console.log(matcher('https://app.trusted-domain.com')); // true
26
+ * console.log(matcher('https://staging.trusted-domain.com')); // true
27
+ * console.log(matcher('https://trusted-domain.com')); // false, because it's not a subdomain
28
+ * console.log(matcher('https://evil-domain.com')); // false
29
+ * ```
30
+ */
31
+ declare function experimental_createOriginMatcher(origins: string[]): (origin: string) => boolean;
32
+ /**
33
+ * Create a function to apply CORS headers with sane defaults for most apps.
34
+ *
35
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
36
+ *
37
+ * @param options Options object
38
+ * @returns A function that will mutate the Response object by applying the CORS headers
39
+ * @example
40
+ * ```ts
41
+ * const cors = createCORSHandler({
42
+ * origins: ['https://example.com', "*.allows-subdomains.com", "http://localhost:3000"],
43
+ * });
44
+ *
45
+ * const handler = createKaitoHandler({
46
+ * // ...
47
+ * transform: async (request, response) => {
48
+ * cors(request, response);
49
+ * }
50
+ * });
51
+ * ```
52
+ */
53
+ declare function experimental_createCORSTransform(origins: string[]): (request: Request, response: Response) => void;
54
+
55
+ export { experimental_createCORSTransform, experimental_createOriginMatcher };
@@ -0,0 +1,34 @@
1
+ // src/cors/cors.ts
2
+ function experimental_createOriginMatcher(origins) {
3
+ if (origins.length === 0) {
4
+ return () => false;
5
+ }
6
+ const source = origins.map((origin) => {
7
+ if (origin.startsWith("*.")) {
8
+ const escapedDomain = origin.slice(2).replace(/[.+?^${}()|[\]\\]/g, "\\$&");
9
+ return `^(?:https?://)[^.]+\\.${escapedDomain}$`;
10
+ } else {
11
+ const escapedOrigin = origin.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
12
+ return `^${escapedOrigin}$`;
13
+ }
14
+ }).join("|");
15
+ const regex = new RegExp(source);
16
+ return (origin) => regex.test(origin);
17
+ }
18
+ function experimental_createCORSTransform(origins) {
19
+ const matcher = experimental_createOriginMatcher(origins);
20
+ return (request, response) => {
21
+ const origin = request.headers.get("Origin");
22
+ if (origin && matcher(origin)) {
23
+ response.headers.set("Access-Control-Allow-Origin", origin);
24
+ response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
25
+ response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
26
+ response.headers.set("Access-Control-Max-Age", "86400");
27
+ response.headers.set("Access-Control-Allow-Credentials", "true");
28
+ }
29
+ };
30
+ }
31
+ export {
32
+ experimental_createCORSTransform,
33
+ experimental_createOriginMatcher
34
+ };
package/dist/index.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  KaitoError: () => KaitoError,
24
+ KaitoHead: () => KaitoHead,
24
25
  KaitoRequest: () => KaitoRequest,
25
26
  Router: () => Router,
26
27
  WrappedError: () => WrappedError,
@@ -77,44 +78,6 @@ function createKaitoHandler(config) {
77
78
  };
78
79
  }
79
80
 
80
- // src/request.ts
81
- var KaitoRequest = class {
82
- url;
83
- _request;
84
- constructor(url, request) {
85
- this._request = request;
86
- this.url = url;
87
- }
88
- get headers() {
89
- return this._request.headers;
90
- }
91
- get method() {
92
- return this._request.method;
93
- }
94
- async arrayBuffer() {
95
- return this._request.arrayBuffer();
96
- }
97
- async blob() {
98
- return this._request.blob();
99
- }
100
- async formData() {
101
- return this._request.formData();
102
- }
103
- async bytes() {
104
- const buffer = await this.arrayBuffer();
105
- return new Uint8Array(buffer);
106
- }
107
- async json() {
108
- return this._request.json();
109
- }
110
- async text() {
111
- return this._request.text();
112
- }
113
- get request() {
114
- return this._request;
115
- }
116
- };
117
-
118
81
  // src/head.ts
119
82
  var KaitoHead = class {
120
83
  _headers;
@@ -158,6 +121,44 @@ var KaitoHead = class {
158
121
  }
159
122
  };
160
123
 
124
+ // src/request.ts
125
+ var KaitoRequest = class {
126
+ url;
127
+ _request;
128
+ constructor(url, request) {
129
+ this._request = request;
130
+ this.url = url;
131
+ }
132
+ get headers() {
133
+ return this._request.headers;
134
+ }
135
+ get method() {
136
+ return this._request.method;
137
+ }
138
+ async arrayBuffer() {
139
+ return this._request.arrayBuffer();
140
+ }
141
+ async blob() {
142
+ return this._request.blob();
143
+ }
144
+ async formData() {
145
+ return this._request.formData();
146
+ }
147
+ async bytes() {
148
+ const buffer = await this.arrayBuffer();
149
+ return new Uint8Array(buffer);
150
+ }
151
+ async json() {
152
+ return this._request.json();
153
+ }
154
+ async text() {
155
+ return this._request.text();
156
+ }
157
+ get request() {
158
+ return this._request;
159
+ }
160
+ };
161
+
161
162
  // src/util.ts
162
163
  var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "undefined" && process.env.NODE_ENV === "development";
163
164
  function createUtilities(getContext) {
@@ -334,6 +335,7 @@ var Router = class _Router {
334
335
  // Annotate the CommonJS export names for ESM import in node:
335
336
  0 && (module.exports = {
336
337
  KaitoError,
338
+ KaitoHead,
337
339
  KaitoRequest,
338
340
  Router,
339
341
  WrappedError,
package/dist/index.d.cts CHANGED
@@ -154,7 +154,7 @@ type Route<ContextFrom, ContextTo, Result, Path extends string, Method extends K
154
154
  method: Method;
155
155
  run(arg: RouteArgument<Path, ContextTo, {
156
156
  [Key in keyof Query]: InferParsable<Query[Key]>['output'];
157
- }, InferParsable<Body>['output']>): Promise<Result>;
157
+ }, InferParsable<Body>['output']>): Promise<Result> | Result;
158
158
  };
159
159
  type AnyRoute<ContextFrom = any, ContextTo = any> = Route<ContextFrom, ContextTo, any, any, any, AnyQueryDefinition, any>;
160
160
 
@@ -175,13 +175,13 @@ declare class Router<ContextFrom, ContextTo, R extends AnyRoute> {
175
175
  readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends AnyRoute>(pathPrefix: PathPrefix, other: Router<ContextFrom, unknown, OtherRoutes>) => Router<ContextFrom, ContextTo, Extract<R | PrefixRoutesPath<PathPrefix, OtherRoutes>, AnyRoute>>;
176
176
  freeze: (server: Omit<HandlerConfig<ContextFrom>, "router">) => (req: Request) => Promise<Response>;
177
177
  private readonly method;
178
- get: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>>;
179
- post: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>>;
180
- put: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>>;
181
- patch: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>>;
182
- delete: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>>;
183
- head: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>>;
184
- options: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>>;
178
+ get: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>>;
179
+ post: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>>;
180
+ put: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>>;
181
+ patch: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>>;
182
+ delete: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>>;
183
+ head: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>>;
184
+ options: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>>;
185
185
  through: <NextContext>(through: (context: ContextTo) => Promise<NextContext>) => Router<ContextFrom, NextContext, R>;
186
186
  }
187
187
 
@@ -228,7 +228,7 @@ type HandlerConfig<ContextFrom> = {
228
228
  * }
229
229
  * ```
230
230
  */
231
- before?: (req: Request) => Promise<Response | void | undefined>;
231
+ before?: (req: Request) => Promise<Response | void | undefined> | Response | void | undefined;
232
232
  /**
233
233
  * Transforms the response before it is sent to the client. Very useful for settings headers like CORS.
234
234
  *
@@ -248,8 +248,8 @@ type HandlerConfig<ContextFrom> = {
248
248
  * }
249
249
  * ```
250
250
  */
251
- transform?: (req: Request, res: Response) => Promise<Response | void | undefined>;
251
+ transform?: (req: Request, res: Response) => Promise<Response | void | undefined> | Response | void | undefined;
252
252
  };
253
253
  declare function createKaitoHandler<Context>(config: HandlerConfig<Context>): (request: Request) => Promise<Response>;
254
254
 
255
- export { type APIResponse, type AnyQueryDefinition, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type HandlerConfig, type InferParsable, type InferRoutes, KaitoError, type KaitoMethod, KaitoRequest, type MakeOptional, type Parsable, type Route, type RouteArgument, Router, type RouterState, type SuccessfulAPIResponse, type Through, WrappedError, createKaitoHandler, createUtilities, isNodeLikeDev, parsable };
255
+ export { type APIResponse, type AnyQueryDefinition, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type HandlerConfig, type InferParsable, type InferRoutes, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MakeOptional, type Parsable, type Route, type RouteArgument, Router, type RouterState, type SuccessfulAPIResponse, type Through, WrappedError, createKaitoHandler, createUtilities, isNodeLikeDev, parsable };
package/dist/index.d.ts CHANGED
@@ -154,7 +154,7 @@ type Route<ContextFrom, ContextTo, Result, Path extends string, Method extends K
154
154
  method: Method;
155
155
  run(arg: RouteArgument<Path, ContextTo, {
156
156
  [Key in keyof Query]: InferParsable<Query[Key]>['output'];
157
- }, InferParsable<Body>['output']>): Promise<Result>;
157
+ }, InferParsable<Body>['output']>): Promise<Result> | Result;
158
158
  };
159
159
  type AnyRoute<ContextFrom = any, ContextTo = any> = Route<ContextFrom, ContextTo, any, any, any, AnyQueryDefinition, any>;
160
160
 
@@ -175,13 +175,13 @@ declare class Router<ContextFrom, ContextTo, R extends AnyRoute> {
175
175
  readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends AnyRoute>(pathPrefix: PathPrefix, other: Router<ContextFrom, unknown, OtherRoutes>) => Router<ContextFrom, ContextTo, Extract<R | PrefixRoutesPath<PathPrefix, OtherRoutes>, AnyRoute>>;
176
176
  freeze: (server: Omit<HandlerConfig<ContextFrom>, "router">) => (req: Request) => Promise<Response>;
177
177
  private readonly method;
178
- get: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>>;
179
- post: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>>;
180
- put: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>>;
181
- patch: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>>;
182
- delete: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>>;
183
- head: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>>;
184
- options: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>>;
178
+ get: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "GET", Query, Body>>;
179
+ post: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "POST", Query, Body>>;
180
+ put: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, Body>>;
181
+ patch: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, Body>>;
182
+ delete: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, Body>>;
183
+ head: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, Body>>;
184
+ options: <Result, Path extends string, Query extends AnyQueryDefinition = {}, Body extends Parsable = never>(path: Path, route: ((arg: RouteArgument<Path, ContextTo, { [Key in keyof Query]: InferParsable<Query[Key]>["output"]; }, InferParsable<Body>["output"]>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, Body>>;
185
185
  through: <NextContext>(through: (context: ContextTo) => Promise<NextContext>) => Router<ContextFrom, NextContext, R>;
186
186
  }
187
187
 
@@ -228,7 +228,7 @@ type HandlerConfig<ContextFrom> = {
228
228
  * }
229
229
  * ```
230
230
  */
231
- before?: (req: Request) => Promise<Response | void | undefined>;
231
+ before?: (req: Request) => Promise<Response | void | undefined> | Response | void | undefined;
232
232
  /**
233
233
  * Transforms the response before it is sent to the client. Very useful for settings headers like CORS.
234
234
  *
@@ -248,8 +248,8 @@ type HandlerConfig<ContextFrom> = {
248
248
  * }
249
249
  * ```
250
250
  */
251
- transform?: (req: Request, res: Response) => Promise<Response | void | undefined>;
251
+ transform?: (req: Request, res: Response) => Promise<Response | void | undefined> | Response | void | undefined;
252
252
  };
253
253
  declare function createKaitoHandler<Context>(config: HandlerConfig<Context>): (request: Request) => Promise<Response>;
254
254
 
255
- export { type APIResponse, type AnyQueryDefinition, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type HandlerConfig, type InferParsable, type InferRoutes, KaitoError, type KaitoMethod, KaitoRequest, type MakeOptional, type Parsable, type Route, type RouteArgument, Router, type RouterState, type SuccessfulAPIResponse, type Through, WrappedError, createKaitoHandler, createUtilities, isNodeLikeDev, parsable };
255
+ export { type APIResponse, type AnyQueryDefinition, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type HandlerConfig, type InferParsable, type InferRoutes, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MakeOptional, type Parsable, type Route, type RouteArgument, Router, type RouterState, type SuccessfulAPIResponse, type Through, WrappedError, createKaitoHandler, createUtilities, isNodeLikeDev, parsable };
package/dist/index.js CHANGED
@@ -44,44 +44,6 @@ function createKaitoHandler(config) {
44
44
  };
45
45
  }
46
46
 
47
- // src/request.ts
48
- var KaitoRequest = class {
49
- url;
50
- _request;
51
- constructor(url, request) {
52
- this._request = request;
53
- this.url = url;
54
- }
55
- get headers() {
56
- return this._request.headers;
57
- }
58
- get method() {
59
- return this._request.method;
60
- }
61
- async arrayBuffer() {
62
- return this._request.arrayBuffer();
63
- }
64
- async blob() {
65
- return this._request.blob();
66
- }
67
- async formData() {
68
- return this._request.formData();
69
- }
70
- async bytes() {
71
- const buffer = await this.arrayBuffer();
72
- return new Uint8Array(buffer);
73
- }
74
- async json() {
75
- return this._request.json();
76
- }
77
- async text() {
78
- return this._request.text();
79
- }
80
- get request() {
81
- return this._request;
82
- }
83
- };
84
-
85
47
  // src/head.ts
86
48
  var KaitoHead = class {
87
49
  _headers;
@@ -125,6 +87,44 @@ var KaitoHead = class {
125
87
  }
126
88
  };
127
89
 
90
+ // src/request.ts
91
+ var KaitoRequest = class {
92
+ url;
93
+ _request;
94
+ constructor(url, request) {
95
+ this._request = request;
96
+ this.url = url;
97
+ }
98
+ get headers() {
99
+ return this._request.headers;
100
+ }
101
+ get method() {
102
+ return this._request.method;
103
+ }
104
+ async arrayBuffer() {
105
+ return this._request.arrayBuffer();
106
+ }
107
+ async blob() {
108
+ return this._request.blob();
109
+ }
110
+ async formData() {
111
+ return this._request.formData();
112
+ }
113
+ async bytes() {
114
+ const buffer = await this.arrayBuffer();
115
+ return new Uint8Array(buffer);
116
+ }
117
+ async json() {
118
+ return this._request.json();
119
+ }
120
+ async text() {
121
+ return this._request.text();
122
+ }
123
+ get request() {
124
+ return this._request;
125
+ }
126
+ };
127
+
128
128
  // src/util.ts
129
129
  var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "undefined" && process.env.NODE_ENV === "development";
130
130
  function createUtilities(getContext) {
@@ -300,6 +300,7 @@ var Router = class _Router {
300
300
  };
301
301
  export {
302
302
  KaitoError,
303
+ KaitoHead,
303
304
  KaitoRequest,
304
305
  Router,
305
306
  WrappedError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaito-http/core",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "type": "module",
5
5
  "author": "Alistair Smith <hi@alistair.sh>",
6
6
  "description": "Functional HTTP Framework for TypeScript",
@@ -12,18 +12,16 @@
12
12
  "exports": {
13
13
  "./package.json": "./package.json",
14
14
  ".": {
15
- "import": {
16
- "types": "./src/index.ts",
17
- "default": "./dist/index.js"
18
- },
15
+ "import": "./dist/index.js",
19
16
  "require": "./dist/index.cjs"
20
17
  },
21
18
  "./stream": {
22
- "import": {
23
- "types": "./src/stream/stream.ts",
24
- "default": "./dist/stream/stream.js"
25
- },
19
+ "import": "./dist/stream/stream.js",
26
20
  "require": "./dist/stream/stream.cjs"
21
+ },
22
+ "./cors": {
23
+ "import": "./dist/cors/cors.js",
24
+ "require": "./dist/cors/cors.cjs"
27
25
  }
28
26
  },
29
27
  "homepage": "https://github.com/kaito-http/kaito",
@@ -43,8 +41,7 @@
43
41
  "files": [
44
42
  "package.json",
45
43
  "README.md",
46
- "dist",
47
- "src"
44
+ "dist"
48
45
  ],
49
46
  "bugs": {
50
47
  "url": "https://github.com/kaito-http/kaito/issues"
package/src/error.ts DELETED
@@ -1,26 +0,0 @@
1
- export class WrappedError<T> extends Error {
2
- public static maybe<T>(maybeError: T) {
3
- if (maybeError instanceof Error) {
4
- return maybeError;
5
- }
6
-
7
- return WrappedError.from(maybeError);
8
- }
9
-
10
- public static from<T>(data: T) {
11
- return new WrappedError(data);
12
- }
13
-
14
- private constructor(public readonly data: T) {
15
- super('Something was thrown, but it was not an instance of Error, so a WrappedError was created.');
16
- }
17
- }
18
-
19
- export class KaitoError extends Error {
20
- constructor(
21
- public readonly status: number,
22
- message: string,
23
- ) {
24
- super(message);
25
- }
26
- }