@plugable-io/js 0.0.1 → 0.0.2
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 +126 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @plugable-io/js
|
|
2
|
+
|
|
3
|
+
JavaScript/TypeScript client for the Plugable File Management API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @plugable-io/js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Bucket Usage Patterns
|
|
14
|
+
|
|
15
|
+
When creating a bucket, you can choose a usage pattern that dictates how files are accessed and isolated:
|
|
16
|
+
|
|
17
|
+
- **Public**: Files are publicly readable by anyone (no auth token required). However, only authenticated users (your end-users) can upload files.
|
|
18
|
+
- **Organization**: Files are isolated by `org_id`. Users can only access files belonging to their organization (as defined in their auth token).
|
|
19
|
+
- **Personal**: Files are isolated by `owner_id`. Users can only access their own files.
|
|
20
|
+
|
|
21
|
+
### Authentication
|
|
22
|
+
|
|
23
|
+
We work with your auth provider, to simplify the process of integration.
|
|
24
|
+
Currently we support:
|
|
25
|
+
- Clerk
|
|
26
|
+
- Supabase
|
|
27
|
+
- Firebase Auth
|
|
28
|
+
|
|
29
|
+
Please setup provider on admin first.
|
|
30
|
+
|
|
31
|
+
If you would like to see any other providers supported, please let us know.
|
|
32
|
+
|
|
33
|
+
### Initialization
|
|
34
|
+
|
|
35
|
+
Initialize the client with your Bucket ID and a function to retrieve the user's authentication token.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { BucketClient } from '@plugable-io/js';
|
|
39
|
+
|
|
40
|
+
const client = new BucketClient({
|
|
41
|
+
bucketId: 'YOUR_BUCKET_ID',
|
|
42
|
+
// Function that returns a Promise resolving to the auth token (e.g., from Clerk, Supabase, etc.). It should return a valid JWT token.
|
|
43
|
+
getToken: async () => {
|
|
44
|
+
// Example: return await auth.getToken();
|
|
45
|
+
return 'user-jwt-token';
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### File metadata
|
|
51
|
+
|
|
52
|
+
You can attach custom metadata to your files. This is useful for storing external IDs, tags, or other application-specific data. You can later search for files based on this metadata.
|
|
53
|
+
|
|
54
|
+
**Limitations:**
|
|
55
|
+
- **Size**: Maximum 5KB of JSON data.
|
|
56
|
+
- **Structure**: Must be a flat object (no nested objects).
|
|
57
|
+
- **Arrays**: Arrays are supported, but they cannot contain objects.
|
|
58
|
+
### Uploading a File
|
|
59
|
+
|
|
60
|
+
You can upload a file using the `upload` method. You can also pass metadata that you can use for search later, such as external IDs or tags.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const fileInput = document.querySelector('input[type="file"]');
|
|
64
|
+
const file = fileInput.files[0];
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const uploadedFile = await client.upload(file, {
|
|
68
|
+
metadata: {
|
|
69
|
+
custom_key: 'custom_value'
|
|
70
|
+
},
|
|
71
|
+
onProgress: (percent) => {
|
|
72
|
+
console.log(`Upload progress: ${percent}%`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
console.log('File uploaded:', uploadedFile);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('Upload failed:', error);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Listing Files
|
|
82
|
+
|
|
83
|
+
The results depend on your bucket's usage pattern:
|
|
84
|
+
- **Public**: Returns all files in the bucket.
|
|
85
|
+
- **Organization**: Returns files matching the user's `org_id`.
|
|
86
|
+
- **Personal**: Returns files matching the user's `owner_id`.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Example: Find all invoices for a specific order
|
|
90
|
+
const response = await client.list({
|
|
91
|
+
metadata: {
|
|
92
|
+
type: 'invoice',
|
|
93
|
+
order_id: 'ord_12345'
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
console.log('Files:', response.files);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Getting a File
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const file = await client.get('file_id');
|
|
105
|
+
console.log('File details:', file);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Updating File Metadata
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const updatedFile = await client.update('file_id', {
|
|
112
|
+
metadata: {
|
|
113
|
+
new_key: 'new_value'
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Deleting a File
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
await client.delete('file_id');
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|