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