@kaito-http/core 4.0.0-beta.17 → 4.0.0-beta.19
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/{chunk-ISGKY24N.js → chunk-TL3E52YN.js} +12 -0
- package/dist/index.cjs +24 -0
- package/dist/index.d.cts +19 -19
- package/dist/index.d.ts +19 -19
- package/dist/index.js +13 -1
- package/dist/schema/schema.cjs +12 -0
- package/dist/schema/schema.d.cts +4 -0
- package/dist/schema/schema.d.ts +4 -0
- package/dist/schema/schema.js +1 -1
- package/package.json +1 -1
|
@@ -708,6 +708,18 @@ var KScalar = class _KScalar extends BaseSchema {
|
|
|
708
708
|
}
|
|
709
709
|
});
|
|
710
710
|
}
|
|
711
|
+
example(example) {
|
|
712
|
+
if (example === void 0) {
|
|
713
|
+
return this.def.example;
|
|
714
|
+
}
|
|
715
|
+
return this.clone({ example });
|
|
716
|
+
}
|
|
717
|
+
description(description) {
|
|
718
|
+
if (description === void 0) {
|
|
719
|
+
return this.def.schema.description();
|
|
720
|
+
}
|
|
721
|
+
return this.clone({ description });
|
|
722
|
+
}
|
|
711
723
|
parse(json) {
|
|
712
724
|
const result = this.parseSafe(json);
|
|
713
725
|
if (!result.success) {
|
package/dist/index.cjs
CHANGED
|
@@ -866,6 +866,18 @@ var KScalar = class _KScalar extends BaseSchema {
|
|
|
866
866
|
}
|
|
867
867
|
});
|
|
868
868
|
}
|
|
869
|
+
example(example) {
|
|
870
|
+
if (example === void 0) {
|
|
871
|
+
return this.def.example;
|
|
872
|
+
}
|
|
873
|
+
return this.clone({ example });
|
|
874
|
+
}
|
|
875
|
+
description(description) {
|
|
876
|
+
if (description === void 0) {
|
|
877
|
+
return this.def.schema.description();
|
|
878
|
+
}
|
|
879
|
+
return this.clone({ description });
|
|
880
|
+
}
|
|
869
881
|
parse(json) {
|
|
870
882
|
const result = this.parseSafe(json);
|
|
871
883
|
if (!result.success) {
|
|
@@ -1083,6 +1095,18 @@ var Router = class _Router {
|
|
|
1083
1095
|
}
|
|
1084
1096
|
return result;
|
|
1085
1097
|
}
|
|
1098
|
+
if (route.openapi?.schema) {
|
|
1099
|
+
if (route.openapi.type !== "json") {
|
|
1100
|
+
throw new Error(
|
|
1101
|
+
`Cannot use openapi schema for ${route.method} ${route.path} because it is not a json output type`
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
const parsed = route.openapi.schema.serialize(result);
|
|
1105
|
+
return head.toResponse({
|
|
1106
|
+
success: true,
|
|
1107
|
+
data: parsed
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1086
1110
|
return head.toResponse({
|
|
1087
1111
|
success: true,
|
|
1088
1112
|
data: result
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as OpenAPI from 'openapi3-ts/oas31';
|
|
2
|
-
import { JSONValue, AnySchemaFor } from './schema/schema.cjs';
|
|
3
|
-
export { ArrayChecks, ArrayDef,
|
|
2
|
+
import { JSONValue, AnySchemaFor, BaseSchema, BaseSchemaDef } from './schema/schema.cjs';
|
|
3
|
+
export { ArrayChecks, ArrayDef, BooleanDef, Issue, JSONPrimitive, KArray, KBoolean, KLiteral, KNull, KNumber, KObject, KObjectFromURLSearchParams, KRef, KScalar, KString, KUnion, LiteralDef, NullDef, NumberChecks, NumberDef, NumberFormat, ObjectDef, ParseContext, ParseResult, RefDef, STRING_FORMAT_REGEXES, ScalarDef, ScalarOptions, SchemaError, StringChecks, StringDef, StringFormat, UnionDef, isPrimitiveJSONValue, k } from './schema/schema.cjs';
|
|
4
4
|
import { KaitoSSEResponse } from './stream/stream.cjs';
|
|
5
5
|
|
|
6
6
|
declare class WrappedError<T> extends Error {
|
|
@@ -74,7 +74,7 @@ declare class KaitoHead {
|
|
|
74
74
|
get touched(): boolean;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer
|
|
77
|
+
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer ResultOutput, infer ResultInput, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyOutput> ? Route<ContextFrom, ContextTo, RouterInput, ResultInput, ResultOutput, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyOutput> : never;
|
|
78
78
|
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
79
79
|
type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
|
|
80
80
|
routes: Set<Routes>;
|
|
@@ -100,15 +100,15 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, R ex
|
|
|
100
100
|
openapi: ({ info, servers, }: {
|
|
101
101
|
info: OpenAPI.InfoObject;
|
|
102
102
|
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
103
|
-
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
103
|
+
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Response, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
104
104
|
private readonly method;
|
|
105
|
-
get: <Path extends string,
|
|
106
|
-
post: <Path extends string,
|
|
107
|
-
put: <Path extends string,
|
|
108
|
-
patch: <Path extends string,
|
|
109
|
-
delete: <Path extends string,
|
|
110
|
-
head: <Path extends string,
|
|
111
|
-
options: <Path extends string,
|
|
105
|
+
readonly get: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "GET", Query, Body>, Input>;
|
|
106
|
+
readonly post: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "POST", Query, Body>, Input>;
|
|
107
|
+
readonly put: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PUT", Query, Body>, Input>;
|
|
108
|
+
readonly patch: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PATCH", Query, Body>, Input>;
|
|
109
|
+
readonly delete: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "DELETE", Query, Body>, Input>;
|
|
110
|
+
readonly head: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "HEAD", Query, Body>, Input>;
|
|
111
|
+
readonly options: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
|
|
112
112
|
through: <NextContext>(through: (context: ContextTo, params: Record<RequiredParams, string>) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -127,26 +127,26 @@ type SSEOutputSpec<Result extends JSONValue> = {
|
|
|
127
127
|
schema: AnySchemaFor<Result>;
|
|
128
128
|
description?: string;
|
|
129
129
|
};
|
|
130
|
-
type JSONOutputSpec<
|
|
130
|
+
type JSONOutputSpec<ResultInput, ResultOutput extends JSONValue> = {
|
|
131
131
|
type: 'json';
|
|
132
|
-
schema:
|
|
132
|
+
schema: BaseSchema<ResultOutput, ResultInput, BaseSchemaDef<ResultOutput>>;
|
|
133
133
|
description?: string;
|
|
134
134
|
};
|
|
135
|
-
type OutputSpec<
|
|
135
|
+
type OutputSpec<ResultInput, ResultOutput> = ResultOutput extends KaitoSSEResponse<infer R> ? SSEOutputSpec<Extract<R, JSONValue>> : JSONOutputSpec<ResultInput, Extract<ResultOutput, JSONValue>> & {
|
|
136
136
|
description?: string;
|
|
137
137
|
};
|
|
138
|
-
type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[],
|
|
138
|
+
type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], ResultInput, ResultOutput, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
|
|
139
139
|
body?: AnySchemaFor<Body>;
|
|
140
140
|
query?: {
|
|
141
141
|
[Key in keyof Query]: AnySchemaFor<Query[Key]>;
|
|
142
142
|
};
|
|
143
143
|
path: Path;
|
|
144
144
|
method: Method;
|
|
145
|
-
openapi?:
|
|
145
|
+
openapi?: OutputSpec<ResultOutput, ResultInput>;
|
|
146
146
|
router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
|
|
147
|
-
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<
|
|
147
|
+
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<ResultOutput> | ResultOutput;
|
|
148
148
|
};
|
|
149
|
-
type AnyRoute = Route<any, any, any, any, string, any, KaitoMethod, any, any>;
|
|
149
|
+
type AnyRoute = Route<any, any, any, any, any, string, any, KaitoMethod, any, any>;
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
152
|
* A helper to check if the environment is Node.js-like and the `NODE_ENV` environment variable is set to `'development'`
|
|
@@ -258,4 +258,4 @@ interface KaitoConfig<ContextFrom, Input extends readonly unknown[]> {
|
|
|
258
258
|
*/
|
|
259
259
|
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
260
260
|
|
|
261
|
-
export { type APIResponse, type AnyQuery, type AnyRoute, AnySchemaFor, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
|
261
|
+
export { type APIResponse, type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, BaseSchemaDef, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as OpenAPI from 'openapi3-ts/oas31';
|
|
2
|
-
import { JSONValue, AnySchemaFor } from './schema/schema.js';
|
|
3
|
-
export { ArrayChecks, ArrayDef,
|
|
2
|
+
import { JSONValue, AnySchemaFor, BaseSchema, BaseSchemaDef } from './schema/schema.js';
|
|
3
|
+
export { ArrayChecks, ArrayDef, BooleanDef, Issue, JSONPrimitive, KArray, KBoolean, KLiteral, KNull, KNumber, KObject, KObjectFromURLSearchParams, KRef, KScalar, KString, KUnion, LiteralDef, NullDef, NumberChecks, NumberDef, NumberFormat, ObjectDef, ParseContext, ParseResult, RefDef, STRING_FORMAT_REGEXES, ScalarDef, ScalarOptions, SchemaError, StringChecks, StringDef, StringFormat, UnionDef, isPrimitiveJSONValue, k } from './schema/schema.js';
|
|
4
4
|
import { KaitoSSEResponse } from './stream/stream.js';
|
|
5
5
|
|
|
6
6
|
declare class WrappedError<T> extends Error {
|
|
@@ -74,7 +74,7 @@ declare class KaitoHead {
|
|
|
74
74
|
get touched(): boolean;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer
|
|
77
|
+
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer ResultOutput, infer ResultInput, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyOutput> ? Route<ContextFrom, ContextTo, RouterInput, ResultInput, ResultOutput, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyOutput> : never;
|
|
78
78
|
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
79
79
|
type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
|
|
80
80
|
routes: Set<Routes>;
|
|
@@ -100,15 +100,15 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, R ex
|
|
|
100
100
|
openapi: ({ info, servers, }: {
|
|
101
101
|
info: OpenAPI.InfoObject;
|
|
102
102
|
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
103
|
-
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
103
|
+
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Response, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
104
104
|
private readonly method;
|
|
105
|
-
get: <Path extends string,
|
|
106
|
-
post: <Path extends string,
|
|
107
|
-
put: <Path extends string,
|
|
108
|
-
patch: <Path extends string,
|
|
109
|
-
delete: <Path extends string,
|
|
110
|
-
head: <Path extends string,
|
|
111
|
-
options: <Path extends string,
|
|
105
|
+
readonly get: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "GET", Query, Body>, Input>;
|
|
106
|
+
readonly post: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "POST", Query, Body>, Input>;
|
|
107
|
+
readonly put: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PUT", Query, Body>, Input>;
|
|
108
|
+
readonly patch: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "PATCH", Query, Body>, Input>;
|
|
109
|
+
readonly delete: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "DELETE", Query, Body>, Input>;
|
|
110
|
+
readonly head: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "HEAD", Query, Body>, Input>;
|
|
111
|
+
readonly options: <Path extends string, ResultOutput, ResultInput = ResultOutput, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | Omit<Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, ResultInput, ResultOutput, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
|
|
112
112
|
through: <NextContext>(through: (context: ContextTo, params: Record<RequiredParams, string>) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -127,26 +127,26 @@ type SSEOutputSpec<Result extends JSONValue> = {
|
|
|
127
127
|
schema: AnySchemaFor<Result>;
|
|
128
128
|
description?: string;
|
|
129
129
|
};
|
|
130
|
-
type JSONOutputSpec<
|
|
130
|
+
type JSONOutputSpec<ResultInput, ResultOutput extends JSONValue> = {
|
|
131
131
|
type: 'json';
|
|
132
|
-
schema:
|
|
132
|
+
schema: BaseSchema<ResultOutput, ResultInput, BaseSchemaDef<ResultOutput>>;
|
|
133
133
|
description?: string;
|
|
134
134
|
};
|
|
135
|
-
type OutputSpec<
|
|
135
|
+
type OutputSpec<ResultInput, ResultOutput> = ResultOutput extends KaitoSSEResponse<infer R> ? SSEOutputSpec<Extract<R, JSONValue>> : JSONOutputSpec<ResultInput, Extract<ResultOutput, JSONValue>> & {
|
|
136
136
|
description?: string;
|
|
137
137
|
};
|
|
138
|
-
type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[],
|
|
138
|
+
type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], ResultInput, ResultOutput, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
|
|
139
139
|
body?: AnySchemaFor<Body>;
|
|
140
140
|
query?: {
|
|
141
141
|
[Key in keyof Query]: AnySchemaFor<Query[Key]>;
|
|
142
142
|
};
|
|
143
143
|
path: Path;
|
|
144
144
|
method: Method;
|
|
145
|
-
openapi?:
|
|
145
|
+
openapi?: OutputSpec<ResultOutput, ResultInput>;
|
|
146
146
|
router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
|
|
147
|
-
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<
|
|
147
|
+
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<ResultOutput> | ResultOutput;
|
|
148
148
|
};
|
|
149
|
-
type AnyRoute = Route<any, any, any, any, string, any, KaitoMethod, any, any>;
|
|
149
|
+
type AnyRoute = Route<any, any, any, any, any, string, any, KaitoMethod, any, any>;
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
152
|
* A helper to check if the environment is Node.js-like and the `NODE_ENV` environment variable is set to `'development'`
|
|
@@ -258,4 +258,4 @@ interface KaitoConfig<ContextFrom, Input extends readonly unknown[]> {
|
|
|
258
258
|
*/
|
|
259
259
|
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
260
260
|
|
|
261
|
-
export { type APIResponse, type AnyQuery, type AnyRoute, AnySchemaFor, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
|
261
|
+
export { type APIResponse, type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, BaseSchemaDef, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
SchemaError,
|
|
17
17
|
isPrimitiveJSONValue,
|
|
18
18
|
k
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-TL3E52YN.js";
|
|
20
20
|
|
|
21
21
|
// src/router/router.ts
|
|
22
22
|
import "openapi3-ts/oas31";
|
|
@@ -250,6 +250,18 @@ var Router = class _Router {
|
|
|
250
250
|
}
|
|
251
251
|
return result;
|
|
252
252
|
}
|
|
253
|
+
if (route.openapi?.schema) {
|
|
254
|
+
if (route.openapi.type !== "json") {
|
|
255
|
+
throw new Error(
|
|
256
|
+
`Cannot use openapi schema for ${route.method} ${route.path} because it is not a json output type`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
const parsed = route.openapi.schema.serialize(result);
|
|
260
|
+
return head.toResponse({
|
|
261
|
+
success: true,
|
|
262
|
+
data: parsed
|
|
263
|
+
});
|
|
264
|
+
}
|
|
253
265
|
return head.toResponse({
|
|
254
266
|
success: true,
|
|
255
267
|
data: result
|
package/dist/schema/schema.cjs
CHANGED
|
@@ -748,6 +748,18 @@ var KScalar = class _KScalar extends BaseSchema {
|
|
|
748
748
|
}
|
|
749
749
|
});
|
|
750
750
|
}
|
|
751
|
+
example(example) {
|
|
752
|
+
if (example === void 0) {
|
|
753
|
+
return this.def.example;
|
|
754
|
+
}
|
|
755
|
+
return this.clone({ example });
|
|
756
|
+
}
|
|
757
|
+
description(description) {
|
|
758
|
+
if (description === void 0) {
|
|
759
|
+
return this.def.schema.description();
|
|
760
|
+
}
|
|
761
|
+
return this.clone({ description });
|
|
762
|
+
}
|
|
751
763
|
parse(json) {
|
|
752
764
|
const result = this.parseSafe(json);
|
|
753
765
|
if (!result.success) {
|
package/dist/schema/schema.d.cts
CHANGED
|
@@ -282,6 +282,10 @@ declare class KScalar<ClientRepresentation extends JSONPrimitive, ServerRepresen
|
|
|
282
282
|
serialize(value: ServerRepresentation): ClientRepresentation;
|
|
283
283
|
toOpenAPI(): SchemaObject | ReferenceObject;
|
|
284
284
|
parseSafe(json: unknown): ParseResult<ServerRepresentation>;
|
|
285
|
+
example(example: ClientRepresentation): this;
|
|
286
|
+
example(): ClientRepresentation | undefined;
|
|
287
|
+
description(description: string): this;
|
|
288
|
+
description(): string | undefined;
|
|
285
289
|
parse(json: unknown): ServerRepresentation;
|
|
286
290
|
}
|
|
287
291
|
interface UnionDef<Input extends JSONValue, Output> extends BaseSchemaDef<Input> {
|
package/dist/schema/schema.d.ts
CHANGED
|
@@ -282,6 +282,10 @@ declare class KScalar<ClientRepresentation extends JSONPrimitive, ServerRepresen
|
|
|
282
282
|
serialize(value: ServerRepresentation): ClientRepresentation;
|
|
283
283
|
toOpenAPI(): SchemaObject | ReferenceObject;
|
|
284
284
|
parseSafe(json: unknown): ParseResult<ServerRepresentation>;
|
|
285
|
+
example(example: ClientRepresentation): this;
|
|
286
|
+
example(): ClientRepresentation | undefined;
|
|
287
|
+
description(description: string): this;
|
|
288
|
+
description(): string | undefined;
|
|
285
289
|
parse(json: unknown): ServerRepresentation;
|
|
286
290
|
}
|
|
287
291
|
interface UnionDef<Input extends JSONValue, Output> extends BaseSchemaDef<Input> {
|
package/dist/schema/schema.js
CHANGED