Skip to content

Commit 5f659e9

Browse files
committed
Added response status feature to package
1 parent 5ebb72f commit 5f659e9

7 files changed

Lines changed: 160 additions & 13 deletions

File tree

.github/workflows/run-tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ jobs:
1616
fail-fast: true
1717
matrix:
1818
os: [ubuntu-latest, windows-latest]
19-
node: [16, 18, 20] # Specify Node.js versions to test
20-
npm: [6, 7, 8] # Specify npm versions to test
19+
node: [21, 22, 23] # Specify Node.js versions to test
20+
npm: [9,10] # Specify npm versions to test
2121
name: Test Node.js${{ matrix.node }} and npm${{ matrix.npm }} on ${{ matrix.os }}
2222

2323
steps:
@@ -46,4 +46,4 @@ jobs:
4646
run: npm list --depth=0
4747

4848
- name: Execute tests
49-
run: npm test -- --ci
49+
run: npm test -- --ci --verbose

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ npm install @decodeblock/expressjs-api-utility
2424
## Usage
2525

2626
### Example
27-
Using the `ApiResponder` class for handling JSON responses in an ExpressJS application:
27+
Using the `ResponseStatus` and `ApiResponder` class for handling JSON responses in an ExpressJS application:
2828

2929
```javascript
3030
const express = require('express');
31-
const ApiResponder = require('@decodeblock/expressjs-api-utility');
31+
const { ApiResponder, ResponseStatus } = require('@decodeblock/expressjs-api-utility');
3232

3333
const app = express();
34-
const apiResponder = new ApiResponder();
3534

3635
app.get('/success', (req, res) => {
37-
apiResponder.successResponse(res, 'Request was successful', 200, { data: 'Your data here' });
36+
ApiResponder.successResponse(res, 'Request was successful', ResponseStatus.HTTP_OK, { data: 'Your data here' });
3837
});
3938

4039
app.get('/failure', (req, res) => {
41-
apiResponder.failureResponse(res, 'Request failed', 400, { error: 'Bad Request' });
40+
ApiResponder.failureResponse(res, 'Request failed', ResponseStatus.HTTP_NOT_FOUND, { error: 'Bad Request' });
4241
});
4342

4443
app.listen(3000, () => {

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResponseStatus.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
const HTTP_CONTINUE = 100;
2+
const HTTP_SWITCHING_PROTOCOLS = 101;
3+
const HTTP_PROCESSING = 102; // RFC2518
4+
const HTTP_EARLY_HINTS = 103; // RFC8297
5+
const HTTP_OK = 200;
6+
const HTTP_CREATED = 201;
7+
const HTTP_ACCEPTED = 202;
8+
const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
9+
const HTTP_NO_CONTENT = 204;
10+
const HTTP_RESET_CONTENT = 205;
11+
const HTTP_PARTIAL_CONTENT = 206;
12+
const HTTP_MULTI_STATUS = 207; // RFC4918
13+
const HTTP_ALREADY_REPORTED = 208; // RFC5842
14+
const HTTP_IM_USED = 226; // RFC3229
15+
const HTTP_MULTIPLE_CHOICES = 300;
16+
const HTTP_MOVED_PERMANENTLY = 301;
17+
const HTTP_FOUND = 302;
18+
const HTTP_SEE_OTHER = 303;
19+
const HTTP_NOT_MODIFIED = 304;
20+
const HTTP_USE_PROXY = 305;
21+
const HTTP_RESERVED = 306;
22+
const HTTP_TEMPORARY_REDIRECT = 307;
23+
const HTTP_PERMANENTLY_REDIRECT = 308; // RFC7238
24+
const HTTP_BAD_REQUEST = 400;
25+
const HTTP_UNAUTHORIZED = 401;
26+
const HTTP_PAYMENT_REQUIRED = 402;
27+
const HTTP_FORBIDDEN = 403;
28+
const HTTP_NOT_FOUND = 404;
29+
const HTTP_METHOD_NOT_ALLOWED = 405;
30+
const HTTP_NOT_ACCEPTABLE = 406;
31+
const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
32+
const HTTP_REQUEST_TIMEOUT = 408;
33+
const HTTP_CONFLICT = 409;
34+
const HTTP_GONE = 410;
35+
const HTTP_LENGTH_REQUIRED = 411;
36+
const HTTP_PRECONDITION_FAILED = 412;
37+
const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
38+
const HTTP_REQUEST_URI_TOO_LONG = 414;
39+
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
40+
const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
41+
const HTTP_EXPECTATION_FAILED = 417;
42+
const HTTP_I_AM_A_TEAPOT = 418; // RFC2324
43+
const HTTP_MISDIRECTED_REQUEST = 421; // RFC7540
44+
const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918
45+
const HTTP_LOCKED = 423; // RFC4918
46+
const HTTP_FAILED_DEPENDENCY = 424; // RFC4918
47+
const HTTP_TOO_EARLY = 425; // RFC-ietf-httpbis-replay-04
48+
const HTTP_UPGRADE_REQUIRED = 426; // RFC2817
49+
const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585
50+
const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585
51+
const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
52+
const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
53+
const HTTP_INTERNAL_SERVER_ERROR = 500;
54+
const HTTP_NOT_IMPLEMENTED = 501;
55+
const HTTP_BAD_GATEWAY = 502;
56+
const HTTP_SERVICE_UNAVAILABLE = 503;
57+
const HTTP_GATEWAY_TIMEOUT = 504;
58+
const HTTP_VERSION_NOT_SUPPORTED = 505;
59+
const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
60+
const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918
61+
const HTTP_LOOP_DETECTED = 508; // RFC5842
62+
const HTTP_NOT_EXTENDED = 510; // RFC2774
63+
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
64+
65+
module.exports = {
66+
HTTP_CONTINUE,
67+
HTTP_SWITCHING_PROTOCOLS,
68+
HTTP_PROCESSING,
69+
HTTP_EARLY_HINTS,
70+
HTTP_OK,
71+
HTTP_CREATED,
72+
HTTP_ACCEPTED,
73+
HTTP_NON_AUTHORITATIVE_INFORMATION,
74+
HTTP_NO_CONTENT,
75+
HTTP_RESET_CONTENT,
76+
HTTP_PARTIAL_CONTENT,
77+
HTTP_MULTI_STATUS,
78+
HTTP_ALREADY_REPORTED,
79+
HTTP_IM_USED,
80+
HTTP_MULTIPLE_CHOICES,
81+
HTTP_MOVED_PERMANENTLY,
82+
HTTP_FOUND,
83+
HTTP_SEE_OTHER,
84+
HTTP_NOT_MODIFIED,
85+
HTTP_USE_PROXY,
86+
HTTP_RESERVED,
87+
HTTP_TEMPORARY_REDIRECT,
88+
HTTP_PERMANENTLY_REDIRECT,
89+
HTTP_BAD_REQUEST,
90+
HTTP_UNAUTHORIZED,
91+
HTTP_PAYMENT_REQUIRED,
92+
HTTP_FORBIDDEN,
93+
HTTP_NOT_FOUND,
94+
HTTP_METHOD_NOT_ALLOWED,
95+
HTTP_NOT_ACCEPTABLE,
96+
HTTP_PROXY_AUTHENTICATION_REQUIRED,
97+
HTTP_REQUEST_TIMEOUT,
98+
HTTP_CONFLICT,
99+
HTTP_GONE,
100+
HTTP_LENGTH_REQUIRED,
101+
HTTP_PRECONDITION_FAILED,
102+
HTTP_REQUEST_ENTITY_TOO_LARGE,
103+
HTTP_REQUEST_URI_TOO_LONG,
104+
HTTP_UNSUPPORTED_MEDIA_TYPE,
105+
HTTP_REQUESTED_RANGE_NOT_SATISFIABLE,
106+
HTTP_EXPECTATION_FAILED,
107+
HTTP_I_AM_A_TEAPOT,
108+
HTTP_MISDIRECTED_REQUEST,
109+
HTTP_UNPROCESSABLE_ENTITY,
110+
HTTP_LOCKED,
111+
HTTP_FAILED_DEPENDENCY,
112+
HTTP_TOO_EARLY,
113+
HTTP_UPGRADE_REQUIRED,
114+
HTTP_PRECONDITION_REQUIRED,
115+
HTTP_TOO_MANY_REQUESTS,
116+
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE,
117+
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS,
118+
HTTP_INTERNAL_SERVER_ERROR,
119+
HTTP_NOT_IMPLEMENTED,
120+
HTTP_BAD_GATEWAY,
121+
HTTP_SERVICE_UNAVAILABLE,
122+
HTTP_GATEWAY_TIMEOUT,
123+
HTTP_VERSION_NOT_SUPPORTED,
124+
HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL,
125+
HTTP_INSUFFICIENT_STORAGE,
126+
HTTP_LOOP_DETECTED,
127+
HTTP_NOT_EXTENDED,
128+
HTTP_NETWORK_AUTHENTICATION_REQUIRED,
129+
};

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const ApiResponder = require('./ApiResponder');
2+
const ResponseStatus = require('./ResponseStatus');
23

34
module.exports = {
4-
ApiResponder,
5+
ApiResponder,
6+
ResponseStatus,
57
};

tests/ApiResponder.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const request = require('supertest');
33
const ApiResponder = require('../src/ApiResponder');
4+
const ResponseStatus = require('../src/ResponseStatus');
45

56
describe('ApiResponder', () => {
67
let app;

tests/ResponseStatus.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const ResponseStatus = require('../src/ResponseStatus');
2+
3+
describe('ResponseStatus Module', () => {
4+
test('should export the correct HTTP_OK value', () => {
5+
expect(ResponseStatus.HTTP_OK).toBe(200);
6+
});
7+
8+
test('should export the correct HTTP_NOT_FOUND value', () => {
9+
expect(ResponseStatus.HTTP_NOT_FOUND).toBe(404);
10+
});
11+
12+
test('should contain all expected status codes', () => {
13+
expect(Object.keys(ResponseStatus)).toContain('HTTP_INTERNAL_SERVER_ERROR');
14+
expect(ResponseStatus.HTTP_INTERNAL_SERVER_ERROR).toBe(500);
15+
});
16+
});

0 commit comments

Comments
 (0)