@xrystal/core 3.26.4 → 3.26.5

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.26.4",
4
+ "version": "3.26.5",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -18,6 +18,7 @@ export interface CustomResponse {
18
18
  status: (code: number) => CustomResponse;
19
19
  send: (data: any) => any;
20
20
  json: (data: any) => any;
21
+ cookie: (name: string, value: string, options?: any) => CustomResponse;
21
22
  locals: Record<string, any>;
22
23
  }
23
24
  export declare const controllerContextStorage: AsyncLocalStorage<{
@@ -33,6 +34,10 @@ export declare const controllerContextStorage: AsyncLocalStorage<{
33
34
  _isRaw?: boolean;
34
35
  _parsedBody?: any;
35
36
  _parsedQuery?: any;
37
+ _cookiesToSet?: Record<string, {
38
+ value: string;
39
+ options?: any;
40
+ }>;
36
41
  };
37
42
  }>;
38
43
  export declare const getControllerCtx: () => {
@@ -48,6 +53,10 @@ export declare const getControllerCtx: () => {
48
53
  _isRaw?: boolean;
49
54
  _parsedBody?: any;
50
55
  _parsedQuery?: any;
56
+ _cookiesToSet?: Record<string, {
57
+ value: string;
58
+ options?: any;
59
+ }>;
51
60
  };
52
61
  };
53
62
  export declare abstract class BaseController {
@@ -68,6 +77,10 @@ export declare abstract class BaseController {
68
77
  _isRaw?: boolean;
69
78
  _parsedBody?: any;
70
79
  _parsedQuery?: any;
80
+ _cookiesToSet?: Record<string, {
81
+ value: string;
82
+ options?: any;
83
+ }>;
71
84
  };
72
85
  };
73
86
  protected get req(): CustomRequest;
@@ -36,11 +36,20 @@ export class BaseController {
36
36
  ...(ctx.request?.params || {}),
37
37
  ...(ctx.request?.routeParams || {})
38
38
  };
39
- const cookies = ctx.cookie || req.cookies || ctx.request?.cookie || ctx.request?.cookies || {};
39
+ const headers = ctx.headers || req.headers || ctx.request?.headers || {};
40
+ let cookies = ctx.cookies || ctx.cookie || req.cookies || ctx.request?.cookie || ctx.request?.cookies || {};
41
+ if (Object.keys(cookies).length === 0 && headers.cookie) {
42
+ cookies = headers.cookie.split(';').reduce((acc, v) => {
43
+ const parts = v.split('=');
44
+ if (parts.length === 2)
45
+ acc[decodeURIComponent(parts[0].trim())] = decodeURIComponent(parts[1].trim());
46
+ return acc;
47
+ }, {});
48
+ }
40
49
  return {
41
50
  url: urlStr,
42
51
  method: (ctx.method || req.method || ctx.request?.method || "").toUpperCase(),
43
- headers: ctx.headers || req.headers || ctx.request?.headers || {},
52
+ headers,
44
53
  body,
45
54
  query: query || {},
46
55
  params: params || {},
@@ -54,17 +63,26 @@ export class BaseController {
54
63
  get res() {
55
64
  const store = this.currentStore;
56
65
  const self = this;
57
- const fallbackRes = { status: function () { return this; }, send: (d) => d, json: function (d) { return this.send(d); }, locals: {} };
66
+ const fallbackRes = { status: function () { return this; }, send: (d) => d, json: function (d) { return this.send(d); }, cookie: function () { return this; }, locals: {} };
58
67
  if (!store)
59
68
  return fallbackRes;
60
69
  if (!store.metadata)
61
- store.metadata = { locals: {} };
70
+ store.metadata = { locals: {}, _cookiesToSet: {} };
62
71
  return {
63
72
  get locals() { return store.metadata.locals; },
64
73
  status(code) {
65
74
  store.metadata._code = code;
66
75
  return this;
67
76
  },
77
+ cookie(name, value, options = {}) {
78
+ if (!store.metadata._cookiesToSet)
79
+ store.metadata._cookiesToSet = {};
80
+ store.metadata._cookiesToSet[name] = { value, options };
81
+ const ctx = store.ctx;
82
+ if (ctx?.res?.cookie)
83
+ ctx.res.cookie(name, value, options);
84
+ return this;
85
+ },
68
86
  send(data) {
69
87
  if (self.protocol === ProtocolEnum.WEBSOCKET)
70
88
  return data;
@@ -78,8 +96,30 @@ export class BaseController {
78
96
  if (!httpStatus) {
79
97
  httpStatus = (bizCode >= 100 && bizCode <= 101) || (bizCode >= 200 && bizCode <= 599) ? bizCode : (bizCode === 0 || (bizCode >= 200 && bizCode <= 299) ? 200 : 400);
80
98
  }
99
+ const responseHeaders = new Headers();
100
+ responseHeaders.set("content-type", contentType);
101
+ if (store.metadata._cookiesToSet) {
102
+ Object.entries(store.metadata._cookiesToSet).forEach(([name, { value, options }]) => {
103
+ let cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
104
+ if (options.expires)
105
+ cookieStr += `; Expires=${options.expires.toUTCString()}`;
106
+ if (options.maxAge)
107
+ cookieStr += `; Max-Age=${options.maxAge}`;
108
+ if (options.domain)
109
+ cookieStr += `; Domain=${options.domain}`;
110
+ if (options.path)
111
+ cookieStr += `; Path=${options.path}`;
112
+ if (options.secure)
113
+ cookieStr += `; Secure`;
114
+ if (options.httpOnly)
115
+ cookieStr += `; HttpOnly`;
116
+ if (options.sameSite)
117
+ cookieStr += `; SameSite=${options.sameSite}`;
118
+ responseHeaders.append("set-cookie", cookieStr);
119
+ });
120
+ }
81
121
  const body = isRaw || typeof data === "string" ? data : JSON.stringify(data?.getResponse ? data.getResponse : data);
82
- return new Response(body, { status: httpStatus, headers: { "content-type": contentType } });
122
+ return new Response(body, { status: httpStatus, headers: responseHeaders });
83
123
  },
84
124
  json(data) { return this.send(data); }
85
125
  };