@kizunajs/fetch 2.0.0-beta.13 → 2.0.0-beta.14

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/index.d.cts CHANGED
@@ -1,166 +1,57 @@
1
- import { $brand, z } from "zod";
2
- import { AUTO_GUARD_BRAND, AUTO_RESPONSES_BRAND, ExtractPathParams, HasPathParams, Method, ProblemDetails, RequestContextHeaderInputs, RouteDefinition, RoutePath, Routes, StreamMessageOf, StreamResponseDefinition, ValidationError, ValidationError as ValidationError$1, ValidationErrorFor, isProblemDetails, isValidationError } from "kizunajs";
1
+ import * as _$kizunajs from "kizunajs";
2
+ import { ApiDefinition } from "kizunajs";
3
+ import { z } from "zod";
4
+ import { GeneratedFileOptions } from "kizunajs/generator";
3
5
 
4
- //#region src/client.d.ts
5
- type ResponseUnion<R extends RouteDefinition> = { [S in keyof R['responses']]: {
6
- status: S extends number ? S : never;
7
- body: R['responses'][S] extends z.ZodType ? z.infer<R['responses'][S]> : R['responses'][S] extends StreamResponseDefinition ? AsyncIterable<StreamMessageOf<R['responses'][S]>> : R['responses'][S] extends {
8
- body: z.ZodType;
9
- } ? z.infer<R['responses'][S]['body']> : never;
10
- headers: R['responses'][S] extends {
11
- headers: z.ZodType;
12
- } ? z.infer<R['responses'][S]['headers']> : Record<string, string>;
13
- } }[keyof R['responses']];
14
- /**
15
- * The type a caller passes for a body, query, or headers argument, the schema's
16
- * input type.
17
- */
18
- type ClientPayload<T extends z.ZodType> = z.input<T>;
19
- /**
20
- * Path params are typed from the route's `pathParams` schema output when one is declared,
21
- * mirroring the server-side `HandlerArgs`. This makes refinements like `.brand()` flow to
22
- * the caller. Falls back to the path template (`:param` → `string`).
23
- */
24
- type ClientParams<R extends RouteDefinition> = R extends {
25
- pathParams: z.ZodType;
26
- } ? z.output<R['pathParams']> : ExtractPathParams<R['path']>;
27
- /**
28
- * True when the arg can be omitted: its input accepts `{}` or `undefined`.
29
- */
30
- type IsOmittable<Payload> = {} extends Payload ? true : [undefined] extends [Payload] ? true : false;
31
- /**
32
- * Everything a caller passes for one route: `params`, `body`, `query`, `headers`,
33
- * and `fetchOptions`. Each key is present only when the route declares it, and
34
- * optional when its input accepts `{}` or `undefined`.
35
- */
36
- type ClientArgs<R extends RouteDefinition> = (HasPathParams<R['path']> extends true ? {
37
- params: ClientParams<R>;
38
- } : {}) & (R extends {
39
- body: z.ZodType;
40
- } ? (ClientPayload<R['body']> extends void ? {} : {
41
- body: ClientPayload<R['body']>;
42
- }) : {}) & (R extends {
43
- query: z.ZodType;
44
- } ? IsOmittable<ClientPayload<R['query']>> extends true ? {
45
- query?: ClientPayload<R['query']>;
46
- } : {
47
- query: ClientPayload<R['query']>;
48
- } : {}) & (R extends {
49
- headers: z.ZodType;
50
- } ? IsOmittable<ClientPayload<R['headers']>> extends true ? {
51
- headers?: ClientPayload<R['headers']>;
52
- } : {
53
- headers: ClientPayload<R['headers']>;
54
- } : {
55
- headers?: Record<string, string>;
56
- }) & {
57
- fetchOptions?: RequestInit;
58
- };
59
- type ValidationErrorResult<Codes extends string> = {
60
- status: 400;
61
- body: [Codes] extends [never] ? ValidationError$1 : ValidationErrorFor<Codes>;
62
- headers: Record<string, string>;
63
- };
64
- type HasValidation<R extends RouteDefinition> = R extends {
65
- body: z.ZodType;
66
- } ? true : R extends {
67
- query: z.ZodType;
68
- } ? true : false;
69
- /**
70
- * The statuses the api's auth map put on a guarded route. A declared `403`
71
- * sits alongside, as a declared `400` does beside the validation error.
72
- */
73
- type AutoStatuses<R extends RouteDefinition> = Extract<typeof AUTO_RESPONSES_BRAND extends keyof R ? NonNullable<R[typeof AUTO_RESPONSES_BRAND]> : never, number>;
74
- /**
75
- * The api's `guardSchema`, or plain Problem Details when it declares none.
76
- */
77
- type AutoBody<R extends RouteDefinition> = typeof AUTO_GUARD_BRAND extends keyof R ? NonNullable<R[typeof AUTO_GUARD_BRAND]> : ProblemDetails;
78
- type AutoErrorResult<R extends RouteDefinition> = { [Status in AutoStatuses<R>]: {
79
- status: Status;
80
- body: AutoBody<R>;
81
- headers: Record<string, string>;
82
- } }[AutoStatuses<R>];
83
- /**
84
- * Every response one route can produce, as a union discriminated on `status`.
85
- * Routes with a `body` or `query` schema also carry the automatic `400`, and
86
- * guarded routes the `401` and `403`.
87
- */
88
- type ClientResponse<R extends RouteDefinition, Codes extends string = never> = (HasValidation<R> extends true ? ResponseUnion<R> | ValidationErrorResult<Codes> : ResponseUnion<R>) | AutoErrorResult<R>;
89
- type ClientFn<R extends RouteDefinition, Codes extends string> = {} extends ClientArgs<R> ? (args?: ClientArgs<R>) => Promise<ClientResponse<R, Codes>> : (args: ClientArgs<R>) => Promise<ClientResponse<R, Codes>>;
90
- type Client<T extends Routes, Codes extends string = never> = { [K in keyof T]: T[K] extends RouteDefinition ? ClientFn<T[K], Codes> : T[K] extends Routes ? Client<T[K], Codes> : never };
91
- type UnionToIntersection<Union> = (Union extends unknown ? (distributed: Union) => void : never) extends ((intersected: infer Intersection) => void) ? Intersection : never;
92
- /**
93
- * Every header input the api's request context declares, flattened. Set
94
- * once on the client under `requestContext` and sent with every request.
95
- */
96
- type ContextHeaderInputs<Declarations> = string extends keyof Declarations ? {} : [keyof Declarations] extends [never] ? {} : UnionToIntersection<{ [Name in keyof Declarations]: RequestContextHeaderInputs<Declarations[Name]> }[keyof Declarations]> extends infer Merged ? { [Key in keyof Merged]: Merged[Key] } : never;
97
- /**
98
- * The request `onRequest` is handed, on its way out. Setting a header on
99
- * `headers` sets it on the request that is sent.
100
- */
101
- interface OutgoingRequest {
102
- url: string;
103
- method: string;
104
- headers: Headers;
105
- route: RouteDefinition;
6
+ //#region src/generator.d.ts
7
+ /**
8
+ * What a generated client is called, and what its header says.
9
+ */
10
+ interface FetchClientOptions {
11
+ /**
12
+ * Name of the generated namespace holding every model and every route's
13
+ * `Params`, `Query`, `Body`, `Headers` and `Result`.
14
+ *
15
+ * @default 'API'
16
+ */
17
+ namespace?: string;
18
+ /**
19
+ * Command the file's header tells a reader to run.
20
+ *
21
+ * @default 'kizuna generate'
22
+ */
23
+ regenerateCommand?: string;
24
+ /**
25
+ * Path to the api this was generated from, relative to the generated
26
+ * file. Named in the header so tooling can tell when the client has fallen
27
+ * behind, and so a reader knows where the API is declared.
28
+ */
29
+ source?: string;
106
30
  }
107
- interface ClientConfig {
108
- baseUrl: string;
109
- baseHeaders?: Record<string, string>;
110
- credentials?: RequestCredentials;
111
- fetch?: typeof fetch;
112
- onRequest?: (request: OutgoingRequest) => void | Promise<void>;
113
- }
114
- /**
115
- * Registry-global: what a client method was built from. Anything wrapping a
116
- * client reads the route here rather than being handed the api a second time.
117
- */
118
- /**
119
- * One method on a generated client: the call itself, carrying the method it
120
- * sends and whether that response streams. A wrapper reads those off the client
121
- * rather than being handed the routes a second time.
122
- */
123
- type ClientMethod<Method extends string, Streams extends boolean, Args, Result> = ({} extends Args ? (args?: Args) => Promise<Result> : (args: Args) => Promise<Result>) & {
124
- readonly [CLIENT_ROUTE]?: {
125
- readonly method: Method;
126
- readonly streams: Streams;
127
- };
128
- };
129
- declare const CLIENT_ROUTE: unique symbol;
130
- /**
131
- * The route a client method answers, or `undefined` for anything that is not
132
- * one.
133
- */
134
- declare const routeOf: (value: unknown) => RouteDefinition | undefined;
135
- /**
136
- * What a generated client knows about one response: nothing for a body it
137
- * parses as JSON, the media type for one it streams.
138
- */
139
- type GeneratedResponse = Record<string, never> | {
140
- stream: {
141
- contentType?: string;
142
- };
143
- };
144
- /**
145
- * One route in a generated client's table: how to reach it, and enough about
146
- * each response to read the body.
147
- */
148
- interface GeneratedRoute {
149
- method: Method;
150
- path: RoutePath;
151
- contentType?: string;
152
- responses: Record<number, GeneratedResponse>;
153
- }
154
- /**
155
- * The route table a generated client carries, nested the way the client is.
156
- */
157
- interface GeneratedRoutes {
158
- [key: string]: GeneratedRoutes | GeneratedRoute;
159
- }
160
- /**
161
- * Builds the client tree a generated client exposes. The generated file owns the
162
- * types; this owns the requests.
163
- */
164
- declare const createGeneratedClient: (routes: GeneratedRoutes, config: ClientConfig) => Record<string, unknown>;
31
+ declare const generateFetchClient: (contract: ApiDefinition, options?: FetchClientOptions) => string;
32
+ //#endregion
33
+ //#region src/client-target.d.ts
34
+ declare const FetchClientOptionsSchema: z.ZodObject<{
35
+ namespace: z.ZodOptional<z.ZodString>;
36
+ regenerateCommand: z.ZodOptional<z.ZodString>;
37
+ source: z.ZodOptional<z.ZodString>;
38
+ }, z.core.$strip>;
39
+ /**
40
+ * Where a generated TypeScript client is written.
41
+ */
42
+ type FetchClientTargetOptions = z.input<typeof FetchClientOptionsSchema> & GeneratedFileOptions;
43
+ /**
44
+ * A TypeScript client for `kizuna.config.ts`.
45
+ *
46
+ * @example
47
+ * fetchClient({
48
+ * output: './packages/api-client/src/generated.ts',
49
+ * });
50
+ */
51
+ declare const fetchClient: (options: {
52
+ namespace?: string | undefined;
53
+ regenerateCommand?: string | undefined;
54
+ source?: string | undefined;
55
+ } & GeneratedFileOptions) => _$kizunajs.GeneratedFile;
165
56
  //#endregion
166
- export { type $brand, CLIENT_ROUTE, Client, ClientArgs, ClientConfig, ClientMethod, ClientResponse, ContextHeaderInputs, GeneratedResponse, GeneratedRoute, GeneratedRoutes, OutgoingRequest, type ValidationError, createGeneratedClient, isProblemDetails, isValidationError, routeOf };
57
+ export { type FetchClientOptions, type FetchClientTargetOptions, fetchClient, generateFetchClient };
package/dist/index.d.mts CHANGED
@@ -1,166 +1,57 @@
1
- import { AUTO_GUARD_BRAND, AUTO_RESPONSES_BRAND, ExtractPathParams, HasPathParams, Method, ProblemDetails, RequestContextHeaderInputs, RouteDefinition, RoutePath, Routes, StreamMessageOf, StreamResponseDefinition, ValidationError, ValidationError as ValidationError$1, ValidationErrorFor, isProblemDetails, isValidationError } from "kizunajs";
2
- import { $brand, z } from "zod";
1
+ import { GeneratedFileOptions } from "kizunajs/generator";
2
+ import { z } from "zod";
3
+ import * as _$kizunajs from "kizunajs";
4
+ import { ApiDefinition } from "kizunajs";
3
5
 
4
- //#region src/client.d.ts
5
- type ResponseUnion<R extends RouteDefinition> = { [S in keyof R['responses']]: {
6
- status: S extends number ? S : never;
7
- body: R['responses'][S] extends z.ZodType ? z.infer<R['responses'][S]> : R['responses'][S] extends StreamResponseDefinition ? AsyncIterable<StreamMessageOf<R['responses'][S]>> : R['responses'][S] extends {
8
- body: z.ZodType;
9
- } ? z.infer<R['responses'][S]['body']> : never;
10
- headers: R['responses'][S] extends {
11
- headers: z.ZodType;
12
- } ? z.infer<R['responses'][S]['headers']> : Record<string, string>;
13
- } }[keyof R['responses']];
14
- /**
15
- * The type a caller passes for a body, query, or headers argument, the schema's
16
- * input type.
17
- */
18
- type ClientPayload<T extends z.ZodType> = z.input<T>;
19
- /**
20
- * Path params are typed from the route's `pathParams` schema output when one is declared,
21
- * mirroring the server-side `HandlerArgs`. This makes refinements like `.brand()` flow to
22
- * the caller. Falls back to the path template (`:param` → `string`).
23
- */
24
- type ClientParams<R extends RouteDefinition> = R extends {
25
- pathParams: z.ZodType;
26
- } ? z.output<R['pathParams']> : ExtractPathParams<R['path']>;
27
- /**
28
- * True when the arg can be omitted: its input accepts `{}` or `undefined`.
29
- */
30
- type IsOmittable<Payload> = {} extends Payload ? true : [undefined] extends [Payload] ? true : false;
31
- /**
32
- * Everything a caller passes for one route: `params`, `body`, `query`, `headers`,
33
- * and `fetchOptions`. Each key is present only when the route declares it, and
34
- * optional when its input accepts `{}` or `undefined`.
35
- */
36
- type ClientArgs<R extends RouteDefinition> = (HasPathParams<R['path']> extends true ? {
37
- params: ClientParams<R>;
38
- } : {}) & (R extends {
39
- body: z.ZodType;
40
- } ? (ClientPayload<R['body']> extends void ? {} : {
41
- body: ClientPayload<R['body']>;
42
- }) : {}) & (R extends {
43
- query: z.ZodType;
44
- } ? IsOmittable<ClientPayload<R['query']>> extends true ? {
45
- query?: ClientPayload<R['query']>;
46
- } : {
47
- query: ClientPayload<R['query']>;
48
- } : {}) & (R extends {
49
- headers: z.ZodType;
50
- } ? IsOmittable<ClientPayload<R['headers']>> extends true ? {
51
- headers?: ClientPayload<R['headers']>;
52
- } : {
53
- headers: ClientPayload<R['headers']>;
54
- } : {
55
- headers?: Record<string, string>;
56
- }) & {
57
- fetchOptions?: RequestInit;
58
- };
59
- type ValidationErrorResult<Codes extends string> = {
60
- status: 400;
61
- body: [Codes] extends [never] ? ValidationError$1 : ValidationErrorFor<Codes>;
62
- headers: Record<string, string>;
63
- };
64
- type HasValidation<R extends RouteDefinition> = R extends {
65
- body: z.ZodType;
66
- } ? true : R extends {
67
- query: z.ZodType;
68
- } ? true : false;
69
- /**
70
- * The statuses the api's auth map put on a guarded route. A declared `403`
71
- * sits alongside, as a declared `400` does beside the validation error.
72
- */
73
- type AutoStatuses<R extends RouteDefinition> = Extract<typeof AUTO_RESPONSES_BRAND extends keyof R ? NonNullable<R[typeof AUTO_RESPONSES_BRAND]> : never, number>;
74
- /**
75
- * The api's `guardSchema`, or plain Problem Details when it declares none.
76
- */
77
- type AutoBody<R extends RouteDefinition> = typeof AUTO_GUARD_BRAND extends keyof R ? NonNullable<R[typeof AUTO_GUARD_BRAND]> : ProblemDetails;
78
- type AutoErrorResult<R extends RouteDefinition> = { [Status in AutoStatuses<R>]: {
79
- status: Status;
80
- body: AutoBody<R>;
81
- headers: Record<string, string>;
82
- } }[AutoStatuses<R>];
83
- /**
84
- * Every response one route can produce, as a union discriminated on `status`.
85
- * Routes with a `body` or `query` schema also carry the automatic `400`, and
86
- * guarded routes the `401` and `403`.
87
- */
88
- type ClientResponse<R extends RouteDefinition, Codes extends string = never> = (HasValidation<R> extends true ? ResponseUnion<R> | ValidationErrorResult<Codes> : ResponseUnion<R>) | AutoErrorResult<R>;
89
- type ClientFn<R extends RouteDefinition, Codes extends string> = {} extends ClientArgs<R> ? (args?: ClientArgs<R>) => Promise<ClientResponse<R, Codes>> : (args: ClientArgs<R>) => Promise<ClientResponse<R, Codes>>;
90
- type Client<T extends Routes, Codes extends string = never> = { [K in keyof T]: T[K] extends RouteDefinition ? ClientFn<T[K], Codes> : T[K] extends Routes ? Client<T[K], Codes> : never };
91
- type UnionToIntersection<Union> = (Union extends unknown ? (distributed: Union) => void : never) extends ((intersected: infer Intersection) => void) ? Intersection : never;
92
- /**
93
- * Every header input the api's request context declares, flattened. Set
94
- * once on the client under `requestContext` and sent with every request.
95
- */
96
- type ContextHeaderInputs<Declarations> = string extends keyof Declarations ? {} : [keyof Declarations] extends [never] ? {} : UnionToIntersection<{ [Name in keyof Declarations]: RequestContextHeaderInputs<Declarations[Name]> }[keyof Declarations]> extends infer Merged ? { [Key in keyof Merged]: Merged[Key] } : never;
97
- /**
98
- * The request `onRequest` is handed, on its way out. Setting a header on
99
- * `headers` sets it on the request that is sent.
100
- */
101
- interface OutgoingRequest {
102
- url: string;
103
- method: string;
104
- headers: Headers;
105
- route: RouteDefinition;
6
+ //#region src/generator.d.ts
7
+ /**
8
+ * What a generated client is called, and what its header says.
9
+ */
10
+ interface FetchClientOptions {
11
+ /**
12
+ * Name of the generated namespace holding every model and every route's
13
+ * `Params`, `Query`, `Body`, `Headers` and `Result`.
14
+ *
15
+ * @default 'API'
16
+ */
17
+ namespace?: string;
18
+ /**
19
+ * Command the file's header tells a reader to run.
20
+ *
21
+ * @default 'kizuna generate'
22
+ */
23
+ regenerateCommand?: string;
24
+ /**
25
+ * Path to the api this was generated from, relative to the generated
26
+ * file. Named in the header so tooling can tell when the client has fallen
27
+ * behind, and so a reader knows where the API is declared.
28
+ */
29
+ source?: string;
106
30
  }
107
- interface ClientConfig {
108
- baseUrl: string;
109
- baseHeaders?: Record<string, string>;
110
- credentials?: RequestCredentials;
111
- fetch?: typeof fetch;
112
- onRequest?: (request: OutgoingRequest) => void | Promise<void>;
113
- }
114
- /**
115
- * Registry-global: what a client method was built from. Anything wrapping a
116
- * client reads the route here rather than being handed the api a second time.
117
- */
118
- /**
119
- * One method on a generated client: the call itself, carrying the method it
120
- * sends and whether that response streams. A wrapper reads those off the client
121
- * rather than being handed the routes a second time.
122
- */
123
- type ClientMethod<Method extends string, Streams extends boolean, Args, Result> = ({} extends Args ? (args?: Args) => Promise<Result> : (args: Args) => Promise<Result>) & {
124
- readonly [CLIENT_ROUTE]?: {
125
- readonly method: Method;
126
- readonly streams: Streams;
127
- };
128
- };
129
- declare const CLIENT_ROUTE: unique symbol;
130
- /**
131
- * The route a client method answers, or `undefined` for anything that is not
132
- * one.
133
- */
134
- declare const routeOf: (value: unknown) => RouteDefinition | undefined;
135
- /**
136
- * What a generated client knows about one response: nothing for a body it
137
- * parses as JSON, the media type for one it streams.
138
- */
139
- type GeneratedResponse = Record<string, never> | {
140
- stream: {
141
- contentType?: string;
142
- };
143
- };
144
- /**
145
- * One route in a generated client's table: how to reach it, and enough about
146
- * each response to read the body.
147
- */
148
- interface GeneratedRoute {
149
- method: Method;
150
- path: RoutePath;
151
- contentType?: string;
152
- responses: Record<number, GeneratedResponse>;
153
- }
154
- /**
155
- * The route table a generated client carries, nested the way the client is.
156
- */
157
- interface GeneratedRoutes {
158
- [key: string]: GeneratedRoutes | GeneratedRoute;
159
- }
160
- /**
161
- * Builds the client tree a generated client exposes. The generated file owns the
162
- * types; this owns the requests.
163
- */
164
- declare const createGeneratedClient: (routes: GeneratedRoutes, config: ClientConfig) => Record<string, unknown>;
31
+ declare const generateFetchClient: (contract: ApiDefinition, options?: FetchClientOptions) => string;
32
+ //#endregion
33
+ //#region src/client-target.d.ts
34
+ declare const FetchClientOptionsSchema: z.ZodObject<{
35
+ namespace: z.ZodOptional<z.ZodString>;
36
+ regenerateCommand: z.ZodOptional<z.ZodString>;
37
+ source: z.ZodOptional<z.ZodString>;
38
+ }, z.core.$strip>;
39
+ /**
40
+ * Where a generated TypeScript client is written.
41
+ */
42
+ type FetchClientTargetOptions = z.input<typeof FetchClientOptionsSchema> & GeneratedFileOptions;
43
+ /**
44
+ * A TypeScript client for `kizuna.config.ts`.
45
+ *
46
+ * @example
47
+ * fetchClient({
48
+ * output: './packages/api-client/src/generated.ts',
49
+ * });
50
+ */
51
+ declare const fetchClient: (options: {
52
+ namespace?: string | undefined;
53
+ regenerateCommand?: string | undefined;
54
+ source?: string | undefined;
55
+ } & GeneratedFileOptions) => _$kizunajs.GeneratedFile;
165
56
  //#endregion
166
- export { type $brand, CLIENT_ROUTE, Client, ClientArgs, ClientConfig, ClientMethod, ClientResponse, ContextHeaderInputs, GeneratedResponse, GeneratedRoute, GeneratedRoutes, OutgoingRequest, type ValidationError, createGeneratedClient, isProblemDetails, isValidationError, routeOf };
57
+ export { type FetchClientOptions, type FetchClientTargetOptions, fetchClient, generateFetchClient };