@xrystal/core 3.14.8 → 3.15.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.14.8",
4
+ "version": "3.15.1",
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,12 +5,20 @@ export const controllerContextStorage = new AsyncLocalStorage();
5
5
  export const getControllerCtx = () => controllerContextStorage.getStore();
6
6
  class Controller {
7
7
  loggerService;
8
+ supportedProtocols = [ProtocolEnum.HTTP, ProtocolEnum.WEBSOCKET];
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} not supported. Supported: ${this.supportedProtocols.join(', ')}`);
19
+ }
20
+ return incomingProtocol;
21
+ }
14
22
  get req() {
15
23
  const store = this.currentStore;
16
24
  if (!store)
@@ -43,8 +51,8 @@ class Controller {
43
51
  const store = this.currentStore;
44
52
  if (!store)
45
53
  return {};
54
+ const currentProtocol = this.protocol;
46
55
  if (store.ctx) {
47
- const protocol = this.controllerType || ProtocolEnum.HTTP;
48
56
  return {
49
57
  locals: {},
50
58
  status(code) {
@@ -52,9 +60,10 @@ class Controller {
52
60
  return this;
53
61
  },
54
62
  send(data) {
55
- if (protocol === ProtocolEnum.WEBSOCKET)
56
- return data;
57
- return new Response(JSON.stringify(data), {
63
+ const plainData = JSON.parse(JSON.stringify(data));
64
+ if (currentProtocol === ProtocolEnum.WEBSOCKET)
65
+ return plainData;
66
+ return new Response(JSON.stringify(plainData), {
58
67
  status: this.locals._code || 200,
59
68
  headers: { 'content-type': 'application/json' }
60
69
  });
@@ -91,56 +100,54 @@ 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) {
110
- return await this.responseProtocolSwitch({
111
- req: currentReq,
112
- res: currentRes,
113
- context: () => new ResponseSchema(checkResult).getResponse
114
- });
119
+ const errorObj = new ResponseSchema(checkResult).getResponse;
120
+ return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, context: () => errorObj });
115
121
  }
116
122
  }
117
123
  const logicResult = await logic({ payload, convertedPayload });
118
124
  if (logicResult.response instanceof Function)
119
125
  return logicResult.response(logicResult.payload);
120
126
  if (logicResult.message) {
121
- return await this.responseProtocolSwitch({
122
- req: currentReq,
123
- res: currentRes,
124
- resStatus: 400,
125
- context: () => new ResponseSchema(logicResult).getResponse
126
- });
127
+ const logicErrorObj = new ResponseSchema(logicResult).getResponse;
128
+ return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 400, context: () => logicErrorObj });
127
129
  }
128
130
  if (response) {
129
- const resResult = await response({ payload, convertedPayload });
130
- return await this.responseProtocolSwitch({
131
- req: currentReq,
132
- res: currentRes,
133
- resStatus: 200,
134
- context: ({ localeLanguageConverter }) => new ResponseSchema({
135
- status: true,
136
- message: responseMessageHelper.successFully(resResult.message[0], resResult.message[1], localeLanguageConverter),
137
- payload: logicResult.payload
138
- }).getResponse
139
- });
131
+ const resResult = await response({ payload, convertedPayload, logicResult });
132
+ const successObj = new ResponseSchema({
133
+ status: true,
134
+ message: Array.isArray(resResult.message)
135
+ ? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
136
+ : resResult.message,
137
+ payload: logicResult.payload
138
+ }).getResponse;
139
+ return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 200, context: () => successObj });
140
140
  }
141
+ return JSON.parse(JSON.stringify(logicResult.payload || logicResult));
141
142
  }
142
143
  catch (error) {
143
- return currentRes.status(500).send({ status: false, message: error.message });
144
+ const errorPayload = { status: false, message: error.message };
145
+ const storeRes = this.currentStore?.res || this.currentStore?.ctx?.set;
146
+ if (storeRes && typeof storeRes.status === 'function') {
147
+ return storeRes.status(500).send(errorPayload);
148
+ }
149
+ return errorPayload;
144
150
  }
145
151
  }
146
152
  }
153
+ export { Controller };
@@ -59,7 +59,7 @@ const coreLoader = async ({}) => {
59
59
  {
60
60
  service: ControllerService,
61
61
  props: {
62
- type: configs.loaders.controller.protocol,
62
+ type: configs.loaders.controller.protocol?.[0],
63
63
  }
64
64
  },
65
65
  {