skyguard-js 1.2.0 → 1.2.2

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.
Files changed (52) hide show
  1. package/README.md +9 -462
  2. package/dist/app.d.ts +34 -10
  3. package/dist/app.js +59 -27
  4. package/dist/crypto/hasher.js +2 -1
  5. package/dist/crypto/jwt.js +2 -1
  6. package/dist/http/context.d.ts +115 -0
  7. package/dist/http/context.js +147 -0
  8. package/dist/http/httpAdapter.d.ts +4 -4
  9. package/dist/http/index.d.ts +2 -0
  10. package/dist/http/index.js +3 -1
  11. package/dist/http/logger.d.ts +10 -1
  12. package/dist/http/logger.js +44 -8
  13. package/dist/http/nodeNativeHttp.d.ts +6 -5
  14. package/dist/http/nodeNativeHttp.js +13 -6
  15. package/dist/http/request.d.ts +4 -0
  16. package/dist/http/request.js +12 -4
  17. package/dist/http/response.d.ts +21 -2
  18. package/dist/http/response.js +30 -2
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +3 -2
  21. package/dist/middlewares/cors.d.ts +10 -4
  22. package/dist/middlewares/cors.js +35 -18
  23. package/dist/middlewares/csrf.js +33 -33
  24. package/dist/middlewares/index.d.ts +1 -1
  25. package/dist/middlewares/rateLimiter.d.ts +58 -6
  26. package/dist/middlewares/rateLimiter.js +149 -40
  27. package/dist/middlewares/session.js +4 -4
  28. package/dist/routing/routeResolveFunc.d.ts +10 -0
  29. package/dist/routing/routeResolveFunc.js +21 -0
  30. package/dist/routing/router.d.ts +16 -10
  31. package/dist/routing/router.js +32 -25
  32. package/dist/routing/routerGroup.d.ts +10 -5
  33. package/dist/routing/routerGroup.js +11 -10
  34. package/dist/sessions/databaseSessionStorage.d.ts +52 -0
  35. package/dist/sessions/databaseSessionStorage.js +166 -0
  36. package/dist/sessions/fileSessionStorage.js +1 -1
  37. package/dist/sessions/index.d.ts +1 -0
  38. package/dist/sessions/index.js +3 -1
  39. package/dist/sessions/memorySessionStorage.js +1 -1
  40. package/dist/storage/storage.d.ts +3 -3
  41. package/dist/storage/storage.js +7 -7
  42. package/dist/storage/types.d.ts +5 -5
  43. package/dist/storage/uploader.d.ts +9 -9
  44. package/dist/storage/uploader.js +62 -62
  45. package/dist/types/index.d.ts +11 -10
  46. package/dist/validators/rules/bigIntRule.d.ts +0 -6
  47. package/dist/validators/rules/bigIntRule.js +0 -24
  48. package/dist/validators/validationRule.js +1 -1
  49. package/dist/validators/validationSchema.js +8 -8
  50. package/package.json +2 -2
  51. package/dist/helpers/http.d.ts +0 -95
  52. package/dist/helpers/http.js +0 -112
@@ -1,18 +1,18 @@
1
- import { Response, Request, HttpMethods } from "../http/index";
1
+ import { Response, Context, HttpMethods } from "../http/index";
2
2
  import { Layer } from "../routing/layer";
3
3
  /**
4
4
  * Route controller function.
5
5
  *
6
6
  * Represents the final endpoint in the execution pipeline.
7
- * Receives a normalized {@link Request} and must return
7
+ * Receives a normalized {@link Context} and must return
8
8
  * a {@link Response}.
9
9
  *
10
10
  * @example
11
- * const handler: RouteHandler = (req) => {
12
- * return Response.json({ message: "Hello world" });
11
+ * const handler: RouteHandler = context => {
12
+ * return context.json({ message: "Hello world" });
13
13
  * };
14
14
  */
