@withvlibe/storage-sdk 1.0.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/README.md +256 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# @withvlibe/storage-sdk
|
|
2
|
+
|
|
3
|
+
Official Vlibe Storage SDK for file uploads in Vlibe apps.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @withvlibe/storage-sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @withvlibe/storage-sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @withvlibe/storage-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { VlibeStorage } from '@withvlibe/storage-sdk';
|
|
19
|
+
|
|
20
|
+
// Initialize the client
|
|
21
|
+
const storage = new VlibeStorage({
|
|
22
|
+
appId: 'your-app-id',
|
|
23
|
+
appSecret: 'your-app-secret',
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Set user auth token (from Vlibe Auth)
|
|
27
|
+
storage.setAuthToken(userToken);
|
|
28
|
+
|
|
29
|
+
// Upload a file
|
|
30
|
+
const result = await storage.upload(file, {
|
|
31
|
+
folder: 'images',
|
|
32
|
+
isPublic: true,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
console.log(result.url); // File URL
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## React Hooks
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { VlibeStorage, useStorage, useStorageUsage } from '@withvlibe/storage-sdk/react';
|
|
42
|
+
|
|
43
|
+
// Create client instance
|
|
44
|
+
const storage = new VlibeStorage({
|
|
45
|
+
appId: 'your-app-id',
|
|
46
|
+
appSecret: 'your-app-secret',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function FileUploader() {
|
|
50
|
+
const {
|
|
51
|
+
files,
|
|
52
|
+
loading,
|
|
53
|
+
upload,
|
|
54
|
+
uploadWithProgress,
|
|
55
|
+
uploadProgress,
|
|
56
|
+
uploading,
|
|
57
|
+
remove,
|
|
58
|
+
refresh,
|
|
59
|
+
} = useStorage(storage, { folder: 'documents' });
|
|
60
|
+
|
|
61
|
+
const {
|
|
62
|
+
usage,
|
|
63
|
+
usagePercent,
|
|
64
|
+
usageFormatted,
|
|
65
|
+
isNearLimit,
|
|
66
|
+
canUpload,
|
|
67
|
+
} = useStorageUsage(storage);
|
|
68
|
+
|
|
69
|
+
const handleUpload = async (file: File) => {
|
|
70
|
+
if (!canUpload(file.size)) {
|
|
71
|
+
alert('Storage limit would be exceeded');
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const result = await uploadWithProgress(file, {
|
|
76
|
+
onProgress: (progress) => console.log(`${progress}%`),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log('Uploaded:', result.url);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div>
|
|
84
|
+
<p>Storage: {usageFormatted} ({usagePercent}%)</p>
|
|
85
|
+
{isNearLimit && <p>Warning: Running low on storage!</p>}
|
|
86
|
+
|
|
87
|
+
<input type="file" onChange={(e) => handleUpload(e.target.files[0])} />
|
|
88
|
+
{uploading && <p>Uploading: {uploadProgress}%</p>}
|
|
89
|
+
|
|
90
|
+
<h3>Files ({files.length})</h3>
|
|
91
|
+
{files.map((file) => (
|
|
92
|
+
<div key={file.id}>
|
|
93
|
+
{file.filename} ({file.size} bytes)
|
|
94
|
+
<button onClick={() => remove(file.id)}>Delete</button>
|
|
95
|
+
</div>
|
|
96
|
+
))}
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## API Reference
|
|
103
|
+
|
|
104
|
+
### VlibeStorage
|
|
105
|
+
|
|
106
|
+
#### Constructor
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
new VlibeStorage({
|
|
110
|
+
appId: string; // Your Vlibe App ID
|
|
111
|
+
appSecret: string; // Your Vlibe App Secret
|
|
112
|
+
baseUrl?: string; // API base URL (optional)
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Methods
|
|
117
|
+
|
|
118
|
+
##### `setAuthToken(token: string)`
|
|
119
|
+
Set the user's authentication token.
|
|
120
|
+
|
|
121
|
+
##### `upload(file: File | Blob, options?: UploadOptions): Promise<UploadResult>`
|
|
122
|
+
Upload a file directly.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
const result = await storage.upload(file, {
|
|
126
|
+
folder: 'images',
|
|
127
|
+
isPublic: true,
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
##### `uploadWithProgress(file: File, options?: UploadOptions): Promise<UploadResult>`
|
|
132
|
+
Upload a file with progress tracking.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
const result = await storage.uploadWithProgress(file, {
|
|
136
|
+
onProgress: (percent) => console.log(`${percent}%`),
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
##### `getDownloadUrl(fileId: string, expiresIn?: number): Promise<string>`
|
|
141
|
+
Get a signed download URL for a file.
|
|
142
|
+
|
|
143
|
+
##### `list(options?: ListOptions): Promise<ListResult>`
|
|
144
|
+
List files.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const { files, total, hasMore } = await storage.list({
|
|
148
|
+
folder: 'images',
|
|
149
|
+
limit: 50,
|
|
150
|
+
offset: 0,
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
##### `get(fileId: string): Promise<StorageFile | null>`
|
|
155
|
+
Get file metadata.
|
|
156
|
+
|
|
157
|
+
##### `delete(fileId: string): Promise<boolean>`
|
|
158
|
+
Delete a file.
|
|
159
|
+
|
|
160
|
+
##### `deleteMany(fileIds: string[]): Promise<{ deleted: string[]; failed: string[] }>`
|
|
161
|
+
Delete multiple files.
|
|
162
|
+
|
|
163
|
+
##### `getUsage(): Promise<StorageStats>`
|
|
164
|
+
Get storage usage statistics.
|
|
165
|
+
|
|
166
|
+
##### `canUpload(size: number): Promise<CanUploadResult>`
|
|
167
|
+
Check if a file of given size can be uploaded.
|
|
168
|
+
|
|
169
|
+
### React Hooks
|
|
170
|
+
|
|
171
|
+
#### `useStorage(client, options?)`
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const {
|
|
175
|
+
files, // StorageFile[]
|
|
176
|
+
total, // number
|
|
177
|
+
loading, // boolean
|
|
178
|
+
error, // string | null
|
|
179
|
+
upload, // (file: File, options?) => Promise<UploadResult>
|
|
180
|
+
uploadWithProgress, // (file: File, options?) => Promise<UploadResult>
|
|
181
|
+
uploadProgress, // number (0-100)
|
|
182
|
+
uploading, // boolean
|
|
183
|
+
remove, // (fileId: string) => Promise<boolean>
|
|
184
|
+
refresh, // () => Promise<void>
|
|
185
|
+
loadMore, // () => Promise<void>
|
|
186
|
+
hasMore, // boolean
|
|
187
|
+
} = useStorage(client, {
|
|
188
|
+
folder: 'images', // optional
|
|
189
|
+
autoFetch: true, // optional, default true
|
|
190
|
+
limit: 50, // optional
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### `useStorageUsage(client, options?)`
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
const {
|
|
198
|
+
usage, // StorageStats | null
|
|
199
|
+
loading, // boolean
|
|
200
|
+
error, // string | null
|
|
201
|
+
canUpload, // (size: number) => boolean
|
|
202
|
+
refresh, // () => Promise<void>
|
|
203
|
+
usagePercent, // number
|
|
204
|
+
usageFormatted, // string (e.g., "1.5 GB of 5 GB")
|
|
205
|
+
isLimitReached, // boolean
|
|
206
|
+
isNearLimit, // boolean (> 80%)
|
|
207
|
+
} = useStorageUsage(client, {
|
|
208
|
+
autoFetch: true, // optional
|
|
209
|
+
refreshInterval: 0, // optional, ms
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Types
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
interface UploadOptions {
|
|
217
|
+
folder?: string;
|
|
218
|
+
filename?: string;
|
|
219
|
+
isPublic?: boolean;
|
|
220
|
+
onProgress?: (progress: number) => void;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
interface UploadResult {
|
|
224
|
+
id: string;
|
|
225
|
+
key: string;
|
|
226
|
+
filename: string;
|
|
227
|
+
size: number;
|
|
228
|
+
mimeType: string;
|
|
229
|
+
url: string;
|
|
230
|
+
isPublic: boolean;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
interface StorageFile {
|
|
234
|
+
id: string;
|
|
235
|
+
key: string;
|
|
236
|
+
filename: string;
|
|
237
|
+
size: number;
|
|
238
|
+
mimeType: string;
|
|
239
|
+
isPublic: boolean;
|
|
240
|
+
folder: string | null;
|
|
241
|
+
createdAt: string;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
interface StorageStats {
|
|
245
|
+
bytesUsed: number;
|
|
246
|
+
bytesUsedFormatted: string;
|
|
247
|
+
fileCount: number;
|
|
248
|
+
storageLimit: number;
|
|
249
|
+
storageLimitFormatted: string;
|
|
250
|
+
usagePercent: number;
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@withvlibe/storage-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vlibe Storage SDK - File storage for Vlibe apps",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./react": {
|
|
15
|
+
"import": "./dist/react.mjs",
|
|
16
|
+
"require": "./dist/react.js",
|
|
17
|
+
"types": "./dist/react.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup",
|
|
25
|
+
"dev": "tsup --watch",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"clean": "rm -rf dist"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"vlibe",
|
|
31
|
+
"storage",
|
|
32
|
+
"file-upload",
|
|
33
|
+
"s3",
|
|
34
|
+
"sdk"
|
|
35
|
+
],
|
|
36
|
+
"author": "Vlibe",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=18.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"react": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^20.0.0",
|
|
48
|
+
"@types/react": "^18.0.0",
|
|
49
|
+
"tsup": "^8.5.1",
|
|
50
|
+
"typescript": "^5.0.0"
|
|
51
|
+
}
|
|
52
|
+
}
|