@nocobase/server 2.0.0-alpha.9 → 2.0.0-beta.2

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.
Files changed (40) hide show
  1. package/lib/acl/available-action.js +1 -1
  2. package/lib/aes-encryptor.js +4 -0
  3. package/lib/app-command.d.ts +1 -0
  4. package/lib/app-command.js +3 -0
  5. package/lib/app-supervisor.d.ts +7 -2
  6. package/lib/app-supervisor.js +54 -15
  7. package/lib/application.d.ts +9 -6
  8. package/lib/application.js +24 -12
  9. package/lib/commands/index.js +2 -0
  10. package/lib/commands/pm.js +11 -0
  11. package/lib/commands/repair.d.ts +11 -0
  12. package/lib/commands/repair.js +43 -0
  13. package/lib/commands/start.js +1 -1
  14. package/lib/event-queue.d.ts +7 -1
  15. package/lib/event-queue.js +23 -21
  16. package/lib/gateway/errors.js +50 -12
  17. package/lib/gateway/index.d.ts +8 -0
  18. package/lib/gateway/index.js +26 -2
  19. package/lib/gateway/ws-server.js +3 -0
  20. package/lib/helper.d.ts +359 -0
  21. package/lib/helper.js +58 -0
  22. package/lib/index.d.ts +2 -1
  23. package/lib/index.js +6 -3
  24. package/lib/locale/locale.js +1 -1
  25. package/lib/locale/resource.js +6 -9
  26. package/lib/main-data-source.d.ts +11 -0
  27. package/lib/main-data-source.js +128 -0
  28. package/lib/middlewares/data-template.js +1 -6
  29. package/lib/middlewares/parse-variables.js +2 -49
  30. package/lib/plugin-manager/deps.js +1 -1
  31. package/lib/plugin-manager/options/resource.js +48 -25
  32. package/lib/plugin-manager/plugin-manager.d.ts +1 -0
  33. package/lib/plugin-manager/plugin-manager.js +36 -1
  34. package/lib/pub-sub-manager/pub-sub-manager.d.ts +1 -1
  35. package/lib/pub-sub-manager/pub-sub-manager.js +14 -20
  36. package/lib/snowflake-id-field.d.ts +2 -1
  37. package/lib/snowflake-id-field.js +2 -2
  38. package/package.json +17 -16
  39. package/lib/background-job-manager.d.ts +0 -40
  40. package/lib/background-job-manager.js +0 -111
