@hackmd/api 2.6.0 → 2.7.0-beta.20261120073810.060eeab
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 +147 -16
- package/dist/generated/client/client.gen.d.ts +2 -0
- package/dist/generated/client/index.d.ts +10 -0
- package/dist/generated/client/types.gen.d.ts +87 -0
- package/dist/generated/client/utils.gen.d.ts +14 -0
- package/dist/generated/client.gen.d.ts +12 -0
- package/dist/generated/core/auth.gen.d.ts +25 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +25 -0
- package/dist/generated/core/params.gen.d.ts +43 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +33 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/generated/core/types.gen.d.ts +83 -0
- package/dist/generated/core/utils.gen.d.ts +19 -0
- package/dist/generated/index.d.ts +2 -0
- package/dist/generated/operationRegistry.gen.d.ts +1323 -0
- package/dist/generated/sdk.gen.d.ts +274 -0
- package/dist/generated/types.gen.d.ts +2962 -0
- package/dist/index.cjs +434 -75
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +149 -12
- package/dist/index.js +430 -75
- package/dist/index.js.map +1 -1
- package/dist/raw.cjs +132 -0
- package/dist/raw.cjs.map +1 -0
- package/dist/raw.d.ts +15 -0
- package/dist/raw.js +69 -0
- package/dist/raw.js.map +1 -0
- package/dist/sdk.gen-0nBaZJhE.js +1286 -0
- package/dist/sdk.gen-0nBaZJhE.js.map +1 -0
- package/dist/sdk.gen-COYJyg7W.cjs +1349 -0
- package/dist/sdk.gen-COYJyg7W.cjs.map +1 -0
- package/dist/type.d.ts +73 -123
- package/package.json +31 -14
package/README.md
CHANGED
|
@@ -80,17 +80,20 @@ The client supports automatic retry for failed requests with exponential backoff
|
|
|
80
80
|
```javascript
|
|
81
81
|
const client = new HackMDAPI('YOUR_ACCESS_TOKEN', 'https://api.hackmd.io/v1', {
|
|
82
82
|
retryConfig: {
|
|
83
|
-
maxRetries: 3,
|
|
83
|
+
maxRetries: 3, // Maximum number of retry attempts
|
|
84
84
|
baseDelay: 100 // Base delay in milliseconds for exponential backoff
|
|
85
85
|
}
|
|
86
86
|
})
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
The client will automatically retry requests that fail with:
|
|
90
|
+
|
|
90
91
|
- 5xx server errors
|
|
91
92
|
- 429 Too Many Requests errors
|
|
92
93
|
- Network errors
|
|
93
94
|
|
|
95
|
+
POST, PATCH, and trash restore operations are never retried automatically.
|
|
96
|
+
|
|
94
97
|
### Response Data Handling
|
|
95
98
|
|
|
96
99
|
By default, the client automatically unwraps the response data from the Axios response object. You can control this behavior using the `unwrapData` option:
|
|
@@ -103,51 +106,179 @@ const response = await client.getMe({ unwrapData: false })
|
|
|
103
106
|
const data = await client.getMe({ unwrapData: true })
|
|
104
107
|
```
|
|
105
108
|
|
|
109
|
+
`createNote` and `createTeamNote` also include `status` and optional `etag` in the unwrapped result. For `status === 207`, the note was created but folder placement failed; use `result.note` and `result.error` rather than retrying creation.
|
|
110
|
+
|
|
106
111
|
### ETag Support
|
|
107
112
|
|
|
108
|
-
The client
|
|
113
|
+
The client can send an ETag to check whether a note changed. The server
|
|
114
|
+
generates the ETag and decides whether to return 304; the client does not cache
|
|
115
|
+
the note body for you:
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
const first = await client.getNote('note-id', { unwrapData: false })
|
|
119
|
+
const response = await client.getNote('note-id', {
|
|
120
|
+
etag: first.headers.etag,
|
|
121
|
+
unwrapData: false,
|
|
122
|
+
})
|
|
123
|
+
// 304 has no body; keep first.data. Otherwise, use response.data.
|
|
124
|
+
const note = response.status === 304 ? first.data : response.data
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Team notes work the same way through the existing client:
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
const teamFirst = await client.getTeamNote('team-path', 'note-id')
|
|
131
|
+
const teamResponse = await client.getTeamNote('team-path', 'note-id', {
|
|
132
|
+
etag: teamFirst.etag,
|
|
133
|
+
})
|
|
134
|
+
const teamNote = teamResponse.status === 304 ? teamFirst : teamResponse
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Image Upload
|
|
138
|
+
|
|
139
|
+
Upload an image to a note with `uploadNoteImage`. The API returns the uploaded image link in `data.link`.
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
// Browser: pass a File from an <input type="file">
|
|
143
|
+
const uploaded = await client.uploadNoteImage('note-id', file)
|
|
144
|
+
console.log(uploaded.data.link)
|
|
145
|
+
|
|
146
|
+
// Node.js 18+: pass a Blob and optional filename
|
|
147
|
+
const image = new Blob([imageBuffer], { type: 'image/png' })
|
|
148
|
+
const uploadedFromNode = await client.uploadNoteImage('note-id', image, {
|
|
149
|
+
filename: 'diagram.png'
|
|
150
|
+
})
|
|
151
|
+
console.log(uploadedFromNode.data.link)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Webhooks
|
|
155
|
+
|
|
156
|
+
Manage personal or team webhooks through the same `API` client. Save the secret
|
|
157
|
+
returned by `createWebhook` when you create a webhook; later reads do not return it.
|
|
109
158
|
|
|
110
159
|
```javascript
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
160
|
+
const webhook = await client.createWebhook({
|
|
161
|
+
scope: { type: 'workspace' },
|
|
162
|
+
url: 'https://example.com/webhook',
|
|
163
|
+
})
|
|
164
|
+
console.log(webhook.secret)
|
|
165
|
+
|
|
166
|
+
const teamWebhooks = await client.listTeamWebhooks('team-path')
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
`listWebhookDeliveries('hook-id', { page: 1, limit: 20 })` returns delivery data
|
|
170
|
+
and pagination metadata. `exportWebhookDeliveries('hook-id')` returns
|
|
171
|
+
newline-delimited JSON as a string; parse the lines yourself if needed.
|
|
114
172
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
173
|
+
### Trash
|
|
174
|
+
|
|
175
|
+
Use `listTrash()` or `listTeamTrash('team-path')` to find deleted notes.
|
|
176
|
+
`restoreNote('note-id')` restores one note; `batchRestore({ noteIds })` reports
|
|
177
|
+
success or failure for each note. Pass `{ unwrapData: false }` to inspect the
|
|
178
|
+
batch HTTP status (200 or 207).
|
|
179
|
+
|
|
180
|
+
### Versions
|
|
181
|
+
|
|
182
|
+
`listVersions('note-id', { named_only: true, page: 1, limit: 20 })` lists saved
|
|
183
|
+
versions. Use `getVersion('note-id', versionId)` for content, or
|
|
184
|
+
`compareVersions('note-id', { base: 'version:<id>', target: 'note_content' })`
|
|
185
|
+
to compare it with the live note. `createVersion` and `updateVersion` accept the
|
|
186
|
+
corresponding OpenAPI request bodies.
|
|
187
|
+
|
|
188
|
+
### Comments
|
|
189
|
+
|
|
190
|
+
Use `listNoteComments('note-id', { page: 1, limit: 20, threadStatus: 'open' })`
|
|
191
|
+
to browse comments. `getNoteComment('note-id', commentId)` reads one comment;
|
|
192
|
+
`resolveNoteComment` and `unresolveNoteComment` return the updated comment and
|
|
193
|
+
any affected thread.
|
|
194
|
+
|
|
195
|
+
### Generated Raw API
|
|
196
|
+
|
|
197
|
+
The `@hackmd/api/raw` entry point exposes every OpenAPI operation as a generated,
|
|
198
|
+
one-to-one function. Create a client to share authentication and the API endpoint:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { createClient, getNote } from '@hackmd/api/raw'
|
|
202
|
+
|
|
203
|
+
const client = createClient({
|
|
204
|
+
auth: 'YOUR_ACCESS_TOKEN',
|
|
205
|
+
baseURL: 'https://api.hackmd.io/v1',
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
const response = await getNote({
|
|
209
|
+
client,
|
|
210
|
+
path: { noteId: 'NOTE_ID' },
|
|
211
|
+
throwOnError: true,
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
console.log(response.data.content)
|
|
118
215
|
```
|
|
119
216
|
|
|
217
|
+
The package root retains the existing `API` class. Files under
|
|
218
|
+
`src/generated` are generated from the vendored OpenAPI document and must not
|
|
219
|
+
be edited manually.
|
|
220
|
+
|
|
221
|
+
On `API`, `listNotes`, `listFolders`, `listTeamNotes`, `listTeamFolders`, and
|
|
222
|
+
`listTeams` match the OpenAPI operation names. Existing `getNoteList`,
|
|
223
|
+
`getFolderList`, `getTeamNotes`, `getTeamFolderList`, and `getTeams` remain
|
|
224
|
+
compatible aliases.
|
|
225
|
+
|
|
120
226
|
## API
|
|
121
227
|
|
|
122
|
-
|
|
228
|
+
The [API reference](https://hackmdio.github.io/api-client/) covers the existing
|
|
229
|
+
`API` class and every raw operation and DTO. To explore autocomplete
|
|
230
|
+
without a real token, open the type-only example:
|
|
231
|
+
|
|
232
|
+
[](https://stackblitz.com/fork/github/hackmdio/api-client/tree/master/nodejs?file=tests/types/playground.mts)
|
|
233
|
+
|
|
234
|
+
The Pages site is deployed from `master` only.
|
|
235
|
+
|
|
236
|
+
Run `pnpm docs:dev` from `nodejs` and open `http://127.0.0.1:3000` to preview
|
|
237
|
+
the reference locally. It builds the HTML once before serving; rerun the
|
|
238
|
+
command after changing source or docs. The output in `.docs-dist` is not
|
|
239
|
+
committed.
|
|
240
|
+
|
|
241
|
+
## Regenerating the raw client
|
|
242
|
+
|
|
243
|
+
Generation requires Node.js 22.18 or newer.
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
pnpm spec:pull
|
|
247
|
+
pnpm codegen
|
|
248
|
+
pnpm check:generated
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
The OpenAPI document is committed at `spec/hackmd-openapi.json`, and generated
|
|
252
|
+
sources are committed under `src/generated` so package builds remain offline and
|
|
253
|
+
deterministic.
|
|
123
254
|
|
|
124
255
|
## E2E tests (live API)
|
|
125
256
|
|
|
126
|
-
Integration tests call a real HackMD API (staging or production). They are **not** run by `
|
|
257
|
+
Integration tests call a real HackMD API (staging or production). They are **not** run by `pnpm test` or the default CI job.
|
|
127
258
|
|
|
128
|
-
|
|
259
|
+
## Requirements
|
|
129
260
|
|
|
130
261
|
- `HACKMD_ACCESS_TOKEN` — a valid personal access token for the environment you target.
|
|
131
262
|
- Optional: `HACKMD_API_ENDPOINT` — defaults to `https://api.hackmd.io/v1`. For staging, use `https://api-stage.hackmd.io/v1`.
|
|
132
263
|
|
|
133
|
-
|
|
264
|
+
## Read-only (default e2e)
|
|
134
265
|
|
|
135
266
|
```bash
|
|
136
267
|
cd nodejs
|
|
137
268
|
export HACKMD_ACCESS_TOKEN=your_token
|
|
138
269
|
export HACKMD_API_ENDPOINT=https://api-stage.hackmd.io/v1 # optional
|
|
139
|
-
|
|
270
|
+
pnpm test:e2e
|
|
140
271
|
```
|
|
141
272
|
|
|
142
|
-
|
|
273
|
+
## With CRUD / mutations
|
|
143
274
|
|
|
144
275
|
Set `HACKMD_E2E_MUTATIONS=1` to run write tests against your account:
|
|
145
276
|
|
|
146
|
-
- **Notes:** create → get → update (title, content, tags) → list → delete.
|
|
277
|
+
- **Notes:** create → get → update (title, content, tags) → upload fixture image → list → delete.
|
|
147
278
|
- **Folders:** one integration test runs create (root + nested) → get → update → list → folder-order round-trip (skipped if that API returns 404) → delete. If **POST `/folders`** returns 404 (common before full production rollout), the test exits early with a warning; use staging or `HACKMD_E2E_FOLDERS=0`.
|
|
148
279
|
|
|
149
280
|
```bash
|
|
150
|
-
HACKMD_E2E_MUTATIONS=1
|
|
281
|
+
HACKMD_E2E_MUTATIONS=1 pnpm test:e2e
|
|
151
282
|
```
|
|
152
283
|
|
|
153
284
|
Folder CRUD touches folder display order briefly, then restores the previous order in an `afterAll` hook. To skip folder mutations (e.g. production without `/folders`), set `HACKMD_E2E_FOLDERS=0`.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type { Auth } from '../core/auth.gen.js';
|
|
2
|
+
export type { QuerySerializerOptions } from '../core/bodySerializer.gen.js';
|
|
3
|
+
export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, } from '../core/bodySerializer.gen.js';
|
|
4
|
+
export { buildClientParams } from '../core/params.gen.js';
|
|
5
|
+
export { serializeQueryKeyValue } from '../core/queryKeySerializer.gen.js';
|
|
6
|
+
export type { ServerSentEventsResult } from '../core/serverSentEvents.gen.js';
|
|
7
|
+
export type { ClientMeta } from '../core/types.gen.js';
|
|
8
|
+
export { createClient } from './client.gen.js';
|
|
9
|
+
export type { Client, ClientOptions, Config, CreateClientConfig, Options, RequestOptions, RequestResult, TDataShape, } from './types.gen.js';
|
|
10
|
+
export { createConfig } from './utils.gen.js';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { AxiosError, AxiosInstance, AxiosRequestHeaders, AxiosResponse, AxiosStatic, CreateAxiosDefaults } from 'axios';
|
|
2
|
+
import type { Auth } from '../core/auth.gen.js';
|
|
3
|
+
import type { ServerSentEventsOptions, ServerSentEventsResult } from '../core/serverSentEvents.gen.js';
|
|
4
|
+
import type { Client as CoreClient, Config as CoreConfig } from '../core/types.gen.js';
|
|
5
|
+
export interface Config<T extends ClientOptions = ClientOptions> extends Omit<CreateAxiosDefaults, 'auth' | 'baseURL' | 'headers' | 'method'>, CoreConfig {
|
|
6
|
+
/**
|
|
7
|
+
* Axios implementation. You can use this option to provide either an
|
|
8
|
+
* `AxiosStatic` or an `AxiosInstance`.
|
|
9
|
+
*
|
|
10
|
+
* @default axios
|
|
11
|
+
*/
|
|
12
|
+
axios?: AxiosStatic | AxiosInstance;
|
|
13
|
+
/**
|
|
14
|
+
* Base URL for all requests made by this client.
|
|
15
|
+
*/
|
|
16
|
+
baseURL?: T['baseURL'];
|
|
17
|
+
/**
|
|
18
|
+
* An object containing any HTTP headers that you want to pre-populate your
|
|
19
|
+
* `Headers` object with.
|
|
20
|
+
*
|
|
21
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more}
|
|
22
|
+
*/
|
|
23
|
+
headers?: AxiosRequestHeaders | Record<string, string | number | boolean | (string | number | boolean)[] | null | undefined | unknown>;
|
|
24
|
+
/**
|
|
25
|
+
* Throw an error instead of returning it in the response?
|
|
26
|
+
*
|
|
27
|
+
* @default false
|
|
28
|
+
*/
|
|
29
|
+
throwOnError?: T['throwOnError'];
|
|
30
|
+
}
|
|
31
|
+
export interface RequestOptions<TData = unknown, ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<{
|
|
32
|
+
throwOnError: ThrowOnError;
|
|
33
|
+
}>, Pick<ServerSentEventsOptions<TData>, 'onRequest' | 'onSseError' | 'onSseEvent' | 'sseDefaultRetryDelay' | 'sseMaxRetryAttempts' | 'sseMaxRetryDelay'> {
|
|
34
|
+
/**
|
|
35
|
+
* Any body that you want to add to your request.
|
|
36
|
+
*
|
|
37
|
+
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
|
|
38
|
+
*/
|
|
39
|
+
body?: unknown;
|
|
40
|
+
path?: Record<string, unknown>;
|
|
41
|
+
query?: Record<string, unknown>;
|
|
42
|
+
/**
|
|
43
|
+
* Security mechanism(s) to use for the request.
|
|
44
|
+
*/
|
|
45
|
+
security?: ReadonlyArray<Auth>;
|
|
46
|
+
url: Url;
|
|
47
|
+
}
|
|
48
|
+
export interface ClientOptions {
|
|
49
|
+
baseURL?: string;
|
|
50
|
+
throwOnError?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean> = ThrowOnError extends true ? Promise<AxiosResponse<TData extends Record<string, unknown> ? TData[keyof TData] : TData>> : Promise<(AxiosResponse<TData extends Record<string, unknown> ? TData[keyof TData] : TData> & {
|
|
53
|
+
error: undefined;
|
|
54
|
+
}) | (AxiosError<TError extends Record<string, unknown> ? TError[keyof TError] : TError> & {
|
|
55
|
+
data: undefined;
|
|
56
|
+
error: TError extends Record<string, unknown> ? TError[keyof TError] : TError;
|
|
57
|
+
})>;
|
|
58
|
+
type MethodFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>) => RequestResult<TData, TError, ThrowOnError>;
|
|
59
|
+
type SseFn = <TData = unknown, _TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<never, ThrowOnError>, 'method'>) => Promise<ServerSentEventsResult<TData>>;
|
|
60
|
+
type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(options: Omit<RequestOptions<TData, ThrowOnError>, 'method'> & Pick<Required<RequestOptions<TData, ThrowOnError>>, 'method'>) => RequestResult<TData, TError, ThrowOnError>;
|
|
61
|
+
type BuildUrlFn = <TData extends {
|
|
62
|
+
path?: Record<string, unknown>;
|
|
63
|
+
query?: Record<string, unknown>;
|
|
64
|
+
url: string;
|
|
65
|
+
}>(options: TData & Pick<RequestOptions<unknown, boolean>, 'axios' | 'baseURL' | 'paramsSerializer' | 'querySerializer'>) => string;
|
|
66
|
+
export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn, SseFn> & {
|
|
67
|
+
instance: AxiosInstance;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The `createClientConfig()` function will be called on client initialization
|
|
71
|
+
* and the returned object will become the client's initial configuration.
|
|
72
|
+
*
|
|
73
|
+
* You may want to initialize your client this way instead of calling
|
|
74
|
+
* `setConfig()`. This is useful for example if you're using Next.js
|
|
75
|
+
* to ensure your client always has the correct values.
|
|
76
|
+
*/
|
|
77
|
+
export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>;
|
|
78
|
+
export interface TDataShape {
|
|
79
|
+
body?: unknown;
|
|
80
|
+
headers?: unknown;
|
|
81
|
+
path?: unknown;
|
|
82
|
+
query?: unknown;
|
|
83
|
+
url: string;
|
|
84
|
+
}
|
|
85
|
+
type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
|
|
86
|
+
export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown> = OmitKeys<RequestOptions<TResponse, ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & ([TData] extends [never] ? unknown : Omit<TData, 'url'>);
|
|
87
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { QuerySerializerOptions } from '../core/bodySerializer.gen.js';
|
|
2
|
+
import type { Client, ClientOptions, Config, RequestOptions } from './types.gen.js';
|
|
3
|
+
export declare const createQuerySerializer: <T = unknown>({ parameters, ...args }?: QuerySerializerOptions) => ((queryParams: T) => string);
|
|
4
|
+
export declare function setAuthParams(options: Pick<RequestOptions, 'auth' | 'query' | 'security'> & {
|
|
5
|
+
headers: Record<any, unknown>;
|
|
6
|
+
}): Promise<void>;
|
|
7
|
+
export declare const buildUrl: Client['buildUrl'];
|
|
8
|
+
export declare const mergeConfigs: (a: Config, b: Config) => Config;
|
|
9
|
+
/**
|
|
10
|
+
* Special Axios headers keywords allowing to set headers by request method.
|
|
11
|
+
*/
|
|
12
|
+
export declare const axiosHeadersKeywords: readonly ["common", "delete", "get", "head", "patch", "post", "put"];
|
|
13
|
+
export declare const mergeHeaders: (...headers: Array<Required<Config>["headers"] | undefined>) => Record<any, unknown>;
|
|
14
|
+
export declare const createConfig: <T extends ClientOptions = ClientOptions>(override?: Config<Omit<ClientOptions, keyof T> & T>) => Config<Omit<ClientOptions, keyof T> & T>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Client, type ClientOptions, type Config } from './client/index.js';
|
|
2
|
+
import type { ClientOptions as ClientOptions2 } from './types.gen.js';
|
|
3
|
+
/**
|
|
4
|
+
* The `createClientConfig()` function will be called on client initialization
|
|
5
|
+
* and the returned object will become the client's initial configuration.
|
|
6
|
+
*
|
|
7
|
+
* You may want to initialize your client this way instead of calling
|
|
8
|
+
* `setConfig()`. This is useful for example if you're using Next.js
|
|
9
|
+
* to ensure your client always has the correct values.
|
|
10
|
+
*/
|
|
11
|
+
export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>;
|
|
12
|
+
export declare const client: Client;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type AuthToken = string | undefined;
|
|
2
|
+
export interface Auth {
|
|
3
|
+
/**
|
|
4
|
+
* Which part of the request do we use to send the auth?
|
|
5
|
+
*
|
|
6
|
+
* @default 'header'
|
|
7
|
+
*/
|
|
8
|
+
in?: 'header' | 'query' | 'cookie';
|
|
9
|
+
/**
|
|
10
|
+
* A unique identifier for the security scheme.
|
|
11
|
+
*
|
|
12
|
+
* Defined only when there are multiple security schemes whose `Auth`
|
|
13
|
+
* shape would otherwise be identical.
|
|
14
|
+
*/
|
|
15
|
+
key?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Header or query parameter name.
|
|
18
|
+
*
|
|
19
|
+
* @default 'Authorization'
|
|
20
|
+
*/
|
|
21
|
+
name?: string;
|
|
22
|
+
scheme?: 'basic' | 'bearer';
|
|
23
|
+
type: 'apiKey' | 'http';
|
|
24
|
+
}
|
|
25
|
+
export declare const getAuthToken: (auth: Auth, callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken) => Promise<string | undefined>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ArrayStyle, ObjectStyle, SerializerOptions } from './pathSerializer.gen.js';
|
|
2
|
+
export type QuerySerializer = (query: Record<string, unknown>) => string;
|
|
3
|
+
export type BodySerializer = (body: unknown) => unknown;
|
|
4
|
+
type QuerySerializerOptionsObject = {
|
|
5
|
+
allowReserved?: boolean;
|
|
6
|
+
array?: Partial<SerializerOptions<ArrayStyle>>;
|
|
7
|
+
object?: Partial<SerializerOptions<ObjectStyle>>;
|
|
8
|
+
};
|
|
9
|
+
export type QuerySerializerOptions = QuerySerializerOptionsObject & {
|
|
10
|
+
/**
|
|
11
|
+
* Per-parameter serialization overrides. When provided, these settings
|
|
12
|
+
* override the global array/object settings for specific parameter names.
|
|
13
|
+
*/
|
|
14
|
+
parameters?: Record<string, QuerySerializerOptionsObject>;
|
|
15
|
+
};
|
|
16
|
+
export declare const formDataBodySerializer: {
|
|
17
|
+
bodySerializer: (body: unknown) => FormData;
|
|
18
|
+
};
|
|
19
|
+
export declare const jsonBodySerializer: {
|
|
20
|
+
bodySerializer: (body: unknown) => string;
|
|
21
|
+
};
|
|
22
|
+
export declare const urlSearchParamsBodySerializer: {
|
|
23
|
+
bodySerializer: (body: unknown) => string;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
type Slot = 'body' | 'headers' | 'path' | 'query';
|
|
2
|
+
export type Field = {
|
|
3
|
+
in: Exclude<Slot, 'body'>;
|
|
4
|
+
/**
|
|
5
|
+
* Field name. This is the name we want the user to see and use.
|
|
6
|
+
*/
|
|
7
|
+
key: string;
|
|
8
|
+
/**
|
|
9
|
+
* Field mapped name. This is the name we want to use in the request.
|
|
10
|
+
* If omitted, we use the same value as `key`.
|
|
11
|
+
*/
|
|
12
|
+
map?: string;
|
|
13
|
+
} | {
|
|
14
|
+
in: Extract<Slot, 'body'>;
|
|
15
|
+
/**
|
|
16
|
+
* Key isn't required for bodies.
|
|
17
|
+
*/
|
|
18
|
+
key?: string;
|
|
19
|
+
map?: string;
|
|
20
|
+
} | {
|
|
21
|
+
/**
|
|
22
|
+
* Field name. This is the name we want the user to see and use.
|
|
23
|
+
*/
|
|
24
|
+
key: string;
|
|
25
|
+
/**
|
|
26
|
+
* Field mapped name. This is the name we want to use in the request.
|
|
27
|
+
* If `in` is omitted, `map` aliases `key` to the transport layer.
|
|
28
|
+
*/
|
|
29
|
+
map: Slot;
|
|
30
|
+
};
|
|
31
|
+
export interface Fields {
|
|
32
|
+
allowExtra?: Partial<Record<Slot, boolean>>;
|
|
33
|
+
args?: ReadonlyArray<Field>;
|
|
34
|
+
}
|
|
35
|
+
export type FieldsConfig = ReadonlyArray<Field | Fields>;
|
|
36
|
+
interface Params {
|
|
37
|
+
body: unknown;
|
|
38
|
+
headers: Record<string, unknown>;
|
|
39
|
+
path: Record<string, unknown>;
|
|
40
|
+
query: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
export declare function buildClientParams(args: ReadonlyArray<unknown>, fields: FieldsConfig): Params;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
interface SerializeOptions<T> extends SerializePrimitiveOptions, SerializerOptions<T> {
|
|
2
|
+
}
|
|
3
|
+
interface SerializePrimitiveOptions {
|
|
4
|
+
allowReserved?: boolean;
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SerializerOptions<T> {
|
|
8
|
+
/**
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
explode: boolean;
|
|
12
|
+
style: T;
|
|
13
|
+
}
|
|
14
|
+
export type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited';
|
|
15
|
+
export type ArraySeparatorStyle = ArrayStyle | MatrixStyle;
|
|
16
|
+
type MatrixStyle = 'label' | 'matrix' | 'simple';
|
|
17
|
+
export type ObjectStyle = 'form' | 'deepObject';
|
|
18
|
+
type ObjectSeparatorStyle = ObjectStyle | MatrixStyle;
|
|
19
|
+
interface SerializePrimitiveParam extends SerializePrimitiveOptions {
|
|
20
|
+
value: string;
|
|
21
|
+
}
|
|
22
|
+
export declare const separatorArrayExplode: (style: ArraySeparatorStyle) => "." | ";" | "," | "&";
|
|
23
|
+
export declare const separatorArrayNoExplode: (style: ArraySeparatorStyle) => "," | "|" | "%20";
|
|
24
|
+
export declare const separatorObjectExplode: (style: ObjectSeparatorStyle) => "." | ";" | "," | "&";
|
|
25
|
+
export declare const serializeArrayParam: ({ allowReserved, explode, name, style, value, }: SerializeOptions<ArraySeparatorStyle> & {
|
|
26
|
+
value: unknown[];
|
|
27
|
+
}) => string;
|
|
28
|
+
export declare const serializePrimitiveParam: ({ allowReserved, name, value, }: SerializePrimitiveParam) => string;
|
|
29
|
+
export declare const serializeObjectParam: ({ allowReserved, explode, name, style, value, valueOnly, }: SerializeOptions<ObjectSeparatorStyle> & {
|
|
30
|
+
value: Record<string, unknown> | Date;
|
|
31
|
+
valueOnly?: boolean;
|
|
32
|
+
}) => string;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-friendly union that mirrors what Pinia Colada can hash.
|
|
3
|
+
*/
|
|
4
|
+
export type JsonValue = null | string | number | boolean | JsonValue[] | {
|
|
5
|
+
[key: string]: JsonValue;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Replacer that converts non-JSON values (bigint, Date, etc.) to safe substitutes.
|
|
9
|
+
*/
|
|
10
|
+
export declare const queryKeyJsonReplacer: (_key: string, value: unknown) => unknown | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Safely stringifies a value and parses it back into a JsonValue.
|
|
13
|
+
*/
|
|
14
|
+
export declare const stringifyToJsonValue: (input: unknown) => JsonValue | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Normalizes any accepted value into a JSON-friendly shape for query keys.
|
|
17
|
+
*/
|
|
18
|
+
export declare const serializeQueryKeyValue: (value: unknown) => JsonValue | undefined;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Config } from './types.gen.js';
|
|
2
|
+
export type ServerSentEventsOptions<TData = unknown> = Omit<RequestInit, 'method'> & Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
|
|
3
|
+
/**
|
|
4
|
+
* Fetch API implementation. You can use this option to provide a custom
|
|
5
|
+
* fetch instance.
|
|
6
|
+
*
|
|
7
|
+
* @default globalThis.fetch
|
|
8
|
+
*/
|
|
9
|
+
fetch?: typeof fetch;
|
|
10
|
+
/**
|
|
11
|
+
* Implementing clients can call request interceptors inside this hook.
|
|
12
|
+
*/
|
|
13
|
+
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
|
|
14
|
+
/**
|
|
15
|
+
* Callback invoked when a network or parsing error occurs during streaming.
|
|
16
|
+
*
|
|
17
|
+
* This option applies only if the endpoint returns a stream of events.
|
|
18
|
+
*
|
|
19
|
+
* @param error The error that occurred.
|
|
20
|
+
*/
|
|
21
|
+
onSseError?: (error: unknown) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Callback invoked when an event is streamed from the server.
|
|
24
|
+
*
|
|
25
|
+
* This option applies only if the endpoint returns a stream of events.
|
|
26
|
+
*
|
|
27
|
+
* @param event Event streamed from the server.
|
|
28
|
+
* @returns Nothing (void).
|
|
29
|
+
*/
|
|
30
|
+
onSseEvent?: (event: StreamEvent<TData>) => void;
|
|
31
|
+
serializedBody?: RequestInit['body'];
|
|
32
|
+
/**
|
|
33
|
+
* Default retry delay in milliseconds.
|
|
34
|
+
*
|
|
35
|
+
* This option applies only if the endpoint returns a stream of events.
|
|
36
|
+
*
|
|
37
|
+
* @default 3000
|
|
38
|
+
*/
|
|
39
|
+
sseDefaultRetryDelay?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Maximum number of retry attempts before giving up.
|
|
42
|
+
*/
|
|
43
|
+
sseMaxRetryAttempts?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Maximum retry delay in milliseconds.
|
|
46
|
+
*
|
|
47
|
+
* Applies only when exponential backoff is used.
|
|
48
|
+
*
|
|
49
|
+
* This option applies only if the endpoint returns a stream of events.
|
|
50
|
+
*
|
|
51
|
+
* @default 30000
|
|
52
|
+
*/
|
|
53
|
+
sseMaxRetryDelay?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Optional sleep function for retry backoff.
|
|
56
|
+
*
|
|
57
|
+
* Defaults to using `setTimeout`.
|
|
58
|
+
*/
|
|
59
|
+
sseSleepFn?: (ms: number) => Promise<void>;
|
|
60
|
+
url: string;
|
|
61
|
+
};
|
|
62
|
+
export interface StreamEvent<TData = unknown> {
|
|
63
|
+
data: TData;
|
|
64
|
+
event?: string;
|
|
65
|
+
id?: string;
|
|
66
|
+
retry?: number;
|
|
67
|
+
}
|
|
68
|
+
export type ServerSentEventsResult<TData = unknown, TReturn = void, TNext = unknown> = {
|
|
69
|
+
stream: AsyncGenerator<TData extends Record<string, unknown> ? TData[keyof TData] : TData, TReturn, TNext>;
|
|
70
|
+
};
|
|
71
|
+
export declare function createSseClient<TData = unknown>({ onRequest, onSseError, onSseEvent, responseTransformer, responseValidator, sseDefaultRetryDelay, sseMaxRetryAttempts, sseMaxRetryDelay, sseSleepFn, url, ...options }: ServerSentEventsOptions): ServerSentEventsResult<TData>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Auth, AuthToken } from './auth.gen.js';
|
|
2
|
+
import type { BodySerializer, QuerySerializer, QuerySerializerOptions } from './bodySerializer.gen.js';
|
|
3
|
+
export type HttpMethod = 'connect' | 'delete' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace';
|
|
4
|
+
export type Client<RequestFn = never, Config = unknown, MethodFn = never, BuildUrlFn = never, SseFn = never> = {
|
|
5
|
+
/**
|
|
6
|
+
* Returns the final request URL.
|
|
7
|
+
*/
|
|
8
|
+
buildUrl: BuildUrlFn;
|
|
9
|
+
getConfig: () => Config;
|
|
10
|
+
request: RequestFn;
|
|
11
|
+
setConfig: (config: Config) => Config;
|
|
12
|
+
} & {
|
|
13
|
+
[K in HttpMethod]: MethodFn;
|
|
14
|
+
} & ([SseFn] extends [never] ? {
|
|
15
|
+
sse?: never;
|
|
16
|
+
} : {
|
|
17
|
+
sse: {
|
|
18
|
+
[K in HttpMethod]: SseFn;
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
export interface Config {
|
|
22
|
+
/**
|
|
23
|
+
* Auth token or a function returning auth token. The resolved value will be
|
|
24
|
+
* added to the request payload as defined by its `security` array.
|
|
25
|
+
*/
|
|
26
|
+
auth?: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken;
|
|
27
|
+
/**
|
|
28
|
+
* A function for serializing request body parameter. By default,
|
|
29
|
+
* {@link JSON.stringify()} will be used.
|
|
30
|
+
*/
|
|
31
|
+
bodySerializer?: BodySerializer | null;
|
|
32
|
+
/**
|
|
33
|
+
* An object containing any HTTP headers that you want to pre-populate your
|
|
34
|
+
* `Headers` object with.
|
|
35
|
+
*
|
|
36
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more}
|
|
37
|
+
*/
|
|
38
|
+
headers?: RequestInit['headers'] | Record<string, string | number | boolean | (string | number | boolean)[] | null | undefined | unknown>;
|
|
39
|
+
/**
|
|
40
|
+
* The request method.
|
|
41
|
+
*
|
|
42
|
+
* {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more}
|
|
43
|
+
*/
|
|
44
|
+
method?: Uppercase<HttpMethod>;
|
|
45
|
+
/**
|
|
46
|
+
* A function for serializing request query parameters. By default, arrays
|
|
47
|
+
* will be exploded in form style, objects will be exploded in deepObject
|
|
48
|
+
* style, and reserved characters are percent-encoded.
|
|
49
|
+
*
|
|
50
|
+
* This method will have no effect if the native `paramsSerializer()` Axios
|
|
51
|
+
* API function is used.
|
|
52
|
+
*
|
|
53
|
+
* {@link https://swagger.io/docs/specification/serialization/#query View examples}
|
|
54
|
+
*/
|
|
55
|
+
querySerializer?: QuerySerializer | QuerySerializerOptions;
|
|
56
|
+
/**
|
|
57
|
+
* A function validating request data. This is useful if you want to ensure
|
|
58
|
+
* the request conforms to the desired shape, so it can be safely sent to
|
|
59
|
+
* the server.
|
|
60
|
+
*/
|
|
61
|
+
requestValidator?: (data: unknown) => Promise<unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* A function transforming response data before it's returned. This is useful
|
|
64
|
+
* for post-processing data, e.g., converting ISO strings into Date objects.
|
|
65
|
+
*/
|
|
66
|
+
responseTransformer?: (data: unknown) => Promise<unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* A function validating response data. This is useful if you want to ensure
|
|
69
|
+
* the response conforms to the desired shape, so it can be safely passed to
|
|
70
|
+
* the transformers and returned to the user.
|
|
71
|
+
*/
|
|
72
|
+
responseValidator?: (data: unknown) => Promise<unknown>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Arbitrary metadata passed through the `meta` request option.
|
|
76
|
+
*/
|
|
77
|
+
export interface ClientMeta {
|
|
78
|
+
}
|
|
79
|
+
type IsExactlyNeverOrNeverUndefined<T> = [T] extends [never] ? true : [T] extends [never | undefined] ? [undefined] extends [T] ? false : true : false;
|
|
80
|
+
export type OmitNever<T extends Record<string, unknown>> = {
|
|
81
|
+
[K in keyof T as IsExactlyNeverOrNeverUndefined<T[K]> extends true ? never : K]: T[K];
|
|
82
|
+
};
|
|
83
|
+
export {};
|