@digibuffer/file-manager-core 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.
@@ -0,0 +1,339 @@
1
+ //#region src/types.d.ts
2
+ /**
3
+ * File metadata stored in database or retrieved from storage
4
+ */
5
+ interface FileMetadata {
6
+ key: string;
7
+ size: number;
8
+ lastModified: Date;
9
+ contentType?: string;
10
+ etag?: string;
11
+ customMetadata?: Record<string, string>;
12
+ }
13
+ /**
14
+ * Database file record (metadata only)
15
+ */
16
+ interface DatabaseFile extends FileMetadata {
17
+ id: string;
18
+ userId?: string;
19
+ uploadedAt: Date;
20
+ updatedAt: Date;
21
+ }
22
+ /**
23
+ * Storage file from cloud provider
24
+ */
25
+ interface StorageFile extends FileMetadata {
26
+ bucket: string;
27
+ }
28
+ /**
29
+ * Unified file type that can come from either source
30
+ */
31
+ type ManagedFile = DatabaseFile | StorageFile;
32
+ /**
33
+ * Pagination cursor for listing files
34
+ */
35
+ interface PaginationCursor {
36
+ continuationToken?: string;
37
+ offset?: number;
38
+ }
39
+ /**
40
+ * Paginated response wrapper
41
+ */
42
+ interface PaginatedResponse<T> {
43
+ data: T[];
44
+ nextCursor?: PaginationCursor;
45
+ hasMore: boolean;
46
+ totalCount?: number;
47
+ }
48
+ /**
49
+ * List options for querying files
50
+ */
51
+ interface ListOptions {
52
+ prefix?: string;
53
+ delimiter?: string;
54
+ maxKeys?: number;
55
+ cursor?: PaginationCursor;
56
+ sortBy?: 'name' | 'size' | 'lastModified';
57
+ sortOrder?: 'asc' | 'desc';
58
+ userId?: string;
59
+ }
60
+ /**
61
+ * Download URL options
62
+ */
63
+ interface DownloadUrlOptions {
64
+ expiresIn?: number;
65
+ disposition?: 'inline' | 'attachment';
66
+ filename?: string;
67
+ }
68
+ /**
69
+ * File operation result
70
+ */
71
+ interface OperationResult {
72
+ success: boolean;
73
+ key: string;
74
+ error?: string;
75
+ }
76
+ /**
77
+ * Batch operation result
78
+ */
79
+ interface BatchOperationResult {
80
+ succeeded: string[];
81
+ failed: Array<{
82
+ key: string;
83
+ error: string;
84
+ }>;
85
+ totalProcessed: number;
86
+ }
87
+ /**
88
+ * Move/Rename operation options
89
+ */
90
+ interface MoveOptions {
91
+ sourceKey: string;
92
+ destinationKey: string;
93
+ deleteSource?: boolean;
94
+ }
95
+ /**
96
+ * Copy operation options
97
+ */
98
+ interface CopyOptions {
99
+ sourceKey: string;
100
+ destinationKey: string;
101
+ metadata?: Record<string, string>;
102
+ }
103
+ /**
104
+ * Data source mode
105
+ */
106
+ type DataSource = 'storage' | 'database' | 'both';
107
+ /**
108
+ * Storage provider interface
109
+ */
110
+ interface StorageProvider {
111
+ list(options: ListOptions): Promise<PaginatedResponse<StorageFile>>;
112
+ delete(key: string): Promise<OperationResult>;
113
+ deleteBatch(keys: string[]): Promise<BatchOperationResult>;
114
+ exists(key: string): Promise<boolean>;
115
+ getMetadata(key: string): Promise<StorageFile | null>;
116
+ getDownloadUrl(key: string, options?: DownloadUrlOptions): Promise<string>;
117
+ move(options: MoveOptions): Promise<OperationResult>;
118
+ copy(options: CopyOptions): Promise<OperationResult>;
119
+ rename(oldKey: string, newKey: string): Promise<OperationResult>;
120
+ }
121
+ /**
122
+ * Database provider interface
123
+ */
124
+ interface DatabaseProvider {
125
+ list(options: ListOptions): Promise<PaginatedResponse<DatabaseFile>>;
126
+ get(key: string): Promise<DatabaseFile | null>;
127
+ create(file: Omit<DatabaseFile, 'id' | 'uploadedAt' | 'updatedAt'>): Promise<DatabaseFile>;
128
+ update(key: string, data: Partial<DatabaseFile>): Promise<DatabaseFile | null>;
129
+ delete(key: string): Promise<OperationResult>;
130
+ deleteBatch(keys: string[]): Promise<BatchOperationResult>;
131
+ exists(key: string): Promise<boolean>;
132
+ sync(storageFiles: StorageFile[]): Promise<void>;
133
+ }
134
+ /**
135
+ * S3/R2 configuration
136
+ */
137
+ interface StorageConfig {
138
+ bucket: string;
139
+ accessKeyId: string;
140
+ secretAccessKey: string;
141
+ endpoint: string;
142
+ region?: string;
143
+ publicUrl?: string;
144
+ }
145
+ /**
146
+ * PostgreSQL configuration
147
+ */
148
+ interface DatabaseConfig {
149
+ connectionString?: string;
150
+ host?: string;
151
+ port?: number;
152
+ database?: string;
153
+ user?: string;
154
+ password?: string;
155
+ tableName?: string;
156
+ }
157
+ /**
158
+ * File manager configuration
159
+ */
160
+ interface FileManagerConfig {
161
+ storage: StorageConfig;
162
+ database?: DatabaseConfig;
163
+ defaultSource?: DataSource;
164
+ autoSync?: boolean;
165
+ }
166
+ /**
167
+ * Authorization context for file operations
168
+ */
169
+ interface AuthContext {
170
+ userId?: string;
171
+ roles?: string[];
172
+ permissions?: string[];
173
+ customData?: Record<string, unknown>;
174
+ }
175
+ /**
176
+ * Authorization callback
177
+ */
178
+ type AuthorizationCallback = (context: AuthContext, operation: string, resource: string) => Promise<boolean> | boolean;
179
+ /**
180
+ * Before/After operation hooks
181
+ */
182
+ interface OperationHooks {
183
+ beforeDelete?: (keys: string[]) => Promise<void> | void;
184
+ afterDelete?: (keys: string[]) => Promise<void> | void;
185
+ beforeMove?: (options: MoveOptions) => Promise<void> | void;
186
+ afterMove?: (options: MoveOptions) => Promise<void> | void;
187
+ beforeCopy?: (options: CopyOptions) => Promise<void> | void;
188
+ afterCopy?: (options: CopyOptions) => Promise<void> | void;
189
+ }
190
+ /**
191
+ * Custom error class for file manager operations
192
+ */
193
+ declare class FileManagerError extends Error {
194
+ code: string;
195
+ statusCode: number;
196
+ details?: unknown | undefined;
197
+ constructor(message: string, code: string, statusCode?: number, details?: unknown | undefined);
198
+ }
199
+ /**
200
+ * Request action types
201
+ */
202
+ type RequestAction = 'list' | 'delete' | 'batchDelete' | 'exists' | 'getMetadata' | 'getUrl' | 'move' | 'copy' | 'rename';
203
+ /**
204
+ * Request body for file operations
205
+ */
206
+ interface FileOperationRequest {
207
+ action: RequestAction;
208
+ key?: string;
209
+ keys?: string[];
210
+ options?: Record<string, unknown>;
211
+ source?: DataSource;
212
+ }
213
+ /**
214
+ * Response for file operations
215
+ */
216
+ interface FileOperationResponse<T = unknown> {
217
+ success: boolean;
218
+ data?: T;
219
+ error?: string;
220
+ code?: string;
221
+ }
222
+ //#endregion
223
+ //#region src/file-manager.d.ts
224
+ /**
225
+ * Main FileManager class - Orchestrates file operations across storage and database
226
+ */
227
+ declare class FileManager {
228
+ private storage;
229
+ private database?;
230
+ private defaultSource;
231
+ private autoSync;
232
+ private hooks?;
233
+ constructor(config: FileManagerConfig, hooks?: OperationHooks);
234
+ /**
235
+ * List files from specified source
236
+ */
237
+ list(options?: ListOptions, source?: DataSource): Promise<PaginatedResponse<ManagedFile>>;
238
+ /**
239
+ * List files from storage only
240
+ */
241
+ listFromStorage(options?: ListOptions): Promise<PaginatedResponse<StorageFile>>;
242
+ /**
243
+ * List files from database only
244
+ */
245
+ listFromDatabase(options?: ListOptions): Promise<PaginatedResponse<DatabaseFile>>;
246
+ /**
247
+ * List files from both storage and database, merged
248
+ */
249
+ listFromBoth(options?: ListOptions): Promise<PaginatedResponse<ManagedFile>>;
250
+ /**
251
+ * Get file metadata
252
+ */
253
+ getMetadata(key: string, source?: DataSource): Promise<ManagedFile | null>;
254
+ /**
255
+ * Check if file exists
256
+ */
257
+ exists(key: string, source?: DataSource): Promise<boolean>;
258
+ /**
259
+ * Get download URL for a file
260
+ */
261
+ getDownloadUrl(key: string, options?: DownloadUrlOptions): Promise<string>;
262
+ /**
263
+ * Delete a single file
264
+ */
265
+ delete(key: string): Promise<OperationResult>;
266
+ /**
267
+ * Delete multiple files in batch
268
+ */
269
+ deleteBatch(keys: string[]): Promise<BatchOperationResult>;
270
+ /**
271
+ * Move a file to a new location
272
+ */
273
+ move(options: MoveOptions): Promise<OperationResult>;
274
+ /**
275
+ * Copy a file to a new location
276
+ */
277
+ copy(options: CopyOptions): Promise<OperationResult>;
278
+ /**
279
+ * Rename a file (alias for move)
280
+ */
281
+ rename(oldKey: string, newKey: string): Promise<OperationResult>;
282
+ /**
283
+ * Sync storage files to database
284
+ */
285
+ syncToDatabase(options?: ListOptions): Promise<void>;
286
+ /**
287
+ * Create a file record in database (useful after upload)
288
+ */
289
+ createDatabaseRecord(file: Omit<DatabaseFile, 'id' | 'uploadedAt' | 'updatedAt'>): Promise<DatabaseFile | null>;
290
+ /**
291
+ * Update file metadata in database
292
+ */
293
+ updateDatabaseRecord(key: string, data: Partial<DatabaseFile>): Promise<DatabaseFile | null>;
294
+ }
295
+ //#endregion
296
+ //#region src/router.d.ts
297
+ /**
298
+ * Router configuration
299
+ */
300
+ interface RouterConfig {
301
+ fileManager: FileManager;
302
+ authorize?: AuthorizationCallback;
303
+ getAuthContext?: (request: Request) => Promise<AuthContext> | AuthContext;
304
+ }
305
+ /**
306
+ * FileManager Router - Handles HTTP requests for file operations
307
+ */
308
+ declare class FileManagerRouter {
309
+ private fileManager;
310
+ private authorize?;
311
+ private getAuthContext?;
312
+ constructor(config: RouterConfig);
313
+ /**
314
+ * Handle incoming request
315
+ */
316
+ handle(request: Request): Promise<Response>;
317
+ /**
318
+ * Route action to appropriate handler
319
+ */
320
+ private routeAction;
321
+ /**
322
+ * Success response
323
+ */
324
+ private successResponse;
325
+ /**
326
+ * Error response
327
+ */
328
+ private errorResponse;
329
+ /**
330
+ * Handle errors
331
+ */
332
+ private handleError;
333
+ }
334
+ /**
335
+ * Create a new FileManager router instance
336
+ */
337
+ declare function createFileManagerRouter(config: RouterConfig): FileManagerRouter;
338
+ //#endregion
339
+ export { PaginatedResponse as C, StorageFile as D, StorageConfig as E, StorageProvider as O, OperationResult as S, RequestAction as T, FileOperationResponse as _, AuthorizationCallback as a, MoveOptions as b, DataSource as c, DatabaseProvider as d, DownloadUrlOptions as f, FileOperationRequest as g, FileMetadata as h, AuthContext as i, DatabaseConfig as l, FileManagerError as m, createFileManagerRouter as n, BatchOperationResult as o, FileManagerConfig as p, FileManager as r, CopyOptions as s, FileManagerRouter as t, DatabaseFile as u, ListOptions as v, PaginationCursor as w, OperationHooks as x, ManagedFile as y };