@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/server.mjs CHANGED
@@ -3,6 +3,7 @@ import { createServerClient as createServerClient$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
  function createServerClient(supabaseUrl, supabaseAnonKey, options) {
23
68
  return createServerClient$1(supabaseUrl, supabaseAnonKey, {
24
69
  cookies: (options == null ? void 0 : options.cookies) || {
@@ -224,6 +269,7 @@ var BasePublicAPIClient = class {
224
269
  return {
225
270
  id: row.id,
226
271
  name: row.name,
272
+ slug: row.slug,
227
273
  description: row.description,
228
274
  tableName: row.table_name,
229
275
  type: row.type,
@@ -346,6 +392,38 @@ var BasePublicAPIClient = class {
346
392
  }
347
393
  return config;
348
394
  }
395
+ /**
396
+ * Получить все EntityDefinitions проекта с полями одним запросом (JOIN)
397
+ * Используется для загрузки всех сущностей в layout
398
+ *
399
+ * @returns Массив EntityDefinitionConfig с полями
400
+ */
401
+ async getAllEntityDefinitions() {
402
+ const { data, error } = await this.supabase.from("entity_definition").select(
403
+ `
404
+ *,
405
+ field!field_entity_definition_id_fkey (*)
406
+ `
407
+ ).eq("project_id", this.projectId).order("name");
408
+ if (error) {
409
+ throw new Error(`Failed to load entity definitions: ${error.message}`);
410
+ }
411
+ if (!data || data.length === 0) {
412
+ return [];
413
+ }
414
+ return data.map((row) => {
415
+ const entityDefinition = this.transformEntityDefinitionFromDB(row);
416
+ const fields = (row.field || []).map(
417
+ (fieldRow) => this.transformFieldFromDB(fieldRow)
418
+ );
419
+ fields.sort(
420
+ (a, b) => a.displayIndex - b.displayIndex
421
+ );
422
+ return __spreadProps(__spreadValues({}, entityDefinition), {
423
+ fields
424
+ });
425
+ });
426
+ }
349
427
  /**
350
428
  * Преобразование EntityDefinitionConfig в EntityDefinition
351
429
  */
@@ -353,6 +431,7 @@ var BasePublicAPIClient = class {
353
431
  return {
354
432
  id: config.id,
355
433
  name: config.name,
434
+ slug: config.slug,
356
435
  description: config.description,
357
436
  tableName: config.tableName,
358
437
  type: config.type,
@@ -464,6 +543,7 @@ var BasePublicAPIClient = class {
464
543
  function transformEntityInstance(row) {
465
544
  return {
466
545
  id: row.id,
546
+ slug: row.slug,
467
547
  entityDefinitionId: row.entity_definition_id,
468
548
  projectId: row.project_id,
469
549
  data: row.data || {},
@@ -474,6 +554,7 @@ function transformEntityInstance(row) {
474
554
  function flattenInstance(instance, fields, relationsAsIds = false) {
475
555
  const result = {
476
556
  id: instance.id,
557
+ slug: instance.slug,
477
558
  entityDefinitionId: instance.entityDefinitionId,
478
559
  projectId: instance.projectId,
479
560
  createdAt: instance.createdAt,
@@ -1099,9 +1180,27 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
1099
1180
  );
1100
1181
  }
1101
1182
  const { data: instanceData, relations } = data;
1183
+ const name = instanceData.name;
1184
+ if (!name || typeof name !== "string") {
1185
+ throw new Error(
1186
+ "Field 'name' is required and must be a string for slug generation"
1187
+ );
1188
+ }
1189
+ const {
1190
+ generateUniqueSlugForInstance: generateUniqueSlugForInstance2
1191
+ } = await Promise.resolve().then(() => (init_slug(), slug_exports));
1192
+ const slug = await generateUniqueSlugForInstance2(
1193
+ name,
1194
+ entityDefinitionId,
1195
+ async (slugToCheck, entityDefIdToCheck) => {
1196
+ const { data: existing } = await this.supabase.from("entity_instance").select("id").eq("entity_definition_id", entityDefIdToCheck).eq("slug", slugToCheck).single();
1197
+ return !!existing;
1198
+ }
1199
+ );
1102
1200
  const { data: instance, error: instanceError } = await this.supabase.from("entity_instance").insert({
1103
1201
  entity_definition_id: entityDefinitionId,
1104
1202
  project_id: this.projectId,
1203
+ slug,
1105
1204
  data: instanceData,
1106
1205
  created_by: (user == null ? void 0 : user.id) || null
1107
1206
  }).select().single();