@octabyte-io/paperstack-api-client 0.0.0-stage → 0.12.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/LICENSE +203 -0
- package/README.md +48 -2
- package/dist/answer.d.ts +15 -0
- package/dist/answer.js +86 -0
- package/dist/api-reports/experimental.api.md +9 -0
- package/dist/api-reports/index.api.md +6768 -0
- package/dist/api-reports/node.api.md +23 -0
- package/dist/errors.d.ts +38 -0
- package/dist/errors.js +37 -0
- package/dist/events.d.ts +31 -0
- package/dist/events.gen.d.ts +258 -0
- package/dist/events.gen.js +1 -0
- package/dist/events.js +1 -0
- package/dist/experimental/index.d.ts +1 -0
- package/dist/experimental/index.js +1 -0
- package/dist/file-answer.d.ts +13 -0
- package/dist/file-answer.js +55 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +4 -0
- package/dist/node/file-from-path.d.ts +6 -0
- package/dist/node/file-from-path.js +12 -0
- package/dist/node/index.d.ts +3 -0
- package/dist/node/index.js +4 -0
- package/dist/node/save-to-file.d.ts +7 -0
- package/dist/node/save-to-file.js +18 -0
- package/dist/operations.gen.d.ts +9252 -0
- package/dist/operations.gen.js +5 -0
- package/dist/paginate.d.ts +25 -0
- package/dist/paginate.js +17 -0
- package/dist/paper-stack.d.ts +45 -0
- package/dist/paper-stack.js +86 -0
- package/dist/resources.d.ts +15 -0
- package/dist/resources.js +9 -0
- package/dist/route-types.d.ts +75 -0
- package/dist/route-types.js +1 -0
- package/dist/routes.gen.d.ts +165 -0
- package/dist/routes.gen.js +177 -0
- package/dist/upload-body.d.ts +9 -0
- package/dist/upload-body.js +141 -0
- package/examples/file-invoice.ts +80 -0
- package/package.json +49 -3
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## API Report File for "@octabyte-io/paperstack-api-client"
|
|
2
|
+
|
|
3
|
+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
|
|
7
|
+
// @public
|
|
8
|
+
export interface FileAnswer {
|
|
9
|
+
body: ReadableStream<Uint8Array>;
|
|
10
|
+
contentType: string | null;
|
|
11
|
+
fileName: string | null;
|
|
12
|
+
size: number | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// @public
|
|
16
|
+
export function fileFromPath(path: string): Promise<File>;
|
|
17
|
+
|
|
18
|
+
// @public
|
|
19
|
+
export function saveToFile(download: Pick<FileAnswer, 'body'>, path: string): Promise<void>;
|
|
20
|
+
|
|
21
|
+
// (No @packageDocumentation comment for this package)
|
|
22
|
+
|
|
23
|
+
```
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { components } from './operations.gen.js';
|
|
2
|
+
export type ServerProblem = components['schemas']['Problem'];
|
|
3
|
+
/**
|
|
4
|
+
* Every error code PaperStack is known to answer with, plus `unexpected_response`, the only one the
|
|
5
|
+
* client makes itself (for an answer that is not a PaperStack answer).
|
|
6
|
+
*/
|
|
7
|
+
export type ErrorCode = ServerProblem['code'] | 'unexpected_response';
|
|
8
|
+
/** A known code, or any other string, so a newer server's new code still type-checks. */
|
|
9
|
+
export type AnyErrorCode = ErrorCode | (string & {});
|
|
10
|
+
/** An RFC 9457 problem body: the fields every refusal has, and the extra members some carry. */
|
|
11
|
+
export type Problem = Omit<ServerProblem, 'code'> & {
|
|
12
|
+
code: AnyErrorCode;
|
|
13
|
+
};
|
|
14
|
+
/** A call PaperStack refused, or answered with something that is not a PaperStack answer. */
|
|
15
|
+
export declare class PaperStackError extends Error {
|
|
16
|
+
readonly name = "PaperStackError";
|
|
17
|
+
/** The answer's HTTP status. */
|
|
18
|
+
readonly status: number;
|
|
19
|
+
/** What went wrong, such as `tag_not_found`. */
|
|
20
|
+
readonly code: AnyErrorCode;
|
|
21
|
+
/** What went wrong, in words. */
|
|
22
|
+
readonly detail: string;
|
|
23
|
+
/** The whole problem body, with any extra members such as the clashing `tag`. */
|
|
24
|
+
readonly problem: Problem;
|
|
25
|
+
/** The answer's headers, such as `Retry-After` after a 429. */
|
|
26
|
+
readonly headers: Headers;
|
|
27
|
+
/** For `unexpected_response` only: the start of the answer's raw text, to see what came back. */
|
|
28
|
+
readonly rawText: string | undefined;
|
|
29
|
+
constructor(problem: Problem, headers: Headers, rawText?: string);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A call that got no answer at all: a wrong address, the server down, or the connection dropped.
|
|
33
|
+
* The error `fetch` gave is its `cause`.
|
|
34
|
+
*/
|
|
35
|
+
export declare class PaperStackConnectionError extends Error {
|
|
36
|
+
readonly name = "PaperStackConnectionError";
|
|
37
|
+
constructor(address: string, cause: unknown);
|
|
38
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** A call PaperStack refused, or answered with something that is not a PaperStack answer. */
|
|
2
|
+
export class PaperStackError extends Error {
|
|
3
|
+
name = 'PaperStackError';
|
|
4
|
+
/** The answer's HTTP status. */
|
|
5
|
+
status;
|
|
6
|
+
/** What went wrong, such as `tag_not_found`. */
|
|
7
|
+
code;
|
|
8
|
+
/** What went wrong, in words. */
|
|
9
|
+
detail;
|
|
10
|
+
/** The whole problem body, with any extra members such as the clashing `tag`. */
|
|
11
|
+
problem;
|
|
12
|
+
/** The answer's headers, such as `Retry-After` after a 429. */
|
|
13
|
+
headers;
|
|
14
|
+
/** For `unexpected_response` only: the start of the answer's raw text, to see what came back. */
|
|
15
|
+
rawText;
|
|
16
|
+
constructor(problem, headers, rawText) {
|
|
17
|
+
super(`${problem.code}: ${problem.detail}`);
|
|
18
|
+
this.status = problem.status;
|
|
19
|
+
this.code = problem.code;
|
|
20
|
+
this.detail = problem.detail;
|
|
21
|
+
this.problem = problem;
|
|
22
|
+
this.headers = headers;
|
|
23
|
+
this.rawText = rawText;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A call that got no answer at all: a wrong address, the server down, or the connection dropped.
|
|
28
|
+
* The error `fetch` gave is its `cause`.
|
|
29
|
+
*/
|
|
30
|
+
export class PaperStackConnectionError extends Error {
|
|
31
|
+
name = 'PaperStackConnectionError';
|
|
32
|
+
constructor(address, cause) {
|
|
33
|
+
super(`Could not reach ${address}: ${cause instanceof Error ? cause.message : String(cause)}`, {
|
|
34
|
+
cause,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { EventVersions } from './events.gen.js';
|
|
2
|
+
/** A catalogue's form: each event name's latest version and each version's shape. */
|
|
3
|
+
export type Catalogue<C> = {
|
|
4
|
+
[Name in keyof C]: {
|
|
5
|
+
latest: number;
|
|
6
|
+
versions: object;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
/** An object type written out in one piece, so an editor shows its fields, not an intersection. */
|
|
10
|
+
export type Flat<T> = {
|
|
11
|
+
[K in keyof T]: T[K];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The event `Name` of `C` at version `Version` (its latest when left out); for a union of names,
|
|
15
|
+
* the union of each one at its own latest version.
|
|
16
|
+
*/
|
|
17
|
+
export type EventFrom<C extends Catalogue<C>, Name extends keyof C = keyof C, Version extends number = never> = Name extends keyof C ? Flat<C[Name]['versions'][([Version] extends [never] ? C[Name]['latest'] : Version) & keyof C[Name]['versions']]> : never;
|
|
18
|
+
/** Every event name PaperStack sends or has sent. */
|
|
19
|
+
export type EventName = keyof EventVersions;
|
|
20
|
+
/**
|
|
21
|
+
* An event PaperStack sends, told apart by `name`: `switch (event.name)` narrows it to that event's
|
|
22
|
+
* fields. `PaperStackEvent<'folder.moved'>` is one event at its latest version, and
|
|
23
|
+
* `PaperStackEvent<'document.moved', 1>` pins a version (any version the catalogue still lists,
|
|
24
|
+
* including one no longer sent).
|
|
25
|
+
*/
|
|
26
|
+
export type PaperStackEvent<Name extends EventName = EventName, Version extends keyof EventVersions[Name]['versions'] & number = never> = EventFrom<EventVersions, Name, Version>;
|
|
27
|
+
/**
|
|
28
|
+
* Every version of event `Name` the catalogue lists, such as `1 | 2`: pass it as the version of
|
|
29
|
+
* {@link PaperStackEvent} for the union of them all, as a handler of events saved over time gets.
|
|
30
|
+
*/
|
|
31
|
+
export type PaperStackEventVersion<Name extends EventName> = keyof EventVersions[Name]['versions'] & number;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import type { webhooks } from './operations.gen.js';
|
|
2
|
+
/** An event's shape, as the public file's webhook for its name and version sends it. */
|
|
3
|
+
export type Payload<Key extends keyof webhooks> = NonNullable<webhooks[Key]['post']['requestBody']>['content']['application/json'];
|
|
4
|
+
/** Every event name: the shape of each of its versions, and its latest (highest) version. */
|
|
5
|
+
export interface EventVersions {
|
|
6
|
+
'document.created': {
|
|
7
|
+
latest: 1;
|
|
8
|
+
versions: {
|
|
9
|
+
1: Payload<'document.created v1'>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
'document.updated': {
|
|
13
|
+
latest: 1;
|
|
14
|
+
versions: {
|
|
15
|
+
1: Payload<'document.updated v1'>;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
'document.moved': {
|
|
19
|
+
latest: 1;
|
|
20
|
+
versions: {
|
|
21
|
+
1: Payload<'document.moved v1'>;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
'document.tagged': {
|
|
25
|
+
latest: 1;
|
|
26
|
+
versions: {
|
|
27
|
+
1: Payload<'document.tagged v1'>;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
'document.untagged': {
|
|
31
|
+
latest: 1;
|
|
32
|
+
versions: {
|
|
33
|
+
1: Payload<'document.untagged v1'>;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
'document.value_changed': {
|
|
37
|
+
latest: 1;
|
|
38
|
+
versions: {
|
|
39
|
+
1: Payload<'document.value_changed v1'>;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
'document.trashed': {
|
|
43
|
+
latest: 1;
|
|
44
|
+
versions: {
|
|
45
|
+
1: Payload<'document.trashed v1'>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
'document.restored': {
|
|
49
|
+
latest: 1;
|
|
50
|
+
versions: {
|
|
51
|
+
1: Payload<'document.restored v1'>;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
'document.deleted': {
|
|
55
|
+
latest: 1;
|
|
56
|
+
versions: {
|
|
57
|
+
1: Payload<'document.deleted v1'>;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
'version.created': {
|
|
61
|
+
latest: 1;
|
|
62
|
+
versions: {
|
|
63
|
+
1: Payload<'version.created v1'>;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
'version.text_saved': {
|
|
67
|
+
latest: 1;
|
|
68
|
+
versions: {
|
|
69
|
+
1: Payload<'version.text_saved v1'>;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
'version.text_read': {
|
|
73
|
+
latest: 1;
|
|
74
|
+
versions: {
|
|
75
|
+
1: Payload<'version.text_read v1'>;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
'folder.created': {
|
|
79
|
+
latest: 1;
|
|
80
|
+
versions: {
|
|
81
|
+
1: Payload<'folder.created v1'>;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
'folder.updated': {
|
|
85
|
+
latest: 1;
|
|
86
|
+
versions: {
|
|
87
|
+
1: Payload<'folder.updated v1'>;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
'folder.moved': {
|
|
91
|
+
latest: 1;
|
|
92
|
+
versions: {
|
|
93
|
+
1: Payload<'folder.moved v1'>;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
'folder.deleted': {
|
|
97
|
+
latest: 1;
|
|
98
|
+
versions: {
|
|
99
|
+
1: Payload<'folder.deleted v1'>;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
'folder.access_changed': {
|
|
103
|
+
latest: 1;
|
|
104
|
+
versions: {
|
|
105
|
+
1: Payload<'folder.access_changed v1'>;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
'document.access_changed': {
|
|
109
|
+
latest: 1;
|
|
110
|
+
versions: {
|
|
111
|
+
1: Payload<'document.access_changed v1'>;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
'tag.created': {
|
|
115
|
+
latest: 1;
|
|
116
|
+
versions: {
|
|
117
|
+
1: Payload<'tag.created v1'>;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
'tag.updated': {
|
|
121
|
+
latest: 1;
|
|
122
|
+
versions: {
|
|
123
|
+
1: Payload<'tag.updated v1'>;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
'tag.deleted': {
|
|
127
|
+
latest: 1;
|
|
128
|
+
versions: {
|
|
129
|
+
1: Payload<'tag.deleted v1'>;
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
'field.created': {
|
|
133
|
+
latest: 1;
|
|
134
|
+
versions: {
|
|
135
|
+
1: Payload<'field.created v1'>;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
'field.updated': {
|
|
139
|
+
latest: 1;
|
|
140
|
+
versions: {
|
|
141
|
+
1: Payload<'field.updated v1'>;
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
'field.deleted': {
|
|
145
|
+
latest: 1;
|
|
146
|
+
versions: {
|
|
147
|
+
1: Payload<'field.deleted v1'>;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
'option.created': {
|
|
151
|
+
latest: 1;
|
|
152
|
+
versions: {
|
|
153
|
+
1: Payload<'option.created v1'>;
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
'option.updated': {
|
|
157
|
+
latest: 1;
|
|
158
|
+
versions: {
|
|
159
|
+
1: Payload<'option.updated v1'>;
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
'option.deleted': {
|
|
163
|
+
latest: 1;
|
|
164
|
+
versions: {
|
|
165
|
+
1: Payload<'option.deleted v1'>;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
'invite.created': {
|
|
169
|
+
latest: 1;
|
|
170
|
+
versions: {
|
|
171
|
+
1: Payload<'invite.created v1'>;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
'invite.renewed': {
|
|
175
|
+
latest: 1;
|
|
176
|
+
versions: {
|
|
177
|
+
1: Payload<'invite.renewed v1'>;
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
'invite.cancelled': {
|
|
181
|
+
latest: 1;
|
|
182
|
+
versions: {
|
|
183
|
+
1: Payload<'invite.cancelled v1'>;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
'membership.created': {
|
|
187
|
+
latest: 1;
|
|
188
|
+
versions: {
|
|
189
|
+
1: Payload<'membership.created v1'>;
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
'membership.updated': {
|
|
193
|
+
latest: 1;
|
|
194
|
+
versions: {
|
|
195
|
+
1: Payload<'membership.updated v1'>;
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
'membership.deactivated': {
|
|
199
|
+
latest: 1;
|
|
200
|
+
versions: {
|
|
201
|
+
1: Payload<'membership.deactivated v1'>;
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
'membership.reactivated': {
|
|
205
|
+
latest: 1;
|
|
206
|
+
versions: {
|
|
207
|
+
1: Payload<'membership.reactivated v1'>;
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
'organization.created': {
|
|
211
|
+
latest: 1;
|
|
212
|
+
versions: {
|
|
213
|
+
1: Payload<'organization.created v1'>;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
'organization.updated': {
|
|
217
|
+
latest: 1;
|
|
218
|
+
versions: {
|
|
219
|
+
1: Payload<'organization.updated v1'>;
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
'plugin.installed': {
|
|
223
|
+
latest: 1;
|
|
224
|
+
versions: {
|
|
225
|
+
1: Payload<'plugin.installed v1'>;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
'plugin.enabled': {
|
|
229
|
+
latest: 1;
|
|
230
|
+
versions: {
|
|
231
|
+
1: Payload<'plugin.enabled v1'>;
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
'plugin.disabled': {
|
|
235
|
+
latest: 1;
|
|
236
|
+
versions: {
|
|
237
|
+
1: Payload<'plugin.disabled v1'>;
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
'plugin.uninstalled': {
|
|
241
|
+
latest: 1;
|
|
242
|
+
versions: {
|
|
243
|
+
1: Payload<'plugin.uninstalled v1'>;
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
'plugin.data_deleted': {
|
|
247
|
+
latest: 1;
|
|
248
|
+
versions: {
|
|
249
|
+
1: Payload<'plugin.data_deleted v1'>;
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
'plugin.updated': {
|
|
253
|
+
latest: 1;
|
|
254
|
+
versions: {
|
|
255
|
+
1: Payload<'plugin.updated v1'>;
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/events.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** What a call answering a file resolves to. */
|
|
2
|
+
export interface FileAnswer {
|
|
3
|
+
/** The file's bytes as they arrive: read it, pipe it or cancel it; it is never loaded whole. */
|
|
4
|
+
body: ReadableStream<Uint8Array>;
|
|
5
|
+
/** The file's name from `Content-Disposition` (its `filename*` first), or `null` when not given. */
|
|
6
|
+
fileName: string | null;
|
|
7
|
+
/** The file's size in bytes from `Content-Length`, or `null` when not given. */
|
|
8
|
+
size: number | null;
|
|
9
|
+
/** The answer's `Content-Type`, or `null` when not given. */
|
|
10
|
+
contentType: string | null;
|
|
11
|
+
}
|
|
12
|
+
/** The file answer of a successful `response`, its body left unread. */
|
|
13
|
+
export declare function fileAnswerOf(response: Response): FileAnswer;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// A file answer (a download, a preview, a thumbnail, the logo): the answer's own stream with what its
|
|
2
|
+
// headers say about the file. The client only reads those headers, never changes them.
|
|
3
|
+
/** The parameters of a `Content-Disposition` value, names in lower case, quoted values unquoted. */
|
|
4
|
+
function parametersOf(disposition) {
|
|
5
|
+
const parameters = new Map();
|
|
6
|
+
const parameter = /;\s*([^\s=;]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s;]*))/g;
|
|
7
|
+
for (const [, name = '', quoted, bare = ''] of disposition.matchAll(parameter)) {
|
|
8
|
+
const value = quoted === undefined ? bare : quoted.replace(/\\(.)/g, '$1');
|
|
9
|
+
parameters.set(name.toLowerCase(), value);
|
|
10
|
+
}
|
|
11
|
+
return parameters;
|
|
12
|
+
}
|
|
13
|
+
/** An RFC 8187 extended value (`UTF-8''Rechnung%20M%C3%A4rz.pdf`) as text, or `null` when unreadable. */
|
|
14
|
+
function extendedValue(value) {
|
|
15
|
+
const [, charset = '', encoded = ''] = /^([^']*)'[^']*'(.*)$/.exec(value) ?? [];
|
|
16
|
+
const bytes = [];
|
|
17
|
+
for (let at = 0; at < encoded.length; at += 1) {
|
|
18
|
+
if (encoded[at] === '%' && /^[\da-f]{2}$/i.test(encoded.slice(at + 1, at + 3))) {
|
|
19
|
+
bytes.push(Number.parseInt(encoded.slice(at + 1, at + 3), 16));
|
|
20
|
+
at += 2;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
bytes.push(encoded.charCodeAt(at));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return new TextDecoder(charset.toLowerCase(), { fatal: true }).decode(new Uint8Array(bytes));
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** The file name a `Content-Disposition` value gives, preferring `filename*` over `filename`. */
|
|
34
|
+
function fileNameOf(disposition) {
|
|
35
|
+
if (disposition === null)
|
|
36
|
+
return null;
|
|
37
|
+
const parameters = parametersOf(disposition);
|
|
38
|
+
const extended = parameters.get('filename*');
|
|
39
|
+
const fromExtended = extended === undefined ? null : extendedValue(extended);
|
|
40
|
+
return fromExtended ?? parameters.get('filename') ?? null;
|
|
41
|
+
}
|
|
42
|
+
function sizeOf(length) {
|
|
43
|
+
return length !== null && /^\d+$/.test(length.trim()) ? Number(length) : null;
|
|
44
|
+
}
|
|
45
|
+
/** The file answer of a successful `response`, its body left unread. */
|
|
46
|
+
export function fileAnswerOf(response) {
|
|
47
|
+
const { headers } = response;
|
|
48
|
+
return {
|
|
49
|
+
// An empty file has no body at all; it still gets a stream, an empty one.
|
|
50
|
+
body: response.body ?? new Blob([]).stream(),
|
|
51
|
+
fileName: fileNameOf(headers.get('content-disposition')),
|
|
52
|
+
size: sizeOf(headers.get('content-length')),
|
|
53
|
+
contentType: headers.get('content-type'),
|
|
54
|
+
};
|
|
55
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { PaperStackConnectionError, PaperStackError, type AnyErrorCode, type ErrorCode, type Problem, type ServerProblem, } from './errors.js';
|
|
2
|
+
export type { Fetch } from './answer.js';
|
|
3
|
+
export type { Catalogue, EventFrom, EventName, Flat, PaperStackEvent, PaperStackEventVersion, } from './events.js';
|
|
4
|
+
export type { EventVersions, Payload } from './events.gen.js';
|
|
5
|
+
export type { FileAnswer } from './file-answer.js';
|
|
6
|
+
export type { ArgsOf, ItemsOf, Page, Paginate, PagingMethod } from './paginate.js';
|
|
7
|
+
export { type Client, PaperStack, type PaperStackOptions } from './paper-stack.js';
|
|
8
|
+
export type { ResourceMethod, Resources, Table } from './resources.js';
|
|
9
|
+
export type { components, operations, webhooks } from './operations.gen.js';
|
|
10
|
+
export type { resources, routes } from './routes.gen.js';
|
|
11
|
+
export type { Answer, AnswerOfSuccess, BodyOf, BodyRequired, CallOptions, ContentOf, Flatten, HasRequired, IsCallable, IsNever, Json, Multipart, OperationOf, ParameterOf, Part, PathOf, PublicRoute, QueryOf, RequestArgs, RequestBodyOf, RequestRest, Route, Sendable, SuccessOf, SuccessStatus, } from './route-types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The file at `path` as a `File` to upload, named after the path's last part. Its bytes are read
|
|
3
|
+
* from disk bit by bit as the upload sends them, never loaded whole into memory. Changing the file
|
|
4
|
+
* on disk before it is sent makes reading it fail.
|
|
5
|
+
*/
|
|
6
|
+
export declare function fileFromPath(path: string): Promise<File>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { openAsBlob } from 'node:fs';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* The file at `path` as a `File` to upload, named after the path's last part. Its bytes are read
|
|
5
|
+
* from disk bit by bit as the upload sends them, never loaded whole into memory. Changing the file
|
|
6
|
+
* on disk before it is sent makes reading it fail.
|
|
7
|
+
*/
|
|
8
|
+
export async function fileFromPath(path) {
|
|
9
|
+
const blob = await openAsBlob(path);
|
|
10
|
+
// A File made of a Blob keeps pointing at the same bytes on disk; nothing is read here.
|
|
11
|
+
return new File([blob], basename(path));
|
|
12
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The `/node` entry: helpers that need Node itself, reading a file from disk to upload and saving a
|
|
2
|
+
// download to disk. It has its own tsconfig with Node's types; the main entry never imports from here.
|
|
3
|
+
export { fileFromPath } from './file-from-path.js';
|
|
4
|
+
export { saveToFile } from './save-to-file.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FileAnswer } from '../file-answer.js';
|
|
2
|
+
/**
|
|
3
|
+
* Streams a file answer (`ps.documents.download(...)` and the like) straight to disk at `path`,
|
|
4
|
+
* piece by piece as it arrives, so it never sits whole in memory. When the download breaks off or
|
|
5
|
+
* is cancelled, the half-written file is removed and the error passes on.
|
|
6
|
+
*/
|
|
7
|
+
export declare function saveToFile(download: Pick<FileAnswer, 'body'>, path: string): Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import { pipeline } from 'node:stream/promises';
|
|
5
|
+
/**
|
|
6
|
+
* Streams a file answer (`ps.documents.download(...)` and the like) straight to disk at `path`,
|
|
7
|
+
* piece by piece as it arrives, so it never sits whole in memory. When the download breaks off or
|
|
8
|
+
* is cancelled, the half-written file is removed and the error passes on.
|
|
9
|
+
*/
|
|
10
|
+
export async function saveToFile(download, path) {
|
|
11
|
+
try {
|
|
12
|
+
await pipeline(Readable.fromWeb(download.body), createWriteStream(path));
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
await rm(path, { force: true });
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
}
|