15
- export type RouteHandler = (request: Request) => Promise<Response> | Response;
15
+ export type RouteHandler = (context: Context) => Promise<Response> | Response;
16
16
  /**
17
17
  * Internal routing table structure.
18
18
  *
@@ -82,19 +82,20 @@ export type Constructor<T = unknown> = new (...args: unknown[]) => T;
82
82
  *
83
83
  * Can be synchronous or asynchronous.
84
84
  *
85
- * @param request - Current {@link Request} instance
85
+ * @param context - Current {@link Context} instance
86
86
  * @param next - Function that executes the next middleware
87
87
  * or the route handler
88
88
  * @returns A {@link Response} or a Promise resolving to {@link Response}
89
89
  *
90
90
  * @example
91
- * const loggerMiddleware: Middleware = async (request, next) => {
92
- * console.log(request.getMethod, request.getUrl);
91
+ * const loggerMiddleware: Middleware = async (context, next) => {
92
+ * console.log(context.req.method, context.req.url);
93
93
  *
94
- * const response = await next(request);
94
+ * const response = await next(context);
95
95
  * return response;
96
96
  * };
97
97
  *
98
98
  * app.middlewares(loggerMiddleware);
99
99
  */
100
- export type Middleware = (request: Request, next: RouteHandler) => Response | Promise<Response>;
100
+ export type Middleware = (context: Context, next: RouteHandler) => Response | Promise<Response>;
101
+ export type HandlerOrMiddlewares = RouteHandler | Middleware[];
@@ -11,10 +11,4 @@ export interface BigIntRuleOptions extends RuleOptions {
11
11
  export declare class BigIntRule extends BaseValidationRule<bigint> {
12
12
  constructor();
13
13
  validate(context: ValidationContext, options?: BigIntRuleOptions): ValidationError | null;
14
- gt(limit: bigint, message?: string): this;
15
- gte(limit: bigint, message?: string): this;
16
- lt(limit: bigint, message?: string): this;
17
- lte(limit: bigint, message?: string): this;
18
- positive(message?: string): this;
19
- negative(message?: string): this;
20
14
  }
@@ -25,29 +25,5 @@ class BigIntRule extends validationRule_1.BaseValidationRule {
25
25
  return this.createError(field, `${field} must be less than or equal to ${options.lte}`, value);
26
26
  return null;
27
27
  }
28
- gt(limit, message) {
29
- this.rules.push({ rule: this, options: { gt: limit, message } });
30
- return this;
31
- }
32
- gte(limit, message) {
33
- this.rules.push({ rule: this, options: { gte: limit, message } });
34
- return this;
35
- }
36
- lt(limit, message) {
37
- this.rules.push({ rule: this, options: { lt: limit, message } });
38
- return this;
39
- }
40
- lte(limit, message) {
41
- this.rules.push({ rule: this, options: { lte: limit, message } });
42
- return this;
43
- }
44
- positive(message) {
45
- this.rules.push({ rule: this, options: { positive: true, message } });
46
- return this;
47
- }
48
- negative(message) {
49
- this.rules.push({ rule: this, options: { negative: true, message } });
50
- return this;
51
- }
52
28
  }
53
29
  exports.BigIntRule = BigIntRule;
