filejar 0.4.0 → 0.6.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/CHANGELOG.md +22 -0
- package/README.md +29 -11
- package/package.json +1 -1
- package/resources/upload.d.mts +116 -37
- package/resources/upload.d.mts.map +1 -1
- package/resources/upload.d.ts +116 -37
- package/resources/upload.d.ts.map +1 -1
- package/resources/upload.js +122 -10
- package/resources/upload.js.map +1 -1
- package/resources/upload.mjs +122 -10
- package/resources/upload.mjs.map +1 -1
- package/src/resources/upload.ts +229 -39
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.6.0 (2026-01-03)
|
|
4
|
+
|
|
5
|
+
Full Changelog: [v0.5.0...v0.6.0](https://github.com/filejar/node/compare/v0.5.0...v0.6.0)
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
* **api:** api update ([00b875b](https://github.com/filejar/node/commit/00b875bd36e6d6839ca7d294c1831c1f5e70f2a2))
|
|
10
|
+
* **api:** api update ([51aff53](https://github.com/filejar/node/commit/51aff536b805929a85a2a1cf650465f955ece363))
|
|
11
|
+
* **api:** api update ([9f67e36](https://github.com/filejar/node/commit/9f67e365bb6d2324c904dc1f2d4ca6134bffffaa))
|
|
12
|
+
* **api:** manual updates ([1a6c4bf](https://github.com/filejar/node/commit/1a6c4bf09534aa5c91360b28e63e20d361a23a0a))
|
|
13
|
+
* **api:** manual updates ([4e2646f](https://github.com/filejar/node/commit/4e2646f9ebf459caf0663e1ad660b0de1cf1d66d))
|
|
14
|
+
|
|
15
|
+
## 0.5.0 (2026-01-02)
|
|
16
|
+
|
|
17
|
+
Full Changelog: [v0.4.0...v0.5.0](https://github.com/filejar/node/compare/v0.4.0...v0.5.0)
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* **upload:** add uploadFile method for handling various file types ([0b9e760](https://github.com/filejar/node/commit/0b9e760d84b089ee000ec9133c3ea02ed4b8f7f5))
|
|
22
|
+
* **upload:** enhance upload method to return structured response with acknowledgment and preparation details ([20d7d38](https://github.com/filejar/node/commit/20d7d38abef7a8b3a5128b33091783eacf6ecb71))
|
|
23
|
+
* **upload:** improve error handling and enhance upload logging with detailed response information ([2bcfb37](https://github.com/filejar/node/commit/2bcfb379e41f054e61bf79ddd4b3162c453502fb))
|
|
24
|
+
|
|
3
25
|
## 0.4.0 (2026-01-02)
|
|
4
26
|
|
|
5
27
|
Full Changelog: [v0.3.1...v0.4.0](https://github.com/filejar/node/compare/v0.3.1...v0.4.0)
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
This library provides convenient access to the Filejar REST API from server-side TypeScript or JavaScript.
|
|
6
6
|
|
|
7
|
-
The full API of this library can be found in [api.md](api.md).
|
|
7
|
+
The REST API documentation can be found on [docs.filejar.dev](https://docs.filejar.dev). The full API of this library can be found in [api.md](api.md).
|
|
8
8
|
|
|
9
9
|
It is generated with [Stainless](https://www.stainless.com/).
|
|
10
10
|
|
|
@@ -27,9 +27,7 @@ const client = new Filejar({
|
|
|
27
27
|
environment: 'local', // defaults to 'production'
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
const response = await client.upload.prepare({
|
|
31
|
-
|
|
32
|
-
console.log(response.upload_id);
|
|
30
|
+
const response = await client.upload.prepare({ body: [{}, {}, {}] });
|
|
33
31
|
```
|
|
34
32
|
|
|
35
33
|
### Request & Response types
|
|
@@ -45,7 +43,12 @@ const client = new Filejar({
|
|
|
45
43
|
environment: 'local', // defaults to 'production'
|
|
46
44
|
});
|
|
47
45
|
|
|
48
|
-
const params: Filejar.UploadAcknowledgeParams = {
|
|
46
|
+
const params: Filejar.UploadAcknowledgeParams = {
|
|
47
|
+
body: [
|
|
48
|
+
{ upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
49
|
+
{ upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
50
|
+
],
|
|
51
|
+
};
|
|
49
52
|
const response: Filejar.UploadAcknowledgeResponse = await client.upload.acknowledge(params);
|
|
50
53
|
```
|
|
51
54
|
|
|
@@ -60,7 +63,12 @@ a subclass of `APIError` will be thrown:
|
|
|
60
63
|
<!-- prettier-ignore -->
|
|
61
64
|
```ts
|
|
62
65
|
const response = await client.upload
|
|
63
|
-
.acknowledge({
|
|
66
|
+
.acknowledge({
|
|
67
|
+
body: [
|
|
68
|
+
{ upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
69
|
+
{ upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
70
|
+
],
|
|
71
|
+
})
|
|
64
72
|
.catch(async (err) => {
|
|
65
73
|
if (err instanceof Filejar.APIError) {
|
|
66
74
|
console.log(err.status); // 400
|
|
@@ -101,7 +109,7 @@ const client = new Filejar({
|
|
|
101
109
|
});
|
|
102
110
|
|
|
103
111
|
// Or, configure per-request:
|
|
104
|
-
await client.upload.acknowledge({ upload_id: '
|
|
112
|
+
await client.upload.acknowledge({ body: [{ upload_id: '123e4567-e89b-12d3-a456-426614174000' }, { upload_id: '223e4567-e89b-12d3-a456-426614174001' }] }, {
|
|
105
113
|
maxRetries: 5,
|
|
106
114
|
});
|
|
107
115
|
```
|
|
@@ -118,7 +126,7 @@ const client = new Filejar({
|
|
|
118
126
|
});
|
|
119
127
|
|
|
120
128
|
// Override per-request:
|
|
121
|
-
await client.upload.acknowledge({ upload_id: '
|
|
129
|
+
await client.upload.acknowledge({ body: [{ upload_id: '123e4567-e89b-12d3-a456-426614174000' }, { upload_id: '223e4567-e89b-12d3-a456-426614174001' }] }, {
|
|
122
130
|
timeout: 5 * 1000,
|
|
123
131
|
});
|
|
124
132
|
```
|
|
@@ -142,16 +150,26 @@ Unlike `.asResponse()` this method consumes the body, returning once it is parse
|
|
|
142
150
|
const client = new Filejar();
|
|
143
151
|
|
|
144
152
|
const response = await client.upload
|
|
145
|
-
.acknowledge({
|
|
153
|
+
.acknowledge({
|
|
154
|
+
body: [
|
|
155
|
+
{ upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
156
|
+
{ upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
157
|
+
],
|
|
158
|
+
})
|
|
146
159
|
.asResponse();
|
|
147
160
|
console.log(response.headers.get('X-My-Header'));
|
|
148
161
|
console.log(response.statusText); // access the underlying Response object
|
|
149
162
|
|
|
150
163
|
const { data: response, response: raw } = await client.upload
|
|
151
|
-
.acknowledge({
|
|
164
|
+
.acknowledge({
|
|
165
|
+
body: [
|
|
166
|
+
{ upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
167
|
+
{ upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
168
|
+
],
|
|
169
|
+
})
|
|
152
170
|
.withResponse();
|
|
153
171
|
console.log(raw.headers.get('X-My-Header'));
|
|
154
|
-
console.log(response
|
|
172
|
+
console.log(response);
|
|
155
173
|
```
|
|
156
174
|
|
|
157
175
|
### Logging
|
package/package.json
CHANGED
package/resources/upload.d.mts
CHANGED
|
@@ -1,73 +1,152 @@
|
|
|
1
1
|
import { APIResource } from "../core/resource.mjs";
|
|
2
2
|
import { APIPromise } from "../core/api-promise.mjs";
|
|
3
3
|
import { RequestOptions } from "../internal/request-options.mjs";
|
|
4
|
+
import { type ToFileInput } from "../internal/to-file.mjs";
|
|
4
5
|
export declare class Upload extends APIResource {
|
|
5
6
|
/**
|
|
6
|
-
* Acknowledges that
|
|
7
|
-
*
|
|
7
|
+
* Acknowledges that one or more file uploads have been completed. This endpoint
|
|
8
|
+
* verifies files exist in storage and updates upload records with metadata.
|
|
9
|
+
* Processes multiple uploads in parallel. Each result in the response array may be
|
|
10
|
+
* a success or error object.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```ts
|
|
11
14
|
* const response = await client.upload.acknowledge({
|
|
12
|
-
*
|
|
15
|
+
* body: [
|
|
16
|
+
* { upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
17
|
+
* { upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
18
|
+
* ],
|
|
13
19
|
* });
|
|
14
20
|
* ```
|
|
15
21
|
*/
|
|
16
|
-
acknowledge(
|
|
22
|
+
acknowledge(params: UploadAcknowledgeParams, options?: RequestOptions): APIPromise<UploadAcknowledgeResponse>;
|
|
17
23
|
/**
|
|
18
|
-
* Creates
|
|
19
|
-
* authentication.
|
|
24
|
+
* Creates presigned URLs for one or more file uploads to Filejar storage.
|
|
25
|
+
* Processes multiple files in parallel. Requires API key authentication.
|
|
20
26
|
*
|
|
21
27
|
* @example
|
|
22
28
|
* ```ts
|
|
23
|
-
* const response = await client.upload.prepare(
|
|
29
|
+
* const response = await client.upload.prepare({
|
|
30
|
+
* body: [{}, {}, {}],
|
|
31
|
+
* });
|
|
24
32
|
* ```
|
|
25
33
|
*/
|
|
26
|
-
prepare(
|
|
27
|
-
}
|
|
28
|
-
export interface UploadAcknowledgeResponse {
|
|
29
|
-
/**
|
|
30
|
-
* Timestamp when the upload was acknowledged
|
|
31
|
-
*/
|
|
32
|
-
acknowledged_at: string;
|
|
33
|
-
/**
|
|
34
|
-
* MIME type of the uploaded file
|
|
35
|
-
*/
|
|
36
|
-
content_type: string;
|
|
34
|
+
prepare(params: UploadPrepareParams, options?: RequestOptions): APIPromise<UploadPrepareResponse>;
|
|
37
35
|
/**
|
|
38
|
-
*
|
|
36
|
+
* Uploads files to signed URLs.
|
|
37
|
+
*
|
|
38
|
+
* Accepts File objects, Buffers, ArrayBuffers, or other uploadable types.
|
|
39
|
+
* If params are not provided, file names will be extracted from File objects
|
|
40
|
+
* or generated automatically.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // With explicit file names
|
|
45
|
+
* const response = await client.upload.uploadFile([file1, file2], {
|
|
46
|
+
* body: [{ file_name: 'file1.txt' }, { file_name: 'file2.txt' }],
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // Without params (file names extracted from File objects or auto-generated)
|
|
50
|
+
* const response = await client.upload.uploadFile([file1, file2]);
|
|
51
|
+
* ```
|
|
39
52
|
*/
|
|
40
|
-
|
|
53
|
+
uploadFile(files: Array<ToFileInput>, params?: UploadPrepareParams): Promise<{
|
|
54
|
+
response: {
|
|
55
|
+
key: string;
|
|
56
|
+
upload_id: string;
|
|
57
|
+
}[];
|
|
58
|
+
acknowledge: UploadAcknowledgeResponse;
|
|
59
|
+
prepare: UploadPrepareResponse;
|
|
60
|
+
}>;
|
|
41
61
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Array of acknowledgment results. Each item may be a success object or an error
|
|
64
|
+
* object.
|
|
65
|
+
*/
|
|
66
|
+
export type UploadAcknowledgeResponse = Array<UploadAcknowledgeResponse.UnionMember0 | UploadAcknowledgeResponse.UnionMember1>;
|
|
67
|
+
export declare namespace UploadAcknowledgeResponse {
|
|
47
68
|
/**
|
|
48
|
-
*
|
|
69
|
+
* Successful acknowledgment
|
|
49
70
|
*/
|
|
50
|
-
|
|
71
|
+
interface UnionMember0 {
|
|
72
|
+
/**
|
|
73
|
+
* Timestamp when the upload was acknowledged
|
|
74
|
+
*/
|
|
75
|
+
acknowledged_at: string;
|
|
76
|
+
/**
|
|
77
|
+
* MIME type of the uploaded file
|
|
78
|
+
*/
|
|
79
|
+
content_type: string;
|
|
80
|
+
/**
|
|
81
|
+
* File size in bytes
|
|
82
|
+
*/
|
|
83
|
+
size: number;
|
|
84
|
+
/**
|
|
85
|
+
* The upload ID that was acknowledged
|
|
86
|
+
*/
|
|
87
|
+
upload_id: string;
|
|
88
|
+
}
|
|
51
89
|
/**
|
|
52
|
-
*
|
|
90
|
+
* Error for a specific upload
|
|
53
91
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
92
|
+
interface UnionMember1 {
|
|
93
|
+
/**
|
|
94
|
+
* Error message (e.g., 'Upload not found' or 'Upload is not completed yet')
|
|
95
|
+
*/
|
|
96
|
+
error: string;
|
|
97
|
+
/**
|
|
98
|
+
* The upload ID that failed
|
|
99
|
+
*/
|
|
100
|
+
upload_id: string;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Array of prepared uploads
|
|
105
|
+
*/
|
|
106
|
+
export type UploadPrepareResponse = Array<UploadPrepareResponse.UploadPrepareResponseItem>;
|
|
107
|
+
export declare namespace UploadPrepareResponse {
|
|
108
|
+
interface UploadPrepareResponseItem {
|
|
109
|
+
/**
|
|
110
|
+
* File key/path in storage
|
|
111
|
+
*/
|
|
112
|
+
key: string;
|
|
113
|
+
/**
|
|
114
|
+
* Proxy URL for uploading files through the API (PUT request to /v1/files/{key})
|
|
115
|
+
*/
|
|
116
|
+
signed_url: string;
|
|
117
|
+
/**
|
|
118
|
+
* Unique identifier for this upload
|
|
119
|
+
*/
|
|
120
|
+
upload_id: string;
|
|
121
|
+
}
|
|
59
122
|
}
|
|
60
123
|
export interface UploadAcknowledgeParams {
|
|
61
124
|
/**
|
|
62
|
-
*
|
|
125
|
+
* Array of upload IDs to acknowledge
|
|
63
126
|
*/
|
|
64
|
-
|
|
127
|
+
body: Array<UploadAcknowledgeParams.Body>;
|
|
128
|
+
}
|
|
129
|
+
export declare namespace UploadAcknowledgeParams {
|
|
130
|
+
interface Body {
|
|
131
|
+
/**
|
|
132
|
+
* The upload ID returned from /prepare endpoint
|
|
133
|
+
*/
|
|
134
|
+
upload_id: string;
|
|
135
|
+
}
|
|
65
136
|
}
|
|
66
137
|
export interface UploadPrepareParams {
|
|
67
138
|
/**
|
|
68
|
-
*
|
|
139
|
+
* Array of files to prepare for upload
|
|
69
140
|
*/
|
|
70
|
-
|
|
141
|
+
body: Array<UploadPrepareParams.Body>;
|
|
142
|
+
}
|
|
143
|
+
export declare namespace UploadPrepareParams {
|
|
144
|
+
interface Body {
|
|
145
|
+
/**
|
|
146
|
+
* Optional file name. If not provided, a random UUID will be used.
|
|
147
|
+
*/
|
|
148
|
+
file_name?: string;
|
|
149
|
+
}
|
|
71
150
|
}
|
|
72
151
|
export declare namespace Upload {
|
|
73
152
|
export { type UploadAcknowledgeResponse as UploadAcknowledgeResponse, type UploadPrepareResponse as UploadPrepareResponse, type UploadAcknowledgeParams as UploadAcknowledgeParams, type UploadPrepareParams as UploadPrepareParams, };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.d.mts","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;OACf,EAAE,UAAU,EAAE;OACd,EAAE,cAAc,EAAE;
|
|
1
|
+
{"version":3,"file":"upload.d.mts","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;OACf,EAAE,UAAU,EAAE;OACd,EAAE,cAAc,EAAE;OAClB,EAAU,KAAK,WAAW,EAAE;AAEnC,qBAAa,MAAO,SAAQ,WAAW;IACrC;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CACT,MAAM,EAAE,uBAAuB,EAC/B,OAAO,CAAC,EAAE,cAAc,GACvB,UAAU,CAAC,yBAAyB,CAAC;IAKxC;;;;;;;;;;OAUG;IACH,OAAO,CAAC,MAAM,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,UAAU,CAAC,qBAAqB,CAAC;IAKjG;;;;;;;;;;;;;;;;;OAiBG;IACG,UAAU,CACd,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,EACzB,MAAM,CAAC,EAAE,mBAAmB,GAC3B,OAAO,CAAC;QACT,QAAQ,EAAE;YACR,GAAG,EAAE,MAAM,CAAC;YACZ,SAAS,EAAE,MAAM,CAAC;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EG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB;CACF;AAED,MAAM,CAAC,OAAO,WAAW,MAAM,CAAC;IAC9B,OAAO,EACL,KAAK,yBAAyB,IAAI,yBAAyB,EAC3D,KAAK,qBAAqB,IAAI,qBAAqB,EACnD,KAAK,uBAAuB,IAAI,uBAAuB,EACvD,KAAK,mBAAmB,IAAI,mBAAmB,GAChD,CAAC;CACH"}
|
package/resources/upload.d.ts
CHANGED
|
@@ -1,73 +1,152 @@
|
|
|
1
1
|
import { APIResource } from "../core/resource.js";
|
|
2
2
|
import { APIPromise } from "../core/api-promise.js";
|
|
3
3
|
import { RequestOptions } from "../internal/request-options.js";
|
|
4
|
+
import { type ToFileInput } from "../internal/to-file.js";
|
|
4
5
|
export declare class Upload extends APIResource {
|
|
5
6
|
/**
|
|
6
|
-
* Acknowledges that
|
|
7
|
-
*
|
|
7
|
+
* Acknowledges that one or more file uploads have been completed. This endpoint
|
|
8
|
+
* verifies files exist in storage and updates upload records with metadata.
|
|
9
|
+
* Processes multiple uploads in parallel. Each result in the response array may be
|
|
10
|
+
* a success or error object.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```ts
|
|
11
14
|
* const response = await client.upload.acknowledge({
|
|
12
|
-
*
|
|
15
|
+
* body: [
|
|
16
|
+
* { upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
17
|
+
* { upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
18
|
+
* ],
|
|
13
19
|
* });
|
|
14
20
|
* ```
|
|
15
21
|
*/
|
|
16
|
-
acknowledge(
|
|
22
|
+
acknowledge(params: UploadAcknowledgeParams, options?: RequestOptions): APIPromise<UploadAcknowledgeResponse>;
|
|
17
23
|
/**
|
|
18
|
-
* Creates
|
|
19
|
-
* authentication.
|
|
24
|
+
* Creates presigned URLs for one or more file uploads to Filejar storage.
|
|
25
|
+
* Processes multiple files in parallel. Requires API key authentication.
|
|
20
26
|
*
|
|
21
27
|
* @example
|
|
22
28
|
* ```ts
|
|
23
|
-
* const response = await client.upload.prepare(
|
|
29
|
+
* const response = await client.upload.prepare({
|
|
30
|
+
* body: [{}, {}, {}],
|
|
31
|
+
* });
|
|
24
32
|
* ```
|
|
25
33
|
*/
|
|
26
|
-
prepare(
|
|
27
|
-
}
|
|
28
|
-
export interface UploadAcknowledgeResponse {
|
|
29
|
-
/**
|
|
30
|
-
* Timestamp when the upload was acknowledged
|
|
31
|
-
*/
|
|
32
|
-
acknowledged_at: string;
|
|
33
|
-
/**
|
|
34
|
-
* MIME type of the uploaded file
|
|
35
|
-
*/
|
|
36
|
-
content_type: string;
|
|
34
|
+
prepare(params: UploadPrepareParams, options?: RequestOptions): APIPromise<UploadPrepareResponse>;
|
|
37
35
|
/**
|
|
38
|
-
*
|
|
36
|
+
* Uploads files to signed URLs.
|
|
37
|
+
*
|
|
38
|
+
* Accepts File objects, Buffers, ArrayBuffers, or other uploadable types.
|
|
39
|
+
* If params are not provided, file names will be extracted from File objects
|
|
40
|
+
* or generated automatically.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* // With explicit file names
|
|
45
|
+
* const response = await client.upload.uploadFile([file1, file2], {
|
|
46
|
+
* body: [{ file_name: 'file1.txt' }, { file_name: 'file2.txt' }],
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // Without params (file names extracted from File objects or auto-generated)
|
|
50
|
+
* const response = await client.upload.uploadFile([file1, file2]);
|
|
51
|
+
* ```
|
|
39
52
|
*/
|
|
40
|
-
|
|
53
|
+
uploadFile(files: Array<ToFileInput>, params?: UploadPrepareParams): Promise<{
|
|
54
|
+
response: {
|
|
55
|
+
key: string;
|
|
56
|
+
upload_id: string;
|
|
57
|
+
}[];
|
|
58
|
+
acknowledge: UploadAcknowledgeResponse;
|
|
59
|
+
prepare: UploadPrepareResponse;
|
|
60
|
+
}>;
|
|
41
61
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Array of acknowledgment results. Each item may be a success object or an error
|
|
64
|
+
* object.
|
|
65
|
+
*/
|
|
66
|
+
export type UploadAcknowledgeResponse = Array<UploadAcknowledgeResponse.UnionMember0 | UploadAcknowledgeResponse.UnionMember1>;
|
|
67
|
+
export declare namespace UploadAcknowledgeResponse {
|
|
47
68
|
/**
|
|
48
|
-
*
|
|
69
|
+
* Successful acknowledgment
|
|
49
70
|
*/
|
|
50
|
-
|
|
71
|
+
interface UnionMember0 {
|
|
72
|
+
/**
|
|
73
|
+
* Timestamp when the upload was acknowledged
|
|
74
|
+
*/
|
|
75
|
+
acknowledged_at: string;
|
|
76
|
+
/**
|
|
77
|
+
* MIME type of the uploaded file
|
|
78
|
+
*/
|
|
79
|
+
content_type: string;
|
|
80
|
+
/**
|
|
81
|
+
* File size in bytes
|
|
82
|
+
*/
|
|
83
|
+
size: number;
|
|
84
|
+
/**
|
|
85
|
+
* The upload ID that was acknowledged
|
|
86
|
+
*/
|
|
87
|
+
upload_id: string;
|
|
88
|
+
}
|
|
51
89
|
/**
|
|
52
|
-
*
|
|
90
|
+
* Error for a specific upload
|
|
53
91
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
92
|
+
interface UnionMember1 {
|
|
93
|
+
/**
|
|
94
|
+
* Error message (e.g., 'Upload not found' or 'Upload is not completed yet')
|
|
95
|
+
*/
|
|
96
|
+
error: string;
|
|
97
|
+
/**
|
|
98
|
+
* The upload ID that failed
|
|
99
|
+
*/
|
|
100
|
+
upload_id: string;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Array of prepared uploads
|
|
105
|
+
*/
|
|
106
|
+
export type UploadPrepareResponse = Array<UploadPrepareResponse.UploadPrepareResponseItem>;
|
|
107
|
+
export declare namespace UploadPrepareResponse {
|
|
108
|
+
interface UploadPrepareResponseItem {
|
|
109
|
+
/**
|
|
110
|
+
* File key/path in storage
|
|
111
|
+
*/
|
|
112
|
+
key: string;
|
|
113
|
+
/**
|
|
114
|
+
* Proxy URL for uploading files through the API (PUT request to /v1/files/{key})
|
|
115
|
+
*/
|
|
116
|
+
signed_url: string;
|
|
117
|
+
/**
|
|
118
|
+
* Unique identifier for this upload
|
|
119
|
+
*/
|
|
120
|
+
upload_id: string;
|
|
121
|
+
}
|
|
59
122
|
}
|
|
60
123
|
export interface UploadAcknowledgeParams {
|
|
61
124
|
/**
|
|
62
|
-
*
|
|
125
|
+
* Array of upload IDs to acknowledge
|
|
63
126
|
*/
|
|
64
|
-
|
|
127
|
+
body: Array<UploadAcknowledgeParams.Body>;
|
|
128
|
+
}
|
|
129
|
+
export declare namespace UploadAcknowledgeParams {
|
|
130
|
+
interface Body {
|
|
131
|
+
/**
|
|
132
|
+
* The upload ID returned from /prepare endpoint
|
|
133
|
+
*/
|
|
134
|
+
upload_id: string;
|
|
135
|
+
}
|
|
65
136
|
}
|
|
66
137
|
export interface UploadPrepareParams {
|
|
67
138
|
/**
|
|
68
|
-
*
|
|
139
|
+
* Array of files to prepare for upload
|
|
69
140
|
*/
|
|
70
|
-
|
|
141
|
+
body: Array<UploadPrepareParams.Body>;
|
|
142
|
+
}
|
|
143
|
+
export declare namespace UploadPrepareParams {
|
|
144
|
+
interface Body {
|
|
145
|
+
/**
|
|
146
|
+
* Optional file name. If not provided, a random UUID will be used.
|
|
147
|
+
*/
|
|
148
|
+
file_name?: string;
|
|
149
|
+
}
|
|
71
150
|
}
|
|
72
151
|
export declare namespace Upload {
|
|
73
152
|
export { type UploadAcknowledgeResponse as UploadAcknowledgeResponse, type UploadPrepareResponse as UploadPrepareResponse, type UploadAcknowledgeParams as UploadAcknowledgeParams, type UploadPrepareParams as UploadPrepareParams, };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":"OAEO,E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B;;WAEG;QACH,KAAK,EAAE,MAAM,CAAC;QAEd;;WAEG;QACH,SAAS,EAAE,MAAM,CAAC;KACnB;CACF;AAED;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,qBAAqB,CAAC,yBAAyB,CAAC,CAAC;AAE3F,yBAAiB,qBAAqB,CAAC;IACrC,UAAiB,yBAAyB;QACxC;;WAEG;QACH,GAAG,EAAE,MAAM,CAAC;QAEZ;;WAEG;QACH,UAAU,EAAE,MAAM,CAAC;QAEnB;;WAEG;QACH,SAAS,EAAE,MAAM,CAAC;KACnB;CACF;AAED,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,IAAI,EAAE,KAAK,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;CAC3C;AAED,yBAAiB,uBAAuB,CAAC;IACvC,UAAiB,IAAI;QACnB;;WAEG;QACH,SAAS,EAAE,MAAM,CAAC;KACnB;CACF;AAED,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,KAAK,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC;CACvC;AAED,yBAAiB,mBAAmB,CAAC;IACnC,UAAiB,IAAI;QACnB;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB;CACF;AAED,MAAM,CAAC,OAAO,WAAW,MAAM,CAAC;IAC9B,OAAO,EACL,KAAK,yBAAyB,IAAI,yBAAyB,EAC3D,KAAK,qBAAqB,IAAI,qBAAqB,EACnD,KAAK,uBAAuB,IAAI,uBAAuB,EACvD,KAAK,mBAAmB,IAAI,mBAAmB,GAChD,CAAC;CACH"}
|
package/resources/upload.js
CHANGED
|
@@ -3,32 +3,144 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Upload = void 0;
|
|
5
5
|
const resource_1 = require("../core/resource.js");
|
|
6
|
+
const to_file_1 = require("../internal/to-file.js");
|
|
6
7
|
class Upload extends resource_1.APIResource {
|
|
7
8
|
/**
|
|
8
|
-
* Acknowledges that
|
|
9
|
-
*
|
|
9
|
+
* Acknowledges that one or more file uploads have been completed. This endpoint
|
|
10
|
+
* verifies files exist in storage and updates upload records with metadata.
|
|
11
|
+
* Processes multiple uploads in parallel. Each result in the response array may be
|
|
12
|
+
* a success or error object.
|
|
10
13
|
*
|
|
11
14
|
* @example
|
|
12
15
|
* ```ts
|
|
13
16
|
* const response = await client.upload.acknowledge({
|
|
14
|
-
*
|
|
17
|
+
* body: [
|
|
18
|
+
* { upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
19
|
+
* { upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
20
|
+
* ],
|
|
15
21
|
* });
|
|
16
22
|
* ```
|
|
17
23
|
*/
|
|
18
|
-
acknowledge(
|
|
19
|
-
|
|
24
|
+
acknowledge(params, options) {
|
|
25
|
+
const { body } = params;
|
|
26
|
+
return this._client.post('/v1/upload/ack', { body: body, ...options });
|
|
20
27
|
}
|
|
21
28
|
/**
|
|
22
|
-
* Creates
|
|
23
|
-
* authentication.
|
|
29
|
+
* Creates presigned URLs for one or more file uploads to Filejar storage.
|
|
30
|
+
* Processes multiple files in parallel. Requires API key authentication.
|
|
24
31
|
*
|
|
25
32
|
* @example
|
|
26
33
|
* ```ts
|
|
27
|
-
* const response = await client.upload.prepare(
|
|
34
|
+
* const response = await client.upload.prepare({
|
|
35
|
+
* body: [{}, {}, {}],
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
prepare(params, options) {
|
|
40
|
+
const { body } = params;
|
|
41
|
+
return this._client.post('/v1/upload/prepare', { body: body, ...options });
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Uploads files to signed URLs.
|
|
45
|
+
*
|
|
46
|
+
* Accepts File objects, Buffers, ArrayBuffers, or other uploadable types.
|
|
47
|
+
* If params are not provided, file names will be extracted from File objects
|
|
48
|
+
* or generated automatically.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* // With explicit file names
|
|
53
|
+
* const response = await client.upload.uploadFile([file1, file2], {
|
|
54
|
+
* body: [{ file_name: 'file1.txt' }, { file_name: 'file2.txt' }],
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* // Without params (file names extracted from File objects or auto-generated)
|
|
58
|
+
* const response = await client.upload.uploadFile([file1, file2]);
|
|
28
59
|
* ```
|
|
29
60
|
*/
|
|
30
|
-
|
|
31
|
-
|
|
61
|
+
async uploadFile(files, params) {
|
|
62
|
+
// Helper function to extract file extension
|
|
63
|
+
const getFileExtension = (fileName) => {
|
|
64
|
+
const lastDot = fileName.lastIndexOf('.');
|
|
65
|
+
return lastDot > 0 ? fileName.substring(lastDot) : '';
|
|
66
|
+
};
|
|
67
|
+
// Helper function to get extension from MIME type
|
|
68
|
+
const getExtensionFromMimeType = (mimeType) => {
|
|
69
|
+
if (!mimeType)
|
|
70
|
+
return '';
|
|
71
|
+
const mimeToExt = {
|
|
72
|
+
'image/jpeg': '.jpg',
|
|
73
|
+
'image/png': '.png',
|
|
74
|
+
'image/gif': '.gif',
|
|
75
|
+
'image/webp': '.webp',
|
|
76
|
+
'text/plain': '.txt',
|
|
77
|
+
'text/html': '.html',
|
|
78
|
+
'application/json': '.json',
|
|
79
|
+
'application/pdf': '.pdf',
|
|
80
|
+
'video/mp4': '.mp4',
|
|
81
|
+
'audio/mpeg': '.mp3',
|
|
82
|
+
};
|
|
83
|
+
return mimeToExt[mimeType] || '';
|
|
84
|
+
};
|
|
85
|
+
// Generate params if not provided
|
|
86
|
+
const prepareParams = params ??
|
|
87
|
+
{
|
|
88
|
+
body: files.map((file, index) => {
|
|
89
|
+
let fileName;
|
|
90
|
+
let mimeType;
|
|
91
|
+
// Try to extract file name from File object
|
|
92
|
+
if (file instanceof File) {
|
|
93
|
+
fileName = file.name;
|
|
94
|
+
mimeType = file.type;
|
|
95
|
+
}
|
|
96
|
+
// For other types, try to get name from the file-like object
|
|
97
|
+
else if (typeof file === 'object' && file !== null) {
|
|
98
|
+
if ('name' in file && file.name) {
|
|
99
|
+
fileName = String(file.name);
|
|
100
|
+
}
|
|
101
|
+
if ('type' in file && file.type) {
|
|
102
|
+
mimeType = String(file.type);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// If we have a file name, ensure it has an extension
|
|
106
|
+
if (fileName) {
|
|
107
|
+
const ext = getFileExtension(fileName);
|
|
108
|
+
// If no extension found, try to infer from MIME type
|
|
109
|
+
if (!ext && mimeType) {
|
|
110
|
+
const inferredExt = getExtensionFromMimeType(mimeType);
|
|
111
|
+
if (inferredExt) {
|
|
112
|
+
fileName = fileName + inferredExt;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return { file_name: fileName };
|
|
116
|
+
}
|
|
117
|
+
// Generate a unique default name with extension
|
|
118
|
+
const defaultExt = mimeType ? getExtensionFromMimeType(mimeType) : '';
|
|
119
|
+
return {
|
|
120
|
+
file_name: `file-${Date.now()}-${index}-${Math.random().toString(36).substring(7)}${defaultExt}`,
|
|
121
|
+
};
|
|
122
|
+
}),
|
|
123
|
+
};
|
|
124
|
+
const preparedResponse = await this.prepare(prepareParams);
|
|
125
|
+
const uploadResponses = await Promise.all(files.map(async (file) => {
|
|
126
|
+
const fileObj = await (0, to_file_1.toFile)(file);
|
|
127
|
+
const uploadResponse = await this._client.put(preparedResponse[files.indexOf(file)]?.signed_url ?? '', { body: fileObj });
|
|
128
|
+
return {
|
|
129
|
+
key: preparedResponse[files.indexOf(file)]?.key ?? '',
|
|
130
|
+
upload_id: preparedResponse[files.indexOf(file)]?.upload_id ?? '',
|
|
131
|
+
};
|
|
132
|
+
}));
|
|
133
|
+
const acknowledgeResponses = await this.acknowledge({
|
|
134
|
+
body: uploadResponses.map((response) => ({ upload_id: response.upload_id })),
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
response: uploadResponses.map((upload) => ({
|
|
138
|
+
key: upload.key,
|
|
139
|
+
upload_id: upload.upload_id,
|
|
140
|
+
})) ?? [],
|
|
141
|
+
acknowledge: acknowledgeResponses,
|
|
142
|
+
prepare: preparedResponse,
|
|
143
|
+
};
|
|
32
144
|
}
|
|
33
145
|
}
|
|
34
146
|
exports.Upload = Upload;
|
package/resources/upload.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.js","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAA+C;
|
|
1
|
+
{"version":3,"file":"upload.js","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAA+C;AAG/C,oDAA+D;AAE/D,MAAa,MAAO,SAAQ,sBAAW;IACrC;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CACT,MAA+B,EAC/B,OAAwB;QAExB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IACzE,CAAC;IAED;;;;;;;;;;OAUG;IACH,OAAO,CAAC,MAA2B,EAAE,OAAwB;QAC3D,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC7E,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,KAAK,CAAC,UAAU,CACd,KAAyB,EACzB,MAA4B;QAS5B,4CAA4C;QAC5C,MAAM,gBAAgB,GAAG,CAAC,QAAgB,EAAU,EAAE;YACpD,MAAM,OAAO,GAAG,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;YAC1C,OAAO,OAAO,GAAG,CAAC,CAAC,CAAC,CAA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E,IAAI,EAAE,OAAO,EAAE,CAClB,CAAC;YACF,OAAO;gBACL,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE;gBACrD,SAAS,EAAE,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,SAAS,IAAI,EAAE;aAClE,CAAC;QACJ,CAAC,CAAC,CACH,CAAC;QAEF,MAAM,oBAAoB,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC;YAClD,IAAI,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;SAC7E,CAAC,CAAC;QAEH,OAAO;YACL,QAAQ,EACN,eAAe,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;gBAC/B,GAAG,EAAE,MAAM,CAAC,GAAG;gBACf,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B,CAAC,CAAC,IAAI,EAAE;YACX,WAAW,EAAE,oBAAoB;YACjC,OAAO,EAAE,gBAAgB;SAC1B,CAAC;IACJ,CAAC;CACF;AAvKD,wBAuKC"}
|
package/resources/upload.mjs
CHANGED
|
@@ -1,31 +1,143 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
import { APIResource } from "../core/resource.mjs";
|
|
3
|
+
import { toFile } from "../internal/to-file.mjs";
|
|
3
4
|
export class Upload extends APIResource {
|
|
4
5
|
/**
|
|
5
|
-
* Acknowledges that
|
|
6
|
-
*
|
|
6
|
+
* Acknowledges that one or more file uploads have been completed. This endpoint
|
|
7
|
+
* verifies files exist in storage and updates upload records with metadata.
|
|
8
|
+
* Processes multiple uploads in parallel. Each result in the response array may be
|
|
9
|
+
* a success or error object.
|
|
7
10
|
*
|
|
8
11
|
* @example
|
|
9
12
|
* ```ts
|
|
10
13
|
* const response = await client.upload.acknowledge({
|
|
11
|
-
*
|
|
14
|
+
* body: [
|
|
15
|
+
* { upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
16
|
+
* { upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
17
|
+
* ],
|
|
12
18
|
* });
|
|
13
19
|
* ```
|
|
14
20
|
*/
|
|
15
|
-
acknowledge(
|
|
16
|
-
|
|
21
|
+
acknowledge(params, options) {
|
|
22
|
+
const { body } = params;
|
|
23
|
+
return this._client.post('/v1/upload/ack', { body: body, ...options });
|
|
17
24
|
}
|
|
18
25
|
/**
|
|
19
|
-
* Creates
|
|
20
|
-
* authentication.
|
|
26
|
+
* Creates presigned URLs for one or more file uploads to Filejar storage.
|
|
27
|
+
* Processes multiple files in parallel. Requires API key authentication.
|
|
21
28
|
*
|
|
22
29
|
* @example
|
|
23
30
|
* ```ts
|
|
24
|
-
* const response = await client.upload.prepare(
|
|
31
|
+
* const response = await client.upload.prepare({
|
|
32
|
+
* body: [{}, {}, {}],
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
prepare(params, options) {
|
|
37
|
+
const { body } = params;
|
|
38
|
+
return this._client.post('/v1/upload/prepare', { body: body, ...options });
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Uploads files to signed URLs.
|
|
42
|
+
*
|
|
43
|
+
* Accepts File objects, Buffers, ArrayBuffers, or other uploadable types.
|
|
44
|
+
* If params are not provided, file names will be extracted from File objects
|
|
45
|
+
* or generated automatically.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // With explicit file names
|
|
50
|
+
* const response = await client.upload.uploadFile([file1, file2], {
|
|
51
|
+
* body: [{ file_name: 'file1.txt' }, { file_name: 'file2.txt' }],
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* // Without params (file names extracted from File objects or auto-generated)
|
|
55
|
+
* const response = await client.upload.uploadFile([file1, file2]);
|
|
25
56
|
* ```
|
|
26
57
|
*/
|
|
27
|
-
|
|
28
|
-
|
|
58
|
+
async uploadFile(files, params) {
|
|
59
|
+
// Helper function to extract file extension
|
|
60
|
+
const getFileExtension = (fileName) => {
|
|
61
|
+
const lastDot = fileName.lastIndexOf('.');
|
|
62
|
+
return lastDot > 0 ? fileName.substring(lastDot) : '';
|
|
63
|
+
};
|
|
64
|
+
// Helper function to get extension from MIME type
|
|
65
|
+
const getExtensionFromMimeType = (mimeType) => {
|
|
66
|
+
if (!mimeType)
|
|
67
|
+
return '';
|
|
68
|
+
const mimeToExt = {
|
|
69
|
+
'image/jpeg': '.jpg',
|
|
70
|
+
'image/png': '.png',
|
|
71
|
+
'image/gif': '.gif',
|
|
72
|
+
'image/webp': '.webp',
|
|
73
|
+
'text/plain': '.txt',
|
|
74
|
+
'text/html': '.html',
|
|
75
|
+
'application/json': '.json',
|
|
76
|
+
'application/pdf': '.pdf',
|
|
77
|
+
'video/mp4': '.mp4',
|
|
78
|
+
'audio/mpeg': '.mp3',
|
|
79
|
+
};
|
|
80
|
+
return mimeToExt[mimeType] || '';
|
|
81
|
+
};
|
|
82
|
+
// Generate params if not provided
|
|
83
|
+
const prepareParams = params ??
|
|
84
|
+
{
|
|
85
|
+
body: files.map((file, index) => {
|
|
86
|
+
let fileName;
|
|
87
|
+
let mimeType;
|
|
88
|
+
// Try to extract file name from File object
|
|
89
|
+
if (file instanceof File) {
|
|
90
|
+
fileName = file.name;
|
|
91
|
+
mimeType = file.type;
|
|
92
|
+
}
|
|
93
|
+
// For other types, try to get name from the file-like object
|
|
94
|
+
else if (typeof file === 'object' && file !== null) {
|
|
95
|
+
if ('name' in file && file.name) {
|
|
96
|
+
fileName = String(file.name);
|
|
97
|
+
}
|
|
98
|
+
if ('type' in file && file.type) {
|
|
99
|
+
mimeType = String(file.type);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// If we have a file name, ensure it has an extension
|
|
103
|
+
if (fileName) {
|
|
104
|
+
const ext = getFileExtension(fileName);
|
|
105
|
+
// If no extension found, try to infer from MIME type
|
|
106
|
+
if (!ext && mimeType) {
|
|
107
|
+
const inferredExt = getExtensionFromMimeType(mimeType);
|
|
108
|
+
if (inferredExt) {
|
|
109
|
+
fileName = fileName + inferredExt;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { file_name: fileName };
|
|
113
|
+
}
|
|
114
|
+
// Generate a unique default name with extension
|
|
115
|
+
const defaultExt = mimeType ? getExtensionFromMimeType(mimeType) : '';
|
|
116
|
+
return {
|
|
117
|
+
file_name: `file-${Date.now()}-${index}-${Math.random().toString(36).substring(7)}${defaultExt}`,
|
|
118
|
+
};
|
|
119
|
+
}),
|
|
120
|
+
};
|
|
121
|
+
const preparedResponse = await this.prepare(prepareParams);
|
|
122
|
+
const uploadResponses = await Promise.all(files.map(async (file) => {
|
|
123
|
+
const fileObj = await toFile(file);
|
|
124
|
+
const uploadResponse = await this._client.put(preparedResponse[files.indexOf(file)]?.signed_url ?? '', { body: fileObj });
|
|
125
|
+
return {
|
|
126
|
+
key: preparedResponse[files.indexOf(file)]?.key ?? '',
|
|
127
|
+
upload_id: preparedResponse[files.indexOf(file)]?.upload_id ?? '',
|
|
128
|
+
};
|
|
129
|
+
}));
|
|
130
|
+
const acknowledgeResponses = await this.acknowledge({
|
|
131
|
+
body: uploadResponses.map((response) => ({ upload_id: response.upload_id })),
|
|
132
|
+
});
|
|
133
|
+
return {
|
|
134
|
+
response: uploadResponses.map((upload) => ({
|
|
135
|
+
key: upload.key,
|
|
136
|
+
upload_id: upload.upload_id,
|
|
137
|
+
})) ?? [],
|
|
138
|
+
acknowledge: acknowledgeResponses,
|
|
139
|
+
prepare: preparedResponse,
|
|
140
|
+
};
|
|
29
141
|
}
|
|
30
142
|
}
|
|
31
143
|
//# sourceMappingURL=upload.mjs.map
|
package/resources/upload.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.mjs","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;
|
|
1
|
+
{"version":3,"file":"upload.mjs","sourceRoot":"","sources":["../src/resources/upload.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;OAGf,EAAE,MAAM,EAAoB;AAEnC,MAAM,OAAO,MAAO,SAAQ,WAAW;IACrC;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CACT,MAA+B,EAC/B,OAAwB;QAExB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IACzE,CAAC;IAED;;;;;;;;;;OAUG;IACH,OAAO,CAAC,MAA2B,EAAE,OAAwB;QAC3D,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC7E,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,KAAK,CAAC,UAAU,CACd,KAAyB,EACzB,MAA4B;QAS5B,4CAA4C;QAC5C,MAAM,gBAAgB,GAAG,CAAC,QAAgB,EAAU,EAAE;YACpD,MAAM,OAAO,GAAG,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;YAC1C,OAAO,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACxD,CAAC,CAAC;QAEF,kDAAkD;QAClD,MAAM,wBAAwB,GAAG,CAAC,QAAiB,EAAU,EAAE;YAC7D,IAAI,CAAC,QAAQ;gBAAE,O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AO,CAAC,IAAI,CAAC,CAAC,EAAE,SAAS,IAAI,EAAE;aAClE,CAAC;QACJ,CAAC,CAAC,CACH,CAAC;QAEF,MAAM,oBAAoB,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC;YAClD,IAAI,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;SAC7E,CAAC,CAAC;QAEH,OAAO;YACL,QAAQ,EACN,eAAe,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;gBAC/B,GAAG,EAAE,MAAM,CAAC,GAAG;gBACf,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B,CAAC,CAAC,IAAI,EAAE;YACX,WAAW,EAAE,oBAAoB;YACjC,OAAO,EAAE,gBAAgB;SAC1B,CAAC;IACJ,CAAC;CACF"}
|
package/src/resources/upload.ts
CHANGED
|
@@ -3,91 +3,281 @@
|
|
|
3
3
|
import { APIResource } from '../core/resource';
|
|
4
4
|
import { APIPromise } from '../core/api-promise';
|
|
5
5
|
import { RequestOptions } from '../internal/request-options';
|
|
6
|
+
import { toFile, type ToFileInput } from '../internal/to-file';
|
|
6
7
|
|
|
7
8
|
export class Upload extends APIResource {
|
|
8
9
|
/**
|
|
9
|
-
* Acknowledges that
|
|
10
|
-
*
|
|
10
|
+
* Acknowledges that one or more file uploads have been completed. This endpoint
|
|
11
|
+
* verifies files exist in storage and updates upload records with metadata.
|
|
12
|
+
* Processes multiple uploads in parallel. Each result in the response array may be
|
|
13
|
+
* a success or error object.
|
|
11
14
|
*
|
|
12
15
|
* @example
|
|
13
16
|
* ```ts
|
|
14
17
|
* const response = await client.upload.acknowledge({
|
|
15
|
-
*
|
|
18
|
+
* body: [
|
|
19
|
+
* { upload_id: '123e4567-e89b-12d3-a456-426614174000' },
|
|
20
|
+
* { upload_id: '223e4567-e89b-12d3-a456-426614174001' },
|
|
21
|
+
* ],
|
|
16
22
|
* });
|
|
17
23
|
* ```
|
|
18
24
|
*/
|
|
19
25
|
acknowledge(
|
|
20
|
-
|
|
26
|
+
params: UploadAcknowledgeParams,
|
|
21
27
|
options?: RequestOptions,
|
|
22
28
|
): APIPromise<UploadAcknowledgeResponse> {
|
|
23
|
-
|
|
29
|
+
const { body } = params;
|
|
30
|
+
return this._client.post('/v1/upload/ack', { body: body, ...options });
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
/**
|
|
27
|
-
* Creates
|
|
28
|
-
* authentication.
|
|
34
|
+
* Creates presigned URLs for one or more file uploads to Filejar storage.
|
|
35
|
+
* Processes multiple files in parallel. Requires API key authentication.
|
|
29
36
|
*
|
|
30
37
|
* @example
|
|
31
38
|
* ```ts
|
|
32
|
-
* const response = await client.upload.prepare(
|
|
39
|
+
* const response = await client.upload.prepare({
|
|
40
|
+
* body: [{}, {}, {}],
|
|
41
|
+
* });
|
|
33
42
|
* ```
|
|
34
43
|
*/
|
|
35
|
-
prepare(
|
|
36
|
-
|
|
44
|
+
prepare(params: UploadPrepareParams, options?: RequestOptions): APIPromise<UploadPrepareResponse> {
|
|
45
|
+
const { body } = params;
|
|
46
|
+
return this._client.post('/v1/upload/prepare', { body: body, ...options });
|
|
37
47
|
}
|
|
38
|
-
}
|
|
39
48
|
|
|
40
|
-
export interface UploadAcknowledgeResponse {
|
|
41
49
|
/**
|
|
42
|
-
*
|
|
50
|
+
* Uploads files to signed URLs.
|
|
51
|
+
*
|
|
52
|
+
* Accepts File objects, Buffers, ArrayBuffers, or other uploadable types.
|
|
53
|
+
* If params are not provided, file names will be extracted from File objects
|
|
54
|
+
* or generated automatically.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // With explicit file names
|
|
59
|
+
* const response = await client.upload.uploadFile([file1, file2], {
|
|
60
|
+
* body: [{ file_name: 'file1.txt' }, { file_name: 'file2.txt' }],
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* // Without params (file names extracted from File objects or auto-generated)
|
|
64
|
+
* const response = await client.upload.uploadFile([file1, file2]);
|
|
65
|
+
* ```
|
|
43
66
|
*/
|
|
44
|
-
|
|
67
|
+
async uploadFile(
|
|
68
|
+
files: Array<ToFileInput>,
|
|
69
|
+
params?: UploadPrepareParams,
|
|
70
|
+
): Promise<{
|
|
71
|
+
response: {
|
|
72
|
+
key: string;
|
|
73
|
+
upload_id: string;
|
|
74
|
+
}[];
|
|
75
|
+
acknowledge: UploadAcknowledgeResponse;
|
|
76
|
+
prepare: UploadPrepareResponse;
|
|
77
|
+
}> {
|
|
78
|
+
// Helper function to extract file extension
|
|
79
|
+
const getFileExtension = (fileName: string): string => {
|
|
80
|
+
const lastDot = fileName.lastIndexOf('.');
|
|
81
|
+
return lastDot > 0 ? fileName.substring(lastDot) : '';
|
|
82
|
+
};
|
|
45
83
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
84
|
+
// Helper function to get extension from MIME type
|
|
85
|
+
const getExtensionFromMimeType = (mimeType?: string): string => {
|
|
86
|
+
if (!mimeType) return '';
|
|
87
|
+
const mimeToExt: Record<string, string> = {
|
|
88
|
+
'image/jpeg': '.jpg',
|
|
89
|
+
'image/png': '.png',
|
|
90
|
+
'image/gif': '.gif',
|
|
91
|
+
'image/webp': '.webp',
|
|
92
|
+
'text/plain': '.txt',
|
|
93
|
+
'text/html': '.html',
|
|
94
|
+
'application/json': '.json',
|
|
95
|
+
'application/pdf': '.pdf',
|
|
96
|
+
'video/mp4': '.mp4',
|
|
97
|
+
'audio/mpeg': '.mp3',
|
|
98
|
+
};
|
|
99
|
+
return mimeToExt[mimeType] || '';
|
|
100
|
+
};
|
|
50
101
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
102
|
+
// Generate params if not provided
|
|
103
|
+
const prepareParams: UploadPrepareParams =
|
|
104
|
+
params ??
|
|
105
|
+
({
|
|
106
|
+
body: files.map((file, index) => {
|
|
107
|
+
let fileName: string | undefined;
|
|
108
|
+
let mimeType: string | undefined;
|
|
109
|
+
|
|
110
|
+
// Try to extract file name from File object
|
|
111
|
+
if (file instanceof File) {
|
|
112
|
+
fileName = file.name;
|
|
113
|
+
mimeType = file.type;
|
|
114
|
+
}
|
|
115
|
+
// For other types, try to get name from the file-like object
|
|
116
|
+
else if (typeof file === 'object' && file !== null) {
|
|
117
|
+
if ('name' in file && file.name) {
|
|
118
|
+
fileName = String(file.name);
|
|
119
|
+
}
|
|
120
|
+
if ('type' in file && file.type) {
|
|
121
|
+
mimeType = String(file.type);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// If we have a file name, ensure it has an extension
|
|
126
|
+
if (fileName) {
|
|
127
|
+
const ext = getFileExtension(fileName);
|
|
128
|
+
// If no extension found, try to infer from MIME type
|
|
129
|
+
if (!ext && mimeType) {
|
|
130
|
+
const inferredExt = getExtensionFromMimeType(mimeType);
|
|
131
|
+
if (inferredExt) {
|
|
132
|
+
fileName = fileName + inferredExt;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { file_name: fileName };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Generate a unique default name with extension
|
|
139
|
+
const defaultExt = mimeType ? getExtensionFromMimeType(mimeType) : '';
|
|
140
|
+
return {
|
|
141
|
+
file_name: `file-${Date.now()}-${index}-${Math.random().toString(36).substring(7)}${defaultExt}`,
|
|
142
|
+
};
|
|
143
|
+
}),
|
|
144
|
+
} as UploadPrepareParams);
|
|
145
|
+
|
|
146
|
+
const preparedResponse = await this.prepare(prepareParams);
|
|
147
|
+
const uploadResponses = await Promise.all(
|
|
148
|
+
files.map(async (file) => {
|
|
149
|
+
const fileObj = await toFile(file);
|
|
150
|
+
const uploadResponse = await this._client.put<Response>(
|
|
151
|
+
preparedResponse[files.indexOf(file)]?.signed_url ?? '',
|
|
152
|
+
{ body: fileObj },
|
|
153
|
+
);
|
|
154
|
+
return {
|
|
155
|
+
key: preparedResponse[files.indexOf(file)]?.key ?? '',
|
|
156
|
+
upload_id: preparedResponse[files.indexOf(file)]?.upload_id ?? '',
|
|
157
|
+
};
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const acknowledgeResponses = await this.acknowledge({
|
|
162
|
+
body: uploadResponses.map((response) => ({ upload_id: response.upload_id })),
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
response:
|
|
167
|
+
uploadResponses.map((upload) => ({
|
|
168
|
+
key: upload.key,
|
|
169
|
+
upload_id: upload.upload_id,
|
|
170
|
+
})) ?? [],
|
|
171
|
+
acknowledge: acknowledgeResponses,
|
|
172
|
+
prepare: preparedResponse,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
55
175
|
}
|
|
56
176
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
177
|
+
/**
|
|
178
|
+
* Array of acknowledgment results. Each item may be a success object or an error
|
|
179
|
+
* object.
|
|
180
|
+
*/
|
|
181
|
+
export type UploadAcknowledgeResponse = Array<
|
|
182
|
+
UploadAcknowledgeResponse.UnionMember0 | UploadAcknowledgeResponse.UnionMember1
|
|
183
|
+
>;
|
|
62
184
|
|
|
185
|
+
export namespace UploadAcknowledgeResponse {
|
|
63
186
|
/**
|
|
64
|
-
*
|
|
187
|
+
* Successful acknowledgment
|
|
65
188
|
*/
|
|
66
|
-
|
|
189
|
+
export interface UnionMember0 {
|
|
190
|
+
/**
|
|
191
|
+
* Timestamp when the upload was acknowledged
|
|
192
|
+
*/
|
|
193
|
+
acknowledged_at: string;
|
|
67
194
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
195
|
+
/**
|
|
196
|
+
* MIME type of the uploaded file
|
|
197
|
+
*/
|
|
198
|
+
content_type: string;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* File size in bytes
|
|
202
|
+
*/
|
|
203
|
+
size: number;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The upload ID that was acknowledged
|
|
207
|
+
*/
|
|
208
|
+
upload_id: string;
|
|
209
|
+
}
|
|
72
210
|
|
|
73
211
|
/**
|
|
74
|
-
*
|
|
212
|
+
* Error for a specific upload
|
|
75
213
|
*/
|
|
76
|
-
|
|
214
|
+
export interface UnionMember1 {
|
|
215
|
+
/**
|
|
216
|
+
* Error message (e.g., 'Upload not found' or 'Upload is not completed yet')
|
|
217
|
+
*/
|
|
218
|
+
error: string;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The upload ID that failed
|
|
222
|
+
*/
|
|
223
|
+
upload_id: string;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Array of prepared uploads
|
|
229
|
+
*/
|
|
230
|
+
export type UploadPrepareResponse = Array<UploadPrepareResponse.UploadPrepareResponseItem>;
|
|
231
|
+
|
|
232
|
+
export namespace UploadPrepareResponse {
|
|
233
|
+
export interface UploadPrepareResponseItem {
|
|
234
|
+
/**
|
|
235
|
+
* File key/path in storage
|
|
236
|
+
*/
|
|
237
|
+
key: string;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Proxy URL for uploading files through the API (PUT request to /v1/files/{key})
|
|
241
|
+
*/
|
|
242
|
+
signed_url: string;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Unique identifier for this upload
|
|
246
|
+
*/
|
|
247
|
+
upload_id: string;
|
|
248
|
+
}
|
|
77
249
|
}
|
|
78
250
|
|
|
79
251
|
export interface UploadAcknowledgeParams {
|
|
80
252
|
/**
|
|
81
|
-
*
|
|
253
|
+
* Array of upload IDs to acknowledge
|
|
82
254
|
*/
|
|
83
|
-
|
|
255
|
+
body: Array<UploadAcknowledgeParams.Body>;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export namespace UploadAcknowledgeParams {
|
|
259
|
+
export interface Body {
|
|
260
|
+
/**
|
|
261
|
+
* The upload ID returned from /prepare endpoint
|
|
262
|
+
*/
|
|
263
|
+
upload_id: string;
|
|
264
|
+
}
|
|
84
265
|
}
|
|
85
266
|
|
|
86
267
|
export interface UploadPrepareParams {
|
|
87
268
|
/**
|
|
88
|
-
*
|
|
269
|
+
* Array of files to prepare for upload
|
|
89
270
|
*/
|
|
90
|
-
|
|
271
|
+
body: Array<UploadPrepareParams.Body>;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export namespace UploadPrepareParams {
|
|
275
|
+
export interface Body {
|
|
276
|
+
/**
|
|
277
|
+
* Optional file name. If not provided, a random UUID will be used.
|
|
278
|
+
*/
|
|
279
|
+
file_name?: string;
|
|
280
|
+
}
|
|
91
281
|
}
|
|
92
282
|
|
|
93
283
|
export declare namespace Upload {
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.6.0'; // x-release-please-version
|
package/version.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.6.0";
|
|
2
2
|
//# sourceMappingURL=version.d.mts.map
|
package/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.6.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/version.js
CHANGED
package/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.6.0'; // x-release-please-version
|
|
2
2
|
//# sourceMappingURL=version.mjs.map
|