kanagata 0.2.2 → 0.2.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.
- package/README.md +15 -0
- package/dist/client.d.ts +16 -0
- package/package.json +1 -1
- package/src/client.ts +18 -0
package/README.md
CHANGED
|
@@ -140,6 +140,21 @@ async function get<
|
|
|
140
140
|
|
|
141
141
|
For `api.request`, the method takes the place of `"get"`: `ArgsOf<paths, "COPY", Path>`.
|
|
142
142
|
|
|
143
|
+
`OkResult<R>` and `NotOkResult<R>` are the results `if (res.ok)` leaves on each side. `default` is on both, since it covers an undocumented 2xx; `Extract<R, { ok: true }>` would drop it. A wrapper that throws the others returns `OkResult<…>["data"]`:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import type { ArgsOf, OkResult, PathsOf, ResultOf } from "kanagata/client";
|
|
147
|
+
|
|
148
|
+
async function getOk<Path extends PathsOf<paths, "get">>(
|
|
149
|
+
path: Path,
|
|
150
|
+
...args: ArgsOf<paths, "get", Path>
|
|
151
|
+
): Promise<OkResult<ResultOf<paths, "get", Path>>["data"]> {
|
|
152
|
+
const res = await api.get(path, ...args);
|
|
153
|
+
if (!res.ok) throw res; // NotOkResult<ResultOf<paths, "get", Path>>
|
|
154
|
+
return res.data;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
143
158
|
### Details
|
|
144
159
|
|
|
145
160
|
- **baseUrl:** a relative one resolves on every call as fetch resolves it: against the page's base URL, or the worker's location. Where there is neither (Node), it throws a TypeError. `""` is the origin of the page's base URL. Its own query is kept, before the call's. An operation that declares `servers` (or whose path item does) is served there, not by the document's servers, so its call names one of them as `baseUrl`; other operations take the config's and cannot be given one.
|
package/dist/client.d.ts
CHANGED
|
@@ -171,6 +171,22 @@ export type Result<O, A extends ParseAs | undefined = undefined, M extends strin
|
|
|
171
171
|
* method given once (so HEAD has no body, as in the call).
|
|
172
172
|
*/
|
|
173
173
|
export type ResultOf<P, M extends string, Path extends PathsOf<P, M>, A extends ParseAs | undefined = undefined> = Result<Op<P, Path, M>, A, M>;
|
|
174
|
+
/**
|
|
175
|
+
* The results of R that `if (res.ok)` leaves: the 2xx statuses, and `default`, which
|
|
176
|
+
* covers an undocumented 2xx (its `ok` is `boolean`). `OkResult<ResultOf<…>>["data"]`
|
|
177
|
+
* types what a wrapper returns when it throws the others.
|
|
178
|
+
*/
|
|
179
|
+
export type OkResult<R> = R extends {
|
|
180
|
+
ok: infer K;
|
|
181
|
+
} ? true extends K ? R & {
|
|
182
|
+
ok: true;
|
|
183
|
+
} : never : never;
|
|
184
|
+
/** The results of R that `if (!res.ok)` leaves: the other statuses, and `default`. */
|
|
185
|
+
export type NotOkResult<R> = R extends {
|
|
186
|
+
ok: infer K;
|
|
187
|
+
} ? false extends K ? R & {
|
|
188
|
+
ok: false;
|
|
189
|
+
} : never : never;
|
|
174
190
|
type MaybePromise<T> = T | Promise<T>;
|
|
175
191
|
export interface ClientConfig extends FetchOptions {
|
|
176
192
|
/** Absolute, or relative to the page (`/api`) as fetch resolves it, on every call. */
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -278,6 +278,24 @@ export type ResultOf<
|
|
|
278
278
|
A extends ParseAs | undefined = undefined,
|
|
279
279
|
> = Result<Op<P, Path, M>, A, M>;
|
|
280
280
|
|
|
281
|
+
/**
|
|
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.
|
|
285
|
+
*/
|
|
286
|
+
export type OkResult<R> = R extends { ok: infer K }
|
|
287
|
+
? true extends K
|
|
288
|
+
? R & { ok: true }
|
|
289
|
+
: never
|
|
290
|
+
: never;
|
|
291
|
+
|
|
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;
|
|
298
|
+
|
|
281
299
|
type MaybePromise<T> = T | Promise<T>;
|
|
282
300
|
|
|
283
301
|
export interface ClientConfig extends FetchOptions {
|