kanagata 0.4.0 → 0.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.
package/src/client.ts CHANGED
@@ -112,7 +112,23 @@ type BodyVariant<K, T> =
112
112
  ?
113
113
  | { contentType: K; body: BodyOf<T> }
114
114
  | { contentType?: K; body: URLSearchParams }
115
- : { contentType: K; body: BodyOf<T> };
115
+ : RangeVariant<K, T>;
116
+
117
+ /**
118
+ * A media type range (`image/*`) is named as written, or as a media type it covers
119
+ * (`image/png`), which is sent. Under `*\/*` and `application/*` a covered type may be
120
+ * one the client encodes a value by (JSON, a form), so naming it takes a raw body.
121
+ */
122
+ type RangeVariant<K, T> = K extends `${infer R}/*` | `${infer R}/*;${string}`
123
+ ? R extends "*" | "application"
124
+ ?
125
+ | { contentType: K; body: BodyOf<T> }
126
+ | {
127
+ contentType: R extends "*" ? string : `application/${string}`;
128
+ body: string | RawBinary;
129
+ }
130
+ : { contentType: K | `${R}/${string}`; body: BodyOf<T> }
131
+ : { contentType: K; body: BodyOf<T> };
116
132
 
117
133
  type BodyVariants<B> = B extends { content: infer C }
118
134
  ? { [K in keyof C & string]: BodyVariant<K, C[K]> }[keyof C & string]
@@ -201,19 +217,31 @@ export type ArgsOf<
201
217
  ? [init?: Init<NeedsAll<Op<P, Path, M>>, A>]
202
218
  : [init: Init<NeedsAll<Op<P, Path, M>>, A>];
203
219
 
204
- /** "2XX" and default have no single status. */
205
- type Status<S> = S extends number ? S : number;
206
- /** Whether a status key is a success: `200`…`299` or `"2XX"`. */
207
- type IsSuccess<S> = S extends number
208
- ? `${S}` extends `2${string}`
209
- ? true
210
- : false
211
- : S extends "2XX"
212
- ? true
213
- : false;
214
-
215
- /** `ok` per status, as fetch sets it. `default` covers any undocumented status, 2xx included. */
216
- type Ok<S> = S extends "default" ? boolean : IsSuccess<S>;
220
+ type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
221
+ type ToNumber<T> = T extends `${infer N extends number}` ? N : never;
222
+ /** The statuses whose first digit is C: `Codes<4>` is `400 | 401 | … | 499`. */
223
+ type Codes<C extends number> = ToNumber<`${C}${Digit}${Digit}`>;
224
+ /**
225
+ * The statuses fetch resolves with: those of the Fetch Standard (0 to 999) but 1xx, which
226
+ * it does not return. 0 is an opaque response (`mode: "no-cors"`, or a redirect with
227
+ * `redirect: "manual"` in a browser).
228
+ */
229
+ type AnyStatus = 0 | Codes<2 | 3 | 4 | 5 | 6 | 7 | 8 | 9>;
230
+ /** The statuses a response key covers: its code, or every code of its range. */
231
+ type Covered<K> = K extends number
232
+ ? K
233
+ : K extends `${infer C extends number}XX`
234
+ ? Codes<C>
235
+ : ToNumber<K>;
236
+ /**
237
+ * The statuses of response key S among the keys K: a code itself, a range its codes K
238
+ * does not list, and `default` every status nothing else covers.
239
+ */
240
+ type StatusesOf<S, K> = S extends "default"
241
+ ? Exclude<AnyStatus, Covered<K>>
242
+ : S extends `${string}XX`
243
+ ? Extract<Exclude<Covered<S>, Covered<Exclude<K, S>>>, AnyStatus>
244
+ : Covered<S>;
217
245
 
218
246
  /** `data` for a response object B (its `content`), read as parseAs A says. */
219
247
  type Parsed<A, B> = A extends "text"
@@ -228,20 +256,38 @@ type Parsed<A, B> = A extends "text"
228
256
  ? JsonContent<B>
229
257
  : Content<B>;
230
258
 
231
- /** fetch gives HEAD responses and these statuses a null body, whatever is declared. */
232
- type NullBodyStatus = 204 | 205 | 304;
259
+ /** fetch gives these statuses a null body, whatever is declared (0: an opaque response). */
260
+ type NullBodyStatus = 0 | 204 | 205 | 304;
261
+ /** The statuses for which fetch sets `ok`. */
262
+ type OkStatus = Codes<2>;
233
263
 
