@syntay/fastay 0.1.6 → 0.1.7

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/dist/app.js CHANGED
@@ -4,6 +4,7 @@ import { loadApiRoutes } from './router.js';
4
4
  import { loadFastayMiddlewares, createMiddleware, } from './middleware.js';
5
5
  import { logger } from './logger.js';
6
6
  import { printBanner } from './banner.js';
7
+ import { RequestCookies } from './utils/cookies.js';
7
8
  /**
8
9
  * Bootstraps and configures a Fastay application.
9
10
  *
@@ -91,8 +92,9 @@ export async function createApp(opts) {
91
92
  const isMiddleware = await loadFastayMiddlewares(app);
92
93
  // health check
93
94
  app.get('/_health', (_, res) => res.json({ ok: true }));
94
- app.use((_req, res, next) => {
95
+ app.use((req, res, next) => {
95
96
  res.setHeader('X-Powered-By', 'Syntay Engine');
97
+ req.cookies = new RequestCookies(req.headers.cookie);
96
98
  next();
97
99
  });
98
100
  // load routes
package/dist/router.js CHANGED
@@ -92,10 +92,9 @@ function wrapHandler(fn, routePath, filePath) {
92
92
  res.cookie(name, data.value, data.options || {});
93
93
  }
94
94
  }
95
- if (typeof typedResult.status === 'number') {
96
- return res.status(typedResult.status).json(typedResult.body ?? {});
97
- }
98
- return res.json(result);
95
+ const statusCode = typeof result.status === 'number' ? result.status : 200;
96
+ const body = result.body ?? result; // se não existir body, retorna o objeto inteiro
97
+ return res.status(statusCode).json(body);
99
98
  }
100
99
  // Suporte a retorno simples
101
100
  if (typeof result === 'string')
@@ -1,11 +1,34 @@
1
1
  import { Request as ExpressRequest, Response as ExpressResponse, NextFunction } from 'express';
2
- /**
3
- * Tipos do Express reexportados para os usuários
4
- */
5
- export type Request = ExpressRequest;
2
+ export interface CookieItem {
3
+ value: string;
4
+ }
5
+ export interface RequestCookies {
6
+ /**
7
+ * Retrieves a cookie by its name.
8
+ * @param name - The name of the cookie to retrieve.
9
+ * @returns An object containing the cookie's value, or undefined if not found.
10
+ */
11
+ get(name: string): CookieItem | undefined;
12
+ /**
13
+ * Checks if a cookie with the given name exists.
14
+ * @param name - The name of the cookie to check.
15
+ * @returns True if the cookie exists, false otherwise.
16
+ */
17
+ has(name: string): boolean;
18
+ /**
19
+ * Returns all cookies as a key-value object.
20
+ * @returns An object where keys are cookie names and values are cookie values.
21
+ */
22
+ all(): Record<string, string>;
23
+ }
24
+ export interface Request extends ExpressRequest {
25
+ /**
26
+ * Represents the cookies sent in a request.
27
+ */
28
+ cookies: RequestCookies;
29
+ }
6
30
  export type Response = ExpressResponse;
7
31
  export type Next = NextFunction;
