Skip to content

Commit

Permalink
feat(success): add success responses
Browse files Browse the repository at this point in the history
- add success response for data
- add success responde without data
  • Loading branch information
IyiKuyoro committed Apr 26, 2019
1 parent d5e8007 commit acfa4ee
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 2 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ RespondEx.createdResource(
);
```

**successWithData**: Sends a HTTP response for a successful request with some returned data. Response will contain a 200 status code, message and data in the body.

| Parameters | Type | Description | Example |
|------------------------|--------|------------------------------------------|-----------------------------------------------|
| message | string | The message to be sent with the response | "Product successfully created." |
| data | object | Newly created resource data | { name: "Chair" } |
| res | object | The express http response object | |
| options (_optional_) | object | Some extra headers | { contentType: "application/json" } |

#### Example
```
RespondEx.successWithData(
'Successful sign-in',
{
name: 'Chair',
quantity: 100,
description: 'A comfortable seat for your afternoon tea.'
},
res,
);
```

**successWithoutData**: Sends a HTTP response for a successful request without an data. Response will contain a 200 status code and message body.

| Parameters | Type | Description | Example |
|------------------------|--------|------------------------------------------|-----------------------------------------------|
| message | string | The message to be sent with the response | "Product successfully created." |
| res | object | The express http response object | |
| options (_optional_) | object | Some extra headers | { contentType: "application/json" } |

#### Example
```
RespondEx.successWithData(
'Successful sign-in',
res,
);
```

**error**: Sends a HTTP response error using an instance of the APIError class.

| Parameter | Type | Description | Example |
Expand Down
103 changes: 101 additions & 2 deletions src/__tests__/RespondEx.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ApiError from '@respondex/apierror';
import ResMock from '../__mocks__/ResMock';

describe('RespondEx', () => {
describe('createdResource', () => {
describe('createdResource()', () => {
let res;
let statusSpy;
let jsonSpy;
Expand Down Expand Up @@ -63,7 +63,106 @@ describe('RespondEx', () => {
});
});

describe('error', () => {
describe('successWithData()', () => {
let res;
let statusSpy;
let jsonSpy;
let setSpy;

beforeEach(() => {
res = new ResMock();
statusSpy = jest.spyOn(res, 'status');
jsonSpy = jest.spyOn(res, 'json');
setSpy = jest.spyOn(res, 'set');
});

afterEach(() => {
jest.resetAllMocks();
});

it('should set the response type to application/json', () => {
RespondEx.successWithData(
'Success message',
{
name: 'Chair',
number: 100,
description: 'A comfortable chair for your afternoon tea',
},
res,
);

expect(setSpy).toHaveBeenCalledWith({
'content-type': 'application/json',
});
});

it('should set response data and code properly', () => {
RespondEx.successWithData(
'Success message',
{
name: 'Chair',
number: 100,
description: 'A comfortable chair for your afternoon tea',
},
res,
);

expect(statusSpy).toHaveBeenCalledWith(200);
expect(jsonSpy).toHaveBeenCalledWith({
success: true,
message: 'Success message',
data: {
name: 'Chair',
number: 100,
description: 'A comfortable chair for your afternoon tea',
},
});
});
});

describe('successWithoutData()', () => {
let res;
let statusSpy;
let jsonSpy;
let setSpy;

beforeEach(() => {
res = new ResMock();
statusSpy = jest.spyOn(res, 'status');
jsonSpy = jest.spyOn(res, 'json');
setSpy = jest.spyOn(res, 'set');
});

afterEach(() => {
jest.resetAllMocks();
});

it('should set the response type to application/json', () => {
RespondEx.successWithoutData(
'Success message',
res,
);

expect(setSpy).toHaveBeenCalledWith({
'content-type': 'application/json',
});
});

it('should set response data and code properly', () => {
RespondEx.successWithoutData(
'Success message',
res,
);

expect(statusSpy).toHaveBeenCalledWith(200);
expect(jsonSpy).toHaveBeenCalledWith({
success: true,
message: 'Success message',
});
});
});

describe('error()', () => {
let res;
let statusSpy;
let jsonSpy;
Expand Down
34 changes: 34 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import ApiError from '@respondex/apierror';

export default class RespondEx {
/**
* @description Send a success response with some data.
* @param {string} message The message of the response
* @param {object} data The data to be sent
* @param {object} res The express response object
* @param {object} options={} Object containing options
*/
static successWithData(message, data, res, options = {}) {
res.set({
'content-type': options.contentType || 'application/json',
});
res.status(200).json({
success: true,
message,
data,
});
}

/**
* @description Send a success response without some data.
* @param {string} message The message of the response
* @param {object} res The express response object
* @param {object} options={} Object containing options
*/
static successWithoutData(message, res, options = {}) {
res.set({
'content-type': options.contentType || 'application/json',
});
res.status(200).json({
success: true,
message,
});
}

/**
* @description Send a success response for a created resource.
* @param {string} message The message of the response
Expand Down

0 comments on commit acfa4ee

Please sign in to comment.