Skip to content

Commit

Permalink
Merge pull request #28 from hckrnews/feature/201
Browse files Browse the repository at this point in the history
Add http status 201
  • Loading branch information
w3nl authored Feb 15, 2023
2 parents 1f80498 + 13e809e commit f0c5c53
Show file tree
Hide file tree
Showing 11 changed files with 853 additions and 7,675 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"parserOptions": {
"sourceType": "module",
"parser": "@babel/eslint-parser",
"ecmaVersion": 2023,
"babelOptions": {
"configFile": "./babel.config.cjs"
}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 19.x]
node-version: [16.x, 18.x, 19.x]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
registry-url: https://registry.npmjs.org/
- run: npm ci --production
- run: npm publish --access public
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.14.0
18.13.0
8,381 changes: 728 additions & 7,653 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hckrnews/express-callback",
"version": "4.1.1",
"version": "4.1.2",
"description": "Express callback",
"files": [
"src/error-status.js",
Expand Down Expand Up @@ -45,7 +45,7 @@
"@babel/eslint-parser": "^7.14.7",
"@babel/plugin-transform-modules-commonjs": "^7.14.0",
"@babel/preset-env": "^7.9.5",
"@hckrnews/eslint-config": "^2.1.0",
"@hckrnews/eslint-config": "^3.0.0",
"@jest/globals": "^29.3.1",
"babel-jest": "^29.3.1",
"codecov": "^3.8.3",
Expand All @@ -64,7 +64,7 @@
},
"type": "module",
"engines": {
"node": ">= 14"
"node": ">= 16"
},
"dependencies": {
"mime": "^3.0.0"
Expand Down
71 changes: 71 additions & 0 deletions src/__tests__/express-callback.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const res = {
json: (value2) => {
this.values.send = value2;
},
end: () => true,
};
},
json(value) {
Expand All @@ -32,6 +33,7 @@ const res = {
send(value) {
this.values.send = value;
},
end: () => true,
setHeader(key, value) {
this.values.headers[key] = value;
},
Expand Down Expand Up @@ -83,6 +85,75 @@ describe('Test the express callback', () => {
expect(currentRes.values.type).toEqual('text/xml');
});

it('Http status 201 shouldnt return anything', async () => {
const currentRes = { ...res, values: { ...res.values } };

const controller = () => ({
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
example: 'ok',
},
statusCode: 201,
});

const expressCallback = makeExpressCallback({
controller,
specification,
logger,
meta,
});
const context = {};
const req = {};
await expressCallback(context, req, currentRes);

expect(currentRes.values.set).toEqual({
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
example: 'ok',
});
expect(currentRes.values.status).toEqual(201);
expect(currentRes.values.type).toEqual('application/json');
expect(currentRes.values.send).toEqual(null);
});

it('Http status 201 with response body', async () => {
const currentRes = { ...res, values: { ...res.values } };

const controller = () => ({
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
example: 'ok',
},
statusCode: 201,
body: {
test: 'ok',
},
});

const expressCallback = makeExpressCallback({
controller,
specification,
logger,
meta,
});
const context = {};
const req = {};
await expressCallback(context, req, currentRes);

expect(currentRes.values.set).toEqual({
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
example: 'ok',
});
expect(currentRes.values.status).toEqual(201);
expect(currentRes.values.type).toEqual('application/json');
expect(currentRes.values.send).toEqual({
test: 'ok',
});
});

it('It should work with attachments', async () => {
const currentRes = { ...res, values: { ...res.values } };

Expand Down
37 changes: 30 additions & 7 deletions src/__tests__/response.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,45 @@ describe.each(TestCases)(

describe('Response without a specification', () => {
it('It should generate a response body if no body is send', () => {
const response = buildResponse({});
const response = buildResponse({
statusCode: 418,
});
expect(response.headers).toEqual({
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
});
expect(response.statusCode).toEqual(200);
expect(response.statusCode).toEqual(418);
expect(response.body.status).toEqual(true);
expect(response.body.version).toEqual('unknown');
expect(response.body.message).toEqual('ok');
});
});

describe('Response with a specification', () => {
it('It shouldnt generate a response body if no body is send for status < 400', () => {
const response = buildResponse(
{
statusCode: 201,
},
{
info: {
version: '1.2.3',
},
}
);
expect(response.headers).toEqual({
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
});
expect(response.statusCode).toEqual(201);
expect(response.body).toEqual(null);
});

it('It should generate a response body if no body is send', () => {
const response = buildResponse(
{},
{
statusCode: 418,
},
{
info: {
version: '1.2.3',
Expand All @@ -127,9 +150,9 @@ describe('Response with a specification', () => {
'Content-Type': 'application/json',
'Cache-Control': 'no-store, max-age=0',
});
expect(response.statusCode).toEqual(200);
expect(response.body.status).toEqual(true);
expect(response.body.version).toEqual('1.2.3');
expect(response.body.message).toEqual('ok');
expect(response.statusCode).toEqual(418);
expect(response.body?.status).toEqual(true);
expect(response.body?.version).toEqual('1.2.3');
expect(response.body?.message).toEqual('ok');
});
});
5 changes: 5 additions & 0 deletions src/__tests__/status-codes.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const TestCases = [
statusCode: 200,
expectedResult: true,
},
{
description: 'The statusCode 201 should be valid',
statusCode: 201,
expectedResult: true,
},
{
description: 'The statusCode 401 should be valid',
statusCode: 401,
Expand Down
4 changes: 3 additions & 1 deletion src/express-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export default function makeExpressCallback({
);
}

if (contentType === 'application/json') {
if (!httpResponse.body) {
res.end();
} else if (contentType === 'application/json') {
res.json(httpResponse.body);
} else {
res.send(httpResponse.body);
Expand Down
3 changes: 2 additions & 1 deletion src/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function buildResponse(
}

if (
statusCode !== 201 &&
body &&
body?.constructor !== Object &&
body?.constructor !== Array &&
Expand All @@ -53,7 +54,7 @@ export default function buildResponse(
...headers,
},
statusCode,
body: body ?? defaultBody,
body: body ?? (statusCode >= 400 ? defaultBody : null),
attachment,
};
}
Expand Down

0 comments on commit f0c5c53

Please sign in to comment.