@settlemint/sdk-minio 2.1.4 → 2.1.5-pr3ad019ac

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/dist/minio.d.cts CHANGED
@@ -1,19 +1,19 @@
1
- import { Client } from 'minio';
1
+ import { Client, ItemBucketMetadata } from 'minio';
2
+ export { Client, ItemBucketMetadata } from 'minio';
2
3
  import { z } from 'zod';
4
+ import { Buffer } from 'node:buffer';
3
5
 
4
6
  /**
5
- * Schema for validating server client options for the Portal client.
6
- * Extends the ClientOptionsSchema with additional server-specific fields.
7
+ * Schema for validating server client options for the MinIO client.
7
8
  */
8
- declare const ServerClientOptionsSchema: z.ZodObject<z.objectUtil.extendShape<{
9
+ declare const ServerClientOptionsSchema: z.ZodObject<{
9
10
  /** The URL of the MinIO instance to connect to */
10
11
  instance: z.ZodString;
11
- }, {
12
12
  /** The MinIO access key used to authenticate with the MinIO server */
13
13
  accessKey: z.ZodString;
14
14
  /** The MinIO secret key used to authenticate with the MinIO server */
15
15
  secretKey: z.ZodString;
16
- }>, "strip", z.ZodTypeAny, {
16
+ }, "strip", z.ZodTypeAny, {
17
17
  instance: string;
18
18
  accessKey: string;
19
19
  secretKey: string;
@@ -27,6 +27,328 @@ declare const ServerClientOptionsSchema: z.ZodObject<z.objectUtil.extendShape<{
27
27
  */
28
28
  type ServerClientOptions = z.infer<typeof ServerClientOptionsSchema>;
29
29
 
30
+ /**
31
+ * Base interface for all MinIO operations
32
+ *
33
+ * @template T The return type of the operation
34
+ */
35
+ interface MinioOperation<T> {
36
+ execute: (client: Client) => Promise<T>;
37
+ }
38
+ /**
39
+ * Creates an operation to list objects in a bucket
40
+ *
41
+ * @param bucket - The bucket name to list objects from
42
+ * @param prefix - Optional prefix to filter objects (like a folder path)
43
+ * @returns A MinioOperation that lists objects when executed
44
+ * @throws Will throw an error if the operation fails
45
+ *
46
+ * @example
47
+ * import { createListObjectsOperation, executeMinioOperation } from "@settlemint/sdk-minio";
48
+ *
49
+ * const listOperation = createListObjectsOperation("my-bucket", "folder/");
50
+ * const objects = await executeMinioOperation(client, listOperation);
51
+ */
52
+ declare function createListObjectsOperation(bucket: string, prefix?: string): MinioOperation<Array<{
53
+ name: string;
54
+ prefix?: string;
55
+ size: number;
56
+ etag: string;
57
+ lastModified: Date;
58
+ }>>;
59
+ /**
60
+ * Creates an operation to get an object's metadata
61
+ *
62
+ * @param bucket - The bucket name containing the object
63
+ * @param objectName - The object name/path
64
+ * @returns A MinioOperation that gets object stats when executed
65
+ * @throws Will throw an error if the operation fails
66
+ *
67
+ * @example
68
+ * import { createStatObjectOperation, executeMinioOperation } from "@settlemint/sdk-minio";
69
+ *
70
+ * const statOperation = createStatObjectOperation("my-bucket", "folder/file.txt");
71
+ * const stats = await executeMinioOperation(client, statOperation);
72
+ */
73
+ declare function createStatObjectOperation(bucket: string, objectName: string): MinioOperation<{
74
+ size: number;
75
+ etag: string;
76
+ metaData: Record<string, string>;
77
+ lastModified: Date;
78
+ }>;
79
+ /**
80
+ * Creates an operation to upload a buffer to MinIO
81
+ *
82
+ * @param bucket - The bucket name to upload to
83
+ * @param objectName - The object name/path to create
84
+ * @param buffer - The buffer containing the file data
85
+ * @param metadata - Optional metadata to attach to the object
86
+ * @returns A MinioOperation that uploads the buffer when executed
87
+ * @throws Will throw an error if the operation fails
88
+ *
89
+ * @example
90
+ * import { createUploadOperation, executeMinioOperation } from "@settlemint/sdk-minio";
91
+ *
92
+ * const buffer = Buffer.from("file content");
93
+ * const uploadOperation = createUploadOperation("my-bucket", "folder/file.txt", buffer, { "content-type": "text/plain" });
94
+ * const result = await executeMinioOperation(client, uploadOperation);
95
+ */
96
+ declare function createUploadOperation(bucket: string, objectName: string, buffer: Buffer, metadata?: ItemBucketMetadata): MinioOperation<{
97
+ etag: string;
98
+ }>;
99
+ /**
100
+ * Creates an operation to delete an object from MinIO
101
+ *
102
+ * @param bucket - The bucket name containing the object
103
+ * @param objectName - The object name/path to delete
104
+ * @returns A MinioOperation that deletes the object when executed
105
+ * @throws Will throw an error if the operation fails
106
+ *
107
+ * @example
108
+ * import { createDeleteOperation, executeMinioOperation } from "@settlemint/sdk-minio";
109
+ *
110
+ * const deleteOperation = createDeleteOperation("my-bucket", "folder/file.txt");
111
+ * await executeMinioOperation(client, deleteOperation);
112
+ */
113
+ declare function createDeleteOperation(bucket: string, objectName: string): MinioOperation<void>;
114
+ /**
115
+ * Creates an operation to generate a presigned URL for an object
116
+ *
117
+ * @param bucket - The bucket name containing the object
118
+ * @param objectName - The object name/path
119
+ * @param expirySeconds - How long the URL should be valid for in seconds
120
+ * @returns A MinioOperation that creates a presigned URL when executed
121
+ * @throws Will throw an error if the operation fails
122
+ *
123
+ * @example
124
+ * import { createPresignedUrlOperation, executeMinioOperation } from "@settlemint/sdk-minio";
125
+ *
126
+ * const urlOperation = createPresignedUrlOperation("my-bucket", "folder/file.txt", 3600);
127
+ * const url = await executeMinioOperation(client, urlOperation);
128
+ */
129
+ declare function createPresignedUrlOperation(bucket: string, objectName: string, expirySeconds: number): MinioOperation<string>;
130
+ /**
131
+ * Creates an operation to generate a presigned PUT URL for direct uploads
132
+ *
133
+ * @param bucket - The bucket name to upload to
134
+ * @param objectName - The object name/path to create
135
+ * @param expirySeconds - How long the URL should be valid for in seconds
136
+ * @returns A MinioOperation that creates a presigned PUT URL when executed
137
+ * @throws Will throw an error if the operation fails
138
+ *
139
+ * @example
140
+ * import { createPresignedPutOperation, executeMinioOperation } from "@settlemint/sdk-minio";
141
+ *
142
+ * const putUrlOperation = createPresignedPutOperation("my-bucket", "folder/file.txt", 3600);
143
+ * const url = await executeMinioOperation(client, putUrlOperation);
144
+ */
145
+ declare function createPresignedPutOperation(bucket: string, objectName: string, expirySeconds: number): MinioOperation<string>;
146
+ /**
147
+ * Creates a simplified upload function bound to a specific client
148
+ *
149
+ * @param client - The MinIO client to use for uploads
150
+ * @returns A function that uploads buffers to MinIO
151
+ * @throws Will throw an error if the operation fails
152
+ *
153
+ * @example
154
+ * import { createSimpleUploadOperation, getMinioClient } from "@settlemint/sdk-minio";
155
+ *
156
+ * const client = await getMinioClient();
157
+ * const uploadFn = createSimpleUploadOperation(client);
158
+ * const result = await uploadFn(buffer, "my-bucket", "folder/file.txt", { "content-type": "text/plain" });
159
+ */
160
+ declare function createSimpleUploadOperation(client: Client): (buffer: Buffer, bucket: string, objectName: string, metadata?: ItemBucketMetadata) => Promise<{
161
+ etag: string;
162
+ }>;
163
+
164
+ /**
165
+ * Helper type to extract the inferred type from a Zod schema.
166
+ *
167
+ * @template T - The Zod schema type
168
+ */
169
+ type Static<T extends z.ZodType> = z.infer<T>;
170
+ /**
171
+ * Schema for file metadata stored in MinIO.
172
+ * Defines the structure and validation rules for file information.
173
+ */
174
+ declare const FileMetadataSchema: z.ZodObject<{
175
+ id: z.ZodString;
176
+ name: z.ZodString;
177
+ contentType: z.ZodString;
178
+ size: z.ZodNumber;
179
+ uploadedAt: z.ZodString;
180
+ etag: z.ZodString;
181
+ url: z.ZodOptional<z.ZodString>;
182
+ }, "strip", z.ZodTypeAny, {
183
+ id: string;
184
+ name: string;
185
+ contentType: string;
186
+ size: number;
187
+ uploadedAt: string;
188
+ etag: string;
189
+ url?: string | undefined;
190
+ }, {
191
+ id: string;
192
+ name: string;
193
+ contentType: string;
194
+ size: number;
195
+ uploadedAt: string;
196
+ etag: string;
197
+ url?: string | undefined;
198
+ }>;
199
+ /**
200
+ * Type representing file metadata after validation.
201
+ */
202
+ type FileMetadata = Static<typeof FileMetadataSchema>;
203
+ /**
204
+ * Default bucket name to use for file storage when none is specified.
205
+ */
206
+ declare const DEFAULT_BUCKET = "uploads";
207
+
208
+ /**
209
+ * Gets a list of files with optional prefix filter
210
+ *
211
+ * @param client - The MinIO client to use
212
+ * @param prefix - Optional prefix to filter files (like a folder path)
213
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
214
+ * @returns Array of file metadata objects
215
+ * @throws Will throw an error if the operation fails or client initialization fails
216
+ *
217
+ * @example
218
+ * import { createServerMinioClient, getFilesList } from "@settlemint/sdk-minio";
219
+ *
220
+ * const { client } = createServerMinioClient({
221
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
222
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
223
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
224
+ * });
225
+ *
226
+ * const files = await getFilesList(client, "documents/");
227
+ */
228
+ declare function getFilesList(client: Client, prefix?: string, bucket?: string): Promise<FileMetadata[]>;
229
+ /**
230
+ * Gets a single file by its object name
231
+ *
232
+ * @param client - The MinIO client to use
233
+ * @param objectName - The object name/path
234
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
235
+ * @returns File metadata with presigned URL
236
+ * @throws Will throw an error if the file doesn't exist or client initialization fails
237
+ *
238
+ * @example
239
+ * import { createServerMinioClient, getFileByObjectName } from "@settlemint/sdk-minio";
240
+ *
241
+ * const { client } = createServerMinioClient({
242
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
243
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
244
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
245
+ * });
246
+ *
247
+ * const file = await getFileByObjectName(client, "documents/report.pdf");
248
+ */
249
+ declare function getFileByObjectName(client: Client, objectName: string, bucket?: string): Promise<FileMetadata>;
250
+ /**
251
+ * Deletes a file from storage
252
+ *
253
+ * @param client - The MinIO client to use
254
+ * @param fileId - The file identifier/path to delete
255
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
256
+ * @returns Success status
257
+ * @throws Will throw an error if deletion fails or client initialization fails
258
+ *
259
+ * @example
260
+ * import { createServerMinioClient, deleteFile } from "@settlemint/sdk-minio";
261
+ *
262
+ * const { client } = createServerMinioClient({
263
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
264
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
265
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
266
+ * });
267
+ *
268
+ * await deleteFile(client, "documents/report.pdf");
269
+ */
270
+ declare function deleteFile(client: Client, fileId: string, bucket?: string): Promise<boolean>;
271
+ /**
272
+ * Creates a presigned upload URL for direct browser uploads
273
+ *
274
+ * @param client - The MinIO client to use
275
+ * @param fileName - The file name to use
276
+ * @param path - Optional path/folder
277
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
278
+ * @param expirySeconds - How long the URL should be valid for
279
+ * @returns Presigned URL for PUT operation
280
+ * @throws Will throw an error if URL creation fails or client initialization fails
281
+ *
282
+ * @example
283
+ * import { createServerMinioClient, createPresignedUploadUrl } from "@settlemint/sdk-minio";
284
+ *
285
+ * const { client } = createServerMinioClient({
286
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
287
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
288
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
289
+ * });
290
+ *
291
+ * // Generate the presigned URL on the server
292
+ * const url = await createPresignedUploadUrl(client, "report.pdf", "documents/");
293
+ *
294
+ * // Send the URL to the client/browser via HTTP response
295
+ * return Response.json({ uploadUrl: url });
296
+ *
297
+ * // Then in the browser:
298
+ * const response = await fetch('/api/get-upload-url');
299
+ * const { uploadUrl } = await response.json();
300
+ * await fetch(uploadUrl, {
301
+ * method: 'PUT',
302
+ * headers: { 'Content-Type': 'application/pdf' },
303
+ * body: pdfFile
304
+ * });
305
+ */
306
+ declare function createPresignedUploadUrl(client: Client, fileName: string, path?: string, bucket?: string, expirySeconds?: number): Promise<string>;
307
+ /**
308
+ * Uploads a buffer directly to storage
309
+ *
310
+ * @param client - The MinIO client to use
311
+ * @param buffer - The buffer to upload
312
+ * @param objectName - The full object name/path
313
+ * @param contentType - The content type of the file
314
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
315
+ * @returns The uploaded file metadata
316
+ * @throws Will throw an error if upload fails or client initialization fails
317
+ *
318
+ * @example
319
+ * import { createServerMinioClient, uploadBuffer } from "@settlemint/sdk-minio";
320
+ *
321
+ * const { client } = createServerMinioClient({
322
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
323
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
324
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
325
+ * });
326
+ *
327
+ * const buffer = Buffer.from("Hello, world!");
328
+ * const uploadedFile = await uploadFile(client, buffer, "documents/hello.txt", "text/plain");
329
+ */
330
+ declare function uploadFile(client: Client, buffer: Buffer, objectName: string, contentType: string, bucket?: string): Promise<FileMetadata>;
331
+
332
+ /**
333
+ * Executes a MinIO operation using the provided client
334
+ *
335
+ * @param client - MinIO client to use
336
+ * @param operation - The operation to execute
337
+ * @returns The result of the operation execution
338
+ * @throws Will throw an error if the operation fails
339
+ *
340
+ * @example
341
+ * import { createServerMinioClient, createListObjectsOperation, executeMinioOperation } from "@settlemint/sdk-minio";
342
+ * const { client } = createServerMinioClient({
343
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
344
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
345
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
346
+ * });
347
+ * const listOperation = createListObjectsOperation("my-bucket", "prefix/");
348
+ * const result = await executeMinioOperation(client, listOperation);
349
+ */
350
+ declare function executeMinioOperation<T>(client: Client, operation: MinioOperation<T>): Promise<T>;
351
+
30
352
  /**
31
353
  * Creates a MinIO client for server-side use with authentication.
32
354
  *
@@ -48,4 +370,4 @@ declare function createServerMinioClient(options: ServerClientOptions): {
48
370
  client: Client;
49
371
  };
50
372
 
51
- export { createServerMinioClient };
373
+ export { DEFAULT_BUCKET, type FileMetadata, type MinioOperation, createDeleteOperation, createListObjectsOperation, createPresignedPutOperation, createPresignedUploadUrl, createPresignedUrlOperation, createServerMinioClient, createSimpleUploadOperation, createStatObjectOperation, createUploadOperation, deleteFile, executeMinioOperation, getFileByObjectName, getFilesList, uploadFile };