@xrystal/core 3.15.6 → 3.15.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
|
@@ -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;
|
|
@@ -54,31 +54,30 @@ class Controller {
|
|
|
54
54
|
return { status: () => { }, send: () => { }, json: () => { }, locals: {} };
|
|
55
55
|
const currentProtocol = this.protocol;
|
|
56
56
|
if (store.ctx) {
|
|
57
|
+
// Metadata'yı ALS üzerinden yönetiyoruz (Thread-safe)
|
|
58
|
+
if (!store.metadata)
|
|
59
|
+
store.metadata = { locals: {} };
|
|
57
60
|
return {
|
|
58
|
-
locals:
|
|
59
|
-
status(code) {
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
locals: store.metadata.locals,
|
|
62
|
+
status(code) {
|
|
63
|
+
store.metadata._code = code;
|
|
64
|
+
return this;
|
|
65
|
+
},
|
|
66
|
+
send: (data) => {
|
|
67
|
+
const isRaw = data instanceof Blob || data instanceof Uint8Array || store.metadata?._isRaw;
|
|
68
|
+
const contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
|
|
62
69
|
if (currentProtocol === ProtocolEnum.WEBSOCKET)
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
return data;
|
|
71
|
+
const body = isRaw ? data : JSON.stringify(data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data);
|
|
72
|
+
return new Response(body, {
|
|
73
|
+
status: store.metadata?._code || 200,
|
|
74
|
+
headers: { 'content-type': contentType }
|
|
67
75
|
});
|
|
68
76
|
},
|
|
69
77
|
json(data) { return this.send(data); }
|
|
70
78
|
};
|
|
71
79
|
}
|
|
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
|
-
};
|
|
80
|
+
return store.res;
|
|
82
81
|
}
|
|
83
82
|
responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
|
|
84
83
|
const responseData = context({ localeLanguageConverter: this.req?.t });
|
|
@@ -123,7 +122,8 @@ export class ControllerService extends Controller {
|
|
|
123
122
|
};
|
|
124
123
|
return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => new ResponseSchema(successObj).getResponse });
|
|
125
124
|
}
|
|
126
|
-
|
|
125
|
+
const finalData = logicResult.payload !== undefined ? logicResult.payload : logicResult;
|
|
126
|
+
return finalData && typeof finalData === 'object' ? JSON.parse(JSON.stringify(finalData)) : finalData;
|
|
127
127
|
}
|
|
128
128
|
catch (error) {
|
|
129
129
|
return { status: false, message: error.message };
|