@dnax/core 0.36.0 → 0.38.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
@@ -131,6 +131,7 @@ function HonoInstance(): typeof app {
131
131
  state: sessionData?.state || {},
132
132
  role: sessionData?.role || null,
133
133
  _v: { ...(_v || {}) },
134
+ isAuth: valid ? true : false,
134
135
  });
135
136
  } else {
136
137
  // no valid token
@@ -139,6 +140,7 @@ function HonoInstance(): typeof app {
139
140
  state: {},
140
141
  role: undefined,
141
142
  _v: { ...(_v || {}) },
143
+ isAuth: false,
142
144
  });
143
145
  }
144
146
 
@@ -564,33 +566,95 @@ function HonoInstance(): typeof app {
564
566
  }
565
567
  }
566
568
 
569
+ // if select is array
567
570
  if (
568
- col?.api?.fields?.select?.length &&
571
+ col?.api?.fields?.select &&
572
+ typeof col?.api?.fields?.select == "object" &&
569
573
  Array.isArray(col?.api?.fields?.select)
570
574
  ) {
575
+ const selectedFields = [];
571
576
  if (body?.withMeta) {
572
- try {
573
- response.data = pick(response.data, col?.api?.fields?.select || []);
574
- } catch (err) {}
577
+ response.data = pick(response.data, col?.api?.fields?.select);
575
578
  } else {
576
- response = pick(response, col?.api?.fields?.select || []);
579
+ response = pick(response, col?.api?.fields?.select);
577
580
  }
578
581
  }
582
+ // if select is function
583
+ if (
584
+ col?.api?.fields?.select &&
585
+ typeof col?.api?.fields?.select == "function"
586
+ ) {
587
+ let selectedFields = [];
588
+ if (body?.withMeta) {
589
+ selectedFields = await col?.api?.fields?.select({
590
+ action: action,
591
+ c: c,
592
+ rest: rest,
593
+ session: sessionStorage() as any,
594
+ });
595
+ selectedFields = [...new Set(selectedFields)];
596
+ response.data = pick(response.data, selectedFields);
597
+ } else {
598
+ selectedFields = await col?.api?.fields?.select({
599
+ action: action,
600
+ c: c,
601
+ rest: rest,
602
+ session: sessionStorage() as any,
603
+ });
604
+ selectedFields = [...new Set(selectedFields)];
605
+ response = pick(response, selectedFields);
606
+ }
607
+ }
608
+
609
+ // hidden is array
579
610
  if (
580
611
  col?.api?.fields?.hidden?.length &&
581
612
  Array.isArray(col?.api?.fields?.hidden)
582
613
  ) {
583
614
  let privateFields = col?.api?.fields?.hidden || [];
584
615
  privateFields.push("password");
616
+ privateFields = [...new Set(privateFields)];
585
617
  if (body?.withMeta) {
586
618
  // If use Meta are included
587
619
  try {
588
620
  response.data = omit(response.data, privateFields);
589
621
  } catch (e) {}
622
+ } else {
623
+ response = omit(response, privateFields);
624
+ }
625
+ }
626
+
627
+ // hidden is function
628
+ if (
629
+ col?.api?.fields?.hidden?.length &&
630
+ typeof col?.api?.fields?.hidden == "function"
631
+ ) {
632
+ let privateFields = [];
633
+
634
+ if (body?.withMeta) {
635
+ // If use Meta are included
636
+ try {
637
+ privateFields = await col?.api?.fields?.hidden({
638
+ action: action,
639
+ c: c,
640
+ rest: rest,
641
+ session: sessionStorage() as any,
642
+ });
643
+ privateFields.push("password");
644
+ privateFields = [...new Set(privateFields)];
645
+ response.data = omit(response.data, privateFields);
646
+ } catch (e) {}
647
+ } else {
648
+ privateFields = await col?.api?.fields?.hidden({
649
+ action: action,
650
+ c: c,
651
+ rest: rest,
652
+ session: sessionStorage() as any,
653
+ });
654
+ privateFields.push("password");
655
+ privateFields = [...new Set(privateFields)];
656
+ response = omit(response, privateFields);
590
657
  }
591
- response = omit(response, privateFields);
592
- } else {
593
- response = omit(response, ["password"]);
594
658
  }
595
659
 
596
660
  return c.json(response);
@@ -22,9 +22,14 @@ function isSetAsyncLocalStorage() {
22
22
  const sessionStorage = () => ({
23
23
  get(): {
24
24
  state: object;
25
- _v: object;
25
+ _v: {
26
+ isAuth: boolean;
27
+ reqAt?: string;
28
+ setAt?: string;
29
+ };
26
30
  role: string | null | undefined;
27
31
  token?: string;
32
+ isAuth?: boolean;
28
33
  } {
29
34
  if (!isSetAsyncLocalStorage()) return { state: {}, _v: {}, role: null };
30
35
  let store = asyncLocalStorage?.getStore() as InstanceType<typeof Map>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.36.0",
3
+ "version": "0.38.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -51,6 +51,7 @@ export type Actions =
51
51
  | "updateMany"
52
52
  | "deleteOne"
53
53
  | "deleteMany"
54
+ | "authCollection"
54
55
  | "aggregate";
55
56
 
56
57
  export type Field = {
@@ -170,18 +171,30 @@ export type sessionCtx = {
170
171
  user?: object;
171
172
  };
172
173
  token?: string;
173
- _v?: object;
174
+ _v?: {
175
+ ip: string;
176
+ isAuth: boolean;
177
+ reqAt?: string;
178
+ setAt?: string;
179
+ };
174
180
  role?: string | null | undefined;
175
181
  expiresIn?: string;
182
+ isAuth?: boolean;
176
183
  }) => void;
177
184
  get: () => {
178
185
  state: {
179
186
  [key: string]: any;
180
187
  user?: object;
181
188
  };
182
- _v: object;
189
+ _v: {
190
+ ip: string;
191
+ isAuth: boolean;
192
+ reqAt?: string;
193
+ setAt?: string;
194
+ };
183
195
  role: string | null | undefined;
184
196
  token: string | null | undefined;
197
+ isAuth: boolean;
185
198
  };
186
199
  };
187
200
 
@@ -352,8 +365,22 @@ export type Collection = {
352
365
  api?: {
353
366
  //privateFields?: string[];
354
367
  fields: {
355
- select?: string[];
356
- hidden?: string[];
368
+ select?:
369
+ | string[]
370
+ | ((ctx: {
371
+ action: Actions;
372
+ c: Context;
373
+ rest: InstanceType<typeof useRest>;
374
+ session: sessionCtx;
375
+ }) => string[]);
376
+ hidden?:
377
+ | string[]
378
+ | ((ctx: {
379
+ action: Actions;
380
+ c: Context;
381
+ rest: InstanceType<typeof useRest>;
382
+ session: sessionCtx;
383
+ }) => string[]);
357
384
  };
358
385
  };
359
386
  allow?: ["dropCollection", "renameCollection"];