@dnax/core 0.74.9 → 0.75.1

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,9 +319,13 @@ 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
- throw new ContextError(`Action ${action} not found`, 400);
322
+
323
+ // Controlle des actions à mener
324
+ if (!getAction(action) && !col?.actions?.hasOwnProperty(action)) {
325
+ throw new ContextError(
326
+ `Action ${action} not found on ${collection}`,
327
+ 400
328
+ );
323
329
  }
324
330
 
325
331
  useCache = stringToBoolean(useCache, false);
@@ -330,12 +336,6 @@ function HonoInstance(): typeof app {
330
336
  c: c,
331
337
  });
332
338
 
333
- if (action == "execToolkit") {
334
- let tookit = toolkit;
335
-
336
- return;
337
- }
338
-
339
339
  if (action == "execService") {
340
340
  // Exec service
341
341
  if (!service) return fn.error("Service not found", 404);
@@ -357,7 +357,7 @@ function HonoInstance(): typeof app {
357
357
  return c.json(response);
358
358
  }
359
359
 
360
- if (!getAction(action) || !collection) {
360
+ if (!collection) {
361
361
  throw new ContextError(`Collection ${collection} not found`, 404);
362
362
  }
363
363
 
@@ -660,6 +660,10 @@ function HonoInstance(): typeof app {
660
660
  response = await rest.insertOne(collection, body?.data || {});
661
661
  }
662
662
 
663
+ if (!getAction(action) && col?.actions?.hasOwnProperty(action)) {
664
+ response = await rest.runAction(collection, action, body?.data || {});
665
+ }
666
+
663
667
  if (action == "insertMany") {
664
668
  response = await rest.insertMany(collection, body?.data || {});
665
669
  }
@@ -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,43 @@ 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 runAction(collection: string, actionName: string, data: any) {
2359
+ let action = actionName;
2360
+ return new Promise(async (resolve, reject) => {
2361
+ try {
2362
+ let col = getCollection(collection, this.#tenant_id);
2363
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
2364
+ if (col?.customApi?.[action]) {
2365
+ let result = await col?.customApi?.[action]({
2366
+ error: fn.error,
2367
+ io: Cfg.io,
2368
+ session: sessionStorage(),
2369
+ data: toJson(data),
2370
+ rest: new useRest({
2371
+ session: this.#session,
2372
+ useHook: false,
2373
+ tenant_id: this.#tenant_id,
2374
+ useCustomApi: false,
2375
+ }),
2376
+ });
2377
+ resolve(result);
2378
+ } else {
2379
+ return fn.error(`Action ${action} not found`, 404);
2380
+ }
2381
+ } catch (err) {
2382
+ return reject(err);
2383
+ }
2384
+ });
2385
+ }
2386
+
2349
2387
  async deleteMany(
2350
2388
  collection: string,
2351
2389
  ids: Array<string>,
@@ -2359,6 +2397,7 @@ class useRest {
2359
2397
 
2360
2398
  let sharedData = {};
2361
2399
  let col = getCollection(collection, this.#tenant_id);
2400
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
2362
2401
  if (col?.hooks?.beforeOperation && useHook) {
2363
2402
  await col.hooks.beforeOperation({
2364
2403
  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.1",
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
@@ -322,7 +322,12 @@ export type Collection = {
322
322
  operationType:"insert"|"update"|"delete";
323
323
  })=>Promise<any>;
324
324
  };
325
+ actions?:{
326
+ [key:string]:(ctx:ctxApi)=>any;
327
+ },
325
328
  customApi?: {
329
+ [key:string]:(ctx:ctxApi)=>any;
330
+ } & {
326
331
  aggregate?: (ctx: ctxApi) => Array<object> | null | undefined | typeof fn.error;
327
332
  insertOne?: (ctx: ctxApi) => object | null | undefined | typeof fn.error;
328
333
  insertMany?: (ctx: ctxApi) => Array<any> | null | undefined | typeof fn.error;
@@ -373,7 +378,9 @@ export type Collection = {
373
378
  beforeListActivity?: hooksCtx;
374
379
  afterListActivity?: hooksCtx;
375
380
  };
376
- access?: {
381
+ access?:{
382
+ [key:string]:accessCtx;
383
+ } &{
377
384
  "*"?: accessCtx;
378
385
  beforeAction?: accessCtx;
379
386
  allAction?: accessCtx;
@@ -737,10 +744,7 @@ export type endpointCtx = {
737
744
  serveStatic:typeof serveStatic ;
738
745
  rest: InstanceType<typeof useRest>;
739
746
  io: socketIoType;
740
- router: {
741
- post: (path: string, clb: (c: Context) => any) => {};
742
- get: (path: string, clb: (c: Context) => any) => {};
743
- };
747
+ router: InstanceType<typeof Hono>;
744
748
  }) => any;
745
749
  };
746
750