@or-sdk/files 3.9.0 → 3.10.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 +9 -0
- package/dist/cjs/Files.js +79 -78
- package/dist/cjs/Files.js.map +1 -1
- package/dist/esm/Files.js +50 -44
- package/dist/esm/Files.js.map +1 -1
- package/dist/types/Files.d.ts +15 -166
- package/dist/types/Files.d.ts.map +1 -1
- package/dist/types/types/ttl.d.ts +5 -6
- package/dist/types/types/ttl.d.ts.map +1 -1
- package/dist/types/types/upload-files.d.ts +3 -66
- package/dist/types/types/upload-files.d.ts.map +1 -1
- package/dist/types/types.d.ts +0 -36
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/Files.ts +83 -54
- package/src/types/ttl.ts +12 -1
- package/src/types/upload-files.ts +9 -16
|
@@ -1,55 +1,18 @@
|
|
|
1
1
|
import type { AxiosProgressEvent } from 'axios';
|
|
2
2
|
import type { ReadStream } from 'node:fs';
|
|
3
|
-
import type {
|
|
3
|
+
import type { DateTime } from './ttl';
|
|
4
4
|
export type { AxiosProgressEvent };
|
|
5
5
|
export type RequestOptions = {
|
|
6
|
-
/** Signal from AbortController to abort active request */
|
|
7
6
|
signal?: AbortSignal;
|
|
8
7
|
};
|
|
9
8
|
export type UploadBaseParams = {
|
|
10
|
-
/**
|
|
11
|
-
* Name of the file
|
|
12
|
-
* @example 'my_file.json'
|
|
13
|
-
*/
|
|
14
9
|
fileName: string;
|
|
15
|
-
/**
|
|
16
|
-
* File path prefix
|
|
17
|
-
* @example If fine name is 'my_file.json' and prefix is 'path/to/file/' the result file path is:
|
|
18
|
-
* 'path/to/file/my_file.json'
|
|
19
|
-
*
|
|
20
|
-
* @example If fine name is 'my_file.json' and prefix is 'name_prefix_' the result file path is:
|
|
21
|
-
* 'name_prefix_my_file.json'
|
|
22
|
-
*/
|
|
23
10
|
prefix?: string;
|
|
24
|
-
/**
|
|
25
|
-
* [MIME type](https://www.iana.org/assignments/media-types/media-types.xhtml)
|
|
26
|
-
* of the file.
|
|
27
|
-
* @default 'binary/octet-stream'
|
|
28
|
-
* @example 'application/json'
|
|
29
|
-
*/
|
|
30
11
|
contentType?: string;
|
|
31
|
-
/** Max size of the file in bytes */
|
|
32
12
|
maxFileSize?: number;
|
|
33
|
-
/**
|
|
34
|
-
* Custom cache control setting for a file
|
|
35
|
-
*
|
|
36
|
-
* Examples:
|
|
37
|
-
* - `max-age=<seconds>`
|
|
38
|
-
* Specifies the maximum time (in seconds) a resource is considered fresh.
|
|
39
|
-
* For example, max-age=86400 means the resource is cached for 1 day.
|
|
40
|
-
* - `no-cache`
|
|
41
|
-
* Force to validate with the origin server before serving a cached copy.
|
|
42
|
-
* Useful for content that changes frequently.
|
|
43
|
-
*/
|
|
44
13
|
cacheControl?: string;
|
|
45
14
|
rewriteMode?: 'rewrite' | 'prevent-rewrite';
|
|
46
|
-
|
|
47
|
-
expiresAt?: Timestamp;
|
|
48
|
-
/**
|
|
49
|
-
* If `true` after upload file would be publicly available by it's URL.
|
|
50
|
-
* If `false` access to file is possible only via temporary signed URL to a file.
|
|
51
|
-
* @default false
|
|
52
|
-
*/
|
|
15
|
+
expiresAt?: DateTime;
|
|
53
16
|
isPublic?: boolean;
|
|
54
17
|
};
|
|
55
18
|
export type UploadUrlProps = UploadBaseParams;
|
|
@@ -77,48 +40,22 @@ export type UploadFields = {
|
|
|
77
40
|
'X-Amz-Security-Token': string;
|
|
78
41
|
'cache-control'?: string;
|
|
79
42
|
};
|
|
80
|
-
/** Buffer and ReadStream only supported in Node.js */
|
|
81
43
|
export type FileModel = File | Blob | Buffer | ReadStream;
|
|
82
|
-
|
|
83
|
-
export type ExtendedFileModel = FileModel | string;
|
|
44
|
+
export type ExtendedFileModel = FileModel | string | Uint8Array;
|
|
84
45
|
export type UploadFileBaseParams = UploadBaseParams & {
|
|
85
|
-
/** File contents */
|
|
86
46
|
fileContent: ExtendedFileModel;
|
|
87
|
-
/** Callback function to be called with upload progress events */
|
|
88
47
|
onUploadProgress?: (event: ProgressEvent | AxiosProgressEvent) => void;
|
|
89
|
-
/** Size of the file in bytes. Helpful when file is an instance of ReadStream in Node.js */
|
|
90
48
|
knownLength?: number;
|
|
91
|
-
/** If `true` method will wait after file upload until file system will update to include file */
|
|
92
49
|
waitTillFileAddedInDb?: boolean;
|
|
93
50
|
};
|
|
94
51
|
export type UploadFileProps = UploadFileBaseParams;
|
|
95
|
-
/** Optional config for uploading files */
|
|
96
52
|
export type UploadFileOptions = RequestOptions & {
|
|
97
|
-
/**
|
|
98
|
-
* Maximum wait time for update of the data base in milliseconds.
|
|
99
|
-
* @default 60_000
|
|
100
|
-
*/
|
|
101
53
|
waitForDatabaseUpdateTimeout?: number;
|
|
102
|
-
/**
|
|
103
|
-
* Interval between checking if database was updated in milliseconds.
|
|
104
|
-
* @default 2_000
|
|
105
|
-
*/
|
|
106
54
|
waitForDatabaseUpdatePollInterval?: number;
|
|
107
55
|
};
|
|
108
56
|
export type UploadToSignedUrlParameters = Pick<UploadFileProps, 'fileName' | 'fileContent' | 'contentType' | 'cacheControl' | 'knownLength' | 'onUploadProgress'> & {
|
|
109
57
|
signedUrl: UploadUrlResponse;
|
|
110
58
|
};
|
|
111
|
-
export type UploadToSignedUrlParameters2 = {
|
|
112
|
-
signedUrl: UploadUrlResponse;
|
|
113
|
-
file: FileModel;
|
|
114
|
-
fileName: string;
|
|
115
|
-
cacheControl?: string;
|
|
116
|
-
/** Size of the file in bytes. Helpful when file is an instance of ReadStream in Node.js */
|
|
117
|
-
knownLength?: number;
|
|
118
|
-
contentType?: string;
|
|
119
|
-
signal?: AbortSignal;
|
|
120
|
-
onUploadProgress?: (event: ProgressEvent | AxiosProgressEvent) => void;
|
|
121
|
-
};
|
|
122
59
|
export type UploadFilePropsLegacy = Pick<UploadFileProps, 'prefix' | 'cacheControl' | 'isPublic' | 'rewriteMode' | 'maxFileSize' | 'knownLength' | 'waitTillFileAddedInDb'> & {
|
|
123
60
|
name: string;
|
|
124
61
|
fileModel: ExtendedFileModel;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload-files.d.ts","sourceRoot":"","sources":["../../../src/types/upload-files.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAChD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"upload-files.d.ts","sourceRoot":"","sources":["../../../src/types/upload-files.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAChD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEtC,YAAY,EAAE,kBAAkB,EAAE,CAAC;AAEnC,MAAM,MAAM,cAAc,GAAG;IAE3B,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAK7B,QAAQ,EAAE,MAAM,CAAC;IAUjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAQhB,WAAW,CAAC,EAAE,MAAM,CAAC;IAGrB,WAAW,CAAC,EAAE,MAAM,CAAC;IAarB,YAAY,CAAC,EAAE,MAAM,CAAC;IAGtB,WAAW,CAAC,EAAE,SAAS,GAAG,iBAAiB,CAAC;IAG5C,SAAS,CAAC,EAAE,QAAQ,CAAC;IAOrB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAE9C,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,cAAc,EAAE,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,UAAU,CAAC,GAAG;IAC1G,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,uB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AM,CAAC;CAC5C,CAAA;AAED,MAAM,MAAM,2BAA2B,GAAG,IAAI,CAC5C,eAAe,EACf,UAAU,GAAG,aAAa,GAAG,aAAa,GAAG,cAAc,GAAG,aAAa,GAAG,kBAAkB,CACjG,GAAG;IACF,SAAS,EAAE,iBAAiB,CAAC;CAC9B,CAAA;AAGD,MAAM,MAAM,qBAAqB,GAAG,IAAI,CACtC,eAAe,EACb,QAAQ,GACR,cAAc,GACd,UAAU,GACV,aAAa,GACb,aAAa,GACb,aAAa,GACb,uBAAuB,CAC1B,GAAG;IACF,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,iBAAiB,CAAC;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,GAAG,kBAAkB,KAAK,IAAI,CAAC;IAC/D,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,CAAA"}
|
package/dist/types/types.d.ts
CHANGED
|
@@ -4,53 +4,17 @@ export type * from './types/upload-files';
|
|
|
4
4
|
export type * from './types/ttl';
|
|
5
5
|
export type { Token };
|
|
6
6
|
export type FilesConfigBase = {
|
|
7
|
-
/**
|
|
8
|
-
* token
|
|
9
|
-
*/
|
|
10
7
|
token: Token;
|
|
11
|
-
/**
|
|
12
|
-
* Account ID for cross-account requests (super admin only)
|
|
13
|
-
*/
|
|
14
8
|
accountId?: string;
|
|
15
9
|
};
|
|
16
10
|
export type FilesConfigWithDiscovery = FilesConfigBase & {
|
|
17
|
-
/**
|
|
18
|
-
* URL of OneReach service discovery API.
|
|
19
|
-
*
|
|
20
|
-
* Allows to fetch URLs of needed API's instead of defining them in the constructor.
|
|
21
|
-
*
|
|
22
|
-
* @example 'https://discovery.<env>.api.onereach.ai'
|
|
23
|
-
*/
|
|
24
11
|
discoveryUrl: string;
|
|
25
|
-
/**
|
|
26
|
-
* Direct service url, can be used to avoid discovery api call
|
|
27
|
-
*
|
|
28
|
-
* Not needed if {@link FilesConfig.discoveryUrl} is defined.
|
|
29
|
-
*
|
|
30
|
-
* @example 'https://files-api.svc.<env>.api.onereach.ai'
|
|
31
|
-
*/
|
|
32
12
|
filesApiUrl?: never;
|
|
33
|
-
/** @deprecated Use {@link FilesConfig.filesApiUrl} instead */
|
|
34
13
|
serviceUrl?: never;
|
|
35
14
|
};
|
|
36
15
|
export type FilesConfigWithExplicitUrls = FilesConfigBase & {
|
|
37
|
-
/**
|
|
38
|
-
* Direct service url, can be used to avoid discovery api call
|
|
39
|
-
*
|
|
40
|
-
* Not needed if {@link FilesConfig.discoveryUrl} is defined.
|
|
41
|
-
*
|
|
42
|
-
* @example 'https://files-api.svc.<env>.api.onereach.ai'
|
|
43
|
-
*/
|
|
44
16
|
filesApiUrl: string;
|
|
45
|
-
/** @deprecated Use {@link FilesConfig.filesApiUrl} instead */
|
|
46
17
|
serviceUrl?: string;
|
|
47
|
-
/**
|
|
48
|
-
* URL of OneReach service discovery API.
|
|
49
|
-
*
|
|
50
|
-
* Not needed if {@link FilesConfig.filesApiUrl} is defined.
|
|
51
|
-
*
|
|
52
|
-
* @example 'https://discovery.<env>.api.onereach.ai'
|
|
53
|
-
*/
|
|
54
18
|
discoveryUrl?: never;
|
|
55
19
|
};
|
|
56
20
|
export type FilesConfig = FilesConfigWithDiscovery | FilesConfigWithExplicitUrls;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE1C,mBAAmB,sBAAsB,CAAC;AAC1C,mBAAmB,sBAAsB,CAAC;AAC1C,mBAAmB,aAAa,CAAC;AAEjC,YAAY,EAAE,KAAK,EAAE,CAAC;AAEtB,MAAM,MAAM,eAAe,GAAG;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],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pB,YAAY,EAAE,IAAI,GAAG,IAAI,CAAC;IAC1B,GAAG,EAAE,IAAI,GAAG,IAAI,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,SAAS,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IAC3B,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@or-sdk/files",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "480349cb6b36e66aea6b9b63b93ed61c95497f94"
|
|
36
36
|
}
|
package/src/Files.ts
CHANGED
|
@@ -9,13 +9,15 @@ import type {
|
|
|
9
9
|
FileItem,
|
|
10
10
|
FileItemSelect,
|
|
11
11
|
FileModel,
|
|
12
|
-
|
|
12
|
+
FilesConfigWithDiscovery,
|
|
13
|
+
FilesConfigWithExplicitUrls,
|
|
13
14
|
GetItemsQuery,
|
|
14
15
|
GetSystemFileUploadUrlParams,
|
|
15
16
|
GetUploadUrlDataPayload,
|
|
17
|
+
NormalizeDateOptions,
|
|
16
18
|
RequestOptions,
|
|
17
19
|
SearchQuery,
|
|
18
|
-
|
|
20
|
+
DateTime,
|
|
19
21
|
UploadFileOptions,
|
|
20
22
|
UploadFileProps,
|
|
21
23
|
UploadFilePropsLegacy,
|
|
@@ -28,8 +30,14 @@ import type {
|
|
|
28
30
|
} from './types';
|
|
29
31
|
import { isNode } from './utils';
|
|
30
32
|
|
|
33
|
+
const defaultContentType = 'application/octet-stream';
|
|
34
|
+
|
|
31
35
|
export class Files extends Base {
|
|
32
|
-
|
|
36
|
+
/** Service-Discovery API allows to fetch URLs for required services automatically on bootstrap */
|
|
37
|
+
constructor(params: FilesConfigWithDiscovery);
|
|
38
|
+
/** Providing URLs for APIs explicitly allows to reduce initial delay by avoiding extra calls to discovery service */
|
|
39
|
+
constructor(params: FilesConfigWithExplicitUrls);
|
|
40
|
+
constructor(params: FilesConfigWithDiscovery | FilesConfigWithExplicitUrls) {
|
|
33
41
|
const { token, accountId, discoveryUrl, filesApiUrl, serviceUrl } = params;
|
|
34
42
|
|
|
35
43
|
super({
|
|
@@ -65,13 +73,14 @@ export class Files extends Base {
|
|
|
65
73
|
* @param key the file path, example: /data/images/second.png
|
|
66
74
|
* @returns Folder record
|
|
67
75
|
*/
|
|
68
|
-
async getFolder(key: string): Promise<FileItem> {
|
|
76
|
+
async getFolder(key: string, options?: RequestOptions): Promise<FileItem> {
|
|
69
77
|
return await this.callApiV2({
|
|
70
78
|
method: 'get',
|
|
71
79
|
route: 'folders/find-one',
|
|
72
80
|
params: {
|
|
73
81
|
key,
|
|
74
82
|
},
|
|
83
|
+
signal: options?.signal,
|
|
75
84
|
});
|
|
76
85
|
}
|
|
77
86
|
|
|
@@ -80,13 +89,8 @@ export class Files extends Base {
|
|
|
80
89
|
* @param key the name of folder
|
|
81
90
|
* @returns total folder size in bytes
|
|
82
91
|
*/
|
|
83
|
-
async getFolderSize(key: string): Promise<number> {
|
|
84
|
-
const { size } = await this.
|
|
85
|
-
method: 'get',
|
|
86
|
-
route: 'folders/find-one',
|
|
87
|
-
params: { key },
|
|
88
|
-
});
|
|
89
|
-
|
|
92
|
+
async getFolderSize(key: string, options?: RequestOptions): Promise<number> {
|
|
93
|
+
const { size } = await this.getFolder(key, options);
|
|
90
94
|
return size;
|
|
91
95
|
}
|
|
92
96
|
|
|
@@ -129,11 +133,12 @@ export class Files extends Base {
|
|
|
129
133
|
return [...folders, ...files];
|
|
130
134
|
}
|
|
131
135
|
|
|
132
|
-
async getFoldersList(treePrefix: string) {
|
|
136
|
+
async getFoldersList(treePrefix: string, options?: RequestOptions) {
|
|
133
137
|
return await this.callApiV2<FileItem[]>({
|
|
134
138
|
method: 'GET',
|
|
135
139
|
route: 'folders/search',
|
|
136
140
|
params: { prefix: treePrefix },
|
|
141
|
+
signal: options?.signal,
|
|
137
142
|
});
|
|
138
143
|
}
|
|
139
144
|
|
|
@@ -158,11 +163,7 @@ export class Files extends Base {
|
|
|
158
163
|
|
|
159
164
|
isPublic
|
|
160
165
|
? [] as FileItem[]
|
|
161
|
-
: this.
|
|
162
|
-
method: 'GET',
|
|
163
|
-
route: 'folders/search',
|
|
164
|
-
params: queryParams,
|
|
165
|
-
}),
|
|
166
|
+
: this.getFoldersList(term),
|
|
166
167
|
]);
|
|
167
168
|
|
|
168
169
|
return [...files, ...folders];
|
|
@@ -171,10 +172,11 @@ export class Files extends Base {
|
|
|
171
172
|
/**
|
|
172
173
|
* Create ROOT folder
|
|
173
174
|
*/
|
|
174
|
-
async createRootFolder(): Promise<void> {
|
|
175
|
+
async createRootFolder(options?: RequestOptions): Promise<void> {
|
|
175
176
|
await this.callApiV2({
|
|
176
177
|
method: 'post',
|
|
177
178
|
route: 'folders/root',
|
|
179
|
+
signal: options?.signal,
|
|
178
180
|
});
|
|
179
181
|
}
|
|
180
182
|
|
|
@@ -182,11 +184,12 @@ export class Files extends Base {
|
|
|
182
184
|
* Create new folder
|
|
183
185
|
* @param folderName should have folder path + folder name, example: /main/New folder
|
|
184
186
|
*/
|
|
185
|
-
async createFolder(folderName: string): Promise<void> {
|
|
187
|
+
async createFolder(folderName: string, options?: RequestOptions): Promise<void> {
|
|
186
188
|
await this.callApiV2({
|
|
187
189
|
method: 'post',
|
|
188
190
|
route: 'folders',
|
|
189
191
|
data: { key: folderName },
|
|
192
|
+
signal: options?.signal,
|
|
190
193
|
});
|
|
191
194
|
}
|
|
192
195
|
|
|
@@ -285,11 +288,12 @@ export class Files extends Base {
|
|
|
285
288
|
* Delete folder
|
|
286
289
|
* @param key folder name
|
|
287
290
|
*/
|
|
288
|
-
async deleteFolder(key: string): Promise<void> {
|
|
291
|
+
async deleteFolder(key: string, options?: RequestOptions): Promise<void> {
|
|
289
292
|
await this.callApiV2({
|
|
290
293
|
method: 'delete',
|
|
291
294
|
route: 'folders',
|
|
292
295
|
params: { key },
|
|
296
|
+
signal: options?.signal,
|
|
293
297
|
});
|
|
294
298
|
}
|
|
295
299
|
|
|
@@ -320,7 +324,14 @@ export class Files extends Base {
|
|
|
320
324
|
*/
|
|
321
325
|
public async getUploadUrlV2(
|
|
322
326
|
/** Details of the file to be uploaded */
|
|
323
|
-
{
|
|
327
|
+
{
|
|
328
|
+
fileName,
|
|
329
|
+
prefix,
|
|
330
|
+
contentType = defaultContentType,
|
|
331
|
+
isPublic = false,
|
|
332
|
+
expiresAt,
|
|
333
|
+
...data
|
|
334
|
+
}: UploadUrlProps,
|
|
324
335
|
|
|
325
336
|
/** Additional request options */
|
|
326
337
|
options?: RequestOptions,
|
|
@@ -329,7 +340,7 @@ export class Files extends Base {
|
|
|
329
340
|
const reqData: GetUploadUrlDataPayload = {
|
|
330
341
|
...data,
|
|
331
342
|
key: fileKey,
|
|
332
|
-
contentType
|
|
343
|
+
contentType,
|
|
333
344
|
};
|
|
334
345
|
const signal = options?.signal;
|
|
335
346
|
|
|
@@ -360,7 +371,7 @@ export class Files extends Base {
|
|
|
360
371
|
fileName,
|
|
361
372
|
prefix,
|
|
362
373
|
fileContent,
|
|
363
|
-
contentType,
|
|
374
|
+
contentType = defaultContentType,
|
|
364
375
|
expiresAt,
|
|
365
376
|
isPublic = false,
|
|
366
377
|
rewriteMode,
|
|
@@ -436,7 +447,7 @@ export class Files extends Base {
|
|
|
436
447
|
fileName,
|
|
437
448
|
prefix,
|
|
438
449
|
fileContent,
|
|
439
|
-
contentType,
|
|
450
|
+
contentType = defaultContentType,
|
|
440
451
|
cacheControl = 'max-age=3600',
|
|
441
452
|
expiresAt,
|
|
442
453
|
knownLength,
|
|
@@ -472,13 +483,29 @@ export class Files extends Base {
|
|
|
472
483
|
return signedUrl.downloadUrl;
|
|
473
484
|
}
|
|
474
485
|
|
|
486
|
+
/**
|
|
487
|
+
* Delete system file
|
|
488
|
+
* @param path file path
|
|
489
|
+
*/
|
|
490
|
+
public async deleteSystemFile(path: string, options?: RequestOptions): Promise<void> {
|
|
491
|
+
await this.callApiV2({
|
|
492
|
+
method: 'delete',
|
|
493
|
+
route: 'system-file',
|
|
494
|
+
params: { key: path },
|
|
495
|
+
signal: options?.signal,
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
475
499
|
/** Get signed URL params for uploading of system file */
|
|
476
|
-
|
|
477
|
-
|
|
500
|
+
private async getSystemFileUploadUrl(
|
|
501
|
+
{
|
|
502
|
+
fileName,
|
|
503
|
+
prefix,
|
|
504
|
+
cacheControl,
|
|
505
|
+
expiresAt,
|
|
506
|
+
}: GetSystemFileUploadUrlParams,
|
|
478
507
|
options?: RequestOptions,
|
|
479
508
|
): Promise<UploadUrlResponse> {
|
|
480
|
-
const { fileName, prefix, cacheControl, expiresAt } = params;
|
|
481
|
-
|
|
482
509
|
return await this.callApiV2<UploadUrlResponse>({
|
|
483
510
|
method: 'post',
|
|
484
511
|
route: 'system-file',
|
|
@@ -492,18 +519,6 @@ export class Files extends Base {
|
|
|
492
519
|
});
|
|
493
520
|
}
|
|
494
521
|
|
|
495
|
-
/**
|
|
496
|
-
* Delete system file
|
|
497
|
-
* @param path file path
|
|
498
|
-
*/
|
|
499
|
-
public async deleteSystemFile(path: string): Promise<void> {
|
|
500
|
-
await this.callApiV2({
|
|
501
|
-
method: 'delete',
|
|
502
|
-
route: 'system-file',
|
|
503
|
-
params: { key: path },
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
|
|
507
522
|
// -----------------------
|
|
508
523
|
// --- Files TTL logic ---
|
|
509
524
|
// -----------------------
|
|
@@ -512,16 +527,17 @@ export class Files extends Base {
|
|
|
512
527
|
* Set ttl for a specific file or folder
|
|
513
528
|
* @param key file or folder path
|
|
514
529
|
* @param isPublic `false` for folders, `true` or `false` for files
|
|
515
|
-
* @param
|
|
530
|
+
* @param expiresAt when file should expire and be deleted
|
|
516
531
|
*/
|
|
517
|
-
async addTtl(key: string, isPublic: boolean,
|
|
532
|
+
async addTtl(key: string, isPublic: boolean, expiresAt: DateTime): Promise<void> {
|
|
533
|
+
// TODO: convert to new format
|
|
518
534
|
return this.callApiV2({
|
|
519
535
|
method: 'post',
|
|
520
536
|
route: 'ttl-files',
|
|
521
537
|
data: {
|
|
522
538
|
key,
|
|
523
539
|
isPublic,
|
|
524
|
-
ttl:
|
|
540
|
+
ttl: this.normalizeDate(expiresAt).toISOString(),
|
|
525
541
|
},
|
|
526
542
|
});
|
|
527
543
|
}
|
|
@@ -530,16 +546,17 @@ export class Files extends Base {
|
|
|
530
546
|
* Update ttl for a specific file or folder
|
|
531
547
|
* @param key file or folder path
|
|
532
548
|
* @param isPublic false for folders, true or false for files
|
|
533
|
-
* @param
|
|
549
|
+
* @param newExpiresAt when file should expire and be deleted
|
|
534
550
|
*/
|
|
535
|
-
async updateTtl(key: string, isPublic: boolean,
|
|
551
|
+
async updateTtl(key: string, isPublic: boolean, newExpiresAt: DateTime): Promise<void> {
|
|
552
|
+
// TODO: convert to new format
|
|
536
553
|
return this.callApiV2({
|
|
537
554
|
method: 'patch',
|
|
538
555
|
route: 'ttl-files',
|
|
539
556
|
data: {
|
|
540
557
|
key,
|
|
541
558
|
isPublic,
|
|
542
|
-
newTtl:
|
|
559
|
+
newTtl: this.normalizeDate(newExpiresAt).toISOString(),
|
|
543
560
|
},
|
|
544
561
|
});
|
|
545
562
|
}
|
|
@@ -565,7 +582,7 @@ export class Files extends Base {
|
|
|
565
582
|
fileName,
|
|
566
583
|
fileContent,
|
|
567
584
|
cacheControl,
|
|
568
|
-
contentType,
|
|
585
|
+
contentType = defaultContentType,
|
|
569
586
|
knownLength,
|
|
570
587
|
onUploadProgress,
|
|
571
588
|
}: UploadToSignedUrlParameters, options?: RequestOptions) {
|
|
@@ -619,7 +636,7 @@ export class Files extends Base {
|
|
|
619
636
|
}
|
|
620
637
|
|
|
621
638
|
private getFormDataHeaders(formData: FormDataNode | FormData): Record<string, string> {
|
|
622
|
-
return ('getHeaders' in formData)
|
|
639
|
+
return ('getHeaders' in formData && typeof formData.getHeaders === 'function')
|
|
623
640
|
? formData.getHeaders()
|
|
624
641
|
: {};
|
|
625
642
|
}
|
|
@@ -630,9 +647,19 @@ export class Files extends Base {
|
|
|
630
647
|
return isNode ? Buffer.from(bytes) : new Blob([bytes]);
|
|
631
648
|
}
|
|
632
649
|
|
|
650
|
+
if (fileContent instanceof Uint8Array) {
|
|
651
|
+
const bytes = fileContent as Uint8Array;
|
|
652
|
+
return isNode
|
|
653
|
+
? Buffer.from(bytes)
|
|
654
|
+
: new Blob([bytes.buffer as ArrayBuffer]);
|
|
655
|
+
}
|
|
656
|
+
|
|
633
657
|
return fileContent;
|
|
634
658
|
}
|
|
635
659
|
|
|
660
|
+
private normalizeDate(input: undefined, options?: NormalizeDateOptions): undefined;
|
|
661
|
+
private normalizeDate(input: DateTime, options?: NormalizeDateOptions): Date;
|
|
662
|
+
private normalizeDate(input: DateTime | undefined, options?: NormalizeDateOptions): Date | undefined;
|
|
636
663
|
/**
|
|
637
664
|
* Get Date instance from input that represents date
|
|
638
665
|
*
|
|
@@ -641,20 +668,22 @@ export class Files extends Base {
|
|
|
641
668
|
* @returns Date instance represented by input
|
|
642
669
|
* @throws If input is invalid date
|
|
643
670
|
*/
|
|
644
|
-
private normalizeDate(input:
|
|
671
|
+
private normalizeDate(input: DateTime | undefined, options?: NormalizeDateOptions): Date | undefined {
|
|
645
672
|
if (input == undefined) return undefined;
|
|
646
673
|
|
|
674
|
+
const { errorMessage, validateFutureDate = true } = options ?? {};
|
|
675
|
+
|
|
647
676
|
const date = input instanceof Date ? input : new Date(input);
|
|
648
677
|
|
|
649
|
-
if (isNaN(date.getTime()))
|
|
678
|
+
if (isNaN(date.getTime())) {
|
|
679
|
+
throw new Error(errorMessage ?? 'Invalid date input');
|
|
680
|
+
} else if (validateFutureDate && Date.now() > date.getTime()) {
|
|
681
|
+
throw new Error(errorMessage ?? 'Date must be a future date');
|
|
682
|
+
}
|
|
650
683
|
|
|
651
684
|
return date;
|
|
652
685
|
}
|
|
653
686
|
|
|
654
|
-
private get defaultContentType() {
|
|
655
|
-
return 'binary/octet-stream';
|
|
656
|
-
}
|
|
657
|
-
|
|
658
687
|
// --------------------------
|
|
659
688
|
// --- Deprecated methods ---
|
|
660
689
|
// --------------------------
|
package/src/types/ttl.ts
CHANGED
|
@@ -3,4 +3,15 @@
|
|
|
3
3
|
* - `number` amount of milliseconds since midnight Jan 1 1970 UTC (aka UNIX timestamp in milliseconds)
|
|
4
4
|
* - `string` valid Date string that represents datetime
|
|
5
5
|
* */
|
|
6
|
-
export type
|
|
6
|
+
export type DateTime = Date | number | string;
|
|
7
|
+
|
|
8
|
+
export type NormalizeDateOptions = {
|
|
9
|
+
/** Message of the error to throw if input is invalid date */
|
|
10
|
+
errorMessage?: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* If `true` check that date is in the future, otherwise do not check
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
validateFutureDate?: boolean;
|
|
17
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AxiosProgressEvent } from 'axios';
|
|
2
2
|
import type { ReadStream } from 'node:fs';
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type { DateTime } from './ttl';
|
|
5
5
|
|
|
6
6
|
export type { AxiosProgressEvent };
|
|
7
7
|
|
|
@@ -55,7 +55,7 @@ export type UploadBaseParams = {
|
|
|
55
55
|
rewriteMode?: 'rewrite' | 'prevent-rewrite';
|
|
56
56
|
|
|
57
57
|
/** Datetime when file should expire and be deleted */
|
|
58
|
-
expiresAt?:
|
|
58
|
+
expiresAt?: DateTime;
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
61
|
* If `true` after upload file would be publicly available by it's URL.
|
|
@@ -78,8 +78,13 @@ export type GetUploadUrlDataPayload = Omit<UploadUrlProps, 'expiresAt' | 'fileNa
|
|
|
78
78
|
};
|
|
79
79
|
|
|
80
80
|
export type UploadUrlResponse = {
|
|
81
|
+
/** URL for uploading of the file */
|
|
81
82
|
url: string;
|
|
83
|
+
|
|
84
|
+
/** URL for downloading the file once it will be uploaded */
|
|
82
85
|
downloadUrl: string;
|
|
86
|
+
|
|
87
|
+
/** Additional FormData fields and headers for uploading the file */
|
|
83
88
|
fields: UploadFields;
|
|
84
89
|
};
|
|
85
90
|
|
|
@@ -97,8 +102,8 @@ export type UploadFields = {
|
|
|
97
102
|
|
|
98
103
|
/** Buffer and ReadStream only supported in Node.js */
|
|
99
104
|
export type FileModel = File | Blob | Buffer | ReadStream;
|
|
100
|
-
/** Allows to use string as a file value */
|
|
101
|
-
export type ExtendedFileModel = FileModel | string;
|
|
105
|
+
/** Allows to use a string or Uint8Array<ArrayBuffer> as a file value */
|
|
106
|
+
export type ExtendedFileModel = FileModel | string | Uint8Array;
|
|
102
107
|
|
|
103
108
|
export type UploadFileBaseParams = UploadBaseParams & {
|
|
104
109
|
/** File contents */
|
|
@@ -138,18 +143,6 @@ export type UploadToSignedUrlParameters = Pick<
|
|
|
138
143
|
signedUrl: UploadUrlResponse;
|
|
139
144
|
}
|
|
140
145
|
|
|
141
|
-
export type UploadToSignedUrlParameters2 = {
|
|
142
|
-
signedUrl: UploadUrlResponse;
|
|
143
|
-
file: FileModel;
|
|
144
|
-
fileName: string;
|
|
145
|
-
cacheControl?: string;
|
|
146
|
-
/** Size of the file in bytes. Helpful when file is an instance of ReadStream in Node.js */
|
|
147
|
-
knownLength?: number;
|
|
148
|
-
contentType?: string;
|
|
149
|
-
signal?: AbortSignal;
|
|
150
|
-
onUploadProgress?: (event: ProgressEvent | AxiosProgressEvent) => void;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
146
|
// -------- deprecated ----------
|
|
154
147
|
export type UploadFilePropsLegacy = Pick<
|
|
155
148
|
UploadFileProps,
|