@e-techsolutions/e-baas-sdk 1.2.1 → 1.2.3
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/index.cjs +301 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +88 -48
- package/dist/index.esm.js +301 -141
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -161,7 +161,8 @@ class AuthClient {
|
|
|
161
161
|
console.log('❌ [SDK DEBUG] SignUp - workspaceId é undefined, header não adicionado');
|
|
162
162
|
}
|
|
163
163
|
console.log('🔍 [SDK DEBUG] SignUp - headers finais:', headers);
|
|
164
|
-
const
|
|
164
|
+
const payload = this.buildSignUpPayload(data);
|
|
165
|
+
const response = await this.httpClient.post('/auth/v1/signup', payload, {
|
|
165
166
|
headers
|
|
166
167
|
});
|
|
167
168
|
if (response.data.session) {
|
|
@@ -276,6 +277,55 @@ class AuthClient {
|
|
|
276
277
|
setWorkspaceId(workspaceId) {
|
|
277
278
|
this.workspaceId = workspaceId;
|
|
278
279
|
}
|
|
280
|
+
buildSignUpPayload(data) {
|
|
281
|
+
const { metadata, ...rest } = data;
|
|
282
|
+
const payload = { ...rest };
|
|
283
|
+
if (metadata) {
|
|
284
|
+
payload.metadata = metadata;
|
|
285
|
+
}
|
|
286
|
+
const { firstName, lastName } = this.resolveNamesFromMetadata(rest.firstName, rest.lastName, metadata);
|
|
287
|
+
if (firstName && !payload.firstName) {
|
|
288
|
+
payload.firstName = firstName;
|
|
289
|
+
}
|
|
290
|
+
if (lastName && !payload.lastName) {
|
|
291
|
+
payload.lastName = lastName;
|
|
292
|
+
}
|
|
293
|
+
return payload;
|
|
294
|
+
}
|
|
295
|
+
resolveNamesFromMetadata(baseFirstName, baseLastName, metadata) {
|
|
296
|
+
const result = {};
|
|
297
|
+
if (baseFirstName) {
|
|
298
|
+
result.firstName = this.getStringValue(baseFirstName);
|
|
299
|
+
}
|
|
300
|
+
if (baseLastName) {
|
|
301
|
+
result.lastName = this.getStringValue(baseLastName);
|
|
302
|
+
}
|
|
303
|
+
const metadataFirstName = this.getStringValue(metadata?.['first_name'] ?? metadata?.firstName);
|
|
304
|
+
const metadataLastName = this.getStringValue(metadata?.['last_name'] ?? metadata?.lastName);
|
|
305
|
+
const metadataName = this.getStringValue(metadata?.name);
|
|
306
|
+
if (!result.firstName) {
|
|
307
|
+
result.firstName = metadataFirstName ?? (metadataName ? metadataName.split(/\s+/)[0] : undefined);
|
|
308
|
+
}
|
|
309
|
+
if (!result.lastName) {
|
|
310
|
+
if (metadataLastName) {
|
|
311
|
+
result.lastName = metadataLastName;
|
|
312
|
+
}
|
|
313
|
+
else if (metadataName) {
|
|
314
|
+
const parts = metadataName.split(/\s+/);
|
|
315
|
+
if (parts.length > 1) {
|
|
316
|
+
result.lastName = parts.slice(1).join(' ');
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
getStringValue(value) {
|
|
323
|
+
if (typeof value !== 'string') {
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
const trimmed = value.trim();
|
|
327
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
328
|
+
}
|
|
279
329
|
setSessionInternal(session) {
|
|
280
330
|
this.currentSession = session;
|
|
281
331
|
this.httpClient.setAuthToken(session.access_token);
|
|
@@ -800,246 +850,344 @@ class DatabaseClient {
|
|
|
800
850
|
}
|
|
801
851
|
}
|
|
802
852
|
|
|
853
|
+
const STORAGE_BASE_PATH$1 = '/storage/v1';
|
|
803
854
|
class StorageBucket {
|
|
804
|
-
constructor(httpClient, bucketId) {
|
|
855
|
+
constructor(httpClient, bucketId, workspaceId) {
|
|
805
856
|
this.httpClient = httpClient;
|
|
806
857
|
this.bucketId = bucketId;
|
|
858
|
+
this.workspaceId = workspaceId;
|
|
859
|
+
}
|
|
860
|
+
setWorkspaceId(workspaceId) {
|
|
861
|
+
this.workspaceId = workspaceId;
|
|
862
|
+
}
|
|
863
|
+
getWorkspaceHeaders() {
|
|
864
|
+
if (!this.workspaceId) {
|
|
865
|
+
throw new EBaaSError('Workspace ID is required for storage operations', 400);
|
|
866
|
+
}
|
|
867
|
+
return {
|
|
868
|
+
'x-workspace-id': this.workspaceId,
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
normalizePath(path) {
|
|
872
|
+
return path
|
|
873
|
+
.trim()
|
|
874
|
+
.replace(/^\/+/, '')
|
|
875
|
+
.replace(/\/+$/, '')
|
|
876
|
+
.replace(/\/+/g, '/');
|
|
877
|
+
}
|
|
878
|
+
encodePath(path) {
|
|
879
|
+
const normalized = this.normalizePath(path);
|
|
880
|
+
return normalized
|
|
881
|
+
.split('/')
|
|
882
|
+
.filter(Boolean)
|
|
883
|
+
.map((segment) => encodeURIComponent(segment))
|
|
884
|
+
.join('/');
|
|
885
|
+
}
|
|
886
|
+
extractPathParts(path) {
|
|
887
|
+
const normalized = this.normalizePath(path);
|
|
888
|
+
if (!normalized) {
|
|
889
|
+
throw new EBaaSError('Path must include the file name', 400);
|
|
890
|
+
}
|
|
891
|
+
const segments = normalized.split('/').filter(Boolean);
|
|
892
|
+
if (segments.length === 0) {
|
|
893
|
+
throw new EBaaSError('Path must include the file name', 400);
|
|
894
|
+
}
|
|
895
|
+
const fileName = segments.pop();
|
|
896
|
+
const directory = segments.length ? segments.join('/') : undefined;
|
|
897
|
+
return { fileName, directory };
|
|
898
|
+
}
|
|
899
|
+
serializeTransform(transform) {
|
|
900
|
+
if (!transform) {
|
|
901
|
+
return undefined;
|
|
902
|
+
}
|
|
903
|
+
const parts = Object.entries(transform)
|
|
904
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
905
|
+
.map(([key, value]) => `${key}=${value}`);
|
|
906
|
+
return parts.length ? parts.join(',') : undefined;
|
|
907
|
+
}
|
|
908
|
+
appendFileToFormData(formData, file, fileName, contentType) {
|
|
909
|
+
if (file instanceof File) {
|
|
910
|
+
if (file.name !== fileName) {
|
|
911
|
+
formData.append('file', file, fileName);
|
|
912
|
+
}
|
|
913
|
+
else {
|
|
914
|
+
formData.append('file', file);
|
|
915
|
+
}
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
if (file instanceof Blob) {
|
|
919
|
+
formData.append('file', file, fileName);
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
if (file instanceof ArrayBuffer) {
|
|
923
|
+
formData.append('file', new Blob([file], { type: contentType ?? 'application/octet-stream' }), fileName);
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
if (typeof file === 'string') {
|
|
927
|
+
formData.append('file', new Blob([file], { type: contentType ?? 'text/plain' }), fileName);
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
throw new EBaaSError('Unsupported file type for upload', 400);
|
|
807
931
|
}
|
|
808
932
|
async upload(path, file, options) {
|
|
809
933
|
try {
|
|
934
|
+
const headers = this.getWorkspaceHeaders();
|
|
935
|
+
const normalizedPath = this.normalizePath(path);
|
|
936
|
+
const { fileName, directory } = this.extractPathParts(normalizedPath);
|
|
810
937
|
const formData = new FormData();
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
else if (file instanceof Blob) {
|
|
815
|
-
formData.append('file', file, path);
|
|
816
|
-
}
|
|
817
|
-
else if (file instanceof ArrayBuffer) {
|
|
818
|
-
formData.append('file', new Blob([file]), path);
|
|
938
|
+
this.appendFileToFormData(formData, file, fileName, options?.contentType);
|
|
939
|
+
if (directory) {
|
|
940
|
+
formData.append('path', directory);
|
|
819
941
|
}
|
|
820
|
-
else if (typeof file === 'string') {
|
|
821
|
-
formData.append('file', new Blob([file], { type: 'text/plain' }), path);
|
|
822
|
-
}
|
|
823
|
-
formData.append('path', path);
|
|
824
942
|
if (options?.cacheControl) {
|
|
825
943
|
formData.append('cacheControl', options.cacheControl);
|
|
826
944
|
}
|
|
945
|
+
if (options?.metadata) {
|
|
946
|
+
formData.append('metadata', JSON.stringify(options.metadata));
|
|
947
|
+
}
|
|
827
948
|
if (options?.contentType) {
|
|
828
949
|
formData.append('contentType', options.contentType);
|
|
829
950
|
}
|
|
830
|
-
if (options?.
|
|
831
|
-
formData.append('
|
|
951
|
+
if (options?.expiresIn) {
|
|
952
|
+
formData.append('expiresIn', String(options.expiresIn));
|
|
832
953
|
}
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
954
|
+
if (options?.upsert) {
|
|
955
|
+
formData.append('overwrite', 'true');
|
|
956
|
+
}
|
|
957
|
+
const encodedPath = this.encodePath(normalizedPath);
|
|
958
|
+
const response = await this.httpClient.post(`${STORAGE_BASE_PATH$1}/object/${this.bucketId}/${encodedPath}`, formData, {
|
|
959
|
+
headers,
|
|
838
960
|
});
|
|
839
961
|
return {
|
|
840
962
|
data: response.data,
|
|
841
|
-
error: null
|
|
963
|
+
error: null,
|
|
842
964
|
};
|
|
843
965
|
}
|
|
844
966
|
catch (error) {
|
|
845
967
|
return {
|
|
846
968
|
data: null,
|
|
847
|
-
error: error instanceof EBaaSError
|
|
969
|
+
error: error instanceof EBaaSError
|
|
970
|
+
? error
|
|
971
|
+
: new EBaaSError(error instanceof Error ? error.message : 'Failed to upload file', 0, error),
|
|
848
972
|
};
|
|
849
973
|
}
|
|
850
974
|
}
|
|
851
975
|
async download(path, options) {
|
|
852
976
|
try {
|
|
853
|
-
const
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
const url = `/storage/buckets/${this.bucketId}/files/${path}${params.toString() ? `?${params.toString()}` : ''}`;
|
|
862
|
-
const response = await this.httpClient.get(url, {
|
|
863
|
-
responseType: 'blob'
|
|
977
|
+
const headers = this.getWorkspaceHeaders();
|
|
978
|
+
const normalizedPath = this.normalizePath(path);
|
|
979
|
+
const encodedPath = this.encodePath(normalizedPath);
|
|
980
|
+
const transformString = this.serializeTransform(options?.transform);
|
|
981
|
+
const query = transformString ? `?transform=${encodeURIComponent(transformString)}` : '';
|
|
982
|
+
const response = await this.httpClient.get(`${STORAGE_BASE_PATH$1}/object/authenticated/${this.bucketId}/${encodedPath}${query}`, {
|
|
983
|
+
responseType: 'blob',
|
|
984
|
+
headers,
|
|
864
985
|
});
|
|
865
986
|
return {
|
|
866
987
|
data: response.data,
|
|
867
|
-
error: null
|
|
988
|
+
error: null,
|
|
868
989
|
};
|
|
869
990
|
}
|
|
870
991
|
catch (error) {
|
|
871
992
|
return {
|
|
872
993
|
data: null,
|
|
873
|
-
error: error instanceof EBaaSError
|
|
994
|
+
error: error instanceof EBaaSError
|
|
995
|
+
? error
|
|
996
|
+
: new EBaaSError(error instanceof Error ? error.message : 'Failed to download file', 0, error),
|
|
874
997
|
};
|
|
875
998
|
}
|
|
876
999
|
}
|
|
877
1000
|
async list(prefix, options) {
|
|
878
1001
|
try {
|
|
1002
|
+
const headers = this.getWorkspaceHeaders();
|
|
879
1003
|
const params = new URLSearchParams();
|
|
880
1004
|
if (prefix) {
|
|
881
|
-
|
|
1005
|
+
const normalizedPrefix = prefix.replace(/^\/+/, '');
|
|
1006
|
+
if (normalizedPrefix) {
|
|
1007
|
+
params.append('prefix', normalizedPrefix);
|
|
1008
|
+
}
|
|
882
1009
|
}
|
|
883
1010
|
if (options?.limit) {
|
|
884
1011
|
params.append('limit', String(options.limit));
|
|
885
1012
|
}
|
|
886
1013
|
if (options?.offset) {
|
|
887
|
-
params.append('offset',
|
|
888
|
-
}
|
|
889
|
-
if (options?.sortBy) {
|
|
890
|
-
params.append('sortBy', JSON.stringify(options.sortBy));
|
|
1014
|
+
params.append('offset', options.offset);
|
|
891
1015
|
}
|
|
892
1016
|
if (options?.search) {
|
|
893
1017
|
params.append('search', options.search);
|
|
894
1018
|
}
|
|
895
|
-
|
|
896
|
-
|
|
1019
|
+
if (options?.sortBy && options.sortBy.length > 0) {
|
|
1020
|
+
const sortValue = options.sortBy
|
|
1021
|
+
.map((item) => {
|
|
1022
|
+
const order = item.order ?? 'asc';
|
|
1023
|
+
return `${item.column}:${order}`;
|
|
1024
|
+
})
|
|
1025
|
+
.join(',');
|
|
1026
|
+
params.append('sortBy', sortValue);
|
|
1027
|
+
}
|
|
1028
|
+
const query = params.toString();
|
|
1029
|
+
const response = await this.httpClient.get(`${STORAGE_BASE_PATH$1}/object/list/${this.bucketId}${query ? `?${query}` : ''}`, {
|
|
1030
|
+
headers,
|
|
1031
|
+
});
|
|
897
1032
|
return {
|
|
898
1033
|
data: response.data,
|
|
899
|
-
error: null
|
|
1034
|
+
error: null,
|
|
900
1035
|
};
|
|
901
1036
|
}
|
|
902
1037
|
catch (error) {
|
|
903
1038
|
return {
|
|
904
1039
|
data: null,
|
|
905
|
-
error: error instanceof EBaaSError
|
|
1040
|
+
error: error instanceof EBaaSError
|
|
1041
|
+
? error
|
|
1042
|
+
: new EBaaSError(error instanceof Error ? error.message : 'Failed to list files', 0, error),
|
|
906
1043
|
};
|
|
907
1044
|
}
|
|
908
1045
|
}
|
|
909
1046
|
async remove(paths) {
|
|
910
1047
|
try {
|
|
911
|
-
const
|
|
912
|
-
|
|
913
|
-
|
|
1048
|
+
const headers = this.getWorkspaceHeaders();
|
|
1049
|
+
for (const path of paths) {
|
|
1050
|
+
const normalizedPath = this.normalizePath(path);
|
|
1051
|
+
const encodedPath = this.encodePath(normalizedPath);
|
|
1052
|
+
await this.httpClient.delete(`${STORAGE_BASE_PATH$1}/object/${this.bucketId}/${encodedPath}`, {
|
|
1053
|
+
headers,
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
914
1056
|
return {
|
|
915
|
-
data:
|
|
916
|
-
error: null
|
|
1057
|
+
data: { message: 'Files removed successfully' },
|
|
1058
|
+
error: null,
|
|
917
1059
|
};
|
|
918
1060
|
}
|
|
919
1061
|
catch (error) {
|
|
920
1062
|
return {
|
|
921
1063
|
data: null,
|
|
922
|
-
error: error instanceof EBaaSError
|
|
1064
|
+
error: error instanceof EBaaSError
|
|
1065
|
+
? error
|
|
1066
|
+
: new EBaaSError(error instanceof Error ? error.message : 'Failed to remove files', 0, error),
|
|
923
1067
|
};
|
|
924
1068
|
}
|
|
925
1069
|
}
|
|
926
|
-
async move(
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
});
|
|
932
|
-
return {
|
|
933
|
-
data: response.data,
|
|
934
|
-
error: null
|
|
935
|
-
};
|
|
936
|
-
}
|
|
937
|
-
catch (error) {
|
|
938
|
-
return {
|
|
939
|
-
data: null,
|
|
940
|
-
error: error instanceof EBaaSError ? error : new EBaaSError(error instanceof Error ? error.message : 'Failed to move file', 0, error)
|
|
941
|
-
};
|
|
942
|
-
}
|
|
1070
|
+
async move(_fromPath, _toPath) {
|
|
1071
|
+
return {
|
|
1072
|
+
data: null,
|
|
1073
|
+
error: new EBaaSError('Move operation is not supported by the current E-BaaS backend', 501),
|
|
1074
|
+
};
|
|
943
1075
|
}
|
|
944
|
-
async copy(
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
});
|
|
950
|
-
return {
|
|
951
|
-
data: response.data,
|
|
952
|
-
error: null
|
|
953
|
-
};
|
|
954
|
-
}
|
|
955
|
-
catch (error) {
|
|
956
|
-
return {
|
|
957
|
-
data: null,
|
|
958
|
-
error: error instanceof EBaaSError ? error : new EBaaSError(error instanceof Error ? error.message : 'Failed to copy file', 0, error)
|
|
959
|
-
};
|
|
960
|
-
}
|
|
1076
|
+
async copy(_fromPath, _toPath) {
|
|
1077
|
+
return {
|
|
1078
|
+
data: null,
|
|
1079
|
+
error: new EBaaSError('Copy operation is not supported by the current E-BaaS backend', 501),
|
|
1080
|
+
};
|
|
961
1081
|
}
|
|
962
1082
|
async createSignedUrl(path, expiresIn, options) {
|
|
963
1083
|
try {
|
|
964
|
-
const
|
|
965
|
-
|
|
966
|
-
|
|
1084
|
+
const headers = this.getWorkspaceHeaders();
|
|
1085
|
+
const normalizedPath = this.normalizePath(path);
|
|
1086
|
+
const response = await this.httpClient.post(`${STORAGE_BASE_PATH$1}/object/sign`, {
|
|
1087
|
+
bucketName: this.bucketId,
|
|
1088
|
+
filePath: normalizedPath,
|
|
1089
|
+
operation: options?.operation ?? 'download',
|
|
1090
|
+
expiresIn: options?.expiresIn ?? expiresIn,
|
|
1091
|
+
}, {
|
|
1092
|
+
headers,
|
|
967
1093
|
});
|
|
968
1094
|
return {
|
|
969
1095
|
data: response.data,
|
|
970
|
-
error: null
|
|
1096
|
+
error: null,
|
|
971
1097
|
};
|
|
972
1098
|
}
|
|
973
1099
|
catch (error) {
|
|
974
1100
|
return {
|
|
975
1101
|
data: null,
|
|
976
|
-
error: error instanceof EBaaSError
|
|
1102
|
+
error: error instanceof EBaaSError
|
|
1103
|
+
? error
|
|
1104
|
+
: new EBaaSError(error instanceof Error ? error.message : 'Failed to create signed URL', 0, error),
|
|
977
1105
|
};
|
|
978
1106
|
}
|
|
979
1107
|
}
|
|
980
1108
|
getPublicUrl(path, options) {
|
|
1109
|
+
const baseUrl = this.httpClient.baseURL;
|
|
1110
|
+
if (!baseUrl) {
|
|
1111
|
+
throw new EBaaSError('HTTP client base URL is not configured', 400);
|
|
1112
|
+
}
|
|
1113
|
+
if (!this.workspaceId) {
|
|
1114
|
+
throw new EBaaSError('Workspace ID is required to generate public URLs', 400);
|
|
1115
|
+
}
|
|
1116
|
+
const normalizedPath = this.normalizePath(path);
|
|
1117
|
+
const encodedPath = this.encodePath(normalizedPath);
|
|
981
1118
|
const params = new URLSearchParams();
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
}
|
|
987
|
-
});
|
|
1119
|
+
params.append('workspaceId', this.workspaceId);
|
|
1120
|
+
const transformString = this.serializeTransform(options?.transform);
|
|
1121
|
+
if (transformString) {
|
|
1122
|
+
params.append('transform', transformString);
|
|
988
1123
|
}
|
|
989
|
-
const
|
|
990
|
-
const
|
|
1124
|
+
const publicUrl = `${baseUrl.replace(/\/+$/, '')}${STORAGE_BASE_PATH$1}/object/public/${this.bucketId}/${encodedPath}`;
|
|
1125
|
+
const query = params.toString();
|
|
991
1126
|
return {
|
|
992
1127
|
data: {
|
|
993
|
-
publicUrl
|
|
994
|
-
}
|
|
1128
|
+
publicUrl: query ? `${publicUrl}?${query}` : publicUrl,
|
|
1129
|
+
},
|
|
995
1130
|
};
|
|
996
1131
|
}
|
|
997
|
-
async getFileInfo(
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
error: null
|
|
1003
|
-
};
|
|
1004
|
-
}
|
|
1005
|
-
catch (error) {
|
|
1006
|
-
return {
|
|
1007
|
-
data: null,
|
|
1008
|
-
error: error instanceof EBaaSError ? error : new EBaaSError(error instanceof Error ? error.message : 'Failed to get file info', 0, error)
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1132
|
+
async getFileInfo(_path) {
|
|
1133
|
+
return {
|
|
1134
|
+
data: null,
|
|
1135
|
+
error: new EBaaSError('File info retrieval is not supported by the current E-BaaS backend', 501),
|
|
1136
|
+
};
|
|
1011
1137
|
}
|
|
1012
|
-
async updateFileMetadata(
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
return {
|
|
1018
|
-
data: response.data,
|
|
1019
|
-
error: null
|
|
1020
|
-
};
|
|
1021
|
-
}
|
|
1022
|
-
catch (error) {
|
|
1023
|
-
return {
|
|
1024
|
-
data: null,
|
|
1025
|
-
error: error instanceof EBaaSError ? error : new EBaaSError(error instanceof Error ? error.message : 'Failed to update file metadata', 0, error)
|
|
1026
|
-
};
|
|
1027
|
-
}
|
|
1138
|
+
async updateFileMetadata(_path, _metadata) {
|
|
1139
|
+
return {
|
|
1140
|
+
data: null,
|
|
1141
|
+
error: new EBaaSError('Updating file metadata is not supported by the current E-BaaS backend', 501),
|
|
1142
|
+
};
|
|
1028
1143
|
}
|
|
1029
1144
|
}
|
|
1030
1145
|
|
|
1146
|
+
const STORAGE_BASE_PATH = '/storage/v1';
|
|
1031
1147
|
class StorageClient {
|
|
1032
|
-
constructor(httpClient) {
|
|
1148
|
+
constructor(httpClient, workspaceId) {
|
|
1033
1149
|
this.httpClient = httpClient;
|
|
1150
|
+
this.workspaceId = workspaceId;
|
|
1034
1151
|
}
|
|
1035
1152
|
from(bucketId) {
|
|
1036
|
-
return new StorageBucket(this.httpClient, bucketId);
|
|
1153
|
+
return new StorageBucket(this.httpClient, bucketId, this.workspaceId);
|
|
1154
|
+
}
|
|
1155
|
+
setWorkspaceId(workspaceId) {
|
|
1156
|
+
this.workspaceId = workspaceId;
|
|
1157
|
+
}
|
|
1158
|
+
getWorkspaceHeaders() {
|
|
1159
|
+
if (!this.workspaceId) {
|
|
1160
|
+
throw new EBaaSError('Workspace ID is required for storage operations', 400);
|
|
1161
|
+
}
|
|
1162
|
+
return {
|
|
1163
|
+
'x-workspace-id': this.workspaceId,
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
mapBucketOptions(options) {
|
|
1167
|
+
if (!options) {
|
|
1168
|
+
return {};
|
|
1169
|
+
}
|
|
1170
|
+
const visibility = options.visibility ?? (options.public ? 'public' : undefined);
|
|
1171
|
+
const maxFileSize = options.maxFileSize ?? options.fileSizeLimit;
|
|
1172
|
+
const enableVersioning = options.enableVersioning ?? options.versioningEnabled;
|
|
1173
|
+
return {
|
|
1174
|
+
visibility,
|
|
1175
|
+
description: options.description,
|
|
1176
|
+
allowedMimeTypes: options.allowedMimeTypes,
|
|
1177
|
+
maxFileSize,
|
|
1178
|
+
enableVersioning,
|
|
1179
|
+
enableCors: options.enableCors,
|
|
1180
|
+
corsOrigins: options.corsOrigins,
|
|
1181
|
+
};
|
|
1037
1182
|
}
|
|
1038
1183
|
async createBucket(id, options) {
|
|
1039
1184
|
try {
|
|
1040
|
-
const
|
|
1041
|
-
id,
|
|
1042
|
-
...options
|
|
1185
|
+
const payload = {
|
|
1186
|
+
name: id,
|
|
1187
|
+
...this.mapBucketOptions(options),
|
|
1188
|
+
};
|
|
1189
|
+
const response = await this.httpClient.post(`${STORAGE_BASE_PATH}/buckets`, payload, {
|
|
1190
|
+
headers: this.getWorkspaceHeaders(),
|
|
1043
1191
|
});
|
|
1044
1192
|
return {
|
|
1045
1193
|
data: response.data,
|
|
@@ -1055,7 +1203,9 @@ class StorageClient {
|
|
|
1055
1203
|
}
|
|
1056
1204
|
async getBucket(id) {
|
|
1057
1205
|
try {
|
|
1058
|
-
const response = await this.httpClient.get(
|
|
1206
|
+
const response = await this.httpClient.get(`${STORAGE_BASE_PATH}/buckets/${id}`, {
|
|
1207
|
+
headers: this.getWorkspaceHeaders(),
|
|
1208
|
+
});
|
|
1059
1209
|
return {
|
|
1060
1210
|
data: response.data,
|
|
1061
1211
|
error: null
|
|
@@ -1070,7 +1220,9 @@ class StorageClient {
|
|
|
1070
1220
|
}
|
|
1071
1221
|
async listBuckets() {
|
|
1072
1222
|
try {
|
|
1073
|
-
const response = await this.httpClient.get(
|
|
1223
|
+
const response = await this.httpClient.get(`${STORAGE_BASE_PATH}/buckets`, {
|
|
1224
|
+
headers: this.getWorkspaceHeaders(),
|
|
1225
|
+
});
|
|
1074
1226
|
return {
|
|
1075
1227
|
data: response.data,
|
|
1076
1228
|
error: null
|
|
@@ -1085,7 +1237,10 @@ class StorageClient {
|
|
|
1085
1237
|
}
|
|
1086
1238
|
async updateBucket(id, options) {
|
|
1087
1239
|
try {
|
|
1088
|
-
const
|
|
1240
|
+
const payload = this.mapBucketOptions(options);
|
|
1241
|
+
const response = await this.httpClient.put(`${STORAGE_BASE_PATH}/buckets/${id}`, payload, {
|
|
1242
|
+
headers: this.getWorkspaceHeaders(),
|
|
1243
|
+
});
|
|
1089
1244
|
return {
|
|
1090
1245
|
data: response.data,
|
|
1091
1246
|
error: null
|
|
@@ -1100,7 +1255,9 @@ class StorageClient {
|
|
|
1100
1255
|
}
|
|
1101
1256
|
async deleteBucket(id) {
|
|
1102
1257
|
try {
|
|
1103
|
-
const response = await this.httpClient.delete(
|
|
1258
|
+
const response = await this.httpClient.delete(`${STORAGE_BASE_PATH}/buckets/${id}`, {
|
|
1259
|
+
headers: this.getWorkspaceHeaders(),
|
|
1260
|
+
});
|
|
1104
1261
|
return {
|
|
1105
1262
|
data: response.data,
|
|
1106
1263
|
error: null
|
|
@@ -1115,7 +1272,9 @@ class StorageClient {
|
|
|
1115
1272
|
}
|
|
1116
1273
|
async getUsage() {
|
|
1117
1274
|
try {
|
|
1118
|
-
const response = await this.httpClient.get(
|
|
1275
|
+
const response = await this.httpClient.get(`${STORAGE_BASE_PATH}/usage`, {
|
|
1276
|
+
headers: this.getWorkspaceHeaders(),
|
|
1277
|
+
});
|
|
1119
1278
|
return {
|
|
1120
1279
|
data: response.data,
|
|
1121
1280
|
error: null
|
|
@@ -1946,7 +2105,7 @@ class EBaaSClient {
|
|
|
1946
2105
|
// Initialize all modules
|
|
1947
2106
|
this.auth = new AuthClient(this.httpClient, options?.database?.workspaceId);
|
|
1948
2107
|
this.database = new DatabaseClient(this.httpClient, options?.database?.workspaceId, options?.rest?.url);
|
|
1949
|
-
this.storage = new StorageClient(this.httpClient);
|
|
2108
|
+
this.storage = new StorageClient(this.httpClient, options?.database?.workspaceId);
|
|
1950
2109
|
this.realtime = new RealtimeClient(supabaseUrl, supabaseKey, options?.realtime);
|
|
1951
2110
|
this.functions = new FunctionsClient(this.httpClient);
|
|
1952
2111
|
// Initialize custom database detection if enabled
|
|
@@ -1991,6 +2150,7 @@ class EBaaSClient {
|
|
|
1991
2150
|
async setWorkspaceId(workspaceId) {
|
|
1992
2151
|
this.auth.setWorkspaceId(workspaceId);
|
|
1993
2152
|
this.database.setWorkspaceId(workspaceId);
|
|
2153
|
+
this.storage.setWorkspaceId(workspaceId);
|
|
1994
2154
|
// Se auto-detecção estiver habilitada, re-detecta configuração para o novo workspace
|
|
1995
2155
|
if (this.options?.database?.autoDetectCustomDatabase) {
|
|
1996
2156
|
await this.database.initialize();
|