@xrystal/core 3.15.7 → 3.16.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/package.json
CHANGED
|
@@ -5,12 +5,14 @@ export declare const controllerContextStorage: AsyncLocalStorage<{
|
|
|
5
5
|
ctx?: any;
|
|
6
6
|
req?: any;
|
|
7
7
|
res?: any;
|
|
8
|
+
metadata?: Record<string, any>;
|
|
8
9
|
}>;
|
|
9
10
|
export declare const getControllerCtx: () => {
|
|
10
11
|
protocol?: ProtocolEnum;
|
|
11
12
|
ctx?: any;
|
|
12
13
|
req?: any;
|
|
13
14
|
res?: any;
|
|
15
|
+
metadata?: Record<string, any>;
|
|
14
16
|
};
|
|
15
17
|
export interface CustomRequest {
|
|
16
18
|
accounts?: any;
|
|
@@ -37,6 +39,7 @@ declare abstract class Controller {
|
|
|
37
39
|
ctx?: any;
|
|
38
40
|
req?: any;
|
|
39
41
|
res?: any;
|
|
42
|
+
metadata?: Record<string, any>;
|
|
40
43
|
};
|
|
41
44
|
protected get protocol(): ProtocolEnum;
|
|
42
45
|
protected get req(): CustomRequest;
|
|
@@ -51,34 +51,37 @@ class Controller {
|
|
|
51
51
|
get res() {
|
|
52
52
|
const store = this.currentStore;
|
|
53
53
|
if (!store)
|
|
54
|
-
return {
|
|
54
|
+
return {
|
|
55
|
+
status: function () { return this; },
|
|
56
|
+
send: (data) => new Response(JSON.stringify(data), { status: 500, headers: { 'content-type': 'application/json' } }),
|
|
57
|
+
json: function (data) { return this.send(data); },
|
|
58
|
+
locals: {}
|
|
59
|
+
};
|
|
55
60
|
const currentProtocol = this.protocol;
|
|
56
61
|
if (store.ctx) {
|
|
62
|
+
if (!store.metadata)
|
|
63
|
+
store.metadata = { locals: {} };
|
|
57
64
|
return {
|
|
58
|
-
locals:
|
|
59
|
-
status(code) {
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
locals: store.metadata.locals,
|
|
66
|
+
status(code) {
|
|
67
|
+
store.metadata._code = code;
|
|
68
|
+
return this;
|
|
69
|
+
},
|
|
70
|
+
send: (data) => {
|
|
71
|
+
const isRaw = data instanceof Blob || data instanceof Uint8Array || store.metadata?._isRaw;
|
|
72
|
+
const contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
|
|
62
73
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
74
|
+
return data;
|
|
75
|
+
const body = isRaw ? data : JSON.stringify(data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data);
|
|
76
|
+
return new Response(body, {
|
|
77
|
+
status: store.metadata?._code || 200,
|
|
78
|
+
headers: { 'content-type': contentType }
|
|
67
79
|
});
|
|
68
80
|
},
|
|
69
81
|
json(data) { return this.send(data); }
|
|
70
82
|
};
|
|
71
83
|
}
|
|
72
|
-
return
|
|
73
|
-
locals: store.res?.locals || {},
|
|
74
|
-
status(code) { if (store.res?.status)
|
|
75
|
-
store.res.status(code); return this; },
|
|
76
|
-
send(data) {
|
|
77
|
-
const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
|
|
78
|
-
return store.res?.send ? store.res.send(plainData) : plainData;
|
|
79
|
-
},
|
|
80
|
-
json(data) { return this.send(data); }
|
|
81
|
-
};
|
|
84
|
+
return store.res;
|
|
82
85
|
}
|
|
83
86
|
responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
|
|
84
87
|
const responseData = context({ localeLanguageConverter: this.req?.t });
|
|
@@ -123,7 +126,8 @@ export class ControllerService extends Controller {
|
|
|
123
126
|
};
|
|
124
127
|
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => new ResponseSchema(successObj).getResponse });
|
|
125
128
|
}
|
|
126
|
-
|
|
129
|
+
const finalData = logicResult.payload !== undefined ? logicResult.payload : logicResult;
|
|
130
|
+
return finalData && typeof finalData === 'object' ? JSON.parse(JSON.stringify(finalData)) : finalData;
|
|
127
131
|
}
|
|
128
132
|
catch (error) {
|
|
129
133
|
return { status: false, message: error.message };
|