@arkyn/server 3.0.1-beta.154 → 3.0.1-beta.156
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/index.js +1301 -37
- package/dist/modules/http/api/_logRequest.js +69 -0
- package/dist/modules/http/api/_makeRequest.js +69 -0
- package/dist/modules/http/api/deleteRequest.js +13 -0
- package/dist/modules/http/api/getRequest.js +12 -0
- package/dist/modules/http/api/patchRequest.js +13 -0
- package/dist/modules/http/api/postRequest.js +13 -0
- package/dist/modules/http/api/putRequest.js +13 -0
- package/dist/modules/http/badResponses/_badResponse.js +59 -0
- package/dist/modules/http/badResponses/badGateway.js +30 -0
- package/dist/modules/http/badResponses/badRequest.js +30 -0
- package/dist/modules/http/badResponses/conflict.js +30 -0
- package/dist/modules/http/badResponses/forbidden.js +30 -0
- package/dist/modules/http/badResponses/notFound.js +30 -0
- package/dist/modules/http/badResponses/notImplemented.js +30 -0
- package/dist/modules/http/badResponses/serverError.js +30 -0
- package/dist/modules/http/badResponses/unauthorized.js +30 -0
- package/dist/modules/http/badResponses/unprocessableEntity.js +36 -0
- package/dist/modules/http/successResponses/_successResponse.js +69 -0
- package/dist/modules/http/successResponses/created.js +30 -0
- package/dist/modules/http/successResponses/found.js +30 -0
- package/dist/modules/http/successResponses/noContent.js +21 -0
- package/dist/modules/http/successResponses/success.js +30 -0
- package/dist/modules/http/successResponses/updated.js +30 -0
- package/dist/modules/index.js +68 -0
- package/dist/modules/services/apiService.js +129 -0
- package/dist/modules/services/debugService.js +66 -0
- package/dist/modules/services/logMapperService.js +52 -0
- package/dist/modules/services/logService.js +31 -0
- package/dist/modules/utilities/decodeRequestBody.js +22 -0
- package/dist/modules/utilities/decodeRequestErrorMessage.js +7 -0
- package/dist/modules/utilities/errorHandler.js +54 -0
- package/dist/modules/utilities/flushDebugLogs.js +20 -0
- package/dist/modules/utilities/formAsyncParse.js +19 -0
- package/dist/modules/utilities/formParse.js +19 -0
- package/dist/modules/utilities/getScopedParams.js +11 -0
- package/dist/modules/utilities/schemaValidator.js +106 -0
- package/dist/modules/validations/validateCep.js +9 -0
- package/dist/modules/validations/validateCnpj.js +31 -0
- package/dist/modules/validations/validateCpf.js +28 -0
- package/dist/modules/validations/validateDate.js +27 -0
- package/dist/modules/validations/validateEmail.js +53 -0
- package/dist/modules/validations/validatePassword.js +16 -0
- package/dist/modules/validations/validatePhone.js +10 -0
- package/dist/modules/validations/validateRg.js +8 -0
- package/package.json +121 -87
- package/dist/bundle.js +0 -3014
- package/dist/bundle.umd.cjs +0 -9
- package/dist/http/api/_logRequest.js +0 -109
- package/dist/http/api/_makeRequest.js +0 -116
- package/dist/http/api/deleteRequest.js +0 -18
- package/dist/http/api/getRequest.js +0 -17
- package/dist/http/api/patchRequest.js +0 -18
- package/dist/http/api/postRequest.js +0 -18
- package/dist/http/api/putRequest.js +0 -18
- package/dist/http/badResponses/_badResponse.js +0 -62
- package/dist/http/badResponses/badGateway.js +0 -43
- package/dist/http/badResponses/badRequest.js +0 -41
- package/dist/http/badResponses/conflict.js +0 -42
- package/dist/http/badResponses/forbidden.js +0 -41
- package/dist/http/badResponses/notFound.js +0 -41
- package/dist/http/badResponses/notImplemented.js +0 -41
- package/dist/http/badResponses/serverError.js +0 -41
- package/dist/http/badResponses/unauthorized.js +0 -42
- package/dist/http/badResponses/unprocessableEntity.js +0 -53
- package/dist/http/successResponses/_successResponse.js +0 -75
- package/dist/http/successResponses/created.js +0 -41
- package/dist/http/successResponses/found.js +0 -41
- package/dist/http/successResponses/noContent.js +0 -32
- package/dist/http/successResponses/success.js +0 -41
- package/dist/http/successResponses/updated.js +0 -42
- package/dist/services/apiService.js +0 -174
- package/dist/services/debugService.js +0 -82
- package/dist/services/logMapperService.js +0 -68
- package/dist/services/logService.js +0 -35
- package/dist/utilities/decodeRequestBody.js +0 -40
- package/dist/utilities/decodeRequestErrorMessage.js +0 -36
- package/dist/utilities/errorHandler.js +0 -74
- package/dist/utilities/flushDebugLogs.js +0 -39
- package/dist/utilities/formAsyncParse.js +0 -37
- package/dist/utilities/formParse.js +0 -37
- package/dist/utilities/getScopedParams.js +0 -26
- package/dist/utilities/schemaValidator.js +0 -119
- package/dist/validations/validateCep.js +0 -27
- package/dist/validations/validateCnpj.js +0 -59
- package/dist/validations/validateCpf.js +0 -54
- package/dist/validations/validateDate.js +0 -51
- package/dist/validations/validateEmail.js +0 -111
- package/dist/validations/validatePassword.js +0 -34
- package/dist/validations/validatePhone.js +0 -28
- package/dist/validations/validateRg.js +0 -31
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { flushDebugLogs } from "../..";
|
|
2
|
-
import { LogMapperService } from "../../services/logMapperService";
|
|
3
|
-
import { logRequest } from "./_logRequest";
|
|
4
|
-
/**
|
|
5
|
-
* Makes an HTTP request using the Fetch API and returns a standardized response.
|
|
6
|
-
*
|
|
7
|
-
* @template {any} T - The expected type of the response data.
|
|
8
|
-
*
|
|
9
|
-
* @param {"POST" | "PUT" | "DELETE" | "PATCH" | "GET"} method - The HTTP method to use for the request. Supported methods are:
|
|
10
|
-
* - "POST": Create a new resource.
|
|
11
|
-
* - "PUT": Update an existing resource.
|
|
12
|
-
* - "DELETE": Remove a resource.
|
|
13
|
-
* - "PATCH": Partially update a resource.
|
|
14
|
-
* - "GET": Retrieve a resource.
|
|
15
|
-
* @param {string} url - The URL to which the request is sent.
|
|
16
|
-
* @param {HeadersInit} headers - Optional headers to include in the request. Defaults to an empty object.
|
|
17
|
-
* @param {any} body - Optional body to include in the request. Should be serializable to JSON.
|
|
18
|
-
*
|
|
19
|
-
* @returns {ApiResponseDTO<T>} A promise that resolves to an `ApiResponseDTO<T>` object containing:
|
|
20
|
-
* - `success`: A boolean indicating whether the request was successful.
|
|
21
|
-
* - `status`: The HTTP status code of the response.
|
|
22
|
-
* - `message`: A message describing the result of the request.
|
|
23
|
-
* - `response`: The parsed JSON response data, or `null` if parsing fails.
|
|
24
|
-
* - `cause`: Additional error information, if applicable.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* import { makeRequest } from "./makeRequest";
|
|
29
|
-
*
|
|
30
|
-
* async function fetchData() {
|
|
31
|
-
* const response = await makeRequest("GET", "https://api.example.com/data");
|
|
32
|
-
* if (response.success) {
|
|
33
|
-
* console.log("Data:", response.response);
|
|
34
|
-
* } else {
|
|
35
|
-
* console.error("Error:", response.message);
|
|
36
|
-
* }
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
async function makeRequest(input) {
|
|
41
|
-
let url = input.url;
|
|
42
|
-
if (input.urlParams) {
|
|
43
|
-
Object.entries(input.urlParams).forEach(([key, value]) => {
|
|
44
|
-
url = url.replaceAll(`:${key}`, value);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
const successMessage = {
|
|
48
|
-
POST: "Resource created successfully",
|
|
49
|
-
PUT: "Resource updated successfully",
|
|
50
|
-
DELETE: "Resource deleted successfully",
|
|
51
|
-
PATCH: "Resource patched successfully",
|
|
52
|
-
GET: "Request successful",
|
|
53
|
-
};
|
|
54
|
-
try {
|
|
55
|
-
const startTime = performance.now();
|
|
56
|
-
const headers = { ...input.headers, "Content-Type": "application/json" };
|
|
57
|
-
const response = await fetch(url, {
|
|
58
|
-
headers,
|
|
59
|
-
method: input.method,
|
|
60
|
-
body: input.body ? JSON.stringify(input.body) : undefined,
|
|
61
|
-
});
|
|
62
|
-
const elapsedTime = performance.now() - startTime;
|
|
63
|
-
const status = response.status;
|
|
64
|
-
let data = null;
|
|
65
|
-
try {
|
|
66
|
-
data = await response.json();
|
|
67
|
-
}
|
|
68
|
-
catch {
|
|
69
|
-
data = null;
|
|
70
|
-
}
|
|
71
|
-
const logData = LogMapperService.handle({
|
|
72
|
-
elapsedTime,
|
|
73
|
-
method: input.method,
|
|
74
|
-
queryParams: new URL(url).searchParams,
|
|
75
|
-
requestHeaders: headers,
|
|
76
|
-
requestBody: input.body,
|
|
77
|
-
responseBody: data,
|
|
78
|
-
responseHeaders: response.headers,
|
|
79
|
-
status,
|
|
80
|
-
rawUrl: input.url,
|
|
81
|
-
urlParams: input.urlParams,
|
|
82
|
-
});
|
|
83
|
-
logRequest(logData);
|
|
84
|
-
if (!response.ok) {
|
|
85
|
-
return {
|
|
86
|
-
success: false,
|
|
87
|
-
status,
|
|
88
|
-
message: data?.message || response.statusText || "Request failed",
|
|
89
|
-
response: data,
|
|
90
|
-
cause: null,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
success: true,
|
|
95
|
-
status,
|
|
96
|
-
message: data?.message || successMessage[input.method],
|
|
97
|
-
response: data,
|
|
98
|
-
cause: null,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
catch (err) {
|
|
102
|
-
flushDebugLogs({
|
|
103
|
-
debugs: [`Network error or request failed: ${err}`],
|
|
104
|
-
name: "MakeRequestError",
|
|
105
|
-
scheme: "red",
|
|
106
|
-
});
|
|
107
|
-
return {
|
|
108
|
-
success: false,
|
|
109
|
-
status: 0,
|
|
110
|
-
message: "Network error or request failed",
|
|
111
|
-
response: null,
|
|
112
|
-
cause: err instanceof Error ? err.message : String(err),
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
export { makeRequest };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { makeRequest } from "./_makeRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Sends a DELETE request to the specified URL with optional headers and body.
|
|
4
|
-
*
|
|
5
|
-
* @template T - The expected type of the response data.
|
|
6
|
-
* @param {InputProps} input - The options for the DELETE request, including URL, URL parameters, headers, and body.
|
|
7
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
8
|
-
*/
|
|
9
|
-
async function deleteRequest(input) {
|
|
10
|
-
return makeRequest({
|
|
11
|
-
method: "DELETE",
|
|
12
|
-
url: input.url,
|
|
13
|
-
urlParams: input.urlParams,
|
|
14
|
-
headers: input.headers,
|
|
15
|
-
body: input.body,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export { deleteRequest };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { makeRequest } from "./_makeRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Sends a GET request to the specified URL with optional headers.
|
|
4
|
-
*
|
|
5
|
-
* @template T - The expected type of the response data.
|
|
6
|
-
* @param {InputProps} input - The options for the GET request, including URL, URL parameters, and headers.
|
|
7
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
8
|
-
*/
|
|
9
|
-
async function getRequest(input) {
|
|
10
|
-
return makeRequest({
|
|
11
|
-
method: "GET",
|
|
12
|
-
url: input.url,
|
|
13
|
-
urlParams: input.urlParams,
|
|
14
|
-
headers: input.headers,
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
export { getRequest };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { makeRequest } from "./_makeRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Sends a PATCH request to the specified URL with optional headers and body.
|
|
4
|
-
*
|
|
5
|
-
* @template T - The expected type of the response data.
|
|
6
|
-
* @param {InputProps} input - The options for the PATCH request, including URL, URL parameters, headers, and body.
|
|
7
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
8
|
-
*/
|
|
9
|
-
async function patchRequest(input) {
|
|
10
|
-
return makeRequest({
|
|
11
|
-
method: "PATCH",
|
|
12
|
-
url: input.url,
|
|
13
|
-
urlParams: input.urlParams,
|
|
14
|
-
headers: input.headers,
|
|
15
|
-
body: input.body,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export { patchRequest };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { makeRequest } from "./_makeRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Sends a POST request to the specified URL with optional headers and body.
|
|
4
|
-
*
|
|
5
|
-
* @template T - The expected type of the response data.
|
|
6
|
-
* @param {InputProps} input - The options for the POST request, including URL, URL parameters, headers, and body.
|
|
7
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
8
|
-
*/
|
|
9
|
-
async function postRequest(input) {
|
|
10
|
-
return makeRequest({
|
|
11
|
-
method: "POST",
|
|
12
|
-
url: input.url,
|
|
13
|
-
urlParams: input.urlParams,
|
|
14
|
-
headers: input.headers,
|
|
15
|
-
body: input.body,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export { postRequest };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { makeRequest } from "./_makeRequest";
|
|
2
|
-
/**
|
|
3
|
-
* Sends a PUT request to the specified URL with optional headers and body.
|
|
4
|
-
*
|
|
5
|
-
* @template T - The expected type of the response data.
|
|
6
|
-
* @param {InputProps} input - The options for the PUT request, including URL, URL parameters, headers, and body.
|
|
7
|
-
* @returns {Promise<ApiResponseDTO<T>>} A promise that resolves to the API response.
|
|
8
|
-
*/
|
|
9
|
-
async function putRequest(input) {
|
|
10
|
-
return makeRequest({
|
|
11
|
-
method: "PUT",
|
|
12
|
-
url: input.url,
|
|
13
|
-
urlParams: input.urlParams,
|
|
14
|
-
headers: input.headers,
|
|
15
|
-
body: input.body,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export { putRequest };
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { formatJsonString } from "@arkyn/shared";
|
|
2
|
-
import { DebugService } from "../../services/debugService";
|
|
3
|
-
import { flushDebugLogs } from "../../utilities/flushDebugLogs";
|
|
4
|
-
class BadResponse {
|
|
5
|
-
_cause;
|
|
6
|
-
_name = "BadResponse";
|
|
7
|
-
_status = 500;
|
|
8
|
-
_statusText = "Unknown error";
|
|
9
|
-
_debugColor = "red";
|
|
10
|
-
get cause() {
|
|
11
|
-
return this._cause;
|
|
12
|
-
}
|
|
13
|
-
set cause(value) {
|
|
14
|
-
this._cause = value;
|
|
15
|
-
}
|
|
16
|
-
get name() {
|
|
17
|
-
return this._name;
|
|
18
|
-
}
|
|
19
|
-
set name(value) {
|
|
20
|
-
this._name = value;
|
|
21
|
-
}
|
|
22
|
-
get status() {
|
|
23
|
-
return this._status;
|
|
24
|
-
}
|
|
25
|
-
set status(value) {
|
|
26
|
-
this._status = value;
|
|
27
|
-
}
|
|
28
|
-
get statusText() {
|
|
29
|
-
return this._statusText;
|
|
30
|
-
}
|
|
31
|
-
set statusText(value) {
|
|
32
|
-
this._statusText = value;
|
|
33
|
-
}
|
|
34
|
-
get debugColor() {
|
|
35
|
-
return this._debugColor;
|
|
36
|
-
}
|
|
37
|
-
set debugColor(value) {
|
|
38
|
-
if (!["green", "yellow", "cyan", "red"].includes(value))
|
|
39
|
-
return;
|
|
40
|
-
this._debugColor = value;
|
|
41
|
-
}
|
|
42
|
-
onDebug() {
|
|
43
|
-
const debugs = [];
|
|
44
|
-
const { callerInfo, functionName } = DebugService.getCaller();
|
|
45
|
-
debugs.push(`Caller Function: ${functionName}`);
|
|
46
|
-
debugs.push(`Caller Location: ${callerInfo}`);
|
|
47
|
-
if (this._statusText)
|
|
48
|
-
debugs.push(`Message: ${this._statusText}`);
|
|
49
|
-
if (this._cause) {
|
|
50
|
-
debugs.push(`Cause: ${formatJsonString(JSON.stringify(this._cause))}`);
|
|
51
|
-
}
|
|
52
|
-
flushDebugLogs({ scheme: "red", name: this._name, debugs });
|
|
53
|
-
}
|
|
54
|
-
makeBody() {
|
|
55
|
-
return {
|
|
56
|
-
name: this._name,
|
|
57
|
-
message: this._statusText,
|
|
58
|
-
cause: this._cause,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export { BadResponse };
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 502 Bad Gateway — the upstream server returned an invalid or unexpected response.
|
|
4
|
-
*
|
|
5
|
-
* Throw inside a server action/loader and catch with `errorHandler`, or call `.toJson()` directly.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* throw new BadGateway("Payment gateway unavailable");
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
class BadGateway extends BadResponse {
|
|
13
|
-
/**
|
|
14
|
-
* @param message - Error description sent in the response body and logged for debugging.
|
|
15
|
-
* @param cause - Optional extra context (serialized to JSON in the response).
|
|
16
|
-
*/
|
|
17
|
-
constructor(message, cause) {
|
|
18
|
-
super();
|
|
19
|
-
this.name = "BadGateway";
|
|
20
|
-
this.status = 502;
|
|
21
|
-
this.statusText = message;
|
|
22
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
23
|
-
this.onDebug();
|
|
24
|
-
}
|
|
25
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
26
|
-
toResponse() {
|
|
27
|
-
const responseInit = {
|
|
28
|
-
headers: { "Content-Type": "application/json" },
|
|
29
|
-
status: this.status,
|
|
30
|
-
statusText: this.statusText,
|
|
31
|
-
};
|
|
32
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
33
|
-
}
|
|
34
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
35
|
-
toJson() {
|
|
36
|
-
const responseInit = {
|
|
37
|
-
status: this.status,
|
|
38
|
-
statusText: this.statusText,
|
|
39
|
-
};
|
|
40
|
-
return Response.json(this.makeBody(), responseInit);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
export { BadGateway };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 400 Bad Request — the request is malformed or contains invalid data.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new BadRequest("Invalid request body");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class BadRequest extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "BadRequest";
|
|
18
|
-
this.status = 400;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.makeBody(), responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { BadRequest };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 409 Conflict — the request conflicts with the current state of the server (e.g. duplicate record).
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new Conflict("Email already in use");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class Conflict extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "Conflict";
|
|
18
|
-
this.status = 409;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.debugColor = "yellow";
|
|
21
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
22
|
-
this.onDebug();
|
|
23
|
-
}
|
|
24
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
25
|
-
toResponse() {
|
|
26
|
-
const responseInit = {
|
|
27
|
-
headers: { "Content-Type": "application/json" },
|
|
28
|
-
status: this.status,
|
|
29
|
-
statusText: this.statusText,
|
|
30
|
-
};
|
|
31
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
32
|
-
}
|
|
33
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
34
|
-
toJson() {
|
|
35
|
-
const responseInit = {
|
|
36
|
-
status: this.status,
|
|
37
|
-
statusText: this.statusText,
|
|
38
|
-
};
|
|
39
|
-
return Response.json(this.makeBody(), responseInit);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
export { Conflict };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 403 Forbidden — authenticated but not authorized to access this resource.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new Forbidden("You don't have permission to delete this resource");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class Forbidden extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "Forbidden";
|
|
18
|
-
this.status = 403;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.makeBody(), responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { Forbidden };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 404 Not Found — the requested resource does not exist.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new NotFound("Product not found");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class NotFound extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "NotFound";
|
|
18
|
-
this.status = 404;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.makeBody(), responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { NotFound };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 501 Not Implemented — the server does not support the functionality required to fulfill the request.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new NotImplemented("Webhook delivery is not yet implemented");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class NotImplemented extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "NotImplemented";
|
|
18
|
-
this.status = 501;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.makeBody(), responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { NotImplemented };
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 500 Internal Server Error — an unexpected condition prevented the server from fulfilling the request.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new ServerError("Failed to connect to the database");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class ServerError extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "ServerError";
|
|
18
|
-
this.status = 500;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
21
|
-
this.onDebug();
|
|
22
|
-
}
|
|
23
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
24
|
-
toResponse() {
|
|
25
|
-
const responseInit = {
|
|
26
|
-
headers: { "Content-Type": "application/json" },
|
|
27
|
-
status: this.status,
|
|
28
|
-
statusText: this.statusText,
|
|
29
|
-
};
|
|
30
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
31
|
-
}
|
|
32
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
33
|
-
toJson() {
|
|
34
|
-
const responseInit = {
|
|
35
|
-
status: this.status,
|
|
36
|
-
statusText: this.statusText,
|
|
37
|
-
};
|
|
38
|
-
return Response.json(this.makeBody(), responseInit);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export { ServerError };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 401 Unauthorized — the request lacks valid authentication credentials.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* throw new Unauthorized("Invalid or expired token");
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
class Unauthorized extends BadResponse {
|
|
11
|
-
/**
|
|
12
|
-
* @param message - Error description.
|
|
13
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
14
|
-
*/
|
|
15
|
-
constructor(message, cause) {
|
|
16
|
-
super();
|
|
17
|
-
this.name = "Unauthorized";
|
|
18
|
-
this.status = 401;
|
|
19
|
-
this.statusText = message;
|
|
20
|
-
this.debugColor = "yellow";
|
|
21
|
-
this.cause = cause ? JSON.stringify(cause) : undefined;
|
|
22
|
-
this.onDebug();
|
|
23
|
-
}
|
|
24
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
25
|
-
toResponse() {
|
|
26
|
-
const responseInit = {
|
|
27
|
-
headers: { "Content-Type": "application/json" },
|
|
28
|
-
status: this.status,
|
|
29
|
-
statusText: this.statusText,
|
|
30
|
-
};
|
|
31
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
32
|
-
}
|
|
33
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
34
|
-
toJson() {
|
|
35
|
-
const responseInit = {
|
|
36
|
-
status: this.status,
|
|
37
|
-
statusText: this.statusText,
|
|
38
|
-
};
|
|
39
|
-
return Response.json(this.makeBody(), responseInit);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
export { Unauthorized };
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { BadResponse } from "./_badResponse";
|
|
2
|
-
/**
|
|
3
|
-
* HTTP 422 Unprocessable Entity — the request is well-formed but contains semantic validation errors.
|
|
4
|
-
* Typically used for form field validation failures.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* throw new UnprocessableEntity({
|
|
9
|
-
* message: "Validation failed",
|
|
10
|
-
* fieldErrors: { email: "Invalid email format", age: "Must be 18 or older" },
|
|
11
|
-
* fields: { email: "not-an-email", age: "15" },
|
|
12
|
-
* });
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
class UnprocessableEntity extends BadResponse {
|
|
16
|
-
/**
|
|
17
|
-
* @param props.message - Human-readable description of the error.
|
|
18
|
-
* @param props.fieldErrors - Per-field validation messages keyed by field name.
|
|
19
|
-
* @param props.fields - Original submitted field values (useful for repopulating forms).
|
|
20
|
-
* @param props.data - Any extra data to include in the response body.
|
|
21
|
-
*/
|
|
22
|
-
constructor(props) {
|
|
23
|
-
super();
|
|
24
|
-
this.name = "UnprocessableEntity";
|
|
25
|
-
this.status = 422;
|
|
26
|
-
this.statusText = props.message || "Unprocessable entity";
|
|
27
|
-
this.debugColor = "yellow";
|
|
28
|
-
this.cause = {
|
|
29
|
-
data: props.data,
|
|
30
|
-
fieldErrors: props.fieldErrors,
|
|
31
|
-
fields: props.fields,
|
|
32
|
-
};
|
|
33
|
-
this.onDebug();
|
|
34
|
-
}
|
|
35
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
36
|
-
toResponse() {
|
|
37
|
-
const responseInit = {
|
|
38
|
-
headers: { "Content-Type": "application/json" },
|
|
39
|
-
status: this.status,
|
|
40
|
-
statusText: this.statusText,
|
|
41
|
-
};
|
|
42
|
-
return new Response(JSON.stringify(this.makeBody()), responseInit);
|
|
43
|
-
}
|
|
44
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
45
|
-
toJson() {
|
|
46
|
-
const responseInit = {
|
|
47
|
-
status: this.status,
|
|
48
|
-
statusText: this.statusText,
|
|
49
|
-
};
|
|
50
|
-
return Response.json(this.makeBody(), responseInit);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export { UnprocessableEntity };
|