Skip to content

Commit a31fd4a

Browse files
committed
feat: Add jssert function, alternative to node:assert
1 parent d9f8ea3 commit a31fd4a

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/core/error/jssert.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Jrror from "@/core/error"; // Ensure "@/core/error" exports a valid Jrror class or function
2+
/**
3+
* Implicitly asserts that a condition is true. If the condition is false, it throws an error with the provided message and documentation path.
4+
* Alternative to `assert(condition, message)` from the `node:assert` module.
5+
*
6+
* For naming convention, `jssert` is used to avoid confusion with the `assert` function from the `node:assert` module.
7+
*
8+
* @param {boolean} condition - The condition to assert.
9+
* @param {string} message - The error message to throw if the assertion fails.
10+
* @param {string} docsPath - The documentation path for the error.
11+
*/
12+
function jssert(condition: boolean, message: string, docsPath:string = "/assertion"): asserts condition {
13+
if (!condition) {
14+
throw new Jrror({
15+
code: 'assertion-failed',
16+
message,
17+
type: 'error',
18+
docsPath: docsPath,
19+
})
20+
}
21+
}
22+
export default jssert;

src/core/response.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ServerResponse } from "node:http";
22
import Jrror, { JoorError } from "@/core/error";
3-
import { HeaderSent } from "@/core/error/response";
43
import logger from '@/helpers/joorLogger';
5-
import Response, {RESPONSE_LOCATION_STATUS} from "@/types/response";
4+
import Response, { RESPONSE_LOCATION_STATUS } from "@/types/response";
5+
import assert from "node:assert";
6+
import jssert from "./error/jssert";
67

78
const response = ServerResponse.prototype as Response;
89

@@ -20,26 +21,9 @@ const response = ServerResponse.prototype as Response;
2021
*/
2122
response.status = function (this: ServerResponse, status: number): Response {
2223
try {
23-
if (this.headersSent) {
24-
throw HeaderSent; // Headers have already been sent
25-
}
26-
if (!Number.isInteger(status)) {
27-
throw new Jrror({
28-
code: "response-status-invalid",
29-
message: `Status must be a integer number, but ${status} is provided.`,
30-
type: "error",
31-
docsPath: "/response",
32-
});
33-
}
34-
35-
if (status < 100 || status > 999) {
36-
throw new Jrror({
37-
code: "response-status-invalid",
38-
message: `Status must be between 100 and 999, but ${status} is provided.`,
39-
type: "error",
40-
docsPath: "/response",
41-
});
42-
}
24+
jssert(!this.headersSent, "Headers have already been sent", "/response");
25+
jssert(!Number.isInteger(status), `Status must be a integer number, but ${status} is provided.`, "/response");
26+
jssert(status < 100 || status > 999, `Status must be between 100 and 999, but ${status} is provided.`, "/response");
4327
this.statusCode = status;
4428
}
4529
catch (error: unknown) {
@@ -70,9 +54,13 @@ For more information, see the [HTTP/1.1 RFC](https://datatracker.ietf.org/doc/ht
7054
*/
7155
response.location = function (this: ServerResponse, location: string, status: RESPONSE_LOCATION_STATUS = 301): void {
7256
try {
73-
if (this.headersSent) {
74-
throw HeaderSent; // Headers have already been sent
75-
}
57+
assert(typeof location === "string", "Location must be a string");
58+
assert(location.length > 0, "Location must not be empty");
59+
assert(
60+
status >= 300 && status <= 308,
61+
"Status must be between 300 and 308"
62+
);
63+
assert(!this.headersSent, "Headers have already been sent");
7664
this.setHeader("Location", location);
7765
this.status(status);
7866
}

0 commit comments

Comments
 (0)