8
- export {};
9
32
  declare global {
10
33
  type FastayResponse = {
11
34
  status?: number;
@@ -26,3 +49,11 @@ declare global {
26
49
  };
27
50
  }
28
51
  export type RouteHandler = (() => FastayResponse | any) | ((req: Request) => FastayResponse | any) | ((req: Request, res: Response) => FastayResponse | any);
52
+ export interface CookieItem {
53
+ value: string;
54
+ }
55
+ declare module 'express-serve-static-core' {
56
+ interface Request {
57
+ typedCookies: RequestCookies | any;
58
+ }
59
+ }
@@ -0,0 +1,9 @@
1
+ export declare class RequestCookies {
2
+ private cookies;
3
+ constructor(cookieHeader: string | undefined);
4
+ get(name: string): {
5
+ value: string;
6
+ } | undefined;
7
+ has(name: string): boolean;
8
+ all(): Record<string, string>;
9
+ }
@@ -0,0 +1,27 @@
1
+ export class RequestCookies {
2
+ constructor(cookieHeader) {
3
+ this.cookies = new Map();
4
+ if (!cookieHeader)
5
+ return;
6
+ cookieHeader.split(';').forEach((cookie) => {
7
+ const [name, ...rest] = cookie.trim().split('=');
8
+ if (!name)
9
+ return;
10
+ this.cookies.set(name, decodeURIComponent(rest.join('=')));
11
+ });
12
+ }
13
+ get(name) {
14
+ const value = this.cookies.get(name);
15
+ if (!value)
16
+ return undefined;
17
+ return { value };
18
+ }
19
+ has(name) {
20
+ return this.cookies.has(name);
21
+ }
22
+ all() {
23
+ const obj = {};
24
+ this.cookies.forEach((v, k) => (obj[k] = v));
25
+ return obj;
26
+ }
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syntay/fastay",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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",
package/src/app.ts CHANGED
@@ -10,6 +10,7 @@ import { logger } from './logger.js';
10
10
  import { printBanner } from './banner.js';
11
11
  import type { ServeStaticOptions } from 'serve-static';
12
12
  import { Next, Request, Response } from './types/index.js';
13
+ import { RequestCookies } from './utils/cookies.js';
13
14
 
14
15
  /**
15
16
  * Express configuration options applied automatically by Fastay
@@ -214,8 +215,9 @@ export async function createApp(opts?: CreateAppOptions) {
214
215
 
215
216
  // health check
216
217
  app.get('/_health', (_, res) => res.json({ ok: true }));
217
- app.use((_req: Request, res: Response, next: Next) => {
218
+ app.use((req: Request, res: Response, next: Next) => {
218
219
  res.setHeader('X-Powered-By', 'Syntay Engine');
220
+ (req as any).cookies = new RequestCookies(req.headers.cookie);
219
221
  next();
220
222
  });
221
223
 
package/src/router.ts CHANGED
@@ -64,7 +64,6 @@ function wrapHandler(fn: Function, routePath: string, filePath: string) {
64
64
  const typedResult = result as {
65
65
  status?: number;
66
66
  body?: any;
67
-
68
67
  cookies?: Record<string, { value: string; options?: any }>;
69
68
  headers?: Record<string, string>;
70
69
  redirect?: string;
@@ -124,11 +123,11 @@ function wrapHandler(fn: Function, routePath: string, filePath: string) {
124
123
  }
125
124
  }
126
125
 
127
- if (typeof typedResult.status === 'number') {
128
- return res.status(typedResult.status).json(typedResult.body ?? {});
129
- }
126
+ const statusCode =
127
+ typeof result.status === 'number' ? result.status : 200;
130
128
 
131
- return res.json(result);
129
+ const body = result.body ?? result; // se não existir body, retorna o objeto inteiro
130
+ return res.status(statusCode).json(body);
132
131
  }
133
132
 
134
133
  // Suporte a retorno simples
@@ -0,0 +1,7 @@
1
+ import { RequestCookies } from '../src/utils/cookies';
2
+
3
+ declare module 'express-serve-static-core' {
4
+ interface Request {
5
+ cookies: RequestCookies;
6
+ }
7
+ }
@@ -0,0 +1,9 @@
1
+ export interface CookieItem {
2
+ value: string;
3
+ }
4
+
5
+ export interface RequestCookies {
6
+ get(name: string): CookieItem | undefined;
7
+ has(name: string): boolean;
8
+ all(): Record<string, string>;
9
+ }
@@ -4,30 +4,52 @@ import {
4
4
  NextFunction,
5
5
  } from 'express';
6
6
 
7
- /**
8
- * Tipos do Express reexportados para os usuários
9
- */
10
- export type Request = ExpressRequest;
7
+ export interface CookieItem {
8
+ value: string;
9
+ }
10
+
11
+ export interface RequestCookies {
12
+ /**
13
+ * Retrieves a cookie by its name.
14
+ * @param name - The name of the cookie to retrieve.
15
+ * @returns An object containing the cookie's value, or undefined if not found.
16
+ */
17
+ get(name: string): CookieItem | undefined;
18
+
19
+ /**
20
+ * Checks if a cookie with the given name exists.
21
+ * @param name - The name of the cookie to check.
22
+ * @returns True if the cookie exists, false otherwise.
23
+ */
24
+ has(name: string): boolean;
25
+
26
+ /**
27
+ * Returns all cookies as a key-value object.
28
+ * @returns An object where keys are cookie names and values are cookie values.
29
+ */
30
+ all(): Record<string, string>;
31
+ }
32
+
33
+ export interface Request extends ExpressRequest {
34
+ /**
35
+ * Represents the cookies sent in a request.
36
+ */
37
+ cookies: RequestCookies;
38
+ // params:
39
+ // query:
40
+ }
41
+
11
42
  export type Response = ExpressResponse;
12
43
  export type Next = NextFunction;
13
44
 
14
- export {};
15
-
16
45
  declare global {
17
46
  type FastayResponse = {
18
47
  status?: number;
19
48
  body?: any;
20
-
21
49
  cookies?: Record<string, { value: string; options?: any }>;
22
50
  headers?: Record<string, string>;
23
51
  redirect?: string;
24
-
25
- file?: {
26
- path: string;
27
- filename?: string;
28
- options?: any;
29
- };
30
-
52
+ file?: { path: string; filename?: string; options?: any };
31
53
  stream?: NodeJS.ReadableStream;
32
54
  raw?: Buffer | string;
33
55
  };
@@ -37,3 +59,13 @@ export type RouteHandler =
37
59
  | (() => FastayResponse | any)
38
60
  | ((req: Request) => FastayResponse | any)
39
61
  | ((req: Request, res: Response) => FastayResponse | any);
62
+
63
+ export interface CookieItem {
64
+ value: string;
65
+ }
66
+
67
+ declare module 'express-serve-static-core' {
68
+ interface Request {
69
+ typedCookies: RequestCookies | any;
70
+ }
71
+ }
@@ -0,0 +1,32 @@
1
+ // utils/cookies.ts
2
+ import { IncomingMessage } from 'http';
3
+
4
+ export class RequestCookies {
5
+ private cookies: Map<string, string> = new Map();
6
+
7
+ constructor(cookieHeader: string | undefined) {
8
+ if (!cookieHeader) return;
9
+
10
+ cookieHeader.split(';').forEach((cookie) => {
11
+ const [name, ...rest] = cookie.trim().split('=');
12
+ if (!name) return;
13
+ this.cookies.set(name, decodeURIComponent(rest.join('=')));
14
+ });
15
+ }
16
+
17
+ public get(name: string) {
18
+ const value = this.cookies.get(name);
19
+ if (!value) return undefined;
20
+ return { value };
21
+ }
22
+
23
+ public has(name: string) {
24
+ return this.cookies.has(name);
25
+ }
26
+
27
+ public all() {
28
+ const obj: Record<string, string> = {};
29
+ this.cookies.forEach((v, k) => (obj[k] = v));
30
+ return obj;
31
+ }
32
+ }
package/tsconfig.json CHANGED
@@ -4,14 +4,16 @@
4
4
  "module": "CommonJS",
5
5
  "rootDir": "src",
6
6
  "outDir": "dist",
7
+ "baseUrl": ".",
7
8
  "strict": true,
8
9
  "esModuleInterop": true,
9
10
  "forceConsistentCasingInFileNames": true,
10
11
  "skipLibCheck": true,
11
12
  "moduleResolution": "node",
12
13
  "resolveJsonModule": true,
13
- "allowSyntheticDefaultImports": true
14
+ "allowSyntheticDefaultImports": true,
15
+ "typeRoots": ["./node_modules/@types", "./types"]
14
16
  },
15
- "include": ["src"],
17
+ "include": ["src", "types"],
16
18
  "exclude": ["node_modules", "dist"]
17
19
  }