highseat 0.7.0 → 0.8.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/api.d.ts ADDED
@@ -0,0 +1,123 @@
1
+ import { paths } from "./api.gen.js";
2
+ //#region src/api.d.ts
3
+ type Method = "get" | "post" | "patch" | "delete";
4
+ /** A path of the API that answers a method. */
5
+ type PathOf<Name extends Method> = { [Path in keyof paths]: paths[Path] extends Record<Name, object> ? Path : never; }[keyof paths];
6
+ type Operation<Path extends keyof paths, Name extends Method> = paths[Path][Name & keyof paths[Path]];
7
+ type PathOptions<Route> = Route extends {
8
+ parameters: {
9
+ path: infer Values;
10
+ };
11
+ } ? {
12
+ path: Values;
13
+ } : {
14
+ path?: undefined;
15
+ };
16
+ type QueryOptions<Route> = Route extends {
17
+ parameters: {
18
+ query: infer Values;
19
+ };
20
+ } ? {
21
+ query: Values;
22
+ } : Route extends {
23
+ parameters: {
24
+ query?: infer Values;
25
+ };
26
+ } ? {
27
+ query?: Values;
28
+ } : {
29
+ query?: undefined;
30
+ };
31
+ type BodyOptions<Route> = Route extends {
32
+ requestBody: {
33
+ content: {
34
+ "application/json": infer Body;
35
+ };
36
+ };
37
+ } ? {
38
+ body: Body;
39
+ } : {
40
+ body?: undefined;
41
+ };
42
+ /** What every call may add. */
43
+ interface CallOptions {
44
+ /** Makes a `POST` safe to retry: a repeat within 24 hours replays the first answer. */
45
+ idempotencyKey?: string;
46
+ /** Cancels the call. */
47
+ signal?: AbortSignal;
48
+ }
49
+ type Options<Route> = PathOptions<Route> & QueryOptions<Route> & BodyOptions<Route> & CallOptions;
50
+ type Arguments<Route> = object extends Options<Route> ? [options?: Options<Route>] : [options: Options<Route>];
51
+ type JsonOf<Response> = Response extends {
52
+ content: {
53
+ "application/json": infer Body;
54
+ };
55
+ } ? Body : undefined;
56
+ type ResultOf<Route> = Route extends {
57
+ responses: infer Responses;
58
+ } ? JsonOf<Responses[Extract<keyof Responses, 200 | 201 | 202 | 204>]> : never;
59
+ type Call<Name extends Method> = <Path extends PathOf<Name>>(path: Path, ...options: Arguments<Operation<Path, Name>>) => Promise<ResultOf<Operation<Path, Name>>>;
60
+ /** What a call to a path answers, for naming it in your own code. */
61
+ export type ApiResult<Path extends keyof paths, Name extends Method = "get"> = ResultOf<Operation<Path, Name>>;
62
+ /** A client of the Highseat API, with one typed function per HTTP method. */
63
+ export interface HighseatApi {
64
+ get: Call<"get">;
65
+ post: Call<"post">;
66
+ patch: Call<"patch">;
67
+ delete: Call<"delete">;
68
+ }
69
+ /** How to reach the API. */
70
+ export interface ApiOptions {
71
+ /**
72
+ * An API key or an OAuth access token. Pass a function when the token changes, as an OAuth
73
+ * token does when it is refreshed: it runs before every call.
74
+ */
75
+ token: string | (() => string | Promise<string>);
76
+ /** The API's address, with its version. Defaults to `https://api.highseat.com/v1`. */
77
+ baseUrl?: string;
78
+ /** Replaces the global `fetch`. */
79
+ fetch?: typeof globalThis.fetch;
80
+ }
81
+ /** One invalid parameter of a refused call. */
82
+ export interface ApiIssue {
83
+ /** The parameter at fault, such as `["filter", 1]` for the second filter. */
84
+ path: (string | number)[];
85
+ message: string;
86
+ }
87
+ interface ApiFailure {
88
+ code?: string;
89
+ message?: string;
90
+ requestId?: string;
91
+ id?: string;
92
+ issues?: ApiIssue[];
93
+ }
94
+ /** A call the API refused. Branch on `code`, which is stable. */
95
+ export declare class HighseatApiError extends Error {
96
+ /** The HTTP status. */
97
+ readonly status: number;
98
+ /** Stable and machine-readable, such as `RATE_LIMITED`. */
99
+ readonly code: string;
100
+ /** Names the request. Quote it to support. */
101
+ readonly requestId?: string;
102
+ /** On a conflict, the existing row's id. */
103
+ readonly id?: string;
104
+ /** One entry per invalid parameter. */
105
+ readonly issues?: ApiIssue[];
106
+ constructor(status: number, failure: ApiFailure);
107
+ }
108
+ /**
109
+ * Creates a client of the Highseat API. Paths, parameters and answers are typed from the API's
110
+ * OpenAPI document, and a refused call throws {@link HighseatApiError}.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * import { createApi } from "highseat/api"
115
+ *
116
+ * const api = createApi({ token: process.env.HIGHSEAT_API_KEY })
117
+ * const { data } = await api.get("/sites/{site}/realtime", { path: { site: "example.com" } })
118
+ * ```
119
+ */
120
+ export declare function createApi({ token, baseUrl, fetch }: ApiOptions): HighseatApi;
121
+ //#endregion
122
+ export {};
123
+ //# sourceMappingURL=api.d.ts.map