@dnax/core 0.74.9 → 0.75.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/app/hono.ts CHANGED
@@ -44,6 +44,7 @@ import { csrf } from "hono/csrf";
44
44
  import { v4 } from "uuid";
45
45
  import type { SearchParams } from "meilisearch";
46
46
  import { utils } from "..";
47
+ import { get } from "http";
47
48
  const app = new Hono();
48
49
  const API_PATH = "/api";
49
50
 
@@ -95,6 +96,7 @@ function HonoInstance(): typeof app {
95
96
  // blackList: Cfg.server?.ipBlackList || [],
96
97
  })
97
98
  );
99
+
98
100
  app.use(secureHeaders());
99
101
  //app.use(compress());
100
102
 
@@ -172,7 +174,7 @@ function HonoInstance(): typeof app {
172
174
  });
173
175
  });
174
176
 
175
- // Access controle for api
177
+ // Access controle for API
176
178
  app.use(cleanPath(API_PATH), async (c, next) => {
177
179
  let session = sessionStorage();
178
180
 
@@ -317,8 +319,9 @@ function HonoInstance(): typeof app {
317
319
  }
318
320
  const service = getService(name, c.var["tenant-id"]);
319
321
  const col = getCollection(collection, c.var["tenant-id"]);
320
- // Controlle action
321
- if (!getAction(action)) {
322
+
323
+ // Controlle des actions à mener
324
+ if (!getAction(action) && !col?.customApi?.hasOwnProperty(action)) {
322
325
  throw new ContextError(`Action ${action} not found`, 400);
323
326
  }
324
327
 
@@ -330,12 +333,6 @@ function HonoInstance(): typeof app {
330
333
  c: c,
331
334
  });
332
335
 
333
- if (action == "execToolkit") {
334
- let tookit = toolkit;
335
-
336
- return;
337
- }
338
-
339
336
  if (action == "execService") {
340
337
  // Exec service
341
338
  if (!service) return fn.error("Service not found", 404);
@@ -357,7 +354,7 @@ function HonoInstance(): typeof app {
357
354
  return c.json(response);
358
355
  }
359
356
 
360
- if (!getAction(action) || !collection) {
357
+ if (!collection) {
361
358
  throw new ContextError(`Collection ${collection} not found`, 404);
362
359
  }
363
360
 
@@ -660,6 +657,10 @@ function HonoInstance(): typeof app {
660
657
  response = await rest.insertOne(collection, body?.data || {});
661
658
  }
662
659
 
660
+ if (!getAction(action) && col?.customApi?.hasOwnProperty(action)) {
661
+ response = await rest.customApi(collection, action, body?.data || {});
662
+ }
663
+
663
664
  if (action == "insertMany") {
664
665
  response = await rest.insertMany(collection, body?.data || {});
665
666
  }
@@ -2183,6 +2183,7 @@ class useRest {
2183
2183
  let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
2184
2184
 
2185
2185
  let col = getCollection(collection, this.#tenant_id);
2186
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
2186
2187
  let sharedData = {};
2187
2188
  if (col?.hooks?.beforeOperation && useHook) {
2188
2189
  await col.hooks.beforeOperation({
@@ -2346,6 +2347,42 @@ class useRest {
2346
2347
  }
2347
2348
  });
2348
2349
  }
2350
+
2351
+ /**
2352
+ * Custom API function
2353
+ * @param collection
2354
+ * @param action
2355
+ * @param data
2356
+ * @returns
2357
+ */
2358
+ async customApi(collection: string, action: string, data: object) {
2359
+ return new Promise(async (resolve, reject) => {
2360
+ try {
2361
+ let col = getCollection(collection, this.#tenant_id);
2362
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
2363
+ if (col?.customApi?.[action]) {
2364
+ let result = await col?.customApi?.[action]({
2365
+ error: fn.error,
2366
+ io: Cfg.io,
2367
+ session: sessionStorage(),
2368
+ data: toJson(data),
2369
+ rest: new useRest({
2370
+ session: this.#session,
2371
+ useHook: false,
2372
+ tenant_id: this.#tenant_id,
2373
+ useCustomApi: false,
2374
+ }),
2375
+ });
2376
+ resolve(result);
2377
+ } else {
2378
+ return fn.error(`Action ${action} not found`, 404);
2379
+ }
2380
+ } catch (err) {
2381
+ return reject(err);
2382
+ }
2383
+ });
2384
+ }
2385
+
2349
2386
  async deleteMany(
2350
2387
  collection: string,
2351
2388
  ids: Array<string>,
@@ -2359,6 +2396,7 @@ class useRest {
2359
2396
 
2360
2397
  let sharedData = {};
2361
2398
  let col = getCollection(collection, this.#tenant_id);
2399
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
2362
2400
  if (col?.hooks?.beforeOperation && useHook) {
2363
2401
  await col.hooks.beforeOperation({
2364
2402
  sharedData: sharedData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.74.9",
3
+ "version": "0.75.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},
@@ -34,14 +34,14 @@
34
34
  "dot-object": "2.1.5",
35
35
  "fs-extra": "^11.2.0",
36
36
  "generate-unique-id": "^2.0.3",
37
- "hono": "4.9.5",
37
+ "hono": "4.10.0",
38
38
  "joi": "17.13.3",
39
39
  "json-joy": "16.8.0",
40
40
  "jsonwebtoken": "^9.0.2",
41
41
  "mime-types": "^2.1.35",
42
42
  "minio": "^8.0.5",
43
43
  "moment": "^2.30.1",
44
- "mongodb": "6.18.0",
44
+ "mongodb": "6.20.0",
45
45
  "nodemailer": "^7.0.5",
46
46
  "ora": "^8.2.0",
47
47
  "pidusage": "^4.0.0",
@@ -51,6 +51,6 @@
51
51
  "ssh2": "^1.16.0",
52
52
  "ufo": "^1.5.4",
53
53
  "urlencode": "^2.0.0",
54
- "uuid": "^11.1.0"
54
+ "uuid": "^13.0.0"
55
55
  }
56
56
  }
package/types/index.ts CHANGED
@@ -323,6 +323,8 @@ export type Collection = {
323
323
  })=>Promise<any>;
324
324
  };
325
325
  customApi?: {
326
+ [key:string]:(ctx:ctxApi)=>any;
327
+ } & {
326
328
  aggregate?: (ctx: ctxApi) => Array<object> | null | undefined | typeof fn.error;
327
329
  insertOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
328
330
  insertMany?: (ctx: ctxApi) => Array<any> | null | undefined | typeof fn.error;
@@ -373,7 +375,9 @@ export type Collection = {
373
375
  beforeListActivity?: hooksCtx;
374
376
  afterListActivity?: hooksCtx;
375
377
  };
376
- access?: {
378
+ access?:{
379
+ [key:string]:accessCtx;
380
+ } &{
377
381
  "*"?: accessCtx;
378
382
  beforeAction?: accessCtx;
379
383
  allAction?: accessCtx;
@@ -737,10 +741,7 @@ export type endpointCtx = {
737
741
  serveStatic:typeof serveStatic ;
738
742
  rest: InstanceType<typeof useRest>;
739
743
  io: socketIoType;
740
- router: {
741
- post: (path: string, clb: (c: Context) => any) => {};
742
- get: (path: string, clb: (c: Context) => any) => {};
743
- };
744
+ router: InstanceType<typeof Hono>;
744
745
  }) => any;
745
746
  };
746
747