@igorchugurov/public-api-sdk 1.2.0 → 1.4.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/dist/index.mjs CHANGED
@@ -763,6 +763,22 @@ function flattenInstance(instance, fields, relationsAsIds = false) {
763
763
  }
764
764
  return result;
765
765
  }
766
+ function transformEntityFile(row) {
767
+ return {
768
+ id: row.id,
769
+ entityInstanceId: row.entity_instance_id,
770
+ fieldId: row.field_id,
771
+ fileUrl: row.file_url,
772
+ filePath: row.file_path,
773
+ fileName: row.file_name,
774
+ fileSize: row.file_size,
775
+ fileType: row.file_type,
776
+ storageBucket: row.storage_bucket,
777
+ uploadedBy: row.uploaded_by,
778
+ createdAt: row.created_at,
779
+ updatedAt: row.updated_at
780
+ };
781
+ }
766
782
 
767
783
  // src/client.ts
768
784
  init_slug();
@@ -795,6 +811,42 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
795
811
  getMode() {
796
812
  return this.mode;
797
813
  }
814
+ /**
815
+ * Загружает файлы для экземпляра и возвращает их как полные объекты EntityFile,
816
+ * сгруппированные по именам полей
817
+ */
818
+ async loadFilesForInstance(instanceId, fileFields) {
819
+ if (fileFields.length === 0) {
820
+ return /* @__PURE__ */ new Map();
821
+ }
822
+ const { data: allFiles, error: filesError } = await this.supabase.from("entity_file").select("*").eq("entity_instance_id", instanceId);
823
+ if (filesError) {
824
+ throw new SDKError(
825
+ "FILES_LOAD_ERROR",
826
+ `Failed to load files for instance ${instanceId}: ${filesError.message}`,
827
+ 500,
828
+ filesError
829
+ );
830
+ }
831
+ if (!allFiles || allFiles.length === 0) {
832
+ return /* @__PURE__ */ new Map();
833
+ }
834
+ const filesMap = /* @__PURE__ */ new Map();
835
+ const fieldIdToName = new Map(
836
+ fileFields.map((f) => [f.id, f.name])
837
+ );
838
+ allFiles.forEach((fileRow) => {
839
+ if (!fileRow.field_id) return;
840
+ const fieldName = fieldIdToName.get(fileRow.field_id);
841
+ if (!fieldName) return;
842
+ const entityFile = transformEntityFile(fileRow);
843
+ if (!filesMap.has(fieldName)) {
844
+ filesMap.set(fieldName, []);
845
+ }
846
+ filesMap.get(fieldName).push(entityFile);
847
+ });
848
+ return filesMap;
849
+ }
798
850
  /**
799
851
  * Получить один экземпляр
800
852
  */
@@ -871,34 +923,15 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
871
923
  }
872
924
  }
873
925
  }
874
- const fileFields = fields.filter(
875
- (f) => f.type === "files" || f.type === "images"
876
- );
877
- if (fileFields.length > 0) {
878
- const { data: allFiles, error: filesError } = await this.supabase.from("entity_file").select("id, field_id").eq("entity_instance_id", id);
879
- if (filesError) {
880
- throw new SDKError(
881
- "FILES_LOAD_ERROR",
882
- `Failed to load files for instance ${id}: ${filesError.message}`,
883
- 500,
884
- filesError
885
- );
886
- }
887
- if (allFiles) {
888
- const filesByFieldId = /* @__PURE__ */ new Map();
889
- allFiles.forEach((file) => {
890
- if (file.field_id) {
891
- if (!filesByFieldId.has(file.field_id)) {
892
- filesByFieldId.set(file.field_id, []);
893
- }
894
- filesByFieldId.get(file.field_id).push(file.id);
895
- }
896
- });
926
+ if ((params == null ? void 0 : params.loadFiles) === true) {
927
+ const fileFields = fields.filter(
928
+ (f) => f.type === "files" || f.type === "images"
929
+ );
930
+ if (fileFields.length > 0) {
931
+ const filesMap = await this.loadFilesForInstance(id, fileFields);
897
932
  fileFields.forEach((field) => {
898
- const fileIds = filesByFieldId.get(field.id) || [];
899
- if (fileIds.length > 0 || !transformedInstance.data[field.name]) {
900
- transformedInstance.data[field.name] = fileIds;
901
- }
933
+ const files = filesMap.get(field.name) || [];
934
+ transformedInstance.data[field.name] = files;
902
935
  });
903
936
  }
904
937
  }
@@ -999,34 +1032,18 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
999
1032
  }
1000
1033
  }
1001
1034
  }
1002
- const fileFields = fields.filter(
1003
- (f) => f.type === "files" || f.type === "images"
1004
- );
1005
- if (fileFields.length > 0) {
1006
- const { data: allFiles, error: filesError } = await this.supabase.from("entity_file").select("id, field_id").eq("entity_instance_id", transformedInstance.id);
1007
- if (filesError) {
1008
- throw new SDKError(
1009
- "FILES_LOAD_ERROR",
1010
- `Failed to load files for instance with slug ${slug}: ${filesError.message}`,
1011
- 500,
1012
- filesError
1035
+ if ((params == null ? void 0 : params.loadFiles) === true) {
1036
+ const fileFields = fields.filter(
1037
+ (f) => f.type === "files" || f.type === "images"
1038
+ );
1039
+ if (fileFields.length > 0) {
1040
+ const filesMap = await this.loadFilesForInstance(
1041
+ transformedInstance.id,
1042
+ fileFields
1013
1043
  );
1014
- }
1015
- if (allFiles) {
1016
- const filesByFieldId = /* @__PURE__ */ new Map();
1017
- allFiles.forEach((file) => {
1018
- if (file.field_id) {
1019
- if (!filesByFieldId.has(file.field_id)) {
1020
- filesByFieldId.set(file.field_id, []);
1021
- }
1022
- filesByFieldId.get(file.field_id).push(file.id);
1023
- }
1024
- });
1025
1044
  fileFields.forEach((field) => {
1026
- const fileIds = filesByFieldId.get(field.id) || [];
1027
- if (fileIds.length > 0 || !transformedInstance.data[field.name]) {
1028
- transformedInstance.data[field.name] = fileIds;
1029
- }
1045
+ const files = filesMap.get(field.name) || [];
1046
+ transformedInstance.data[field.name] = files;
1030
1047
  });
1031
1048
  }
1032
1049
  }