xypriss 9.10.21 → 9.10.22
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/cjs/src/index.js +2 -0
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/utils/Send.js +170 -0
- package/dist/cjs/src/utils/Send.js.map +1 -0
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/utils/Send.js +168 -0
- package/dist/esm/src/utils/Send.js.map +1 -0
- package/dist/index.d.ts +173 -2
- package/package.json +1 -1
package/dist/cjs/src/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var XyGuard = require('./server/routing/modules/XyGuard.js');
|
|
|
16
16
|
var PluginHookIds = require('./plugins/const/PluginHookIds.js');
|
|
17
17
|
var mergeWithDefaults = require('./utils/mergeWithDefaults.js');
|
|
18
18
|
var getIp = require('./utils/getIp.js');
|
|
19
|
+
var Send = require('./utils/Send.js');
|
|
19
20
|
var ProjectDiscovery = require('./utils/ProjectDiscovery.js');
|
|
20
21
|
var XemsPlugin = require('./plugins/builtin/xems/XemsPlugin.js');
|
|
21
22
|
var getMime = require('./utils/getMime.js');
|
|
@@ -84,6 +85,7 @@ exports.PluginHookIds = PluginHookIds.PluginHookIds;
|
|
|
84
85
|
exports.mergeWithDefaults = mergeWithDefaults.mergeWithDefaults;
|
|
85
86
|
exports.mwdef = mergeWithDefaults.mergeWithDefaults;
|
|
86
87
|
exports.getIp = getIp.getIp;
|
|
88
|
+
exports.Send = Send.Send;
|
|
87
89
|
exports.getCallerProjectRoot = ProjectDiscovery.getCallerProjectRoot;
|
|
88
90
|
exports.identifyProjectRoot = ProjectDiscovery.identifyProjectRoot;
|
|
89
91
|
exports.xems = XemsPlugin.xems;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/index.ts"],"sourcesContent":[null],"names":["configLoader","XyPrissRouter"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/index.ts"],"sourcesContent":[null],"names":["configLoader","XyPrissRouter"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;AAsBiF;AACjF;AAEA;AACA;AAqBA;AACA,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;IACnCA,yBAAY,CAAC,qBAAqB,EAAE;AACxC;AA+BA;;AAEG;SACa,MAAM,GAAA;IAClB,OAAO,IAAIC,sBAAa,CAAC;AACrB,QAAA,aAAa,EAAE,KAAK;AACpB,QAAA,WAAW,EAAE,KAAK;AAClB,QAAA,MAAM,EAAE,KAAK;AAChB,KAAA,CAAC;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// Helpers
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
/**
|
|
7
|
+
* Derives a compact, uppercase error code from a {@link SupportedStatus} key.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* buildErrorCode("BAD_REQUEST") // → "EBADR"
|
|
11
|
+
* buildErrorCode("INTERNAL_SERVER_ERR") // → "EINTE"
|
|
12
|
+
*
|
|
13
|
+
* @param status - The status key to convert.
|
|
14
|
+
* @returns A 5-character string starting with `"E"`.
|
|
15
|
+
*/
|
|
16
|
+
function buildErrorCode(status) {
|
|
17
|
+
// Remove all underscores, take first 4 chars, prepend "E"
|
|
18
|
+
return "E" + status.replace(/_/g, "").slice(0, 4).toUpperCase();
|
|
19
|
+
}
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Default HTTP status code registry
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
const DEFAULT_CONFIGS = {
|
|
24
|
+
BAD_REQUEST: 400,
|
|
25
|
+
NOT_FOUND: 404,
|
|
26
|
+
TOO_MANY_REQUEST: 429,
|
|
27
|
+
INTERNAL_SERVER_ERR: 500,
|
|
28
|
+
};
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Send class
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
/**
|
|
33
|
+
* A structured HTTP response helper that standardises all error responses
|
|
34
|
+
* across the application.
|
|
35
|
+
*
|
|
36
|
+
* Every method sends a JSON body conforming to {@link IResTemplate}, ensuring
|
|
37
|
+
* consistent error shapes for API consumers regardless of where the error
|
|
38
|
+
* originates.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const send = new Send(res);
|
|
43
|
+
*
|
|
44
|
+
* // 400 – validation failure
|
|
45
|
+
* send.badRequest("The 'email' field is required.");
|
|
46
|
+
*
|
|
47
|
+
* // 404 – resource missing
|
|
48
|
+
* send.notFound("User not found.", { userId: 42 });
|
|
49
|
+
*
|
|
50
|
+
* // 429 – rate limit exceeded
|
|
51
|
+
* send.tooManyRequest("Too many requests. Please slow down.");
|
|
52
|
+
*
|
|
53
|
+
* // 500 – unexpected failure
|
|
54
|
+
* send.internalError("An unexpected error occurred.");
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
class Send {
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new `Send` instance bound to the given response object.
|
|
60
|
+
*
|
|
61
|
+
* @param res - The active `XyPrisResponse` to write into.
|
|
62
|
+
* @param configs - Optional override for the default status-code registry.
|
|
63
|
+
* Useful for non-standard or custom HTTP codes.
|
|
64
|
+
*/
|
|
65
|
+
constructor(res, configs = {
|
|
66
|
+
includeServerName: true,
|
|
67
|
+
statusCode: {},
|
|
68
|
+
}) {
|
|
69
|
+
// -------------------------------------------------------------------------
|
|
70
|
+
// Public methods
|
|
71
|
+
// -------------------------------------------------------------------------
|
|
72
|
+
/**
|
|
73
|
+
* Sends a **400 Bad Request** response.
|
|
74
|
+
*
|
|
75
|
+
* Use when the client submits a malformed or invalid request
|
|
76
|
+
* (e.g. missing required fields, invalid format, constraint violation).
|
|
77
|
+
*
|
|
78
|
+
* @param message - Human-readable explanation of why the request was rejected.
|
|
79
|
+
* @param data - Optional payload (e.g. a list of validation errors per field).
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* send.badRequest("The 'username' field must be at least 3 characters.");
|
|
83
|
+
* send.badRequest("Validation failed.", { fields: { email: "Invalid format" } });
|
|
84
|
+
*/
|
|
85
|
+
this.badRequest = (message, data) => {
|
|
86
|
+
this.dispatch("BAD_REQUEST", "Bad Request", message, data);
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Sends a **404 Not Found** response.
|
|
90
|
+
*
|
|
91
|
+
* Use when the requested resource does not exist or has been permanently removed.
|
|
92
|
+
*
|
|
93
|
+
* @param message - Human-readable explanation of what could not be found.
|
|
94
|
+
* @param data - Optional payload (e.g. the identifier that was looked up).
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* send.notFound("No user found with id '42'.");
|
|
98
|
+
* send.notFound("Resource not found.", { id: "42" });
|
|
99
|
+
*/
|
|
100
|
+
this.notFound = (message, data) => {
|
|
101
|
+
this.dispatch("NOT_FOUND", "Not Found", message, data);
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Sends a **429 Too Many Requests** response.
|
|
105
|
+
*
|
|
106
|
+
* Use when the client has exceeded an allowed request rate or quota.
|
|
107
|
+
* Consider pairing this with a `Retry-After` header at the middleware level.
|
|
108
|
+
*
|
|
109
|
+
* @param message - Human-readable explanation of the rate limit breach.
|
|
110
|
+
* @param data - Optional payload (e.g. retry delay, remaining quota).
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* send.tooManyRequest("Rate limit reached. Try again in 60 seconds.");
|
|
114
|
+
* send.tooManyRequest("Quota exceeded.", { retryAfter: 60 });
|
|
115
|
+
*/
|
|
116
|
+
this.tooManyRequest = (message, data) => {
|
|
117
|
+
this.dispatch("TOO_MANY_REQUEST", "Too Many Requests", message, data);
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Sends a **500 Internal Server Error** response.
|
|
121
|
+
*
|
|
122
|
+
* Use for unexpected, unhandled server-side failures.
|
|
123
|
+
* Avoid leaking internal stack traces or sensitive details in the message.
|
|
124
|
+
*
|
|
125
|
+
* @param message - Human-readable explanation safe to expose to the client.
|
|
126
|
+
* @param data - Optional payload (use sparingly — never expose raw stack traces).
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* send.internalError("An unexpected error occurred. Please try again later.");
|
|
130
|
+
*/
|
|
131
|
+
this.internalError = (message, data) => {
|
|
132
|
+
this.dispatch("INTERNAL_SERVER_ERR", "Internal Server Error", message, data);
|
|
133
|
+
};
|
|
134
|
+
this.res = res;
|
|
135
|
+
this.configs = { ...DEFAULT_CONFIGS, ...configs.statusCode };
|
|
136
|
+
this.serverName = __sys__.vars.__name__;
|
|
137
|
+
this.includeServerName = configs?.includeServerName;
|
|
138
|
+
}
|
|
139
|
+
// -------------------------------------------------------------------------
|
|
140
|
+
// Core dispatcher
|
|
141
|
+
// -------------------------------------------------------------------------
|
|
142
|
+
/**
|
|
143
|
+
* Resolves the status code, builds the full response body, and flushes it.
|
|
144
|
+
*
|
|
145
|
+
* @param statusKey - One of the {@link SupportedStatus} keys.
|
|
146
|
+
* @param errorLabel - Human-readable short label for the error type.
|
|
147
|
+
* @param message - Optional caller-supplied message; falls back to `errorLabel`.
|
|
148
|
+
* @param data - Optional payload to attach to the response body.
|
|
149
|
+
*/
|
|
150
|
+
dispatch(statusKey, errorLabel, message, data) {
|
|
151
|
+
const statusCode = this.configs[statusKey];
|
|
152
|
+
const body = {
|
|
153
|
+
success: false,
|
|
154
|
+
message: message ?? errorLabel,
|
|
155
|
+
serverName: this.includeServerName
|
|
156
|
+
? this.serverName
|
|
157
|
+
: undefined,
|
|
158
|
+
...(data !== undefined && { data }),
|
|
159
|
+
details: {
|
|
160
|
+
error: errorLabel,
|
|
161
|
+
errorCode: buildErrorCode(statusKey),
|
|
162
|
+
statusCode,
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
this.res.status(statusCode).json(body);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
exports.Send = Send;
|
|
170
|
+
//# sourceMappingURL=Send.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Send.js","sources":["../../../../src/utils/Send.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAsFA;AACA;AACA;AAEA;;;;;;;;;AASG;AACH,SAAS,cAAc,CAAC,MAAuB,EAAA;;IAE7C,OAAO,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE;AACjE;AAEA;AACA;AACA;AAEA,MAAM,eAAe,GAAe;AAClC,IAAA,WAAW,EAAE,GAAG;AAChB,IAAA,SAAS,EAAE,GAAG;AACd,IAAA,gBAAgB,EAAE,GAAG;AACrB,IAAA,mBAAmB,EAAE,GAAG;CACzB;AAED;AACA;AACA;AAEA;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;MACU,IAAI,CAAA;AAMf;;;;;;AAMG;IACH,WAAA,CACE,GAAmB,EACnB,OAAA,GAGK;AACH,QAAA,iBAAiB,EAAE,IAAI;AACvB,QAAA,UAAU,EAAE,EAAE;AACf,KAAA,EAAA;;;;AAiDH;;;;;;;;;;;;AAYG;AACI,QAAA,IAAA,CAAA,UAAU,GAAiB,CAAC,OAAO,EAAE,IAAI,KAAI;YAClD,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,IAAI,CAAC;AAC5D,QAAA,CAAC;AAED;;;;;;;;;;;AAWG;AACI,QAAA,IAAA,CAAA,QAAQ,GAAiB,CAAC,OAAO,EAAE,IAAI,KAAI;YAChD,IAAI,CAAC,QAAQ,CAAC,WAA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I,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;IACxC;AA4ED;;;;"}
|
package/dist/esm/src/index.js
CHANGED
|
@@ -14,6 +14,7 @@ export { XyGuard } from './server/routing/modules/XyGuard.js';
|
|
|
14
14
|
export { PluginHookIds } from './plugins/const/PluginHookIds.js';
|
|
15
15
|
export { mergeWithDefaults, mergeWithDefaults as mwdef } from './utils/mergeWithDefaults.js';
|
|
16
16
|
export { getIp } from './utils/getIp.js';
|
|
17
|
+
export { Send } from './utils/Send.js';
|
|
17
18
|
export { getCallerProjectRoot, identifyProjectRoot } from './utils/ProjectDiscovery.js';
|
|
18
19
|
export { xems } from './plugins/builtin/xems/XemsPlugin.js';
|
|
19
20
|
export { getMime, getMimes } from './utils/getMime.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;AAsBiF;AACjF;AAEA;AACA;AAqBA;AACA,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;IACnC,YAAY,CAAC,qBAAqB,EAAE;AACxC;AA+BA;;AAEG;SACa,MAAM,GAAA;IAClB,OAAO,IAAI,aAAa,CAAC;AACrB,QAAA,aAAa,EAAE,KAAK;AACpB,QAAA,WAAW,EAAE,KAAK;AAClB,QAAA,MAAM,EAAE,KAAK;AAChB,KAAA,CAAC;AACN;;;;"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Helpers
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
/**
|
|
5
|
+
* Derives a compact, uppercase error code from a {@link SupportedStatus} key.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* buildErrorCode("BAD_REQUEST") // → "EBADR"
|
|
9
|
+
* buildErrorCode("INTERNAL_SERVER_ERR") // → "EINTE"
|
|
10
|
+
*
|
|
11
|
+
* @param status - The status key to convert.
|
|
12
|
+
* @returns A 5-character string starting with `"E"`.
|
|
13
|
+
*/
|
|
14
|
+
function buildErrorCode(status) {
|
|
15
|
+
// Remove all underscores, take first 4 chars, prepend "E"
|
|
16
|
+
return "E" + status.replace(/_/g, "").slice(0, 4).toUpperCase();
|
|
17
|
+
}
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Default HTTP status code registry
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
const DEFAULT_CONFIGS = {
|
|
22
|
+
BAD_REQUEST: 400,
|
|
23
|
+
NOT_FOUND: 404,
|
|
24
|
+
TOO_MANY_REQUEST: 429,
|
|
25
|
+
INTERNAL_SERVER_ERR: 500,
|
|
26
|
+
};
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Send class
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
/**
|
|
31
|
+
* A structured HTTP response helper that standardises all error responses
|
|
32
|
+
* across the application.
|
|
33
|
+
*
|
|
34
|
+
* Every method sends a JSON body conforming to {@link IResTemplate}, ensuring
|
|
35
|
+
* consistent error shapes for API consumers regardless of where the error
|
|
36
|
+
* originates.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const send = new Send(res);
|
|
41
|
+
*
|
|
42
|
+
* // 400 – validation failure
|
|
43
|
+
* send.badRequest("The 'email' field is required.");
|
|
44
|
+
*
|
|
45
|
+
* // 404 – resource missing
|
|
46
|
+
* send.notFound("User not found.", { userId: 42 });
|
|
47
|
+
*
|
|
48
|
+
* // 429 – rate limit exceeded
|
|
49
|
+
* send.tooManyRequest("Too many requests. Please slow down.");
|
|
50
|
+
*
|
|
51
|
+
* // 500 – unexpected failure
|
|
52
|
+
* send.internalError("An unexpected error occurred.");
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
class Send {
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new `Send` instance bound to the given response object.
|
|
58
|
+
*
|
|
59
|
+
* @param res - The active `XyPrisResponse` to write into.
|
|
60
|
+
* @param configs - Optional override for the default status-code registry.
|
|
61
|
+
* Useful for non-standard or custom HTTP codes.
|
|
62
|
+
*/
|
|
63
|
+
constructor(res, configs = {
|
|
64
|
+
includeServerName: true,
|
|
65
|
+
statusCode: {},
|
|
66
|
+
}) {
|
|
67
|
+
// -------------------------------------------------------------------------
|
|
68
|
+
// Public methods
|
|
69
|
+
// -------------------------------------------------------------------------
|
|
70
|
+
/**
|
|
71
|
+
* Sends a **400 Bad Request** response.
|
|
72
|
+
*
|
|
73
|
+
* Use when the client submits a malformed or invalid request
|
|
74
|
+
* (e.g. missing required fields, invalid format, constraint violation).
|
|
75
|
+
*
|
|
76
|
+
* @param message - Human-readable explanation of why the request was rejected.
|
|
77
|
+
* @param data - Optional payload (e.g. a list of validation errors per field).
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* send.badRequest("The 'username' field must be at least 3 characters.");
|
|
81
|
+
* send.badRequest("Validation failed.", { fields: { email: "Invalid format" } });
|
|
82
|
+
*/
|
|
83
|
+
this.badRequest = (message, data) => {
|
|
84
|
+
this.dispatch("BAD_REQUEST", "Bad Request", message, data);
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Sends a **404 Not Found** response.
|
|
88
|
+
*
|
|
89
|
+
* Use when the requested resource does not exist or has been permanently removed.
|
|
90
|
+
*
|
|
91
|
+
* @param message - Human-readable explanation of what could not be found.
|
|
92
|
+
* @param data - Optional payload (e.g. the identifier that was looked up).
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* send.notFound("No user found with id '42'.");
|
|
96
|
+
* send.notFound("Resource not found.", { id: "42" });
|
|
97
|
+
*/
|
|
98
|
+
this.notFound = (message, data) => {
|
|
99
|
+
this.dispatch("NOT_FOUND", "Not Found", message, data);
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Sends a **429 Too Many Requests** response.
|
|
103
|
+
*
|
|
104
|
+
* Use when the client has exceeded an allowed request rate or quota.
|
|
105
|
+
* Consider pairing this with a `Retry-After` header at the middleware level.
|
|
106
|
+
*
|
|
107
|
+
* @param message - Human-readable explanation of the rate limit breach.
|
|
108
|
+
* @param data - Optional payload (e.g. retry delay, remaining quota).
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* send.tooManyRequest("Rate limit reached. Try again in 60 seconds.");
|
|
112
|
+
* send.tooManyRequest("Quota exceeded.", { retryAfter: 60 });
|
|
113
|
+
*/
|
|
114
|
+
this.tooManyRequest = (message, data) => {
|
|
115
|
+
this.dispatch("TOO_MANY_REQUEST", "Too Many Requests", message, data);
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Sends a **500 Internal Server Error** response.
|
|
119
|
+
*
|
|
120
|
+
* Use for unexpected, unhandled server-side failures.
|
|
121
|
+
* Avoid leaking internal stack traces or sensitive details in the message.
|
|
122
|
+
*
|
|
123
|
+
* @param message - Human-readable explanation safe to expose to the client.
|
|
124
|
+
* @param data - Optional payload (use sparingly — never expose raw stack traces).
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* send.internalError("An unexpected error occurred. Please try again later.");
|
|
128
|
+
*/
|
|
129
|
+
this.internalError = (message, data) => {
|
|
130
|
+
this.dispatch("INTERNAL_SERVER_ERR", "Internal Server Error", message, data);
|
|
131
|
+
};
|
|
132
|
+
this.res = res;
|
|
133
|
+
this.configs = { ...DEFAULT_CONFIGS, ...configs.statusCode };
|
|
134
|
+
this.serverName = __sys__.vars.__name__;
|
|
135
|
+
this.includeServerName = configs?.includeServerName;
|
|
136
|
+
}
|
|
137
|
+
// -------------------------------------------------------------------------
|
|
138
|
+
// Core dispatcher
|
|
139
|
+
// -------------------------------------------------------------------------
|
|
140
|
+
/**
|
|
141
|
+
* Resolves the status code, builds the full response body, and flushes it.
|
|
142
|
+
*
|
|
143
|
+
* @param statusKey - One of the {@link SupportedStatus} keys.
|
|
144
|
+
* @param errorLabel - Human-readable short label for the error type.
|
|
145
|
+
* @param message - Optional caller-supplied message; falls back to `errorLabel`.
|
|
146
|
+
* @param data - Optional payload to attach to the response body.
|
|
147
|
+
*/
|
|
148
|
+
dispatch(statusKey, errorLabel, message, data) {
|
|
149
|
+
const statusCode = this.configs[statusKey];
|
|
150
|
+
const body = {
|
|
151
|
+
success: false,
|
|
152
|
+
message: message ?? errorLabel,
|
|
153
|
+
serverName: this.includeServerName
|
|
154
|
+
? this.serverName
|
|
155
|
+
: undefined,
|
|
156
|
+
...(data !== undefined && { data }),
|
|
157
|
+
details: {
|
|
158
|
+
error: errorLabel,
|
|
159
|
+
errorCode: buildErrorCode(statusKey),
|
|
160
|
+
statusCode,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
this.res.status(statusCode).json(body);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export { Send };
|
|
168
|
+
//# sourceMappingURL=Send.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Send.js","sources":["../../../../src/utils/Send.ts"],"sourcesContent":[null],"names":[],"mappings":"AAsFA;AACA;AACA;AAEA;;;;;;;;;AASG;AACH,SAAS,cAAc,CAAC,MAAuB,EAAA;;IAE7C,OAAO,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE;AACjE;AAEA;AACA;AACA;AAEA,MAAM,eAAe,GAAe;AAClC,IAAA,WAAW,EAAE,GAAG;AAChB,IAAA,SAAS,EAAE,GAAG;AACd,IAAA,gBAAgB,EAAE,GAAG;AACrB,IAAA,mBAAmB,EAAE,GAAG;CACzB;AAED;AACA;AACA;AA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OG;AACK,IAAA,QAAQ,CACd,SAA0B,EAC1B,UAAkB,EAClB,OAAgB,EAChB,IAAc,EAAA;QAEd,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;AAE1C,QAAA,MAAM,IAAI,GAAiB;AACzB,YAAA,OAAO,EAAE,KAAK;YACd,OAAO,EAAE,OAAO,IAAI,UAAU;YAC9B,UAAU,EAAE,IAAI,CAAC;kBACb,IAAI,CAAC;AACP,kBAAG,SAA4B;YACjC,IAAI,IAAI,KAAK,SAAS,IAAI,EAAE,IAAI,EAAE,CAAC;AACnC,YAAA,OAAO,EAAE;AACP,gBAAA,KAAK,EAAE,UAAU;AACjB,gBAAA,SAAS,EAAE,cAAc,CAAC,SAAS,CAAC;gBACpC,UAAU;AACX,aAAA;SACF;AAED,QAAA,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;IACxC;AA4ED;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Server, IncomingMessage, ServerResponse } from 'http';
|
|
|
2
2
|
import { SecureCacheAdapter } from 'xypriss-security';
|
|
3
3
|
import * as reliant_type from 'reliant-type';
|
|
4
4
|
import { Readable, Writable } from 'node:stream';
|
|
5
|
+
import { XyPrisResponse as XyPrisResponse$2 } from 'xypriss';
|
|
5
6
|
|
|
6
7
|
declare const LOG_LEVELS: readonly ["silent", "error", "warn", "info", "debug", "verbose"];
|
|
7
8
|
type LogLevel = (typeof LOG_LEVELS)[number];
|
|
@@ -11142,6 +11143,176 @@ type IpSource = "cf-connecting-ip" | "true-client-ip" | "x-real-ip" | "x-forward
|
|
|
11142
11143
|
declare function getIp(req: XyPrisRequest): string;
|
|
11143
11144
|
declare function getIp(req: XyPrisRequest, enriched: true): GetIpResult;
|
|
11144
11145
|
|
|
11146
|
+
/**
|
|
11147
|
+
* @file Send.ts
|
|
11148
|
+
* @description Structured HTTP error response helper for the XyPriss framework.
|
|
11149
|
+
*
|
|
11150
|
+
* @copyright Copyright © 2025–2026 NEHONIX. All Rights Reserved.
|
|
11151
|
+
* @license NEHONIX Open Source License v2.0 (NOSL v2)
|
|
11152
|
+
* https://dll.nehonix.com/licenses/NOSL/v2
|
|
11153
|
+
*
|
|
11154
|
+
* This file is part of a NEHONIX open source project.
|
|
11155
|
+
* You may use, modify, and redistribute it freely — including for commercial
|
|
11156
|
+
* purposes — provided that NEHONIX is always credited as the original author.
|
|
11157
|
+
*
|
|
11158
|
+
* @author NEHONIX
|
|
11159
|
+
*/
|
|
11160
|
+
|
|
11161
|
+
/**
|
|
11162
|
+
* All HTTP status keys supported by the `Send` helper.
|
|
11163
|
+
* Each key maps to a standard HTTP status code.
|
|
11164
|
+
*/
|
|
11165
|
+
type SupportedStatus = "BAD_REQUEST" | "NOT_FOUND" | "TOO_MANY_REQUEST" | "INTERNAL_SERVER_ERR";
|
|
11166
|
+
/**
|
|
11167
|
+
* Maps every {@link SupportedStatus} key to its corresponding HTTP status code.
|
|
11168
|
+
*/
|
|
11169
|
+
type ISeConfigs = Record<SupportedStatus, number>;
|
|
11170
|
+
/**
|
|
11171
|
+
* Signature shared by all public error-dispatch methods.
|
|
11172
|
+
*
|
|
11173
|
+
* @param message - Human-readable description of the error shown to the API consumer.
|
|
11174
|
+
* @param data - Optional payload to attach to the response body (e.g. validation details).
|
|
11175
|
+
*/
|
|
11176
|
+
type TSendPropsFn = (message?: string, data?: unknown) => void;
|
|
11177
|
+
/**
|
|
11178
|
+
* Contract that the {@link Send} class must satisfy.
|
|
11179
|
+
* Ensures every supported error type exposes a consistent public API.
|
|
11180
|
+
*/
|
|
11181
|
+
interface ISeResponder {
|
|
11182
|
+
badRequest: TSendPropsFn;
|
|
11183
|
+
notFound: TSendPropsFn;
|
|
11184
|
+
tooManyRequest: TSendPropsFn;
|
|
11185
|
+
internalError: TSendPropsFn;
|
|
11186
|
+
}
|
|
11187
|
+
/**
|
|
11188
|
+
* Shape of every JSON response body produced by this helper.
|
|
11189
|
+
*
|
|
11190
|
+
* @property success - Always `false` for error responses.
|
|
11191
|
+
* @property message - Human-readable summary of the error.
|
|
11192
|
+
* @property serverName - Identifier of the server that handled the request.
|
|
11193
|
+
* @property data - Optional payload (echoed from the caller when relevant).
|
|
11194
|
+
* @property details - Machine-readable error metadata.
|
|
11195
|
+
*/
|
|
11196
|
+
interface IResTemplate {
|
|
11197
|
+
success: false;
|
|
11198
|
+
message: string;
|
|
11199
|
+
serverName: string;
|
|
11200
|
+
data?: unknown;
|
|
11201
|
+
details: {
|
|
11202
|
+
/** Short error label (e.g. `"Bad Request"`). */
|
|
11203
|
+
error: string;
|
|
11204
|
+
/** Compact error code derived from the status key (e.g. `"EBADR"`). */
|
|
11205
|
+
errorCode: string;
|
|
11206
|
+
/** The HTTP status code that was sent. */
|
|
11207
|
+
statusCode: number;
|
|
11208
|
+
};
|
|
11209
|
+
}
|
|
11210
|
+
/**
|
|
11211
|
+
* A structured HTTP response helper that standardises all error responses
|
|
11212
|
+
* across the application.
|
|
11213
|
+
*
|
|
11214
|
+
* Every method sends a JSON body conforming to {@link IResTemplate}, ensuring
|
|
11215
|
+
* consistent error shapes for API consumers regardless of where the error
|
|
11216
|
+
* originates.
|
|
11217
|
+
*
|
|
11218
|
+
* @example
|
|
11219
|
+
* ```ts
|
|
11220
|
+
* const send = new Send(res);
|
|
11221
|
+
*
|
|
11222
|
+
* // 400 – validation failure
|
|
11223
|
+
* send.badRequest("The 'email' field is required.");
|
|
11224
|
+
*
|
|
11225
|
+
* // 404 – resource missing
|
|
11226
|
+
* send.notFound("User not found.", { userId: 42 });
|
|
11227
|
+
*
|
|
11228
|
+
* // 429 – rate limit exceeded
|
|
11229
|
+
* send.tooManyRequest("Too many requests. Please slow down.");
|
|
11230
|
+
*
|
|
11231
|
+
* // 500 – unexpected failure
|
|
11232
|
+
* send.internalError("An unexpected error occurred.");
|
|
11233
|
+
* ```
|
|
11234
|
+
*/
|
|
11235
|
+
declare class Send implements ISeResponder {
|
|
11236
|
+
private readonly res;
|
|
11237
|
+
private readonly configs;
|
|
11238
|
+
private readonly serverName;
|
|
11239
|
+
private readonly includeServerName;
|
|
11240
|
+
/**
|
|
11241
|
+
* Creates a new `Send` instance bound to the given response object.
|
|
11242
|
+
*
|
|
11243
|
+
* @param res - The active `XyPrisResponse` to write into.
|
|
11244
|
+
* @param configs - Optional override for the default status-code registry.
|
|
11245
|
+
* Useful for non-standard or custom HTTP codes.
|
|
11246
|
+
*/
|
|
11247
|
+
constructor(res: XyPrisResponse$2, configs?: Partial<{
|
|
11248
|
+
statusCode: Partial<ISeConfigs>;
|
|
11249
|
+
includeServerName: boolean;
|
|
11250
|
+
}>);
|
|
11251
|
+
/**
|
|
11252
|
+
* Resolves the status code, builds the full response body, and flushes it.
|
|
11253
|
+
*
|
|
11254
|
+
* @param statusKey - One of the {@link SupportedStatus} keys.
|
|
11255
|
+
* @param errorLabel - Human-readable short label for the error type.
|
|
11256
|
+
* @param message - Optional caller-supplied message; falls back to `errorLabel`.
|
|
11257
|
+
* @param data - Optional payload to attach to the response body.
|
|
11258
|
+
*/
|
|
11259
|
+
private dispatch;
|
|
11260
|
+
/**
|
|
11261
|
+
* Sends a **400 Bad Request** response.
|
|
11262
|
+
*
|
|
11263
|
+
* Use when the client submits a malformed or invalid request
|
|
11264
|
+
* (e.g. missing required fields, invalid format, constraint violation).
|
|
11265
|
+
*
|
|
11266
|
+
* @param message - Human-readable explanation of why the request was rejected.
|
|
11267
|
+
* @param data - Optional payload (e.g. a list of validation errors per field).
|
|
11268
|
+
*
|
|
11269
|
+
* @example
|
|
11270
|
+
* send.badRequest("The 'username' field must be at least 3 characters.");
|
|
11271
|
+
* send.badRequest("Validation failed.", { fields: { email: "Invalid format" } });
|
|
11272
|
+
*/
|
|
11273
|
+
badRequest: TSendPropsFn;
|
|
11274
|
+
/**
|
|
11275
|
+
* Sends a **404 Not Found** response.
|
|
11276
|
+
*
|
|
11277
|
+
* Use when the requested resource does not exist or has been permanently removed.
|
|
11278
|
+
*
|
|
11279
|
+
* @param message - Human-readable explanation of what could not be found.
|
|
11280
|
+
* @param data - Optional payload (e.g. the identifier that was looked up).
|
|
11281
|
+
*
|
|
11282
|
+
* @example
|
|
11283
|
+
* send.notFound("No user found with id '42'.");
|
|
11284
|
+
* send.notFound("Resource not found.", { id: "42" });
|
|
11285
|
+
*/
|
|
11286
|
+
notFound: TSendPropsFn;
|
|
11287
|
+
/**
|
|
11288
|
+
* Sends a **429 Too Many Requests** response.
|
|
11289
|
+
*
|
|
11290
|
+
* Use when the client has exceeded an allowed request rate or quota.
|
|
11291
|
+
* Consider pairing this with a `Retry-After` header at the middleware level.
|
|
11292
|
+
*
|
|
11293
|
+
* @param message - Human-readable explanation of the rate limit breach.
|
|
11294
|
+
* @param data - Optional payload (e.g. retry delay, remaining quota).
|
|
11295
|
+
*
|
|
11296
|
+
* @example
|
|
11297
|
+
* send.tooManyRequest("Rate limit reached. Try again in 60 seconds.");
|
|
11298
|
+
* send.tooManyRequest("Quota exceeded.", { retryAfter: 60 });
|
|
11299
|
+
*/
|
|
11300
|
+
tooManyRequest: TSendPropsFn;
|
|
11301
|
+
/**
|
|
11302
|
+
* Sends a **500 Internal Server Error** response.
|
|
11303
|
+
*
|
|
11304
|
+
* Use for unexpected, unhandled server-side failures.
|
|
11305
|
+
* Avoid leaking internal stack traces or sensitive details in the message.
|
|
11306
|
+
*
|
|
11307
|
+
* @param message - Human-readable explanation safe to expose to the client.
|
|
11308
|
+
* @param data - Optional payload (use sparingly — never expose raw stack traces).
|
|
11309
|
+
*
|
|
11310
|
+
* @example
|
|
11311
|
+
* send.internalError("An unexpected error occurred. Please try again later.");
|
|
11312
|
+
*/
|
|
11313
|
+
internalError: TSendPropsFn;
|
|
11314
|
+
}
|
|
11315
|
+
|
|
11145
11316
|
/**
|
|
11146
11317
|
* Identifies the project root for a given caller path by traversing up the filesystem.
|
|
11147
11318
|
*/
|
|
@@ -11447,5 +11618,5 @@ declare class XStatic {
|
|
|
11447
11618
|
*/
|
|
11448
11619
|
declare function Router(): XyPrissRouter;
|
|
11449
11620
|
|
|
11450
|
-
export { ConfigurationManager as CM, ConfigurationManager as Configs, FileUploadAPI as FLA, FileUploadAPI, Plugin, PluginHookIds, Router, SecurityMiddleware, Upload, XJsonResponseHandler, XStatic, XyGuard, MultiServerApp as XyPMS, XyPrissRouter, XyPrissXHSC, __cfg__, __const__, __sys__, createOptimalCache, createServer, getCallerProjectRoot, getIp, getMime, getMimes, identifyProjectRoot, initializeFileUpload, mergeWithDefaults, mergeWithDefaults as mwdef, quickServer, uploadAny, uploadArray, uploadFields, uploadSingle, xems };
|
|
11451
|
-
export type { ArchiveOptions, BatchRenameChange, CacheConfig, FileUploadConfig as FiUpConfig, FileUploadConfig, GetIpResult, IpSource, MonitorSnapshot, MultiServerConfig, NetworkStats, NextFunction, PerformanceConfig, PluginCreator, PluginServer, ProcessInfo, ProcessMonitorSnapshot, XyPrisRequest as Request, RequestHandler, XyPrisResponse as Response, RoutRateLimit, RouteConfig, RouteGuard, RouteMeta, RouteOptions, ParamType as RouteParamType, SecurityConfig, ServerOptions$1 as ServerOptions, XyPrisRequest$1 as XRequest, XyPrisResponse$1 as XResponse, XemsTypes, XyPrisRequest$1 as XyPrisRequest, XyPrisResponse$1 as XyPrisResponse, XyPrissApp, XyPrissPlugin };
|
|
11621
|
+
export { ConfigurationManager as CM, ConfigurationManager as Configs, FileUploadAPI as FLA, FileUploadAPI, Plugin, PluginHookIds, Router, SecurityMiddleware, Send, Upload, XJsonResponseHandler, XStatic, XyGuard, MultiServerApp as XyPMS, XyPrissRouter, XyPrissXHSC, __cfg__, __const__, __sys__, createOptimalCache, createServer, getCallerProjectRoot, getIp, getMime, getMimes, identifyProjectRoot, initializeFileUpload, mergeWithDefaults, mergeWithDefaults as mwdef, quickServer, uploadAny, uploadArray, uploadFields, uploadSingle, xems };
|
|
11622
|
+
export type { ArchiveOptions, BatchRenameChange, CacheConfig, FileUploadConfig as FiUpConfig, FileUploadConfig, GetIpResult, IResTemplate, ISeConfigs, ISeResponder, IpSource, MonitorSnapshot, MultiServerConfig, NetworkStats, NextFunction, PerformanceConfig, PluginCreator, PluginServer, ProcessInfo, ProcessMonitorSnapshot, XyPrisRequest as Request, RequestHandler, XyPrisResponse as Response, RoutRateLimit, RouteConfig, RouteGuard, RouteMeta, RouteOptions, ParamType as RouteParamType, SecurityConfig, ServerOptions$1 as ServerOptions, SupportedStatus, TSendPropsFn, XyPrisRequest$1 as XRequest, XyPrisResponse$1 as XResponse, XemsTypes, XyPrisRequest$1 as XyPrisRequest, XyPrisResponse$1 as XyPrisResponse, XyPrissApp, XyPrissPlugin };
|