@igorchugurov/public-api-sdk 1.0.0 → 1.1.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
@@ -3,6 +3,7 @@ import { createBrowserClient as createBrowserClient$1 } from '@supabase/ssr';
3
3
  var __defProp = Object.defineProperty;
4
4
  var __defProps = Object.defineProperties;
5
5
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
7
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
9
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -19,6 +20,50 @@ var __spreadValues = (a, b) => {
19
20
  return a;
20
21
  };
21
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __esm = (fn, res) => function __init() {
24
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
25
+ };
26
+ var __export = (target, all) => {
27
+ for (var name in all)
28
+ __defProp(target, name, { get: all[name], enumerable: true });
29
+ };
30
+
31
+ // src/utils/slug.ts
32
+ var slug_exports = {};
33
+ __export(slug_exports, {
34
+ generateSlug: () => generateSlug,
35
+ generateUniqueSlugForInstance: () => generateUniqueSlugForInstance
36
+ });
37
+ function generateSlug(name) {
38
+ if (!name || typeof name !== "string") {
39
+ throw new Error("Name must be a non-empty string");
40
+ }
41
+ return name.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").substring(0, 100);
42
+ }
43
+ function generateRandomSuffix() {
44
+ return Math.random().toString(36).substring(2, 6);
45
+ }
46
+ async function generateUniqueSlugForInstance(name, entityDefinitionId, checkExists, excludeId) {
47
+ const baseSlug = generateSlug(name);
48
+ let slug = baseSlug;
49
+ let attempts = 0;
50
+ const maxAttempts = 100;
51
+ while (attempts < maxAttempts) {
52
+ const exists = await checkExists(slug, entityDefinitionId, excludeId);
53
+ if (!exists) {
54
+ return slug;
55
+ }
56
+ const randomSuffix = generateRandomSuffix();
57
+ slug = `${baseSlug}-${randomSuffix}`;
58
+ attempts++;
59
+ }
60
+ const timestamp = Date.now().toString(36);
61
+ return `${baseSlug}-${timestamp}`;
62
+ }
63
+ var init_slug = __esm({
64
+ "src/utils/slug.ts"() {
65
+ }
66
+ });
22
67
 
23
68
  // src/types/entity-types.ts
24
69
  function isFieldValue(value) {
@@ -395,6 +440,7 @@ var BasePublicAPIClient = class {
395
440
  return {
396
441
  id: row.id,
397
442
  name: row.name,
443
+ slug: row.slug,
398
444
  description: row.description,
399
445
  tableName: row.table_name,
400
446
  type: row.type,
@@ -517,6 +563,38 @@ var BasePublicAPIClient = class {
517
563
  }
518
564
  return config;
519
565
  }
566
+ /**
567
+ * Получить все EntityDefinitions проекта с полями одним запросом (JOIN)
568
+ * Используется для загрузки всех сущностей в layout
569
+ *
570
+ * @returns Массив EntityDefinitionConfig с полями
571
+ */
572
+ async getAllEntityDefinitions() {
573
+ const { data, error } = await this.supabase.from("entity_definition").select(
574
+ `
575
+ *,
576
+ field!field_entity_definition_id_fkey (*)
577
+ `
578
+ ).eq("project_id", this.projectId).order("name");
579
+ if (error) {
580
+ throw new Error(`Failed to load entity definitions: ${error.message}`);
581
+ }
582
+ if (!data || data.length === 0) {
583
+ return [];
584
+ }
585
+ return data.map((row) => {
586
+ const entityDefinition = this.transformEntityDefinitionFromDB(row);
587
+ const fields = (row.field || []).map(
588
+ (fieldRow) => this.transformFieldFromDB(fieldRow)
589
+ );
590
+ fields.sort(
591
+ (a, b) => a.displayIndex - b.displayIndex
592
+ );
593
+ return __spreadProps(__spreadValues({}, entityDefinition), {
594
+ fields
595
+ });
596
+ });
597
+ }
520
598
  /**
521
599
  * Преобразование EntityDefinitionConfig в EntityDefinition
522
600
  */
@@ -524,6 +602,7 @@ var BasePublicAPIClient = class {
524
602
  return {
525
603
  id: config.id,
526
604
  name: config.name,
605
+ slug: config.slug,
527
606
  description: config.description,
528
607
  tableName: config.tableName,
529
608
  type: config.type,
@@ -635,6 +714,7 @@ var BasePublicAPIClient = class {
635
714
  function transformEntityInstance(row) {
636
715
  return {
637
716
  id: row.id,
717
+ slug: row.slug,
638
718
  entityDefinitionId: row.entity_definition_id,
639
719
  projectId: row.project_id,
640
720
  data: row.data || {},
@@ -645,6 +725,7 @@ function transformEntityInstance(row) {
645
725
  function flattenInstance(instance, fields, relationsAsIds = false) {
646
726
  const result = {
647
727
  id: instance.id,
728
+ slug: instance.slug,
648
729
  entityDefinitionId: instance.entityDefinitionId,
649
730
  projectId: instance.projectId,
650
731
  createdAt: instance.createdAt,
@@ -1212,9 +1293,27 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
1212
1293
  );
1213
1294
  }
1214
1295
  const { data: instanceData, relations } = data;
1296
+ const name = instanceData.name;
1297
+ if (!name || typeof name !== "string") {
1298
+ throw new Error(
1299
+ "Field 'name' is required and must be a string for slug generation"
1300
+ );
1301
+ }
1302
+ const {
1303
+ generateUniqueSlugForInstance: generateUniqueSlugForInstance2
1304
+ } = await Promise.resolve().then(() => (init_slug(), slug_exports));
1305
+ const slug = await generateUniqueSlugForInstance2(
1306
+ name,
1307
+ entityDefinitionId,
1308
+ async (slugToCheck, entityDefIdToCheck) => {
1309
+ const { data: existing } = await this.supabase.from("entity_instance").select("id").eq("entity_definition_id", entityDefIdToCheck).eq("slug", slugToCheck).single();
1310
+ return !!existing;
1311
+ }
1312
+ );
1215
1313
  const { data: instance, error: instanceError } = await this.supabase.from("entity_instance").insert({
1216
1314
  entity_definition_id: entityDefinitionId,
1217
1315
  project_id: this.projectId,
1316
+ slug,
1218
1317
  data: instanceData,
1219
1318
  created_by: (user == null ? void 0 : user.id) || null
1220
1319
  }).select().single();