234
- /** The response object as fetch delivers it: without content where fetch gives no body. */
235
- type Delivered<M, S, B> = [M] extends ["head"]
236
- ? Empty
237
- : S extends NullBodyStatus
238
- ? Empty
239
- : B;
240
-
241
- /** A response nothing documents: any status, any body. */
264
+ /** A response nothing documents: any body. */
242
265
  type Undocumented = { content: Record<string, unknown> };
243
266
 
244
- /** Discriminated union over the documented statuses. */
267
+ /** One result, for statuses St, unless there are none. */
268
+ type Member<St, Ok, B, A> = [St] extends [never]
269
+ ? never
270
+ : { status: St; ok: Ok; data: Parsed<A, B>; response: Response };
271
+
272
+ /**
273
+ * The results for statuses St with response object B, split where fetch's `ok` differs
274
+ * and where it gives no body (HEAD, and the null body statuses).
275
+ */
276
+ type Results<St, B, A, M> = [M] extends ["head"]
277
+ ?
278
+ | Member<Extract<St, OkStatus>, true, Empty, A>
279
+ | Member<Exclude<St, OkStatus>, false, Empty, A>
280
+ :
281
+ | Member<Exclude<Extract<St, OkStatus>, NullBodyStatus>, true, B, A>
282
+ | Member<Extract<Extract<St, OkStatus>, NullBodyStatus>, true, Empty, A>
283
+ | Member<Exclude<Exclude<St, OkStatus>, NullBodyStatus>, false, B, A>
284
+ | Member<Extract<Exclude<St, OkStatus>, NullBodyStatus>, false, Empty, A>;
285
+
286
+ /**
287
+ * A union over the statuses: each documented code, each range's codes the document does
288
+ * not list on their own, and `default`'s statuses, or without `default`, the statuses the
289
+ * document does not list, with any body.
290
+ */
245
291
  export type Result<
246
292
  O,
247
293
  A extends ParseAs | undefined = undefined,
@@ -249,21 +295,11 @@ export type Result<
249
295
  > = O extends {
250
296
  responses: infer R;
251
297
  }
252
- ? [keyof R] extends [never]
253
- ? {
254
- status: number;
255
- ok: boolean;
256
- data: Parsed<A, Undocumented>;
257
- response: Response;
258
- }
259
- : {
260
- [S in keyof R]: {
261
- status: Status<S>;
262
- ok: Ok<S>;
263
- data: Parsed<A, Delivered<M, S, R[S]>>;
264
- response: Response;
265
- };
266
- }[keyof R]
298
+ ?
299
+ | { [S in keyof R]: Results<StatusesOf<S, keyof R>, R[S], A, M> }[keyof R]
300
+ | ("default" extends keyof R
301
+ ? never
302
+ : Results<Exclude<AnyStatus, Covered<keyof R>>, Undocumented, A, M>)
267
303
  : never;
268
304
 
269
305
  /**
@@ -279,22 +315,14 @@ export type ResultOf<
279
315
  > = Result<Op<P, Path, M>, A, M>;
280
316
 
281
317
  /**
282
- * The results of R that `if (res.ok)` leaves: the 2xx statuses, and `default`, which
283
- * covers an undocumented 2xx (its `ok` is `boolean`). `OkResult<ResultOf<…>>["data"]`
284
- * types what a wrapper returns when it throws the others.
318
+ * The results of R that `if (res.ok)` leaves: the 2xx statuses, among them those of a
319
+ * range, `default` or the undocumented ones. `OkResult<ResultOf<…>>["data"]` types what
320
+ * a wrapper returns when it throws the others.
285
321
  */
286
- export type OkResult<R> = R extends { ok: infer K }
287
- ? true extends K
288
- ? R & { ok: true }
289
- : never
290
- : never;
322
+ export type OkResult<R> = R extends { ok: true } ? R : never;
291
323
 
292
- /** The results of R that `if (!res.ok)` leaves: the other statuses, and `default`. */
293
- export type NotOkResult<R> = R extends { ok: infer K }
294
- ? false extends K
295
- ? R & { ok: false }
296
- : never
297
- : never;
324
+ /** The results of R that `if (!res.ok)` leaves: the other statuses. */
325
+ export type NotOkResult<R> = R extends { ok: false } ? R : never;
298
326
 
299
327
  type MaybePromise<T> = T | Promise<T>;
300
328