@plugable-io/js 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -3
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +42 -23
- package/dist/index.mjs +42 -23
- package/package.json +12 -2
package/README.md
CHANGED
|
@@ -66,10 +66,18 @@ const file = fileInput.files[0];
|
|
|
66
66
|
try {
|
|
67
67
|
const uploadedFile = await client.upload(file, {
|
|
68
68
|
metadata: {
|
|
69
|
-
|
|
69
|
+
type: 'invoice',
|
|
70
|
+
order_id: 'ord_12345',
|
|
71
|
+
purchased: ['product_1', 'product_2']
|
|
70
72
|
},
|
|
71
73
|
onProgress: (percent) => {
|
|
72
74
|
console.log(`Upload progress: ${percent}%`);
|
|
75
|
+
},
|
|
76
|
+
onCompleted: (file) => {
|
|
77
|
+
console.log('Upload completed:', file);
|
|
78
|
+
},
|
|
79
|
+
onFailed: (error) => {
|
|
80
|
+
console.error('Upload failed:', error);
|
|
73
81
|
}
|
|
74
82
|
});
|
|
75
83
|
console.log('File uploaded:', uploadedFile);
|
|
@@ -85,19 +93,37 @@ The results depend on your bucket's usage pattern:
|
|
|
85
93
|
- **Organization**: Returns files matching the user's `org_id`.
|
|
86
94
|
- **Personal**: Returns files matching the user's `owner_id`.
|
|
87
95
|
|
|
96
|
+
|
|
88
97
|
```typescript
|
|
89
98
|
// Example: Find all invoices for a specific order
|
|
90
99
|
const response = await client.list({
|
|
91
100
|
metadata: {
|
|
92
101
|
type: 'invoice',
|
|
93
|
-
|
|
102
|
+
purchased: ['product_1']
|
|
94
103
|
}
|
|
95
104
|
});
|
|
96
|
-
```
|
|
97
105
|
|
|
98
106
|
console.log('Files:', response.files);
|
|
99
107
|
```
|
|
100
108
|
|
|
109
|
+
### Finding a File
|
|
110
|
+
|
|
111
|
+
You can find a single file using `find`. This is a convenience method that searches for files and returns the first match. It's useful for finding files by metadata or other criteria.
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const file = await client.find({
|
|
115
|
+
metadata: {
|
|
116
|
+
external_id: 'user_123_avatar'
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (file) {
|
|
121
|
+
console.log('Found file:', file);
|
|
122
|
+
} else {
|
|
123
|
+
console.log('File not found');
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
101
127
|
### Getting a File
|
|
102
128
|
|
|
103
129
|
```typescript
|
package/dist/index.d.mts
CHANGED
|
@@ -32,6 +32,8 @@ interface SearchOptions {
|
|
|
32
32
|
interface UploadOptions {
|
|
33
33
|
metadata?: Record<string, any>;
|
|
34
34
|
onProgress?: (progress: number) => void;
|
|
35
|
+
onCompleted?: (file: FileObject) => void;
|
|
36
|
+
onFailed?: (error: Error) => void;
|
|
35
37
|
}
|
|
36
38
|
interface UpdateOptions {
|
|
37
39
|
filename?: string;
|
|
@@ -45,6 +47,7 @@ declare class BucketClient {
|
|
|
45
47
|
private request;
|
|
46
48
|
list(options?: SearchOptions): Promise<ListResponse>;
|
|
47
49
|
get(id: string): Promise<FileObject>;
|
|
50
|
+
find(options: SearchOptions): Promise<FileObject | null>;
|
|
48
51
|
update(id: string, updates: UpdateOptions): Promise<FileObject>;
|
|
49
52
|
delete(id: string): Promise<void>;
|
|
50
53
|
upload(file: File, options?: UploadOptions): Promise<FileObject>;
|
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,8 @@ interface SearchOptions {
|
|
|
32
32
|
interface UploadOptions {
|
|
33
33
|
metadata?: Record<string, any>;
|
|
34
34
|
onProgress?: (progress: number) => void;
|
|
35
|
+
onCompleted?: (file: FileObject) => void;
|
|
36
|
+
onFailed?: (error: Error) => void;
|
|
35
37
|
}
|
|
36
38
|
interface UpdateOptions {
|
|
37
39
|
filename?: string;
|
|
@@ -45,6 +47,7 @@ declare class BucketClient {
|
|
|
45
47
|
private request;
|
|
46
48
|
list(options?: SearchOptions): Promise<ListResponse>;
|
|
47
49
|
get(id: string): Promise<FileObject>;
|
|
50
|
+
find(options: SearchOptions): Promise<FileObject | null>;
|
|
48
51
|
update(id: string, updates: UpdateOptions): Promise<FileObject>;
|
|
49
52
|
delete(id: string): Promise<void>;
|
|
50
53
|
upload(file: File, options?: UploadOptions): Promise<FileObject>;
|
package/dist/index.js
CHANGED
|
@@ -111,7 +111,15 @@ var BucketClient = class {
|
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
113
|
async get(id) {
|
|
114
|
-
|
|
114
|
+
const file = await this.request("GET", `/buckets/${this.config.bucketId}/files/${id}`);
|
|
115
|
+
if (!file.checksum) {
|
|
116
|
+
console.warn(`File ${id} missing checksum`);
|
|
117
|
+
}
|
|
118
|
+
return file;
|
|
119
|
+
}
|
|
120
|
+
async find(options) {
|
|
121
|
+
const response = await this.list({ ...options, per_page: 1 });
|
|
122
|
+
return response.files.length > 0 ? response.files[0] : null;
|
|
115
123
|
}
|
|
116
124
|
async update(id, updates) {
|
|
117
125
|
return this.request("PATCH", `/buckets/${this.config.bucketId}/files/${id}`, updates);
|
|
@@ -120,30 +128,41 @@ var BucketClient = class {
|
|
|
120
128
|
await this.request("DELETE", `/buckets/${this.config.bucketId}/files/${id}`);
|
|
121
129
|
}
|
|
122
130
|
async upload(file, options) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
);
|
|
134
|
-
await import_axios.default.put(uploadInit.url, file, {
|
|
135
|
-
headers: uploadInit.headers,
|
|
136
|
-
onUploadProgress: (progressEvent) => {
|
|
137
|
-
if (options?.onProgress && progressEvent.total) {
|
|
138
|
-
const percentCompleted = Math.round(progressEvent.loaded * 100 / progressEvent.total);
|
|
139
|
-
options.onProgress(percentCompleted);
|
|
131
|
+
try {
|
|
132
|
+
const checksum = await calculateChecksum(file);
|
|
133
|
+
const uploadInit = await this.request(
|
|
134
|
+
"POST",
|
|
135
|
+
`/buckets/${this.config.bucketId}/files`,
|
|
136
|
+
{
|
|
137
|
+
filename: file.name,
|
|
138
|
+
byte_size: file.size,
|
|
139
|
+
checksum,
|
|
140
|
+
content_type: file.type
|
|
140
141
|
}
|
|
142
|
+
);
|
|
143
|
+
await import_axios.default.put(uploadInit.url, file, {
|
|
144
|
+
headers: uploadInit.headers,
|
|
145
|
+
onUploadProgress: (progressEvent) => {
|
|
146
|
+
if (options?.onProgress && progressEvent.total) {
|
|
147
|
+
const percentCompleted = Math.round(progressEvent.loaded * 100 / progressEvent.total);
|
|
148
|
+
options.onProgress(percentCompleted);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
const confirmedFile = await this.request("POST", `/buckets/${this.config.bucketId}/files/confirm`, {
|
|
153
|
+
signed_id: uploadInit.sid,
|
|
154
|
+
metadata: options?.metadata
|
|
155
|
+
});
|
|
156
|
+
if (options?.onCompleted) {
|
|
157
|
+
options.onCompleted(confirmedFile);
|
|
141
158
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
159
|
+
return confirmedFile;
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (options?.onFailed) {
|
|
162
|
+
options.onFailed(error);
|
|
163
|
+
}
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
147
166
|
}
|
|
148
167
|
};
|
|
149
168
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -77,7 +77,15 @@ var BucketClient = class {
|
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
async get(id) {
|
|
80
|
-
|
|
80
|
+
const file = await this.request("GET", `/buckets/${this.config.bucketId}/files/${id}`);
|
|
81
|
+
if (!file.checksum) {
|
|
82
|
+
console.warn(`File ${id} missing checksum`);
|
|
83
|
+
}
|
|
84
|
+
return file;
|
|
85
|
+
}
|
|
86
|
+
async find(options) {
|
|
87
|
+
const response = await this.list({ ...options, per_page: 1 });
|
|
88
|
+
return response.files.length > 0 ? response.files[0] : null;
|
|
81
89
|
}
|
|
82
90
|
async update(id, updates) {
|
|
83
91
|
return this.request("PATCH", `/buckets/${this.config.bucketId}/files/${id}`, updates);
|
|
@@ -86,30 +94,41 @@ var BucketClient = class {
|
|
|
86
94
|
await this.request("DELETE", `/buckets/${this.config.bucketId}/files/${id}`);
|
|
87
95
|
}
|
|
88
96
|
async upload(file, options) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
);
|
|
100
|
-
await axios.put(uploadInit.url, file, {
|
|
101
|
-
headers: uploadInit.headers,
|
|
102
|
-
onUploadProgress: (progressEvent) => {
|
|
103
|
-
if (options?.onProgress && progressEvent.total) {
|
|
104
|
-
const percentCompleted = Math.round(progressEvent.loaded * 100 / progressEvent.total);
|
|
105
|
-
options.onProgress(percentCompleted);
|
|
97
|
+
try {
|
|
98
|
+
const checksum = await calculateChecksum(file);
|
|
99
|
+
const uploadInit = await this.request(
|
|
100
|
+
"POST",
|
|
101
|
+
`/buckets/${this.config.bucketId}/files`,
|
|
102
|
+
{
|
|
103
|
+
filename: file.name,
|
|
104
|
+
byte_size: file.size,
|
|
105
|
+
checksum,
|
|
106
|
+
content_type: file.type
|
|
106
107
|
}
|
|
108
|
+
);
|
|
109
|
+
await axios.put(uploadInit.url, file, {
|
|
110
|
+
headers: uploadInit.headers,
|
|
111
|
+
onUploadProgress: (progressEvent) => {
|
|
112
|
+
if (options?.onProgress && progressEvent.total) {
|
|
113
|
+
const percentCompleted = Math.round(progressEvent.loaded * 100 / progressEvent.total);
|
|
114
|
+
options.onProgress(percentCompleted);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const confirmedFile = await this.request("POST", `/buckets/${this.config.bucketId}/files/confirm`, {
|
|
119
|
+
signed_id: uploadInit.sid,
|
|
120
|
+
metadata: options?.metadata
|
|
121
|
+
});
|
|
122
|
+
if (options?.onCompleted) {
|
|
123
|
+
options.onCompleted(confirmedFile);
|
|
107
124
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
125
|
+
return confirmedFile;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (options?.onFailed) {
|
|
128
|
+
options.onFailed(error);
|
|
129
|
+
}
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
113
132
|
}
|
|
114
133
|
};
|
|
115
134
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plugable-io/js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "JavaScript client for Plugable File Management API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -13,7 +13,17 @@
|
|
|
13
13
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
14
14
|
"lint": "tsc --noEmit"
|
|
15
15
|
},
|
|
16
|
-
"keywords": [
|
|
16
|
+
"keywords": [
|
|
17
|
+
"file-upload",
|
|
18
|
+
"storage",
|
|
19
|
+
"file-management",
|
|
20
|
+
"cloud-storage",
|
|
21
|
+
"typescript",
|
|
22
|
+
"client",
|
|
23
|
+
"sdk",
|
|
24
|
+
"plugable",
|
|
25
|
+
"buckets"
|
|
26
|
+
],
|
|
17
27
|
"author": "",
|
|
18
28
|
"license": "MIT",
|
|
19
29
|
"dependencies": {
|