@igorchugurov/public-api-sdk 1.2.0 → 1.3.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/server.mjs CHANGED
@@ -592,6 +592,22 @@ function flattenInstance(instance, fields, relationsAsIds = false) {
592
592
  }
593
593
  return result;
594
594
  }
595
+ function transformEntityFile(row) {
596
+ return {
597
+ id: row.id,
598
+ entityInstanceId: row.entity_instance_id,
599
+ fieldId: row.field_id,
600
+ fileUrl: row.file_url,
601
+ filePath: row.file_path,
602
+ fileName: row.file_name,
603
+ fileSize: row.file_size,
604
+ fileType: row.file_type,
605
+ storageBucket: row.storage_bucket,
606
+ uploadedBy: row.uploaded_by,
607
+ createdAt: row.created_at,
608
+ updatedAt: row.updated_at
609
+ };
610
+ }
595
611
 
596
612
  // src/client.ts
597
613
  init_slug();
@@ -697,6 +713,42 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
697
713
  getMode() {
698
714
  return this.mode;
699
715
  }
716
+ /**
717
+ * Загружает файлы для экземпляра и возвращает их как полные объекты EntityFile,
718
+ * сгруппированные по именам полей
719
+ */
720
+ async loadFilesForInstance(instanceId, fileFields) {
721
+ if (fileFields.length === 0) {
722
+ return /* @__PURE__ */ new Map();
723
+ }
724
+ const { data: allFiles, error: filesError } = await this.supabase.from("entity_file").select("*").eq("entity_instance_id", instanceId);
725
+ if (filesError) {
726
+ throw new SDKError(
727
+ "FILES_LOAD_ERROR",
728
+ `Failed to load files for instance ${instanceId}: ${filesError.message}`,
729
+ 500,
730
+ filesError
731
+ );
732
+ }
733
+ if (!allFiles || allFiles.length === 0) {
734
+ return /* @__PURE__ */ new Map();
735
+ }
736
+ const filesMap = /* @__PURE__ */ new Map();
737
+ const fieldIdToName = new Map(
738
+ fileFields.map((f) => [f.id, f.name])
739
+ );
740
+ allFiles.forEach((fileRow) => {
741
+ if (!fileRow.field_id) return;
742
+ const fieldName = fieldIdToName.get(fileRow.field_id);
743
+ if (!fieldName) return;
744
+ const entityFile = transformEntityFile(fileRow);
745
+ if (!filesMap.has(fieldName)) {
746
+ filesMap.set(fieldName, []);
747
+ }
748
+ filesMap.get(fieldName).push(entityFile);
749
+ });
750
+ return filesMap;
751
+ }
700
752
  /**
701
753
  * Получить один экземпляр
702
754
  */
@@ -773,34 +825,15 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
773
825
  }
774
826
  }
775
827
  }
776
- const fileFields = fields.filter(
777
- (f) => f.type === "files" || f.type === "images"
778
- );
779
- if (fileFields.length > 0) {
780
- const { data: allFiles, error: filesError } = await this.supabase.from("entity_file").select("id, field_id").eq("entity_instance_id", id);
781
- if (filesError) {
782
- throw new SDKError(
783
- "FILES_LOAD_ERROR",
784
- `Failed to load files for instance ${id}: ${filesError.message}`,
785
- 500,
786
- filesError
787
- );
788
- }
789
- if (allFiles) {
790
- const filesByFieldId = /* @__PURE__ */ new Map();
791
- allFiles.forEach((file) => {
792
- if (file.field_id) {
793
- if (!filesByFieldId.has(file.field_id)) {
794
- filesByFieldId.set(file.field_id, []);
795
- }
796
- filesByFieldId.get(file.field_id).push(file.id);
797
- }
798
- });
828
+ if ((params == null ? void 0 : params.loadFiles) === true) {
829
+ const fileFields = fields.filter(
830
+ (f) => f.type === "files" || f.type === "images"
831
+ );
832
+ if (fileFields.length > 0) {
833
+ const filesMap = await this.loadFilesForInstance(id, fileFields);
799
834
  fileFields.forEach((field) => {
800
- const fileIds = filesByFieldId.get(field.id) || [];
801
- if (fileIds.length > 0 || !transformedInstance.data[field.name]) {
802
- transformedInstance.data[field.name] = fileIds;
803
- }
835
+ const files = filesMap.get(field.name) || [];
836
+ transformedInstance.data[field.name] = files;
804
837
  });
805
838
  }
806
839
  }
@@ -901,34 +934,18 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
901
934
  }
902
935
  }
903
936
  }
904
- const fileFields = fields.filter(
905
- (f) => f.type === "files" || f.type === "images"
906
- );
907
- if (fileFields.length > 0) {
908
- const { data: allFiles, error: filesError } = await this.supabase.from("entity_file").select("id, field_id").eq("entity_instance_id", transformedInstance.id);
909
- if (filesError) {
910
- throw new SDKError(
911
- "FILES_LOAD_ERROR",
912
- `Failed to load files for instance with slug ${slug}: ${filesError.message}`,
913
- 500,
914
- filesError
937
+ if ((params == null ? void 0 : params.loadFiles) === true) {
938
+ const fileFields = fields.filter(
939
+ (f) => f.type === "files" || f.type === "images"
940
+ );
941
+ if (fileFields.length > 0) {
942
+ const filesMap = await this.loadFilesForInstance(
943
+ transformedInstance.id,
944
+ fileFields
915
945
  );
916
- }
917
- if (allFiles) {
918
- const filesByFieldId = /* @__PURE__ */ new Map();
919
- allFiles.forEach((file) => {
920
- if (file.field_id) {
921
- if (!filesByFieldId.has(file.field_id)) {
922
- filesByFieldId.set(file.field_id, []);
923
- }
924
- filesByFieldId.get(file.field_id).push(file.id);
925
- }
926
- });
927
946
  fileFields.forEach((field) => {
928
- const fileIds = filesByFieldId.get(field.id) || [];
929
- if (fileIds.length > 0 || !transformedInstance.data[field.name]) {
930
- transformedInstance.data[field.name] = fileIds;
931
- }
947
+ const files = filesMap.get(field.name) || [];
948
+ transformedInstance.data[field.name] = files;
932
949
  });
933
950
  }
934
951
  }