@settlemint/sdk-minio 2.1.5 → 2.2.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 +269 -2
- package/dist/minio.cjs +248 -10
- package/dist/minio.cjs.map +1 -1
- package/dist/minio.d.cts +166 -4
- package/dist/minio.d.ts +166 -4
- package/dist/minio.mjs +241 -9
- package/dist/minio.mjs.map +1 -1
- package/package.json +2 -2
package/dist/minio.d.cts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Client } 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
|
|
6
|
-
* Extends the ClientOptionsSchema with additional server-specific fields.
|
|
7
|
+
* Schema for validating server client options for the MinIO client.
|
|
7
8
|
*/
|
|
8
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 */
|
|
@@ -27,6 +27,168 @@ declare const ServerClientOptionsSchema: z.ZodObject<{
|
|
|
27
27
|
*/
|
|
28
28
|
type ServerClientOptions = z.infer<typeof ServerClientOptionsSchema>;
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Type representing file metadata after validation.
|
|
32
|
+
*/
|
|
33
|
+
interface FileMetadata {
|
|
34
|
+
/**
|
|
35
|
+
* The unique identifier for the file.
|
|
36
|
+
*/
|
|
37
|
+
id: string;
|
|
38
|
+
/**
|
|
39
|
+
* The name of the file.
|
|
40
|
+
*/
|
|
41
|
+
name: string;
|
|
42
|
+
/**
|
|
43
|
+
* The content type of the file.
|
|
44
|
+
*/
|
|
45
|
+
contentType: string;
|
|
46
|
+
/**
|
|
47
|
+
* The size of the file in bytes.
|
|
48
|
+
*/
|
|
49
|
+
size: number;
|
|
50
|
+
/**
|
|
51
|
+
* The date and time the file was uploaded.
|
|
52
|
+
*/
|
|
53
|
+
uploadedAt: string;
|
|
54
|
+
/**
|
|
55
|
+
* The ETag of the file.
|
|
56
|
+
*/
|
|
57
|
+
etag: string;
|
|
58
|
+
/**
|
|
59
|
+
* The URL of the file.
|
|
60
|
+
*/
|
|
61
|
+
url?: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Default bucket name to use for file storage when none is specified.
|
|
65
|
+
*/
|
|
66
|
+
declare const DEFAULT_BUCKET = "uploads";
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Gets a list of files with optional prefix filter
|
|
70
|
+
*
|
|
71
|
+
* @param client - The MinIO client to use
|
|
72
|
+
* @param prefix - Optional prefix to filter files (like a folder path)
|
|
73
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
74
|
+
* @returns Array of file metadata objects
|
|
75
|
+
* @throws Will throw an error if the operation fails or client initialization fails
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* import { createServerMinioClient, getFilesList } from "@settlemint/sdk-minio";
|
|
79
|
+
*
|
|
80
|
+
* const { client } = createServerMinioClient({
|
|
81
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
82
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
83
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* const files = await getFilesList(client, "documents/");
|
|
87
|
+
*/
|
|
88
|
+
declare function getFilesList(client: Client, prefix?: string, bucket?: string): Promise<FileMetadata[]>;
|
|
89
|
+
/**
|
|
90
|
+
* Gets a single file by its object name
|
|
91
|
+
*
|
|
92
|
+
* @param client - The MinIO client to use
|
|
93
|
+
* @param fileId - The file identifier/path
|
|
94
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
95
|
+
* @returns File metadata with presigned URL
|
|
96
|
+
* @throws Will throw an error if the file doesn't exist or client initialization fails
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* import { createServerMinioClient, getFileByObjectName } from "@settlemint/sdk-minio";
|
|
100
|
+
*
|
|
101
|
+
* const { client } = createServerMinioClient({
|
|
102
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
103
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
104
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* const file = await getFileByObjectName(client, "documents/report.pdf");
|
|
108
|
+
*/
|
|
109
|
+
declare function getFileById(client: Client, fileId: string, bucket?: string): Promise<FileMetadata>;
|
|
110
|
+
/**
|
|
111
|
+
* Deletes a file from storage
|
|
112
|
+
*
|
|
113
|
+
* @param client - The MinIO client to use
|
|
114
|
+
* @param fileId - The file identifier/path
|
|
115
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
116
|
+
* @returns Success status
|
|
117
|
+
* @throws Will throw an error if deletion fails or client initialization fails
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* import { createServerMinioClient, deleteFile } from "@settlemint/sdk-minio";
|
|
121
|
+
*
|
|
122
|
+
* const { client } = createServerMinioClient({
|
|
123
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
124
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
125
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* await deleteFile(client, "documents/report.pdf");
|
|
129
|
+
*/
|
|
130
|
+
declare function deleteFile(client: Client, fileId: string, bucket?: string): Promise<boolean>;
|
|
131
|
+
/**
|
|
132
|
+
* Creates a presigned upload URL for direct browser uploads
|
|
133
|
+
*
|
|
134
|
+
* @param client - The MinIO client to use
|
|
135
|
+
* @param fileName - The file name to use
|
|
136
|
+
* @param path - Optional path/folder
|
|
137
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
138
|
+
* @param expirySeconds - How long the URL should be valid for
|
|
139
|
+
* @returns Presigned URL for PUT operation
|
|
140
|
+
* @throws Will throw an error if URL creation fails or client initialization fails
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* import { createServerMinioClient, createPresignedUploadUrl } from "@settlemint/sdk-minio";
|
|
144
|
+
*
|
|
145
|
+
* const { client } = createServerMinioClient({
|
|
146
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
147
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
148
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
149
|
+
* });
|
|
150
|
+
*
|
|
151
|
+
* // Generate the presigned URL on the server
|
|
152
|
+
* const url = await createPresignedUploadUrl(client, "report.pdf", "documents/");
|
|
153
|
+
*
|
|
154
|
+
* // Send the URL to the client/browser via HTTP response
|
|
155
|
+
* return Response.json({ uploadUrl: url });
|
|
156
|
+
*
|
|
157
|
+
* // Then in the browser:
|
|
158
|
+
* const response = await fetch('/api/get-upload-url');
|
|
159
|
+
* const { uploadUrl } = await response.json();
|
|
160
|
+
* await fetch(uploadUrl, {
|
|
161
|
+
* method: 'PUT',
|
|
162
|
+
* headers: { 'Content-Type': 'application/pdf' },
|
|
163
|
+
* body: pdfFile
|
|
164
|
+
* });
|
|
165
|
+
*/
|
|
166
|
+
declare function createPresignedUploadUrl(client: Client, fileName: string, path?: string, bucket?: string, expirySeconds?: number): Promise<string>;
|
|
167
|
+
/**
|
|
168
|
+
* Uploads a buffer directly to storage
|
|
169
|
+
*
|
|
170
|
+
* @param client - The MinIO client to use
|
|
171
|
+
* @param buffer - The buffer to upload
|
|
172
|
+
* @param objectName - The full object name/path
|
|
173
|
+
* @param contentType - The content type of the file
|
|
174
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
175
|
+
* @returns The uploaded file metadata
|
|
176
|
+
* @throws Will throw an error if upload fails or client initialization fails
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* import { createServerMinioClient, uploadBuffer } from "@settlemint/sdk-minio";
|
|
180
|
+
*
|
|
181
|
+
* const { client } = createServerMinioClient({
|
|
182
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
183
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
184
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
185
|
+
* });
|
|
186
|
+
*
|
|
187
|
+
* const buffer = Buffer.from("Hello, world!");
|
|
188
|
+
* const uploadedFile = await uploadFile(client, buffer, "documents/hello.txt", "text/plain");
|
|
189
|
+
*/
|
|
190
|
+
declare function uploadFile(client: Client, buffer: Buffer, objectName: string, contentType: string, bucket?: string): Promise<FileMetadata>;
|
|
191
|
+
|
|
30
192
|
/**
|
|
31
193
|
* Creates a MinIO client for server-side use with authentication.
|
|
32
194
|
*
|
|
@@ -48,4 +210,4 @@ declare function createServerMinioClient(options: ServerClientOptions): {
|
|
|
48
210
|
client: Client;
|
|
49
211
|
};
|
|
50
212
|
|
|
51
|
-
export { createServerMinioClient };
|
|
213
|
+
export { DEFAULT_BUCKET, type FileMetadata, createPresignedUploadUrl, createServerMinioClient, deleteFile, getFileById, getFilesList, uploadFile };
|
package/dist/minio.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Client } 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
|
|
6
|
-
* Extends the ClientOptionsSchema with additional server-specific fields.
|
|
7
|
+
* Schema for validating server client options for the MinIO client.
|
|
7
8
|
*/
|
|
8
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 */
|
|
@@ -27,6 +27,168 @@ declare const ServerClientOptionsSchema: z.ZodObject<{
|
|
|
27
27
|
*/
|
|
28
28
|
type ServerClientOptions = z.infer<typeof ServerClientOptionsSchema>;
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Type representing file metadata after validation.
|
|
32
|
+
*/
|
|
33
|
+
interface FileMetadata {
|
|
34
|
+
/**
|
|
35
|
+
* The unique identifier for the file.
|
|
36
|
+
*/
|
|
37
|
+
id: string;
|
|
38
|
+
/**
|
|
39
|
+
* The name of the file.
|
|
40
|
+
*/
|
|
41
|
+
name: string;
|
|
42
|
+
/**
|
|
43
|
+
* The content type of the file.
|
|
44
|
+
*/
|
|
45
|
+
contentType: string;
|
|
46
|
+
/**
|
|
47
|
+
* The size of the file in bytes.
|
|
48
|
+
*/
|
|
49
|
+
size: number;
|
|
50
|
+
/**
|
|
51
|
+
* The date and time the file was uploaded.
|
|
52
|
+
*/
|
|
53
|
+
uploadedAt: string;
|
|
54
|
+
/**
|
|
55
|
+
* The ETag of the file.
|
|
56
|
+
*/
|
|
57
|
+
etag: string;
|
|
58
|
+
/**
|
|
59
|
+
* The URL of the file.
|
|
60
|
+
*/
|
|
61
|
+
url?: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Default bucket name to use for file storage when none is specified.
|
|
65
|
+
*/
|
|
66
|
+
declare const DEFAULT_BUCKET = "uploads";
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Gets a list of files with optional prefix filter
|
|
70
|
+
*
|
|
71
|
+
* @param client - The MinIO client to use
|
|
72
|
+
* @param prefix - Optional prefix to filter files (like a folder path)
|
|
73
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
74
|
+
* @returns Array of file metadata objects
|
|
75
|
+
* @throws Will throw an error if the operation fails or client initialization fails
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* import { createServerMinioClient, getFilesList } from "@settlemint/sdk-minio";
|
|
79
|
+
*
|
|
80
|
+
* const { client } = createServerMinioClient({
|
|
81
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
82
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
83
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* const files = await getFilesList(client, "documents/");
|
|
87
|
+
*/
|
|
88
|
+
declare function getFilesList(client: Client, prefix?: string, bucket?: string): Promise<FileMetadata[]>;
|
|
89
|
+
/**
|
|
90
|
+
* Gets a single file by its object name
|
|
91
|
+
*
|
|
92
|
+
* @param client - The MinIO client to use
|
|
93
|
+
* @param fileId - The file identifier/path
|
|
94
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
95
|
+
* @returns File metadata with presigned URL
|
|
96
|
+
* @throws Will throw an error if the file doesn't exist or client initialization fails
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* import { createServerMinioClient, getFileByObjectName } from "@settlemint/sdk-minio";
|
|
100
|
+
*
|
|
101
|
+
* const { client } = createServerMinioClient({
|
|
102
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
103
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
104
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* const file = await getFileByObjectName(client, "documents/report.pdf");
|
|
108
|
+
*/
|
|
109
|
+
declare function getFileById(client: Client, fileId: string, bucket?: string): Promise<FileMetadata>;
|
|
110
|
+
/**
|
|
111
|
+
* Deletes a file from storage
|
|
112
|
+
*
|
|
113
|
+
* @param client - The MinIO client to use
|
|
114
|
+
* @param fileId - The file identifier/path
|
|
115
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
116
|
+
* @returns Success status
|
|
117
|
+
* @throws Will throw an error if deletion fails or client initialization fails
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* import { createServerMinioClient, deleteFile } from "@settlemint/sdk-minio";
|
|
121
|
+
*
|
|
122
|
+
* const { client } = createServerMinioClient({
|
|
123
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
124
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
125
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* await deleteFile(client, "documents/report.pdf");
|
|
129
|
+
*/
|
|
130
|
+
declare function deleteFile(client: Client, fileId: string, bucket?: string): Promise<boolean>;
|
|
131
|
+
/**
|
|
132
|
+
* Creates a presigned upload URL for direct browser uploads
|
|
133
|
+
*
|
|
134
|
+
* @param client - The MinIO client to use
|
|
135
|
+
* @param fileName - The file name to use
|
|
136
|
+
* @param path - Optional path/folder
|
|
137
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
138
|
+
* @param expirySeconds - How long the URL should be valid for
|
|
139
|
+
* @returns Presigned URL for PUT operation
|
|
140
|
+
* @throws Will throw an error if URL creation fails or client initialization fails
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* import { createServerMinioClient, createPresignedUploadUrl } from "@settlemint/sdk-minio";
|
|
144
|
+
*
|
|
145
|
+
* const { client } = createServerMinioClient({
|
|
146
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
147
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
148
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
149
|
+
* });
|
|
150
|
+
*
|
|
151
|
+
* // Generate the presigned URL on the server
|
|
152
|
+
* const url = await createPresignedUploadUrl(client, "report.pdf", "documents/");
|
|
153
|
+
*
|
|
154
|
+
* // Send the URL to the client/browser via HTTP response
|
|
155
|
+
* return Response.json({ uploadUrl: url });
|
|
156
|
+
*
|
|
157
|
+
* // Then in the browser:
|
|
158
|
+
* const response = await fetch('/api/get-upload-url');
|
|
159
|
+
* const { uploadUrl } = await response.json();
|
|
160
|
+
* await fetch(uploadUrl, {
|
|
161
|
+
* method: 'PUT',
|
|
162
|
+
* headers: { 'Content-Type': 'application/pdf' },
|
|
163
|
+
* body: pdfFile
|
|
164
|
+
* });
|
|
165
|
+
*/
|
|
166
|
+
declare function createPresignedUploadUrl(client: Client, fileName: string, path?: string, bucket?: string, expirySeconds?: number): Promise<string>;
|
|
167
|
+
/**
|
|
168
|
+
* Uploads a buffer directly to storage
|
|
169
|
+
*
|
|
170
|
+
* @param client - The MinIO client to use
|
|
171
|
+
* @param buffer - The buffer to upload
|
|
172
|
+
* @param objectName - The full object name/path
|
|
173
|
+
* @param contentType - The content type of the file
|
|
174
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
175
|
+
* @returns The uploaded file metadata
|
|
176
|
+
* @throws Will throw an error if upload fails or client initialization fails
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* import { createServerMinioClient, uploadBuffer } from "@settlemint/sdk-minio";
|
|
180
|
+
*
|
|
181
|
+
* const { client } = createServerMinioClient({
|
|
182
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
183
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
184
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
185
|
+
* });
|
|
186
|
+
*
|
|
187
|
+
* const buffer = Buffer.from("Hello, world!");
|
|
188
|
+
* const uploadedFile = await uploadFile(client, buffer, "documents/hello.txt", "text/plain");
|
|
189
|
+
*/
|
|
190
|
+
declare function uploadFile(client: Client, buffer: Buffer, objectName: string, contentType: string, bucket?: string): Promise<FileMetadata>;
|
|
191
|
+
|
|
30
192
|
/**
|
|
31
193
|
* Creates a MinIO client for server-side use with authentication.
|
|
32
194
|
*
|
|
@@ -48,4 +210,4 @@ declare function createServerMinioClient(options: ServerClientOptions): {
|
|
|
48
210
|
client: Client;
|
|
49
211
|
};
|
|
50
212
|
|
|
51
|
-
export { createServerMinioClient };
|
|
213
|
+
export { DEFAULT_BUCKET, type FileMetadata, createPresignedUploadUrl, createServerMinioClient, deleteFile, getFileById, getFilesList, uploadFile };
|
package/dist/minio.mjs
CHANGED
|
@@ -1,26 +1,252 @@
|
|
|
1
1
|
// src/minio.ts
|
|
2
|
-
import { ensureServer } from "@settlemint/sdk-utils/runtime";
|
|
3
|
-
import { validate } from "@settlemint/sdk-utils/validation";
|
|
2
|
+
import { ensureServer as ensureServer3 } from "@settlemint/sdk-utils/runtime";
|
|
3
|
+
import { validate as validate2 } from "@settlemint/sdk-utils/validation";
|
|
4
4
|
import { Client } from "minio";
|
|
5
5
|
|
|
6
6
|
// src/helpers/client-options.schema.ts
|
|
7
7
|
import { UrlSchema } from "@settlemint/sdk-utils/validation";
|
|
8
8
|
import { z } from "zod";
|
|
9
|
-
var
|
|
9
|
+
var ServerClientOptionsSchema = z.object({
|
|
10
10
|
/** The URL of the MinIO instance to connect to */
|
|
11
|
-
instance: UrlSchema
|
|
12
|
-
});
|
|
13
|
-
var ServerClientOptionsSchema = ClientOptionsSchema.extend({
|
|
11
|
+
instance: UrlSchema,
|
|
14
12
|
/** The MinIO access key used to authenticate with the MinIO server */
|
|
15
13
|
accessKey: z.string(),
|
|
16
14
|
/** The MinIO secret key used to authenticate with the MinIO server */
|
|
17
15
|
secretKey: z.string()
|
|
18
16
|
});
|
|
19
17
|
|
|
18
|
+
// src/helpers/schema.ts
|
|
19
|
+
import { z as z2 } from "zod";
|
|
20
|
+
var FileMetadataSchema = z2.object({
|
|
21
|
+
id: z2.string(),
|
|
22
|
+
name: z2.string(),
|
|
23
|
+
contentType: z2.string(),
|
|
24
|
+
size: z2.number(),
|
|
25
|
+
uploadedAt: z2.string().datetime(),
|
|
26
|
+
etag: z2.string(),
|
|
27
|
+
url: z2.string().url().optional()
|
|
28
|
+
});
|
|
29
|
+
var DEFAULT_BUCKET = "uploads";
|
|
30
|
+
|
|
31
|
+
// src/helpers/functions.ts
|
|
32
|
+
import { ensureServer as ensureServer2 } from "@settlemint/sdk-utils/runtime";
|
|
33
|
+
import { validate } from "@settlemint/sdk-utils/validation";
|
|
34
|
+
|
|
35
|
+
// src/helpers/executor.ts
|
|
36
|
+
import { ensureServer } from "@settlemint/sdk-utils/runtime";
|
|
37
|
+
async function executeMinioOperation(client, operation) {
|
|
38
|
+
ensureServer();
|
|
39
|
+
return operation.execute(client);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/helpers/operations.ts
|
|
43
|
+
function createListObjectsOperation(bucket, prefix = "") {
|
|
44
|
+
return {
|
|
45
|
+
execute: async (client) => {
|
|
46
|
+
const objectsStream = client.listObjects(bucket, prefix, true);
|
|
47
|
+
const objects = [];
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
objectsStream.on("data", (obj) => {
|
|
50
|
+
if (obj.name && typeof obj.size === "number" && obj.etag && obj.lastModified) {
|
|
51
|
+
objects.push({
|
|
52
|
+
name: obj.name,
|
|
53
|
+
prefix: obj.prefix,
|
|
54
|
+
size: obj.size,
|
|
55
|
+
etag: obj.etag,
|
|
56
|
+
lastModified: obj.lastModified
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
objectsStream.on("error", (err) => {
|
|
61
|
+
reject(err);
|
|
62
|
+
});
|
|
63
|
+
objectsStream.on("end", () => {
|
|
64
|
+
resolve(objects);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function createStatObjectOperation(bucket, objectName) {
|
|
71
|
+
return {
|
|
72
|
+
execute: async (client) => {
|
|
73
|
+
return client.statObject(bucket, objectName);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function createDeleteOperation(bucket, objectName) {
|
|
78
|
+
return {
|
|
79
|
+
execute: async (client) => {
|
|
80
|
+
return client.removeObject(bucket, objectName);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function createPresignedUrlOperation(bucket, objectName, expirySeconds) {
|
|
85
|
+
return {
|
|
86
|
+
execute: async (client) => {
|
|
87
|
+
return client.presignedGetObject(bucket, objectName, expirySeconds);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function createPresignedPutOperation(bucket, objectName, expirySeconds) {
|
|
92
|
+
return {
|
|
93
|
+
execute: async (client) => {
|
|
94
|
+
return client.presignedPutObject(bucket, objectName, expirySeconds);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function createSimpleUploadOperation(client) {
|
|
99
|
+
return async (buffer, bucket, objectName, metadata) => {
|
|
100
|
+
return client.putObject(bucket, objectName, buffer, void 0, metadata);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/helpers/functions.ts
|
|
105
|
+
function normalizePath(path, fileName) {
|
|
106
|
+
if (path.length > 1e3) {
|
|
107
|
+
throw new Error("Path is too long");
|
|
108
|
+
}
|
|
109
|
+
const cleanPath = path.replace(/\/+$/, "");
|
|
110
|
+
if (!cleanPath) {
|
|
111
|
+
return fileName;
|
|
112
|
+
}
|
|
113
|
+
return `${cleanPath}/${fileName}`;
|
|
114
|
+
}
|
|
115
|
+
async function getFilesList(client, prefix = "", bucket = DEFAULT_BUCKET) {
|
|
116
|
+
ensureServer2();
|
|
117
|
+
console.log(`Listing files with prefix: "${prefix}" in bucket: "${bucket}"`);
|
|
118
|
+
try {
|
|
119
|
+
const listOperation = createListObjectsOperation(bucket, prefix);
|
|
120
|
+
const objects = await executeMinioOperation(client, listOperation);
|
|
121
|
+
console.log(`Found ${objects.length} files in MinIO`);
|
|
122
|
+
const fileObjects = await Promise.all(
|
|
123
|
+
objects.map(async (obj) => {
|
|
124
|
+
const presignedUrlOperation = createPresignedUrlOperation(
|
|
125
|
+
bucket,
|
|
126
|
+
obj.name,
|
|
127
|
+
3600
|
|
128
|
+
// 1 hour expiry
|
|
129
|
+
);
|
|
130
|
+
const url = await executeMinioOperation(client, presignedUrlOperation);
|
|
131
|
+
return {
|
|
132
|
+
id: obj.name,
|
|
133
|
+
name: obj.name.split("/").pop() || obj.name,
|
|
134
|
+
contentType: "application/octet-stream",
|
|
135
|
+
// Default type
|
|
136
|
+
size: obj.size,
|
|
137
|
+
uploadedAt: obj.lastModified.toISOString(),
|
|
138
|
+
etag: obj.etag,
|
|
139
|
+
url
|
|
140
|
+
};
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
return validate(FileMetadataSchema.array(), fileObjects);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
console.error("Failed to list files:", error);
|
|
146
|
+
throw new Error(`Failed to list files: ${error instanceof Error ? error.message : String(error)}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async function getFileById(client, fileId, bucket = DEFAULT_BUCKET) {
|
|
150
|
+
ensureServer2();
|
|
151
|
+
console.log(`Getting file details for: ${fileId} in bucket: ${bucket}`);
|
|
152
|
+
try {
|
|
153
|
+
const statOperation = createStatObjectOperation(bucket, fileId);
|
|
154
|
+
const statResult = await executeMinioOperation(client, statOperation);
|
|
155
|
+
const presignedUrlOperation = createPresignedUrlOperation(
|
|
156
|
+
bucket,
|
|
157
|
+
fileId,
|
|
158
|
+
3600
|
|
159
|
+
// 1 hour expiry
|
|
160
|
+
);
|
|
161
|
+
const url = await executeMinioOperation(client, presignedUrlOperation);
|
|
162
|
+
let size = 0;
|
|
163
|
+
if (statResult.metaData["content-length"]) {
|
|
164
|
+
const parsedSize = Number.parseInt(statResult.metaData["content-length"], 10);
|
|
165
|
+
if (!Number.isNaN(parsedSize)) {
|
|
166
|
+
size = parsedSize;
|
|
167
|
+
}
|
|
168
|
+
} else if (typeof statResult.size === "number" && !Number.isNaN(statResult.size)) {
|
|
169
|
+
size = statResult.size;
|
|
170
|
+
}
|
|
171
|
+
const fileMetadata = {
|
|
172
|
+
id: fileId,
|
|
173
|
+
name: fileId.split("/").pop() || fileId,
|
|
174
|
+
contentType: statResult.metaData["content-type"] || "application/octet-stream",
|
|
175
|
+
size,
|
|
176
|
+
uploadedAt: statResult.lastModified.toISOString(),
|
|
177
|
+
etag: statResult.etag,
|
|
178
|
+
url
|
|
179
|
+
};
|
|
180
|
+
return validate(FileMetadataSchema, fileMetadata);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error(`Failed to get file ${fileId}:`, error);
|
|
183
|
+
throw new Error(`Failed to get file ${fileId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function deleteFile(client, fileId, bucket = DEFAULT_BUCKET) {
|
|
187
|
+
ensureServer2();
|
|
188
|
+
try {
|
|
189
|
+
const deleteOperation = createDeleteOperation(bucket, fileId);
|
|
190
|
+
await executeMinioOperation(client, deleteOperation);
|
|
191
|
+
return true;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
console.error(`Failed to delete file ${fileId}:`, error);
|
|
194
|
+
throw new Error(`Failed to delete file ${fileId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async function createPresignedUploadUrl(client, fileName, path = "", bucket = DEFAULT_BUCKET, expirySeconds = 3600) {
|
|
198
|
+
ensureServer2();
|
|
199
|
+
try {
|
|
200
|
+
const safeFileName = fileName.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
201
|
+
const objectName = normalizePath(path, safeFileName);
|
|
202
|
+
const presignedPutOperation = createPresignedPutOperation(bucket, objectName, expirySeconds);
|
|
203
|
+
const url = await executeMinioOperation(client, presignedPutOperation);
|
|
204
|
+
if (!url) {
|
|
205
|
+
throw new Error("Failed to generate presigned upload URL");
|
|
206
|
+
}
|
|
207
|
+
return url;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
console.error("Failed to create presigned upload URL:", error);
|
|
210
|
+
throw new Error(`Failed to create presigned upload URL: ${error instanceof Error ? error.message : String(error)}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async function uploadFile(client, buffer, objectName, contentType, bucket = DEFAULT_BUCKET) {
|
|
214
|
+
ensureServer2();
|
|
215
|
+
try {
|
|
216
|
+
const metadata = {
|
|
217
|
+
"content-type": contentType,
|
|
218
|
+
"upload-time": (/* @__PURE__ */ new Date()).toISOString()
|
|
219
|
+
};
|
|
220
|
+
const simpleUploadFn = createSimpleUploadOperation(client);
|
|
221
|
+
const result = await simpleUploadFn(buffer, bucket, objectName, metadata);
|
|
222
|
+
const presignedUrlOperation = createPresignedUrlOperation(
|
|
223
|
+
bucket,
|
|
224
|
+
objectName,
|
|
225
|
+
3600
|
|
226
|
+
// 1 hour expiry
|
|
227
|
+
);
|
|
228
|
+
const url = await executeMinioOperation(client, presignedUrlOperation);
|
|
229
|
+
const fileName = objectName.split("/").pop() || objectName;
|
|
230
|
+
const fileMetadata = {
|
|
231
|
+
id: objectName,
|
|
232
|
+
name: fileName,
|
|
233
|
+
contentType,
|
|
234
|
+
size: buffer.length,
|
|
235
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
236
|
+
etag: result.etag,
|
|
237
|
+
url
|
|
238
|
+
};
|
|
239
|
+
return validate(FileMetadataSchema, fileMetadata);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error("Failed to upload file:", error);
|
|
242
|
+
throw new Error(`Failed to upload file: ${error instanceof Error ? error.message : String(error)}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
20
246
|
// src/minio.ts
|
|
21
247
|
function createServerMinioClient(options) {
|
|
22
|
-
|
|
23
|
-
const validatedOptions =
|
|
248
|
+
ensureServer3();
|
|
249
|
+
const validatedOptions = validate2(ServerClientOptionsSchema, options);
|
|
24
250
|
const url = new URL(validatedOptions.instance);
|
|
25
251
|
return {
|
|
26
252
|
client: new Client({
|
|
@@ -34,6 +260,12 @@ function createServerMinioClient(options) {
|
|
|
34
260
|
};
|
|
35
261
|
}
|
|
36
262
|
export {
|
|
37
|
-
|
|
263
|
+
DEFAULT_BUCKET,
|
|
264
|
+
createPresignedUploadUrl,
|
|
265
|
+
createServerMinioClient,
|
|
266
|
+
deleteFile,
|
|
267
|
+
getFileById,
|
|
268
|
+
getFilesList,
|
|
269
|
+
uploadFile
|
|
38
270
|
};
|
|
39
271
|
//# sourceMappingURL=minio.mjs.map
|