@dnax/core 0.2.3 → 0.2.5
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 +14 -0
- package/bin/index.ts +3 -2
- package/define/index.ts +14 -1
- package/driver/mongo/rest.ts +4 -0
- package/driver/mongo/utils.ts +4 -4
- package/lib/schema.ts +2 -2
- package/package.json +2 -2
- package/types/index.ts +16 -3
package/app/hono.ts
CHANGED
|
@@ -126,6 +126,18 @@ function HonoInstance(): typeof app {
|
|
|
126
126
|
let colAccess = col?.access?.hasOwnProperty(action) || null;
|
|
127
127
|
let nextLifecyle: any = false;
|
|
128
128
|
|
|
129
|
+
// Middleware for apis
|
|
130
|
+
for await (let midd of col?.middlewares!) {
|
|
131
|
+
await midd({
|
|
132
|
+
token: c.var["token"] || null,
|
|
133
|
+
action: action,
|
|
134
|
+
c: c,
|
|
135
|
+
isAuth: c.var?._v?.isAuth || false,
|
|
136
|
+
rest: new useRest({ tenant_id: tenant_id }),
|
|
137
|
+
session: session as any,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
129
141
|
if (col && col?.access?.beforeAction) {
|
|
130
142
|
await col?.access?.beforeAction({
|
|
131
143
|
token: c.var["token"] || null,
|
|
@@ -217,6 +229,7 @@ function HonoInstance(): typeof app {
|
|
|
217
229
|
if (action == "execService") {
|
|
218
230
|
if (!service) return fn.error("Service not found", 404);
|
|
219
231
|
response = await service.fx({
|
|
232
|
+
io: Cfg.io,
|
|
220
233
|
data: body?.data || {},
|
|
221
234
|
rest: rest,
|
|
222
235
|
error: fn.error,
|
|
@@ -243,6 +256,7 @@ function HonoInstance(): typeof app {
|
|
|
243
256
|
) {
|
|
244
257
|
if (action == "authCollection") {
|
|
245
258
|
let reponse;
|
|
259
|
+
|
|
246
260
|
if (!col?.auth?.enabled) fn.error("Auth not enabled", 401);
|
|
247
261
|
if (!col?.auth?.handler) fn.error("Auth handler not set", 401);
|
|
248
262
|
if (col?.auth?.handler) {
|
package/bin/index.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import ch from "chokidar";
|
|
4
4
|
import { spinner } from "@clack/prompts";
|
|
5
|
+
import "@colors/colors";
|
|
5
6
|
var exec;
|
|
6
7
|
var s = spinner();
|
|
7
8
|
function runPkg(
|
|
@@ -52,7 +53,7 @@ ch.watch(".", {
|
|
|
52
53
|
],
|
|
53
54
|
})
|
|
54
55
|
.on("change", (path) => {
|
|
55
|
-
console.log(path);
|
|
56
|
+
console.log("Changed file:".gray, path.green);
|
|
56
57
|
let dev = process.argv.includes("--dev");
|
|
57
58
|
if (dev && (path?.endsWith(".ts") || path?.endsWith(".js"))) {
|
|
58
59
|
s.start("Restarting due to changes ...");
|
|
@@ -61,7 +62,7 @@ ch.watch(".", {
|
|
|
61
62
|
}
|
|
62
63
|
setTimeout(() => {
|
|
63
64
|
runPkg({ stopSpinner: true });
|
|
64
|
-
},
|
|
65
|
+
}, 1000);
|
|
65
66
|
}
|
|
66
67
|
})
|
|
67
68
|
.on("ready", () => {
|
package/define/index.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Collection,
|
|
3
|
+
Config,
|
|
4
|
+
Endpoint,
|
|
5
|
+
Service,
|
|
6
|
+
Socket,
|
|
7
|
+
middlewareCtx,
|
|
8
|
+
} from "../types";
|
|
2
9
|
import { Cfg } from "../config/";
|
|
3
10
|
import { deepMerge, freeze } from "../utils";
|
|
4
11
|
import { config } from "valibot";
|
|
@@ -23,6 +30,11 @@ function WebSocket(config: Socket) {
|
|
|
23
30
|
return config;
|
|
24
31
|
}
|
|
25
32
|
|
|
33
|
+
function Middleware(ctx: middlewareCtx) {
|
|
34
|
+
console.log(ctx);
|
|
35
|
+
return ctx;
|
|
36
|
+
}
|
|
37
|
+
|
|
26
38
|
const define = {
|
|
27
39
|
Service,
|
|
28
40
|
Config,
|
|
@@ -30,6 +42,7 @@ const define = {
|
|
|
30
42
|
Endpoint,
|
|
31
43
|
App: Config,
|
|
32
44
|
WebSocket,
|
|
45
|
+
Middleware,
|
|
33
46
|
};
|
|
34
47
|
|
|
35
48
|
export default define;
|
package/driver/mongo/rest.ts
CHANGED
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
setUUID,
|
|
28
28
|
} from "./utils";
|
|
29
29
|
import { Cfg } from "../../config";
|
|
30
|
+
import cleanDeep from "clean-deep";
|
|
30
31
|
|
|
31
32
|
type options = {
|
|
32
33
|
tenant_id: string;
|
|
@@ -420,6 +421,9 @@ class useRest {
|
|
|
420
421
|
): Promise<object[]> {
|
|
421
422
|
return new Promise(async (resolve, reject) => {
|
|
422
423
|
try {
|
|
424
|
+
if (options?.cleanDeep) {
|
|
425
|
+
params = cleanDeep(params);
|
|
426
|
+
}
|
|
423
427
|
let useHook = options?.useHook ?? this.#useHook;
|
|
424
428
|
let useCustomApi = options?.useCustomApi ?? this.#useCustomApi;
|
|
425
429
|
let sharedData = {};
|
package/driver/mongo/utils.ts
CHANGED
|
@@ -60,13 +60,13 @@ function buildPipeline(params: findParam, col?: Collection | undefined | null) {
|
|
|
60
60
|
if (field) {
|
|
61
61
|
pipeline.push({
|
|
62
62
|
$lookup: {
|
|
63
|
-
from: field.
|
|
63
|
+
from: field.relation?.to,
|
|
64
64
|
localField: inc,
|
|
65
65
|
foreignField: "_id",
|
|
66
66
|
as: inc,
|
|
67
67
|
},
|
|
68
68
|
});
|
|
69
|
-
if (field.
|
|
69
|
+
if (!field.relation?.hasMany) {
|
|
70
70
|
pipeline.push({
|
|
71
71
|
$unwind: {
|
|
72
72
|
path: `$${inc}`,
|
|
@@ -81,7 +81,7 @@ function buildPipeline(params: findParam, col?: Collection | undefined | null) {
|
|
|
81
81
|
let field = getFieldCollection(inc?.localField, col);
|
|
82
82
|
pipeline.push({
|
|
83
83
|
$lookup: {
|
|
84
|
-
from: inc?.from || field?.
|
|
84
|
+
from: inc?.from || field?.relation?.to,
|
|
85
85
|
localField: inc?.localField || field?.name,
|
|
86
86
|
foreignField: inc?.foreignField || "_id",
|
|
87
87
|
as: inc?.as || inc?.localField || field?.name,
|
|
@@ -89,7 +89,7 @@ function buildPipeline(params: findParam, col?: Collection | undefined | null) {
|
|
|
89
89
|
},
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
if (field?.
|
|
92
|
+
if (!field?.relation?.hasMany) {
|
|
93
93
|
pipeline.push({
|
|
94
94
|
$unwind: {
|
|
95
95
|
path: `$${inc?.as || inc?.localField || field?.name}`,
|
package/lib/schema.ts
CHANGED
|
@@ -49,11 +49,11 @@ function buildSchema(col: Collection) {
|
|
|
49
49
|
propertySchema[f.name] = v.string().email();
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
if (f?.type == "relationship"
|
|
52
|
+
if (f?.type == "relationship") {
|
|
53
53
|
propertySchema[f.name] = v.string().optional();
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
if (f?.type == "relationship" && f.
|
|
56
|
+
if (f?.type == "relationship" && f.relation?.hasMany) {
|
|
57
57
|
propertySchema[f.name] = v.array().optional();
|
|
58
58
|
}
|
|
59
59
|
|
package/package.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -102,9 +102,12 @@ export type Field = {
|
|
|
102
102
|
unique?: boolean;
|
|
103
103
|
index?: boolean | "text" | "2dsphere";
|
|
104
104
|
description?: string;
|
|
105
|
-
|
|
105
|
+
relation?: {
|
|
106
|
+
to: string; // collection name
|
|
107
|
+
hasMany?: boolean;
|
|
108
|
+
};
|
|
106
109
|
sparse?: boolean;
|
|
107
|
-
relationType?: "ref-to-one" | "ref-to-many";
|
|
110
|
+
// relationType?: "ref-to-one" | "ref-to-many";
|
|
108
111
|
/**
|
|
109
112
|
* Overwrite custom validation schema
|
|
110
113
|
* use {v} from "@dnax/core"} to build your own validation
|
|
@@ -116,6 +119,15 @@ export type Field = {
|
|
|
116
119
|
relationTo?: string;
|
|
117
120
|
};
|
|
118
121
|
|
|
122
|
+
export type middlewareCtx = (ctx: {
|
|
123
|
+
token?: string;
|
|
124
|
+
action?: Actions;
|
|
125
|
+
c?: Context;
|
|
126
|
+
isAuth: boolean;
|
|
127
|
+
rest: InstanceType<typeof useRest>;
|
|
128
|
+
session: sessionCtx;
|
|
129
|
+
}) => boolean | undefined | void | Promise<any>;
|
|
130
|
+
|
|
119
131
|
export type accessCtx = (ctx: {
|
|
120
132
|
token?: string;
|
|
121
133
|
action?: Actions;
|
|
@@ -123,7 +135,7 @@ export type accessCtx = (ctx: {
|
|
|
123
135
|
isAuth: boolean;
|
|
124
136
|
rest: InstanceType<typeof useRest>;
|
|
125
137
|
session: sessionCtx;
|
|
126
|
-
}) => boolean | undefined | void
|
|
138
|
+
}) => boolean | undefined | void | Promise<any>;
|
|
127
139
|
|
|
128
140
|
export type sessionCtx = {
|
|
129
141
|
set: (session: { state: object | any; token?: string; _v?: object }) => void;
|
|
@@ -209,6 +221,7 @@ export type Collection = {
|
|
|
209
221
|
session: sessionCtx;
|
|
210
222
|
}) => boolean | any;
|
|
211
223
|
};
|
|
224
|
+
middlewares?: Array<middlewareCtx>;
|
|
212
225
|
hooks?: {
|
|
213
226
|
beforeOperation?: hooksCtx;
|
|
214
227
|
beforeFind?: hooksCtx;
|