@syntay/fastay 0.2.5 → 0.2.6

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.
@@ -1,11 +1,28 @@
1
+ import { ServerResponse } from 'http';
1
2
  export declare class RequestCookies {
2
3
  private cookies;
3
- constructor(cookieHeader: string | undefined);
4
+ private setCookies;
5
+ constructor(cookieHeader?: string);
4
6
  get(name: string): {
5
7
  value: string;
6
8
  } | undefined;
7
9
  has(name: string): boolean;
8
10
  all(): Record<string, string>;
11
+ set(name: string, value: string, options?: {
12
+ path?: string;
13
+ httpOnly?: boolean;
14
+ secure?: boolean;
15
+ sameSite?: 'Lax' | 'Strict' | 'None';
16
+ domain?: string;
17
+ maxAge?: number;
18
+ }): void;
19
+ delete(name: string, options?: {
20
+ path?: string;
21
+ domain?: string;
22
+ }): void;
23
+ clear(): void;
24
+ toString(): string;
25
+ applyToResponse(res: ServerResponse): void;
9
26
  }
10
27
  export declare const cookies: typeof RequestCookies;
11
28
  //# sourceMappingURL=cookies.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../../src/utils/cookies.ts"],"names":[],"mappings":"AAGA,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAkC;gBAErC,YAAY,EAAE,MAAM,GAAG,SAAS;IAUrC,GAAG,CAAC,IAAI,EAAE,MAAM;;;IAMhB,GAAG,CAAC,IAAI,EAAE,MAAM;IAIhB,GAAG;CAKX;AAED,eAAO,MAAM,OAAO,uBAAiB,CAAC"}
1
+ {"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../../src/utils/cookies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,cAAc,EAAE,MAAM,MAAM,CAAC;AAEvD,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,UAAU,CAAgB;gBAEtB,YAAY,CAAC,EAAE,MAAM;IAU1B,GAAG,CAAC,IAAI,EAAE,MAAM;;;IAMhB,GAAG,CAAC,IAAI,EAAE,MAAM;IAIhB,GAAG;IAMH,GAAG,CACR,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;QACP,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;QACrC,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,MAAM,CAAC,EAAE,MAAM,CAAC;KACZ;IAcD,MAAM,CACX,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAO;IAU3C,KAAK;IAIL,QAAQ;IAIR,eAAe,CAAC,GAAG,EAAE,cAAc;CAG3C;AAED,eAAO,MAAM,OAAO,uBAAiB,CAAC"}
@@ -1,6 +1,7 @@
1
1
  export class RequestCookies {
2
2
  constructor(cookieHeader) {
3
3
  this.cookies = new Map();
4
+ this.setCookies = []; // para armazenar cookies que serão enviados no response
4
5
  if (!cookieHeader)
5
6
  return;
6
7
  cookieHeader.split(';').forEach((cookie) => {
@@ -24,5 +25,39 @@ export class RequestCookies {
24
25
  this.cookies.forEach((v, k) => (obj[k] = v));
25
26
  return obj;
26
27
  }
28
+ set(name, value, options = {}) {
29
+ let cookieStr = `${name}=${encodeURIComponent(value)}`;
30
+ if (options.path)
31
+ cookieStr += `; Path=${options.path}`;
32
+ if (options.httpOnly)
33
+ cookieStr += `; HttpOnly`;
34
+ if (options.secure)
35
+ cookieStr += `; Secure`;
36
+ if (options.sameSite)
37
+ cookieStr += `; SameSite=${options.sameSite}`;
38
+ if (options.domain)
39
+ cookieStr += `; Domain=${options.domain}`;
40
+ if (options.maxAge)
41
+ cookieStr += `; Max-Age=${options.maxAge}`;
42
+ this.setCookies.push(cookieStr);
43
+ this.cookies.set(name, value);
44
+ }
45
+ delete(name, options = {}) {
46
+ this.set(name, '', {
47
+ path: options.path,
48
+ domain: options.domain,
49
+ maxAge: 0,
50
+ });
51
+ this.cookies.delete(name);
52
+ }
53
+ clear() {
54
+ this.cookies.forEach((_, name) => this.delete(name));
55
+ }
56
+ toString() {
57
+ return this.setCookies.join('; ');
58
+ }
59
+ applyToResponse(res) {
60
+ this.setCookies.forEach((c) => res.setHeader('Set-Cookie', c));
61
+ }
27
62
  }
28
63
  export const cookies = RequestCookies;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syntay/fastay",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Framework backend moderno baseado em Express.js, para criar APIs rapidamente",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",