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