@plugable-io/js 0.0.3 → 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 CHANGED
@@ -72,6 +72,12 @@ try {
72
72
  },
73
73
  onProgress: (percent) => {
74
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);
75
81
  }
76
82
  });
77
83
  console.log('File uploaded:', uploadedFile);
@@ -98,7 +104,24 @@ const response = await client.list({
98
104
  });
99
105
 
100
106
  console.log('Files:', response.files);
107
+ ```
101
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
+ }
102
125
  ```
103
126
 
104
127
  ### Getting a File
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
- return this.request("GET", `/buckets/${this.config.bucketId}/files/${id}`);
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
- const checksum = await calculateChecksum(file);
124
- const uploadInit = await this.request(
125
- "POST",
126
- `/buckets/${this.config.bucketId}/files`,
127
- {
128
- filename: file.name,
129
- byte_size: file.size,
130
- checksum,
131
- content_type: file.type
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
- return this.request("POST", `/buckets/${this.config.bucketId}/files/confirm`, {
144
- signed_id: uploadInit.sid,
145
- metadata: options?.metadata
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
- return this.request("GET", `/buckets/${this.config.bucketId}/files/${id}`);
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
- const checksum = await calculateChecksum(file);
90
- const uploadInit = await this.request(
91
- "POST",
92
- `/buckets/${this.config.bucketId}/files`,
93
- {
94
- filename: file.name,
95
- byte_size: file.size,
96
- checksum,
97
- content_type: file.type
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
- return this.request("POST", `/buckets/${this.config.bucketId}/files/confirm`, {
110
- signed_id: uploadInit.sid,
111
- metadata: options?.metadata
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",
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",