@@ -8,7 +8,7 @@ class BaseValidationRule {
8
8
  name;
9
9
  rules;
10
10
  hasOptional = false;
11
- defaultValue = undefined;
11
+ defaultValue;
12
12
  converter;
13
13
  constructor(name, rules = []) {
14
14
  this.name = name;
@@ -340,20 +340,20 @@ exports.v = new ValidatorRules();
340
340
  * @param schema - Validation rules mapped by field name
341
341
  */
342
342
  const validateRequest = (schema) => {
343
- return (request, next) => {
343
+ return (context, next) => {
344
344
  if (schema.body) {
345
- const validBody = validator_1.Validator.validateOrFail(request.body, schema.body);
346
- request.setBody(validBody);
345
+ const validBody = validator_1.Validator.validateOrFail(context.body, schema.body);
346
+ context.req.setBody(validBody);
347
347
  }
348
348
  if (schema.params) {
349
- const validParams = validator_1.Validator.validateOrFail(request.params, schema.params);
350
- request.setParams(validParams);
349
+ const validParams = validator_1.Validator.validateOrFail(context.params, schema.params);
350
+ context.req.setParams(validParams);
351
351
  }
352
352
  if (schema.query) {
353
- const validQuery = validator_1.Validator.validateOrFail(request.query, schema.query);
354
- request.setQuery(validQuery);
353
+ const validQuery = validator_1.Validator.validateOrFail(context.query, schema.query);
354
+ context.req.setQuery(validQuery);
355
355
  }
356
- return next(request);
356
+ return next(context);
357
357
  };
358
358
  };
359
359
  exports.validateRequest = validateRequest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skyguard-js",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "A lightweight, dependency-free TypeScript backend framework",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "bugs": {
30
30
  "url": "https://github.com/Pipe930/Skyguard-js/issues"
31
31
  },
32
- "homepage": "https://github.com/Pipe930/Skyguard-js#readme",
32
+ "homepage": "https://pipe930.github.io/skyguard-documentation/",
33
33
  "engines": {
34
34
  "node": ">=22"
35
35
  },
@@ -1,95 +0,0 @@
1
- import { Response } from "../http/response";
2
- /**
3
- * Creates an HTTP response with a JSON body.
4
- *
5
- * @typeParam T - Type of the payload to be serialized.
6
- * @param data - Data to be serialized as JSON.
7
- * @returns A `Response` instance with `Content-Type: application/json`.
8
- *
9
- * @example
10
- * return json({ ok: true, userId: 1 });
11
- */
12
- export declare function json(data: unknown): Response;
13
- /**
14
- * Creates an HTTP response with a plain text body.
15
- *
16
- * @param data - Text to be sent as the response body.
17
- * @returns A `Response` instance with a text-based `Content-Type`.
18
- *
19
- * @example
20
- * return text("Hello world");
21
- */
22
- export declare function text(data: string): Response;
23
- /**
24
- * Creates an HTTP redirect response to the given URL.
25
- *
26
- * @param url - Target URL for the redirection (absolute or relative).
27
- * @returns A `Response` instance configured as a redirect.
28
- *
29
- * @example
30
- * return redirect("/login");
31
- */
32
- export declare function redirect(url: string): Response;
33
- /**
34
- * Returns a file as a downloadable response.
35
- *
36
- * @param path - File system path to the file (relative or absolute, depending on the runtime).
37
- * @param filename - Optional filename suggested to the client for the download.
38
- * @param headers - Optional additional headers to include in the response.
39
- * @returns A `Promise<Response>` ready to be returned by a route handler.
40
- *
41
- * @example
42
- * return await download("./storage/reports/sales.pdf", "report.pdf", {
43
- * "Cache-Control": "no-store",
44
- * });
45
- */
46
- export declare function download(path: string, filename?: string, headers?: Record<string, string>): Promise<Response>;
47
- /**
48
- * Renders a template/view and returns an HTTP response with the generated HTML.
49
- *
50
- * @param view - View identifier or template path, depending on the template system.
51
- * @param params - Context variables available inside the template.
52
- * @param layout - Optional layout name used to wrap the rendered view.
53
- * @returns A `Promise<Response>` containing the rendered HTML.
54
- *
55
- * @example
56
- * return await render("users/profile", { user }, "main");
57
- */
58
- export declare function render(data: string, params?: Record<string, unknown>): Promise<Response>;
59
- /**
60
- * Sends a file as an HTTP response.
61
- *
62
- * This helper is a thin wrapper around `Response.sendFile`, allowing a file
63
- * to be streamed to the client while optionally applying custom headers
64
- * and resolving the file path relative to a root directory.
65
- *
66
- * @param filePath - Path to the file to send.
67
- * @param options - Optional configuration for the file response.
68
- * @param options.headers - Additional HTTP headers to include in the response
69
- * (e.g. `Content-Type`, `Cache-Control`, `Content-Disposition`).
70
- * @param options.root - Base directory used to resolve `filePath`.
71
- * @returns A `Response` object that streams the requested file to the client.
72
- *
73
- * @example
74
- * // Send a file using an absolute path
75
- * const response = await sendFile("/var/www/files/report.pdf", {});
76
- *
77
- * @example
78
- * // Send a file relative to a root directory
79
- * const response = await sendFile("report.pdf", {
80
- * root: "/var/www/files",
81
- * });
82
- *
83
- * @example
84
- * // Send a downloadable file
85
- * const response = await sendFile("report.pdf", {
86
- * root: "/var/www/files",
87
- * headers: {
88
- * "Content-Disposition": "attachment; filename=\"report.pdf\"",
89
- * },
90
- * });
91
- */
92
- export declare function sendFile(filePath: string, options: {
93
- headers?: Record<string, string>;
94
- root?: string;
95
- }): Promise<Response>;
@@ -1,112 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.json = json;
4
- exports.text = text;
5
- exports.redirect = redirect;
6
- exports.download = download;
7
- exports.render = render;
8
- exports.sendFile = sendFile;
9
- const response_1 = require("../http/response");
10
- /**
11
- * Creates an HTTP response with a JSON body.
12
- *
13
- * @typeParam T - Type of the payload to be serialized.
14
- * @param data - Data to be serialized as JSON.
15
- * @returns A `Response` instance with `Content-Type: application/json`.
16
- *
17
- * @example
18
- * return json({ ok: true, userId: 1 });
19
- */
20
- function json(data) {
21
- return response_1.Response.json(data);
22
- }
23
- /**
24
- * Creates an HTTP response with a plain text body.
25
- *
26
- * @param data - Text to be sent as the response body.
27
- * @returns A `Response` instance with a text-based `Content-Type`.
28
- *
29
- * @example
30
- * return text("Hello world");
31
- */
32
- function text(data) {
33
- return response_1.Response.text(data);
34
- }
35
- /**
36
- * Creates an HTTP redirect response to the given URL.
37
- *
38
- * @param url - Target URL for the redirection (absolute or relative).
39
- * @returns A `Response` instance configured as a redirect.
40
- *
41
- * @example
42
- * return redirect("/login");
43
- */
44
- function redirect(url) {
45
- return response_1.Response.redirect(url);
46
- }
47
- /**
48
- * Returns a file as a downloadable response.
49
- *
50
- * @param path - File system path to the file (relative or absolute, depending on the runtime).
51
- * @param filename - Optional filename suggested to the client for the download.
52
- * @param headers - Optional additional headers to include in the response.
53
- * @returns A `Promise<Response>` ready to be returned by a route handler.
54
- *
55
- * @example
56
- * return await download("./storage/reports/sales.pdf", "report.pdf", {
57
- * "Cache-Control": "no-store",
58
- * });
59
- */
60
- async function download(path, filename, headers) {
61
- return await response_1.Response.download(path, filename, headers);
62
- }
63
- /**
64
- * Renders a template/view and returns an HTTP response with the generated HTML.
65
- *
66
- * @param view - View identifier or template path, depending on the template system.
67
- * @param params - Context variables available inside the template.
68
- * @param layout - Optional layout name used to wrap the rendered view.
69
- * @returns A `Promise<Response>` containing the rendered HTML.
70
- *
71
- * @example
72
- * return await render("users/profile", { user }, "main");
73
- */
74
- async function render(data, params) {
75
- return await response_1.Response.render(data, params);
76
- }
77
- /**
78
- * Sends a file as an HTTP response.
79
- *
80
- * This helper is a thin wrapper around `Response.sendFile`, allowing a file
81
- * to be streamed to the client while optionally applying custom headers
82
- * and resolving the file path relative to a root directory.
83
- *
84
- * @param filePath - Path to the file to send.
85
- * @param options - Optional configuration for the file response.
86
- * @param options.headers - Additional HTTP headers to include in the response
87
- * (e.g. `Content-Type`, `Cache-Control`, `Content-Disposition`).
88
- * @param options.root - Base directory used to resolve `filePath`.
89
- * @returns A `Response` object that streams the requested file to the client.
90
- *
91
- * @example
92
- * // Send a file using an absolute path
93
- * const response = await sendFile("/var/www/files/report.pdf", {});
94
- *
95
- * @example
96
- * // Send a file relative to a root directory
97
- * const response = await sendFile("report.pdf", {
98
- * root: "/var/www/files",
99
- * });
100
- *
101
- * @example
102
- * // Send a downloadable file
103
- * const response = await sendFile("report.pdf", {
104
- * root: "/var/www/files",
105
- * headers: {
106
- * "Content-Disposition": "attachment; filename=\"report.pdf\"",
107
- * },
108
- * });
109
- */
110
- async function sendFile(filePath, options) {
111
- return await response_1.Response.sendFile(filePath, options);
112
- }