@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.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { S as SDKOptions, P as PublicAPIClient } from './client-DqqjGYgA.mjs';
1
+ import { S as SDKOptions, P as PublicAPIClient } from './client-BV5AAKYo.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-DqqjGYgA.js';
1
+ import { S as SDKOptions, P as PublicAPIClient } from './client-BV5AAKYo.js';
2
2
  import '@supabase/supabase-js';
3
3
 
4
4
  /**
package/dist/server.js CHANGED
@@ -5,6 +5,7 @@ var ssr = require('@supabase/ssr');
5
5
  var __defProp = Object.defineProperty;
6
6
  var __defProps = Object.defineProperties;
7
7
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
8
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
9
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
10
  var __hasOwnProp = Object.prototype.hasOwnProperty;
10
11
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -21,6 +22,50 @@ var __spreadValues = (a, b) => {
21
22
  return a;
22
23
  };
23
24
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __esm = (fn, res) => function __init() {
26
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
27
+ };
28
+ var __export = (target, all) => {
29
+ for (var name in all)
30
+ __defProp(target, name, { get: all[name], enumerable: true });
31
+ };
32
+
33
+ // src/utils/slug.ts
34
+ var slug_exports = {};
35
+ __export(slug_exports, {
36
+ generateSlug: () => generateSlug,
37
+ generateUniqueSlugForInstance: () => generateUniqueSlugForInstance
38
+ });
39
+ function generateSlug(name) {
40
+ if (!name || typeof name !== "string") {
41
+ throw new Error("Name must be a non-empty string");
42
+ }
43
+ return name.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").substring(0, 100);
44
+ }
45
+ function generateRandomSuffix() {
46
+ return Math.random().toString(36).substring(2, 6);
47
+ }
48
+ async function generateUniqueSlugForInstance(name, entityDefinitionId, checkExists, excludeId) {
49
+ const baseSlug = generateSlug(name);
50
+ let slug = baseSlug;
51
+ let attempts = 0;
52
+ const maxAttempts = 100;
53
+ while (attempts < maxAttempts) {
54
+ const exists = await checkExists(slug, entityDefinitionId, excludeId);
55
+ if (!exists) {
56
+ return slug;
57
+ }
58
+ const randomSuffix = generateRandomSuffix();
59
+ slug = `${baseSlug}-${randomSuffix}`;
60
+ attempts++;
61
+ }
62
+ const timestamp = Date.now().toString(36);
63
+ return `${baseSlug}-${timestamp}`;
64
+ }
65
+ var init_slug = __esm({
66
+ "src/utils/slug.ts"() {
67
+ }
68
+ });
24
69
  function createServerClient(supabaseUrl, supabaseAnonKey, options) {
25
70
  return ssr.createServerClient(supabaseUrl, supabaseAnonKey, {
26
71
  cookies: (options == null ? void 0 : options.cookies) || {
@@ -226,6 +271,7 @@ var BasePublicAPIClient = class {
226
271
  return {
227
272
  id: row.id,
228
273
  name: row.name,
274
+ slug: row.slug,
229
275
  description: row.description,
230
276
  tableName: row.table_name,
231
277
  type: row.type,
@@ -348,6 +394,38 @@ var BasePublicAPIClient = class {
348
394
  }
349
395
  return config;
350
396
  }
397
+ /**
398
+ * Получить все EntityDefinitions проекта с полями одним запросом (JOIN)
399
+ * Используется для загрузки всех сущностей в layout
400
+ *
401
+ * @returns Массив EntityDefinitionConfig с полями
402
+ */
403
+ async getAllEntityDefinitions() {
404
+ const { data, error } = await this.supabase.from("entity_definition").select(
405
+ `
406
+ *,
407
+ field!field_entity_definition_id_fkey (*)
408
+ `
409
+ ).eq("project_id", this.projectId).order("name");
410
+ if (error) {
411
+ throw new Error(`Failed to load entity definitions: ${error.message}`);
412
+ }
413
+ if (!data || data.length === 0) {
414
+ return [];
415
+ }
416
+ return data.map((row) => {
417
+ const entityDefinition = this.transformEntityDefinitionFromDB(row);
418
+ const fields = (row.field || []).map(
419
+ (fieldRow) => this.transformFieldFromDB(fieldRow)
420
+ );
421
+ fields.sort(
422
+ (a, b) => a.displayIndex - b.displayIndex
423
+ );
424
+ return __spreadProps(__spreadValues({}, entityDefinition), {
425
+ fields
426
+ });
427
+ });
428
+ }
351
429
  /**
352
430
  * Преобразование EntityDefinitionConfig в EntityDefinition
353
431
  */
@@ -355,6 +433,7 @@ var BasePublicAPIClient = class {
355
433
  return {
356
434
  id: config.id,
357
435
  name: config.name,
436
+ slug: config.slug,
358
437
  description: config.description,
359
438
  tableName: config.tableName,
360
439
  type: config.type,
@@ -466,6 +545,7 @@ var BasePublicAPIClient = class {
466
545
  function transformEntityInstance(row) {
467
546
  return {
468
547
  id: row.id,
548
+ slug: row.slug,
469
549
  entityDefinitionId: row.entity_definition_id,
470
550
  projectId: row.project_id,
471
551
  data: row.data || {},
@@ -476,6 +556,7 @@ function transformEntityInstance(row) {
476
556
  function flattenInstance(instance, fields, relationsAsIds = false) {
477
557
  const result = {
478
558
  id: instance.id,
559
+ slug: instance.slug,
479
560
  entityDefinitionId: instance.entityDefinitionId,
480
561
  projectId: instance.projectId,
481
562
  createdAt: instance.createdAt,
@@ -1101,9 +1182,27 @@ var _PublicAPIClient = class _PublicAPIClient extends BasePublicAPIClient {
1101
1182
  );
1102
1183
  }
1103
1184
  const { data: instanceData, relations } = data;
1185
+ const name = instanceData.name;
1186
+ if (!name || typeof name !== "string") {
1187
+ throw new Error(
1188
+ "Field 'name' is required and must be a string for slug generation"
1189
+ );
1190
+ }
1191
+ const {
1192
+ generateUniqueSlugForInstance: generateUniqueSlugForInstance2
1193
+ } = await Promise.resolve().then(() => (init_slug(), slug_exports));
1194
+ const slug = await generateUniqueSlugForInstance2(
1195
+ name,
1196
+ entityDefinitionId,
1197
+ async (slugToCheck, entityDefIdToCheck) => {
1198
+ const { data: existing } = await this.supabase.from("entity_instance").select("id").eq("entity_definition_id", entityDefIdToCheck).eq("slug", slugToCheck).single();
1199
+ return !!existing;
1200
+ }
1201
+ );
1104
1202
  const { data: instance, error: instanceError } = await this.supabase.from("entity_instance").insert({
1105
1203
  entity_definition_id: entityDefinitionId,
1106
1204
  project_id: this.projectId,
1205
+ slug,
1107
1206
  data: instanceData,
1108
1207
  created_by: (user == null ? void 0 : user.id) || null
1109
1208
  }).select().single();