@@ -44,6 +44,15 @@ __export(errors_exports, {
44
44
  module.exports = __toCommonJS(errors_exports);
45
45
  var import_app_supervisor = require("../app-supervisor");
46
46
  var import_lodash = __toESM(require("lodash"));
47
+ function getMaintaining(app) {
48
+ var _a;
49
+ return (_a = app == null ? void 0 : app.getMaintaining) == null ? void 0 : _a.call(app);
50
+ }
51
+ __name(getMaintaining, "getMaintaining");
52
+ function getAppName(app) {
53
+ return (app == null ? void 0 : app.name) || "unknown";
54
+ }
55
+ __name(getAppName, "getAppName");
47
56
  const errors = {
48
57
  APP_NOT_FOUND: {
49
58
  status: 404,
@@ -53,6 +62,9 @@ const errors = {
53
62
  APP_ERROR: {
54
63
  status: 503,
55
64
  message: /* @__PURE__ */ __name(({ app }) => {
65
+ if (!(app == null ? void 0 : app.name)) {
66
+ return "";
67
+ }
56
68
  const error = import_app_supervisor.AppSupervisor.getInstance().appErrors[app.name];
57
69
  if (!error) {
58
70
  return "";
@@ -64,25 +76,36 @@ const errors = {
64
76
  return message;
65
77
  }, "message"),
66
78
  code: /* @__PURE__ */ __name(({ app }) => {
79
+ if (!(app == null ? void 0 : app.name)) {
80
+ return "APP_ERROR";
81
+ }
67
82
  const error = import_app_supervisor.AppSupervisor.getInstance().appErrors[app.name];
68
- return error["code"] || "APP_ERROR";
83
+ return (error == null ? void 0 : error["code"]) || "APP_ERROR";
69
84
  }, "code"),
70
- command: /* @__PURE__ */ __name(({ app }) => app.getMaintaining().command, "command"),
85
+ command: /* @__PURE__ */ __name(({ app }) => {
86
+ var _a;
87
+ return (_a = getMaintaining(app)) == null ? void 0 : _a.command;
88
+ }, "command"),
89
+ maintaining: true
90
+ },
91
+ APP_PREPARING: {
92
+ status: 503,
93
+ message: /* @__PURE__ */ __name(({ appName }) => `application ${appName} is preparing, please wait patiently`, "message"),
71
94
  maintaining: true
72
95
  },
73
96
  APP_STARTING: {
74
97
  status: 503,
75
- message: /* @__PURE__ */ __name(({ app }) => app.maintainingMessage, "message"),
98
+ message: /* @__PURE__ */ __name(({ app }) => (app == null ? void 0 : app.maintainingMessage) || "", "message"),
76
99
  maintaining: true
77
100
  },
78
101
  APP_STOPPED: {
79
102
  status: 503,
80
- message: /* @__PURE__ */ __name(({ app }) => `application ${app.name} is stopped`, "message"),
103
+ message: /* @__PURE__ */ __name(({ app }) => `application ${getAppName(app)} is stopped`, "message"),
81
104
  maintaining: true
82
105
  },
83
106
  APP_INITIALIZED: {
84
107
  status: 503,
85
- message: /* @__PURE__ */ __name(({ app }) => `application ${app.name} is initialized, waiting for command`, "message"),
108
+ message: /* @__PURE__ */ __name(({ app }) => `application ${getAppName(app)} is initialized, waiting for command`, "message"),
86
109
  maintaining: true
87
110
  },
88
111
  APP_INITIALIZING: {
@@ -93,25 +116,40 @@ const errors = {
93
116
  COMMAND_ERROR: {
94
117
  status: 503,
95
118
  maintaining: true,
96
- message: /* @__PURE__ */ __name(({ app }) => app.getMaintaining().error.message, "message"),
97
- command: /* @__PURE__ */ __name(({ app }) => app.getMaintaining().command, "command")
119
+ message: /* @__PURE__ */ __name(({ app }) => {
120
+ var _a, _b;
121
+ return ((_b = (_a = getMaintaining(app)) == null ? void 0 : _a.error) == null ? void 0 : _b.message) || "";
122
+ }, "message"),
123
+ command: /* @__PURE__ */ __name(({ app }) => {
124
+ var _a;
125
+ return (_a = getMaintaining(app)) == null ? void 0 : _a.command;
126
+ }, "command")
98
127
  },
99
128
  COMMAND_END: {
100
129
  status: 503,
101
130
  maintaining: true,
102
- message: /* @__PURE__ */ __name(({ app }) => `${app.getMaintaining().command.name} running end`, "message"),
103
- command: /* @__PURE__ */ __name(({ app }) => app.getMaintaining().command, "command")
131
+ message: /* @__PURE__ */ __name(({ app }) => {
132
+ var _a, _b;
133
+ return `${((_b = (_a = getMaintaining(app)) == null ? void 0 : _a.command) == null ? void 0 : _b.name) || "command"} running end`;
134
+ }, "message"),
135
+ command: /* @__PURE__ */ __name(({ app }) => {
136
+ var _a;
137
+ return (_a = getMaintaining(app)) == null ? void 0 : _a.command;
138
+ }, "command")
104
139
  },
105
140
  APP_COMMANDING: {
106
141
  status: 503,
107
142
  maintaining: true,
108
- message: /* @__PURE__ */ __name(({ app, message }) => message || app.maintainingMessage, "message"),
109
- command: /* @__PURE__ */ __name(({ app, command }) => command || app.getMaintaining().command, "command")
143
+ message: /* @__PURE__ */ __name(({ app, message }) => message || (app == null ? void 0 : app.maintainingMessage) || "", "message"),
144
+ command: /* @__PURE__ */ __name(({ app, command }) => {
145
+ var _a;
146
+ return command || ((_a = getMaintaining(app)) == null ? void 0 : _a.command);
147
+ }, "command")
110
148
  },
111
149
  APP_RUNNING: {
112
150
  status: 200,
113
151
  maintaining: false,
114
- message: /* @__PURE__ */ __name(({ message, app }) => message || `application ${app.name} is running`, "message")
152
+ message: /* @__PURE__ */ __name(({ message, app }) => message || `application ${getAppName(app)} is running`, "message")
115
153
  },
116
154
  UNKNOWN_ERROR: {
117
155
  status: 500,
@@ -23,6 +23,12 @@ export interface IncomingRequest {
23
23
  url: string;
24
24
  headers: any;
25
25
  }
26
+ export interface GatewayRequestContext {
27
+ req: IncomingMessage;
28
+ res: ServerResponse;
29
+ appName: string;
30
+ }
31
+ type GatewayMiddleware = (ctx: GatewayRequestContext, next: () => Promise<void>) => Promise<void> | void;
26
32
  export type AppSelector = (req: IncomingRequest) => string | Promise<string>;
27
33
  export type AppSelectorMiddleware = (ctx: AppSelectorMiddlewareContext, next: () => Promise<void>) => void;
28
34
  interface StartHttpServerOptions {
@@ -39,6 +45,7 @@ export interface AppSelectorMiddlewareContext {
39
45
  }
40
46
  export declare class Gateway extends EventEmitter {
41
47
  private static instance;
48
+ middlewares: Toposort<GatewayMiddleware>;
42
49
  /**
43
50
  * use main app as default app to handle request
44
51
  */
@@ -54,6 +61,7 @@ export declare class Gateway extends EventEmitter {
54
61
  private onTerminate;
55
62
  private constructor();
56
63
  static getInstance(options?: any): Gateway;
64
+ use(middleware: GatewayMiddleware, options?: ToposortOptions): void;
57
65
  static getIPCSocketClient(): Promise<false | IPCSocketClient>;
58
66
  destroy(): void;
59
67
  reset(): void;
@@ -75,6 +75,7 @@ function getSocketPath() {
75
75
  }
76
76
  __name(getSocketPath, "getSocketPath");
77
77
  const _Gateway = class _Gateway extends import_events.EventEmitter {
78
+ middlewares;
78
79
  /**
79
80
  * use main app as default app to handle request
80
81
  */
@@ -124,6 +125,9 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
124
125
  }
125
126
  return _Gateway.instance;
126
127
  }
128
+ use(middleware, options) {
129
+ this.middlewares.add(middleware, options);
130
+ }
127
131
  static async getIPCSocketClient() {
128
132
  const socketPath = getSocketPath();
129
133
  try {
@@ -139,6 +143,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
139
143
  _Gateway.instance = null;
140
144
  }
141
145
  reset() {
146
+ this.middlewares = new import_utils.Toposort();
142
147
  this.selectorMiddlewares = new import_utils.Toposort();
143
148
  this.addAppSelectorMiddleware(
144
149
  async (ctx, next) => {
@@ -202,7 +207,15 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
202
207
  res.end(JSON.stringify({ error }));
203
208
  }
204
209
  responseErrorWithCode(code, res, options) {
210
+ const log = this.getLogger(options.appName, res);
205
211
  const error = (0, import_errors.applyErrorWithArgs)((0, import_errors.getErrorWithCode)(code), options);
212
+ log.error(error.message, {
213
+ method: "responseErrorWithCode",
214
+ code,
215
+ error,
216
+ statusCode: res.statusCode,
217
+ appName: options.appName
218
+ });
206
219
  this.responseError(res, error);
207
220
  }
208
221
  async requestHandler(req, res) {
@@ -256,11 +269,15 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
256
269
  if (!hasApp) {
257
270
  void import_app_supervisor.AppSupervisor.getInstance().bootStrapApp(handleApp);
258
271
  }
259
- let appStatus = import_app_supervisor.AppSupervisor.getInstance().getAppStatus(handleApp, "initializing");
272
+ let appStatus = import_app_supervisor.AppSupervisor.getInstance().getAppStatus(handleApp, "preparing");
260
273
  if (appStatus === "not_found") {
261
274
  this.responseErrorWithCode("APP_NOT_FOUND", res, { appName: handleApp });
262
275
  return;
263
276
  }
277
+ if (appStatus === "preparing") {
278
+ this.responseErrorWithCode("APP_PREPARING", res, { appName: handleApp });
279
+ return;
280
+ }
264
281
  if (appStatus === "initializing") {
265
282
  this.responseErrorWithCode("APP_INITIALIZING", res, { appName: handleApp });
266
283
  return;
@@ -283,7 +300,14 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
283
300
  if (handleApp !== "main") {
284
301
  import_app_supervisor.AppSupervisor.getInstance().touchApp(handleApp);
285
302
  }
286
- app.callback()(req, res);
303
+ const ctx = { req, res, appName: handleApp };
304
+ const fn = (0, import_koa_compose.default)([
305
+ ...this.middlewares.nodes,
306
+ async (_ctx) => {
307
+ await app.callback()(req, res);
308
+ }
309
+ ]);
310
+ await fn(ctx);
287
311
  }
288
312
  getAppSelectorMiddlewares() {
289
313
  return this.selectorMiddlewares;
@@ -228,6 +228,9 @@ const _WSServer = class _WSServer extends import_events.default {
228
228
  }
229
229
  removeClientTag(clientId, tagKey) {
230
230
  const client = this.webSocketClients.get(clientId);
231
+ if (!client) {
232
+ return;
233
+ }
231
234
  client.tags.forEach((tag) => {
232
235
  if (tag.startsWith(`${tagKey}#`)) {
233
236
  client.tags.delete(tag);
package/lib/helper.d.ts CHANGED
@@ -17,3 +17,362 @@ export declare const getCommandFullName: (command: Command) => string;
17
17
  export declare const tsxRerunning: () => Promise<void>;
18
18
  export declare const enablePerfHooks: (app: Application) => void;
19
19
  export declare function getBodyLimit(): string;
20
+ export declare function createContextVariablesScope(ctx: any): {
21
+ timezone: any;
22
+ now: string;
23
+ getField: (path: any) => any;
24
+ vars: {
25
+ ctx: {
26
+ state: any;
27
+ };
28
+ $system: {
29
+ now: string;
30
+ };
31
+ $date: {
32
+ now: string;
33
+ today: ({ now, timezone, field }: {
34
+ now?: any;
35
+ timezone?: string | number;
36
+ field?: {
37
+ timezone?: string | number;
38
+ };
39
+ }) => any;
40
+ yesterday: ({ now, timezone, field }: {
41
+ now?: any;
42
+ timezone?: string | number;
43
+ field?: {
44
+ timezone?: string | number;
45
+ };
46
+ }) => any;
47
+ tomorrow: ({ now, timezone, field }: {
48
+ now?: any;
49
+ timezone?: string | number;
50
+ field?: {
51
+ timezone?: string | number;
52
+ };
53
+ }) => any;
54
+ thisWeek: ({ now, timezone, field }: {
55
+ now?: any;
56
+ timezone?: string | number;
57
+ field?: {
58
+ timezone?: string | number;
59
+ };
60
+ }) => any;
61
+ lastWeek: ({ now, timezone, field }: {
62
+ now?: any;
63
+ timezone?: string | number;
64
+ field?: {
65
+ timezone?: string | number;
66
+ };
67
+ }) => any;
68
+ nextWeek: ({ now, timezone, field }: {
69
+ now?: any;
70
+ timezone?: string | number;
71
+ field?: {
72
+ timezone?: string | number;
73
+ };
74
+ }) => any;
75
+ thisIsoWeek: ({ now, timezone, field }: {
76
+ now?: any;
77
+ timezone?: string | number;
78
+ field?: {
79
+ timezone?: string | number;
80
+ };
81
+ }) => any;
82
+ lastIsoWeek: ({ now, timezone, field }: {
83
+ now?: any;
84
+ timezone?: string | number;
85
+ field?: {
86
+ timezone?: string | number;
87
+ };
88
+ }) => any;
89
+ nextIsoWeek: ({ now, timezone, field }: {
90
+ now?: any;
91
+ timezone?: string | number;
92
+ field?: {
93
+ timezone?: string | number;
94
+ };
95
+ }) => any;
96
+ thisMonth: ({ now, timezone, field }: {
97
+ now?: any;
98
+ timezone?: string | number;
99
+ field?: {
100
+ timezone?: string | number;
101
+ };
102
+ }) => any;
103
+ lastMonth: ({ now, timezone, field }: {
104
+ now?: any;
105
+ timezone?: string | number;
106
+ field?: {
107
+ timezone?: string | number;
108
+ };
109
+ }) => any;
110
+ nextMonth: ({ now, timezone, field }: {
111
+ now?: any;
112
+ timezone?: string | number;
113
+ field?: {
114
+ timezone?: string | number;
115
+ };
116
+ }) => any;
117
+ thisQuarter: ({ now, timezone, field }: {
118
+ now?: any;
119
+ timezone?: string | number;
120
+ field?: {
121
+ timezone?: string | number;
122
+ };
123
+ }) => any;
124
+ lastQuarter: ({ now, timezone, field }: {
125
+ now?: any;
126
+ timezone?: string | number;
127
+ field?: {
128
+ timezone?: string | number;
129
+ };
130
+ }) => any;
131
+ nextQuarter: ({ now, timezone, field }: {
132
+ now?: any;
133
+ timezone?: string | number;
134
+ field?: {
135
+ timezone?: string | number;
136
+ };
137
+ }) => any;
138
+ thisYear: ({ now, timezone, field }: {
139
+ now?: any;
140
+ timezone?: string | number;
141
+ field?: {
142
+ timezone?: string | number;
143
+ };
144
+ }) => any;
145
+ lastYear: ({ now, timezone, field }: {
146
+ now?: any;
147
+ timezone?: string | number;
148
+ field?: {
149
+ timezone?: string | number;
150
+ };
151
+ }) => any;
152
+ nextYear: ({ now, timezone, field }: {
153
+ now?: any;
154
+ timezone?: string | number;
155
+ field?: {
156
+ timezone?: string | number;
157
+ };
158
+ }) => any;
159
+ last7Days: ({ now, timezone, field }: {
160
+ now?: any;
161
+ timezone?: string | number;
162
+ field?: {
163
+ timezone?: string | number;
164
+ };
165
+ }) => (string | number)[];
166
+ next7Days: ({ now, timezone, field }: {
167
+ now?: any;
168
+ timezone?: string | number;
169
+ field?: {
170
+ timezone?: string | number;
171
+ };
172
+ }) => (string | number)[];
173
+ last30Days: ({ now, timezone, field }: {
174
+ now?: any;
175
+ timezone?: string | number;
176
+ field?: {
177
+ timezone?: string | number;
178
+ };
179
+ }) => (string | number)[];
180
+ next30Days: ({ now, timezone, field }: {
181
+ now?: any;
182
+ timezone?: string | number;
183
+ field?: {
184
+ timezone?: string | number;
185
+ };
186
+ }) => (string | number)[];
187
+ last90Days: ({ now, timezone, field }: {
188
+ now?: any;
189
+ timezone?: string | number;
190
+ field?: {
191
+ timezone?: string | number;
192
+ };
193
+ }) => (string | number)[];
194
+ next90Days: ({ now, timezone, field }: {
195
+ now?: any;
196
+ timezone?: string | number;
197
+ field?: {
198
+ timezone?: string | number;
199
+ };
200
+ }) => (string | number)[];
201
+ };
202
+ $nDate: {
203
+ now: string;
204
+ today: ({ now, timezone, field }: {
205
+ now?: any;
206
+ timezone?: string | number;
207
+ field?: {
208
+ timezone?: string | number;
209
+ };
210
+ }) => any;
211
+ yesterday: ({ now, timezone, field }: {
212
+ now?: any;
213
+ timezone?: string | number;
214
+ field?: {
215
+ timezone?: string | number;
216
+ };
217
+ }) => any;
218
+ tomorrow: ({ now, timezone, field }: {
219
+ now?: any;
220
+ timezone?: string | number;
221
+ field?: {
222
+ timezone?: string | number;
223
+ };
224
+ }) => any;
225
+ thisWeek: ({ now, timezone, field }: {
226
+ now?: any;
227
+ timezone?: string | number;
228
+ field?: {
229
+ timezone?: string | number;
230
+ };
231
+ }) => any;
232
+ lastWeek: ({ now, timezone, field }: {
233
+ now?: any;
234
+ timezone?: string | number;
235
+ field?: {
236
+ timezone?: string | number;
237
+ };
238
+ }) => any;
239
+ nextWeek: ({ now, timezone, field }: {
240
+ now?: any;
241
+ timezone?: string | number;
242
+ field?: {
243
+ timezone?: string | number;
244
+ };
245
+ }) => any;
246
+ thisIsoWeek: ({ now, timezone, field }: {
247
+ now?: any;
248
+ timezone?: string | number;
249
+ field?: {
250
+ timezone?: string | number;
251
+ };
252
+ }) => any;
253
+ lastIsoWeek: ({ now, timezone, field }: {
254
+ now?: any;
255
+ timezone?: string | number;
256
+ field?: {
257
+ timezone?: string | number;
258
+ };
259
+ }) => any;
260
+ nextIsoWeek: ({ now, timezone, field }: {
261
+ now?: any;
262
+ timezone?: string | number;
263
+ field?: {
264
+ timezone?: string | number;
265
+ };
266
+ }) => any;
267
+ thisMonth: ({ now, timezone, field }: {
268
+ now?: any;
269
+ timezone?: string | number;
270
+ field?: {
271
+ timezone?: string | number;
272
+ };
273
+ }) => any;
274
+ lastMonth: ({ now, timezone, field }: {
275
+ now?: any;
276
+ timezone?: string | number;
277
+ field?: {
278
+ timezone?: string | number;
279
+ };
280
+ }) => any;
281
+ nextMonth: ({ now, timezone, field }: {
282
+ now?: any;
283
+ timezone?: string | number;
284
+ field?: {
285
+ timezone?: string | number;
286
+ };
287
+ }) => any;
288
+ thisQuarter: ({ now, timezone, field }: {
289
+ now?: any;
290
+ timezone?: string | number;
291
+ field?: {
292
+ timezone?: string | number;
293
+ };
294
+ }) => any;
295
+ lastQuarter: ({ now, timezone, field }: {
296
+ now?: any;
297
+ timezone?: string | number;
298
+ field?: {
299
+ timezone?: string | number;
300
+ };
301
+ }) => any;
302
+ nextQuarter: ({ now, timezone, field }: {
303
+ now?: any;
304
+ timezone?: string | number;
305
+ field?: {
306
+ timezone?: string | number;
307
+ };
308
+ }) => any;
309
+ thisYear: ({ now, timezone, field }: {
310
+ now?: any;
311
+ timezone?: string | number;
312
+ field?: {
313
+ timezone?: string | number;
314
+ };
315
+ }) => any;
316
+ lastYear: ({ now, timezone, field }: {
317
+ now?: any;
318
+ timezone?: string | number;
319
+ field?: {
320
+ timezone?: string | number;
321
+ };
322
+ }) => any;
323
+ nextYear: ({ now, timezone, field }: {
324
+ now?: any;
325
+ timezone?: string | number;
326
+ field?: {
327
+ timezone?: string | number;
328
+ };
329
+ }) => any;
330
+ last7Days: ({ now, timezone, field }: {
331
+ now?: any;
332
+ timezone?: string | number;
333
+ field?: {
334
+ timezone?: string | number;
335
+ };
336
+ }) => (string | number)[];
337
+ next7Days: ({ now, timezone, field }: {
338
+ now?: any;
339
+ timezone?: string | number;
340
+ field?: {
341
+ timezone?: string | number;
342
+ };
343
+ }) => (string | number)[];
344
+ last30Days: ({ now, timezone, field }: {
345
+ now?: any;
346
+ timezone?: string | number;
347
+ field?: {
348
+ timezone?: string | number;
349
+ };
350
+ }) => (string | number)[];
351
+ next30Days: ({ now, timezone, field }: {
352
+ now?: any;
353
+ timezone?: string | number;
354
+ field?: {
355
+ timezone?: string | number;
356
+ };
357
+ }) => (string | number)[];
358
+ last90Days: ({ now, timezone, field }: {
359
+ now?: any;
360
+ timezone?: string | number;
361
+ field?: {
362
+ timezone?: string | number;
363
+ };
364
+ }) => (string | number)[];
365
+ next90Days: ({ now, timezone, field }: {
366
+ now?: any;
367
+ timezone?: string | number;
368
+ field?: {
369
+ timezone?: string | number;
370
+ };
371
+ }) => (string | number)[];
372
+ };
373
+ $user: ({ fields }: {
374
+ fields: any;
375
+ }) => Promise<any>;
376
+ $nRole: any;
377
+ };
378
+ };
package/lib/helper.js CHANGED
@@ -38,6 +38,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
38
38
  var helper_exports = {};
39
39
  __export(helper_exports, {
40
40
  createAppProxy: () => createAppProxy,
41
+ createContextVariablesScope: () => createContextVariablesScope,
41
42
  createI18n: () => createI18n,
42
43
  createResourcer: () => createResourcer,
43
44
  enablePerfHooks: () => enablePerfHooks,
@@ -193,9 +194,66 @@ function getBodyLimit() {
193
194
  return process.env.REQUEST_BODY_LIMIT || "10mb";
194
195
  }
195
196
  __name(getBodyLimit, "getBodyLimit");
197
+ function getUser(ctx) {
198
+ return async ({ fields }) => {
199
+ var _a, _b;
200
+ const userFields = fields.filter((f) => f && ctx.db.getFieldByPath("users." + f));
201
+ (_a = ctx.logger) == null ? void 0 : _a.info("filter-parse: ", { userFields });
202
+ if (!ctx.state.currentUser) {
203
+ return;
204
+ }
205
+ if (!userFields.length) {
206
+ return;
207
+ }
208
+ const user = await ctx.db.getRepository("users").findOne({
209
+ filterByTk: ctx.state.currentUser.id,
210
+ fields: userFields
211
+ });
212
+ (_b = ctx.logger) == null ? void 0 : _b.info("filter-parse: ", {
213
+ $user: user == null ? void 0 : user.toJSON()
214
+ });
215
+ return user;
216
+ };
217
+ }
218
+ __name(getUser, "getUser");
219
+ function isNumeric(str) {
220
+ if (typeof str === "number") return true;
221
+ if (typeof str != "string") return false;
222
+ return !isNaN(str) && !isNaN(parseFloat(str));
223
+ }
224
+ __name(isNumeric, "isNumeric");
225
+ function createContextVariablesScope(ctx) {
226
+ const state = JSON.parse(JSON.stringify(ctx.state));
227
+ return {
228
+ timezone: ctx.get("x-timezone"),
229
+ now: (/* @__PURE__ */ new Date()).toISOString(),
230
+ getField: /* @__PURE__ */ __name((path) => {
231
+ const fieldPath = path.split(".").filter((p) => !p.startsWith("$") && !isNumeric(p)).join(".");
232
+ const { resourceName } = ctx.action;
233
+ return ctx.database.getFieldByPath(`${resourceName}.${fieldPath}`);
234
+ }, "getField"),
235
+ vars: {
236
+ ctx: {
237
+ state
238
+ },
239
+ // @deprecated
240
+ $system: {
241
+ now: (/* @__PURE__ */ new Date()).toISOString()
242
+ },
243
+ // @deprecated
244
+ $date: (0, import_utils.getDateVars)(),
245
+ // 新的命名方式,防止和 formily 内置变量冲突
246
+ $nDate: (0, import_utils.getDateVars)(),
247
+ $user: getUser(ctx),
248
+ $nRole: ctx.state.currentRole === "__union__" ? ctx.state.currentRoles : ctx.state.currentRole
249
+ }
250
+ };
251
+ }
252
+ __name(createContextVariablesScope, "createContextVariablesScope");
196
253
  // Annotate the CommonJS export names for ESM import in node:
197
254
  0 && (module.exports = {
198
255
  createAppProxy,
256
+ createContextVariablesScope,
199
257
  createI18n,
200
258
  createResourcer,
201
259
  enablePerfHooks,
package/lib/index.d.ts CHANGED
@@ -19,9 +19,10 @@ export * from './plugin';
19
19
  export * from './plugin-manager';
20
20
  export * from './pub-sub-manager';
21
21
  export * from './event-queue';
22
- export * from './background-job-manager';
23
22
  export * from './worker-id-allocator';
24
23
  export * from './redis-connection-manager';
24
+ export * from './main-data-source';
25
25
  export declare const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
26
26
  export { appendToBuiltInPlugins, findAllPlugins, findBuiltInPlugins, findLocalPlugins, packageNameTrim, } from './plugin-manager/findPackageNames';
27
27
  export { runPluginStaticImports } from './run-plugin-static-imports';
28
+ export { createContextVariablesScope } from './helper';