@igorchugurov/public-api-sdk 1.1.0 → 1.2.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/README.md +35 -1
- package/dist/{client-BV5AAKYo.d.mts → client-DS5xnLAo.d.mts} +9 -0
- package/dist/{client-BV5AAKYo.d.ts → client-DS5xnLAo.d.ts} +9 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +141 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +141 -1
- package/dist/index.mjs.map +1 -1
- package/dist/server.d.mts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +156 -1
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +156 -1
- package/dist/server.mjs.map +1 -1
- package/package.json +1 -1
package/dist/server.mjs
CHANGED
|
@@ -32,7 +32,8 @@ var __export = (target, all) => {
|
|
|
32
32
|
var slug_exports = {};
|
|
33
33
|
__export(slug_exports, {
|
|
34
34
|
generateSlug: () => generateSlug,
|
|
35
|
-
generateUniqueSlugForInstance: () => generateUniqueSlugForInstance
|
|
35
|
+
generateUniqueSlugForInstance: () => generateUniqueSlugForInstance,
|
|
36
|
+
validateSlug: () => validateSlug
|
|
36
37
|
});
|
|
37
38
|
function generateSlug(name) {
|
|
38
39
|
if (!name || typeof name !== "string") {
|
|
@@ -40,6 +41,16 @@ function generateSlug(name) {
|
|
|
40
41
|
}
|
|
41
42
|
return name.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").substring(0, 100);
|
|
42
43
|
}
|
|
44
|
+
function validateSlug(slug) {
|
|
45
|
+
if (!slug || typeof slug !== "string") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (slug.length === 0 || slug.length > 100) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
52
|
+
return slugRegex.test(slug);
|
|
53
|
+
}
|
|
43
54
|
function generateRandomSuffix() {
|
|
44
55
|
return Math.random().toString(36).substring(2, 6);
|
|
45
56
|
}
|
|
@@ -582,6 +593,9 @@ function flattenInstance(instance, fields, relationsAsIds = false) {
|
|
|
582
593
|
return result;
|
|
583
594
|
}
|
|
584
595
|
|
|
596
|
+
// src/client.ts
|
|
597
|
+
init_slug();
|
|
598
|
+
|
|
585
599
|
// src/errors.ts
|
|
586
600
|
var SDKError = class extends Error {
|
|
587
601
|
constructor(code, message, statusCode, details) {
|
|
@@ -610,6 +624,19 @@ var PermissionDeniedError = class extends SDKError {
|
|
|
610
624
|
);
|
|
611
625
|
}
|
|
612
626
|
};
|
|
627
|
+
var ValidationError = class extends SDKError {
|
|
628
|
+
constructor(field, message) {
|
|
629
|
+
super(
|
|
630
|
+
"VALIDATION_ERROR",
|
|
631
|
+
`Validation failed for ${field}: ${message}`,
|
|
632
|
+
400,
|
|
633
|
+
{
|
|
634
|
+
field,
|
|
635
|
+
message
|
|
636
|
+
}
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
613
640
|
function handleSupabaseError(error) {
|
|
614
641
|
if (error.code === "PGRST116") {
|
|
615
642
|
throw new NotFoundError("Resource");
|
|
@@ -792,6 +819,134 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
|
|
|
792
819
|
handleSupabaseError(error);
|
|
793
820
|
}
|
|
794
821
|
}
|
|
822
|
+
/**
|
|
823
|
+
* Получить один экземпляр по slug
|
|
824
|
+
*/
|
|
825
|
+
async getInstanceBySlug(entityDefinitionId, slug, params) {
|
|
826
|
+
var _a;
|
|
827
|
+
try {
|
|
828
|
+
if (!validateSlug(slug)) {
|
|
829
|
+
throw new ValidationError(
|
|
830
|
+
"slug",
|
|
831
|
+
"Slug must contain only lowercase letters, numbers, and hyphens, and cannot start or end with a hyphen"
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
const fields = await this.getFields(entityDefinitionId);
|
|
835
|
+
const { data: instance, error: instanceError } = await this.supabase.from("entity_instance").select("*").eq("slug", slug).eq("entity_definition_id", entityDefinitionId).eq("project_id", this.projectId).single();
|
|
836
|
+
if (instanceError || !instance) {
|
|
837
|
+
handleInstanceError(
|
|
838
|
+
instanceError || new Error("Instance not found"),
|
|
839
|
+
slug
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
const transformedInstance = transformEntityInstance(instance);
|
|
843
|
+
if (transformedInstance.entityDefinitionId !== entityDefinitionId) {
|
|
844
|
+
throw new NotFoundError("Entity instance", slug);
|
|
845
|
+
}
|
|
846
|
+
const relationFields = fields.filter(
|
|
847
|
+
(f) => f.dbType === "manyToMany" || f.dbType === "manyToOne" || f.dbType === "oneToMany" || f.dbType === "oneToOne"
|
|
848
|
+
);
|
|
849
|
+
const relations = {};
|
|
850
|
+
if (relationFields.length > 0) {
|
|
851
|
+
const relationFieldIds = relationFields.map((f) => f.id).filter((id) => Boolean(id));
|
|
852
|
+
if (relationFieldIds.length > 0) {
|
|
853
|
+
const { data: allRelations, error: relationsError } = await this.supabase.from("entity_relation").select("target_instance_id, relation_field_id").eq("source_instance_id", transformedInstance.id).in("relation_field_id", relationFieldIds);
|
|
854
|
+
if (relationsError) {
|
|
855
|
+
throw new SDKError(
|
|
856
|
+
"RELATIONS_LOAD_ERROR",
|
|
857
|
+
`Failed to load relations for instance with slug ${slug}: ${relationsError.message}`,
|
|
858
|
+
500,
|
|
859
|
+
relationsError
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
if (allRelations && allRelations.length > 0) {
|
|
863
|
+
const targetInstanceIds = [
|
|
864
|
+
...new Set(allRelations.map((r) => r.target_instance_id))
|
|
865
|
+
];
|
|
866
|
+
const { data: relatedInstances, error: instancesError } = await this.supabase.from("entity_instance").select("*").in("id", targetInstanceIds);
|
|
867
|
+
if (instancesError) {
|
|
868
|
+
throw new SDKError(
|
|
869
|
+
"RELATED_INSTANCES_LOAD_ERROR",
|
|
870
|
+
`Failed to load related instances for instance with slug ${slug}: ${instancesError.message}`,
|
|
871
|
+
500,
|
|
872
|
+
instancesError
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
if (relatedInstances) {
|
|
876
|
+
const relatedInstancesMap = new Map(
|
|
877
|
+
relatedInstances.map((inst) => [
|
|
878
|
+
inst.id,
|
|
879
|
+
transformEntityInstance(inst)
|
|
880
|
+
])
|
|
881
|
+
);
|
|
882
|
+
for (const relation of allRelations) {
|
|
883
|
+
const relationField = relationFields.find(
|
|
884
|
+
(f) => f.id === relation.relation_field_id
|
|
885
|
+
);
|
|
886
|
+
if (relationField) {
|
|
887
|
+
const relatedInstance = relatedInstancesMap.get(
|
|
888
|
+
relation.target_instance_id
|
|
889
|
+
);
|
|
890
|
+
if (relatedInstance) {
|
|
891
|
+
if (!relations[relationField.name]) {
|
|
892
|
+
relations[relationField.name] = [];
|
|
893
|
+
}
|
|
894
|
+
relations[relationField.name].push(
|
|
895
|
+
relatedInstance
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
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
|
|
915
|
+
);
|
|
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
|
+
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
|
+
}
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
const instanceWithRelations = __spreadProps(__spreadValues({}, transformedInstance), {
|
|
936
|
+
relations: Object.keys(relations).length > 0 ? relations : void 0
|
|
937
|
+
});
|
|
938
|
+
return flattenInstance(
|
|
939
|
+
instanceWithRelations,
|
|
940
|
+
fields.map((f) => ({ name: f.name, dbType: f.dbType })),
|
|
941
|
+
(_a = params == null ? void 0 : params.relationsAsIds) != null ? _a : false
|
|
942
|
+
);
|
|
943
|
+
} catch (error) {
|
|
944
|
+
if (error instanceof NotFoundError || error instanceof PermissionDeniedError || error instanceof ValidationError || error instanceof SDKError) {
|
|
945
|
+
throw error;
|
|
946
|
+
}
|
|
947
|
+
handleSupabaseError(error);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
795
950
|
/**
|
|
796
951
|
* Получить список экземпляров
|
|
797
952
|
* Поддерживает поиск, фильтры (JSONB и relation), пагинацию
|