@xrystal/core 3.14.7 → 3.14.9

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.14.7",
4
+ "version": "3.14.9",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -1,11 +1,13 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { ProtocolEnum } from '../../utils/index';
3
3
  export declare const controllerContextStorage: AsyncLocalStorage<{
4
+ protocol?: ProtocolEnum;
4
5
  ctx?: any;
5
6
  req?: any;
6
7
  res?: any;
7
8
  }>;
8
9
  export declare const getControllerCtx: () => {
10
+ protocol?: ProtocolEnum;
9
11
  ctx?: any;
10
12
  req?: any;
11
13
  res?: any;
@@ -28,21 +30,23 @@ export interface CustomResponse {
28
30
  }
29
31
  declare abstract class Controller {
30
32
  protected loggerService: any;
33
+ protected supportedProtocols: ProtocolEnum[];
31
34
  constructor({ loggerService }: any);
32
35
  protected get currentStore(): {
36
+ protocol?: ProtocolEnum;
33
37
  ctx?: any;
34
38
  req?: any;
35
39
  res?: any;
36
40
  };
41
+ protected get protocol(): ProtocolEnum;
37
42
  protected get req(): CustomRequest;
38
43
  protected get res(): CustomResponse;
39
44
  protected responseProtocolSwitch: ({ res, resStatus, context, req }: any) => Promise<any>;
40
45
  protected parsedQuerys: (url: string) => Record<string, any>;
41
46
  }
42
47
  export declare abstract class ControllerService extends Controller {
43
- protected controllerType: ProtocolEnum;
44
48
  load(props?: {
45
- type?: ProtocolEnum;
49
+ type?: ProtocolEnum | ProtocolEnum[];
46
50
  }): Promise<void>;
47
51
  schema({ checks, logic, response }: {
48
52
  checks?: (args: any) => Promise<any>;
@@ -50,4 +54,4 @@ export declare abstract class ControllerService extends Controller {
50
54
  response?: (args: any) => Promise<any>;
51
55
  }): Promise<any>;
52
56
  }
53
- export {};
57
+ export { Controller };
@@ -5,14 +5,23 @@ export const controllerContextStorage = new AsyncLocalStorage();
5
5
  export const getControllerCtx = () => controllerContextStorage.getStore();
6
6
  class Controller {
7
7
  loggerService;
8
+ supportedProtocols = [ProtocolEnum.HTTP];
8
9
  constructor({ loggerService }) {
9
10
  this.loggerService = loggerService;
10
11
  }
11
12
  get currentStore() {
12
13
  return getControllerCtx();
13
14
  }
15
+ get protocol() {
16
+ const incomingProtocol = this.currentStore?.protocol || ProtocolEnum.HTTP;
17
+ if (!this.supportedProtocols.includes(incomingProtocol)) {
18
+ throw new Error(`[DI] Protocol ${incomingProtocol} is not supported by ${this.constructor.name}. Supported: ${this.supportedProtocols.join(', ')}`);
19
+ }
20
+ return incomingProtocol;
21
+ }
14
22
  get req() {
15
23
  const store = this.currentStore;
24
+ const currentProtocol = this.protocol;
16
25
  if (!store)
17
26
  return {};
18
27
  if (store.ctx) {
@@ -41,10 +50,10 @@ class Controller {
41
50
  }
42
51
  get res() {
43
52
  const store = this.currentStore;
53
+ const currentProtocol = this.protocol;
44
54
  if (!store)
45
55
  return {};
46
56
  if (store.ctx) {
47
- const protocol = this.controllerType || ProtocolEnum.HTTP;
48
57
  return {
49
58
  locals: {},
50
59
  status(code) {
@@ -52,7 +61,7 @@ class Controller {
52
61
  return this;
53
62
  },
54
63
  send(data) {
55
- if (protocol === ProtocolEnum.WEBSOCKET)
64
+ if (currentProtocol === ProtocolEnum.WEBSOCKET)
56
65
  return data;
57
66
  return new Response(JSON.stringify(data), {
58
67
  status: this.locals._code || 200,
@@ -91,19 +100,19 @@ class Controller {
91
100
  };
92
101
  }
93
102
  export class ControllerService extends Controller {
94
- controllerType = ProtocolEnum.HTTP;
95
103
  async load(props = {}) {
96
- if (props.type)
97
- this.controllerType = props.type;
104
+ if (props.type) {
105
+ this.supportedProtocols = Array.isArray(props.type) ? props.type : [props.type];
106
+ }
98
107
  }
99
108
  async schema({ checks, logic, response }) {
100
- const currentReq = this.req;
101
- const currentRes = this.res;
102
- if (!currentReq || !currentRes)
103
- return;
104
- const payload = { req: currentReq, res: currentRes };
105
- const convertedPayload = { ...payload, parsedQuerys: this.parsedQuerys(currentReq.url) };
106
109
  try {
110
+ const currentReq = this.req;
111
+ const currentRes = this.res;
112
+ if (!currentReq || !currentRes)
113
+ return;
114
+ const payload = { req: currentReq, res: currentRes };
115
+ const convertedPayload = { ...payload, parsedQuerys: this.parsedQuerys(currentReq.url) };
107
116
  if (checks) {
108
117
  const checkResult = await checks({ payload, convertedPayload });
109
118
  if (checkResult?.message) {
@@ -140,7 +149,14 @@ export class ControllerService extends Controller {
140
149
  }
141
150
  }
142
151
  catch (error) {
143
- return currentRes.status(500).send({ status: false, message: error.message });
152
+ const currentRes = this.currentStore?.res || this.currentStore?.ctx?.set;
153
+ if (currentRes) {
154
+ return typeof currentRes.status === 'function'
155
+ ? currentRes.status(500).send({ status: false, message: error.message })
156
+ : { status: false, message: error.message };
157
+ }
158
+ return { status: false, message: error.message };
144
159
  }
145
160
  }
146
161
  }
162
+ export { Controller };
@@ -1,7 +1,7 @@
1
1
  // => import dependencies
2
2
  import path from 'path';
3
3
  import { SystemService, ConfigsService, LoggerService, EventsService, LocalizationsService, ClientsService, ControllerService } from '../loader/index';
4
- import { packageName, x, kafkaBrokers, systemLoggerLayer, getTmp, ProtocolEnum, } from '../utils/index';
4
+ import { packageName, x, kafkaBrokers, systemLoggerLayer, getTmp, } from '../utils/index';
5
5
  //
6
6
  let coreHasRun = false;
7
7
  export const core = getTmp();
@@ -59,7 +59,7 @@ const coreLoader = async ({}) => {
59
59
  {
60
60
  service: ControllerService,
61
61
  props: {
62
- type: ProtocolEnum.HTTP
62
+ type: configs.loaders.controller.protocol?.[0],
63
63
  }
64
64
  },
65
65
  {
package/x/tmp.yml CHANGED
@@ -21,5 +21,8 @@ configs:
21
21
  - en
22
22
  - tr
23
23
 
24
+ controller:
25
+ protocol: http
26
+
24
27
  end:
25
28
  version: 1