nhb-toolbox 4.14.0-alpha.6 → 4.14.0
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/CHANGELOG.md +4 -0
- package/dist/cjs/constants.js +6 -1
- package/dist/cjs/http-status/HttpStatus.js +158 -0
- package/dist/cjs/http-status/constants.js +895 -0
- package/dist/cjs/http-status/types.js +2 -0
- package/dist/cjs/index.js +5 -1
- package/dist/dts/constants.d.ts +1 -0
- package/dist/dts/constants.d.ts.map +1 -1
- package/dist/dts/http-status/HttpStatus.d.ts +104 -0
- package/dist/dts/http-status/HttpStatus.d.ts.map +1 -0
- package/dist/dts/http-status/constants.d.ts +829 -0
- package/dist/dts/http-status/constants.d.ts.map +1 -0
- package/dist/dts/http-status/types.d.ts +30 -0
- package/dist/dts/http-status/types.d.ts.map +1 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/esm/constants.js +1 -0
- package/dist/esm/http-status/HttpStatus.js +154 -0
- package/dist/esm/http-status/constants.js +892 -0
- package/dist/esm/http-status/types.js +1 -0
- package/dist/esm/index.js +2 -0
- package/package.json +9 -2
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ All notable changes to the package will be documented here.
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## [4.14.0] - 2025-08-11
|
|
10
|
+
|
|
11
|
+
- **Added** new class `HttpStatus` for retrieving and managing HTTP status codes.
|
|
12
|
+
|
|
9
13
|
## [4.13.11] - 2025-08-06
|
|
10
14
|
|
|
11
15
|
- **Fixed** an issue with `getZodiacSign` method in `Chronos` that could not return correct *Vedic* sign.
|
package/dist/cjs/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LOWERCASED_WORDS = exports.LOCALE_CODES = exports.FRANKFURTER_CURRENCIES = exports.CURRENCY_LOCALES = exports.CURRENCY_CODES = exports.CSS_COLORS = exports.NUMBER_COLOR_PALETTE = exports.ALPHABET_COLOR_PALETTE = exports.WESTERN_SEASONS = exports.US_ACADEMIC_SEASONS = exports.SEASON_PRESETS = exports.PHILIPPINES_SEASONS = exports.JAPAN_SEASONS = exports.INDIA_VEDIC_SEASONS = exports.INDIA_TAMIL_SEASONS = exports.INDIA_IMD_SEASONS = exports.ETHIOPIA_SEASONS = exports.BANGLADESH_SEASONS = exports.AUSTRALIA_SEASONS = exports.WESTERN_ZODIAC_SIGNS = exports.WEEK_DAYS = exports.VEDIC_ZODIAC_SIGNS = exports.TIME_ZONES = exports.TIME_ZONE_LABELS = exports.MONTHS = void 0;
|
|
3
|
+
exports.STATUS_CODES = exports.HTTP_CODES = exports.HTTP_STATUS = exports.HTTP_STATUS_CODES = exports.LOWERCASED_WORDS = exports.LOCALE_CODES = exports.FRANKFURTER_CURRENCIES = exports.CURRENCY_LOCALES = exports.CURRENCY_CODES = exports.CSS_COLORS = exports.NUMBER_COLOR_PALETTE = exports.ALPHABET_COLOR_PALETTE = exports.WESTERN_SEASONS = exports.US_ACADEMIC_SEASONS = exports.SEASON_PRESETS = exports.PHILIPPINES_SEASONS = exports.JAPAN_SEASONS = exports.INDIA_VEDIC_SEASONS = exports.INDIA_TAMIL_SEASONS = exports.INDIA_IMD_SEASONS = exports.ETHIOPIA_SEASONS = exports.BANGLADESH_SEASONS = exports.AUSTRALIA_SEASONS = exports.WESTERN_ZODIAC_SIGNS = exports.WEEK_DAYS = exports.VEDIC_ZODIAC_SIGNS = exports.TIME_ZONES = exports.TIME_ZONE_LABELS = exports.MONTHS = void 0;
|
|
4
4
|
var constants_1 = require("./date/constants");
|
|
5
5
|
Object.defineProperty(exports, "MONTHS", { enumerable: true, get: function () { return constants_1.MONTHS; } });
|
|
6
6
|
Object.defineProperty(exports, "TIME_ZONE_LABELS", { enumerable: true, get: function () { return constants_1.TIME_ZONE_LABELS; } });
|
|
@@ -32,3 +32,8 @@ Object.defineProperty(exports, "FRANKFURTER_CURRENCIES", { enumerable: true, get
|
|
|
32
32
|
Object.defineProperty(exports, "LOCALE_CODES", { enumerable: true, get: function () { return constants_3.LOCALE_CODES; } });
|
|
33
33
|
var constants_4 = require("./string/constants");
|
|
34
34
|
Object.defineProperty(exports, "LOWERCASED_WORDS", { enumerable: true, get: function () { return constants_4.LOWERCASE; } });
|
|
35
|
+
var constants_5 = require("./http-status/constants");
|
|
36
|
+
Object.defineProperty(exports, "HTTP_STATUS_CODES", { enumerable: true, get: function () { return constants_5.HTTP_STATUS_CODES; } });
|
|
37
|
+
Object.defineProperty(exports, "HTTP_STATUS", { enumerable: true, get: function () { return constants_5.HTTP_STATUS_CODES; } });
|
|
38
|
+
Object.defineProperty(exports, "HTTP_CODES", { enumerable: true, get: function () { return constants_5.HTTP_STATUS_CODES; } });
|
|
39
|
+
Object.defineProperty(exports, "STATUS_CODES", { enumerable: true, get: function () { return constants_5.HTTP_STATUS_CODES; } });
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.httpStatus = exports.HttpStatus = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
/**
|
|
6
|
+
* * Utility class for retrieving and managing HTTP status codes with rich MDN-based metadata.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* - Supports lookup by code or name (both `SOME_NAME` and `Some Name` formats).
|
|
10
|
+
* - Allows adding custom codes and overriding messages for existing ones.
|
|
11
|
+
* - Provides pre-grouped categories for quick filtering (see {@link HttpStatus.Groups}).
|
|
12
|
+
* - Intended to be reusable — create multiple instances if you want separate registries.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link httpStatus} for the default preloaded singleton instance.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Using the class directly
|
|
19
|
+
* const customStatus = new HttpStatus();
|
|
20
|
+
* customStatus.addCode({
|
|
21
|
+
* code: 799,
|
|
22
|
+
* name: 'CUSTOM_ERROR',
|
|
23
|
+
* readableName: 'Custom Error',
|
|
24
|
+
* message: 'Something custom happened',
|
|
25
|
+
* description: 'This is an example of a user-defined HTTP status.',
|
|
26
|
+
* category: 'clientError'
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* console.log(customStatus.getByCode(799)?.readableName);
|
|
30
|
+
* // "Custom Error"
|
|
31
|
+
*
|
|
32
|
+
* console.log(customStatus.getByCode(404)?.name);
|
|
33
|
+
* // "NOT_FOUND"
|
|
34
|
+
*
|
|
35
|
+
* console.log(customStatus.getByCode(404)?.message);
|
|
36
|
+
* // "Not Found"
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
class HttpStatus {
|
|
40
|
+
#codesByNumber;
|
|
41
|
+
#codesByName;
|
|
42
|
+
/**
|
|
43
|
+
* * Static category groups for quick reference.
|
|
44
|
+
* * Populated at runtime from the provided data.
|
|
45
|
+
*/
|
|
46
|
+
static Groups = {
|
|
47
|
+
informational: [],
|
|
48
|
+
success: [],
|
|
49
|
+
redirection: [],
|
|
50
|
+
clientError: [],
|
|
51
|
+
serverError: [],
|
|
52
|
+
};
|
|
53
|
+
constructor() {
|
|
54
|
+
this.#codesByNumber = new Map();
|
|
55
|
+
this.#codesByName = new Map();
|
|
56
|
+
// Initialize status code data
|
|
57
|
+
for (const entry of constants_1.HTTP_STATUS_DATA) {
|
|
58
|
+
this.#storeEntry(entry);
|
|
59
|
+
HttpStatus.Groups[entry.category].push(entry.code);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* * Get status entry by numeric HTTP code.
|
|
64
|
+
* @param code HTTP status code.
|
|
65
|
+
* @returns Matching status entry or `undefined` if not found.
|
|
66
|
+
*/
|
|
67
|
+
getByCode(code) {
|
|
68
|
+
return this.#codesByNumber.get(code);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* * Get status entry by name (either SOME_NAME or "Some Name").
|
|
72
|
+
* @param name Status name.
|
|
73
|
+
* @returns Matching status entry or `undefined` if not found.
|
|
74
|
+
*/
|
|
75
|
+
getByName(name) {
|
|
76
|
+
return this.#codesByName.get(name);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* * Override the short message of an existing code.
|
|
80
|
+
* @param code HTTP status code.
|
|
81
|
+
* @param newMessage Custom message.
|
|
82
|
+
* @returns `true` if updated, `false` if code not found.
|
|
83
|
+
*/
|
|
84
|
+
setMessage(code, newMessage) {
|
|
85
|
+
const entry = this.#codesByNumber.get(code);
|
|
86
|
+
if (!entry) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
entry.message = newMessage;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* * Add one or more new HTTP status code entries.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* - If a code already exists, it will be skipped and not overwritten.
|
|
99
|
+
* - Returns `true` if at least one code was successfully added.
|
|
100
|
+
* - Returns `false` if all provided codes already exist.
|
|
101
|
+
*
|
|
102
|
+
* @param entries One or more status entries to add.
|
|
103
|
+
* @returns `true` if at least one code was added, otherwise `false`.
|
|
104
|
+
*/
|
|
105
|
+
addCode(...entries) {
|
|
106
|
+
let added = false;
|
|
107
|
+
for (const entry of entries) {
|
|
108
|
+
if (!this.#codesByNumber.has(entry.code)) {
|
|
109
|
+
this.#storeEntry(entry);
|
|
110
|
+
HttpStatus.Groups[entry.category].push(entry.code);
|
|
111
|
+
added = true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return added;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* * List all codes, optionally filtered by category.
|
|
118
|
+
* @param category Optional category filter.
|
|
119
|
+
* @returns Array of status entries.
|
|
120
|
+
*/
|
|
121
|
+
list(category) {
|
|
122
|
+
if (!category) {
|
|
123
|
+
return [...this.#codesByNumber.values()];
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
return [...this.#codesByNumber.values()].filter((entry) => entry.category === category);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** * Internal helper to store an entry in all lookup maps. */
|
|
130
|
+
#storeEntry(entry) {
|
|
131
|
+
this.#codesByNumber.set(entry.code, { ...entry });
|
|
132
|
+
this.#codesByName.set(entry.name, { ...entry });
|
|
133
|
+
this.#codesByName.set(entry.readableName, { ...entry });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.HttpStatus = HttpStatus;
|
|
137
|
+
/**
|
|
138
|
+
* * Default singleton instance of {@link HttpStatus} class.
|
|
139
|
+
*
|
|
140
|
+
* @remarks
|
|
141
|
+
* - Preloaded with all MDN-based HTTP status codes.
|
|
142
|
+
* - Useful when you do not need multiple independent registries.
|
|
143
|
+
* - Provides immediate access to lookups, category groupings, and
|
|
144
|
+
* mutation methods without manual instantiation.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { httpStatus } from 'nhb-toolbox';
|
|
149
|
+
*
|
|
150
|
+
* console.log(httpStatus.getByCode(404)?.readableName);
|
|
151
|
+
* // "Not Found"
|
|
152
|
+
*
|
|
153
|
+
* httpStatus.setMessage(404, 'This page is gone');
|
|
154
|
+
* console.log(httpStatus.getByCode(404)?.message);
|
|
155
|
+
* // "This page is gone"
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
exports.httpStatus = new HttpStatus();
|