Skip to content

Commit

Permalink
refactor: extract code validation to static function
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-koval committed Jan 22, 2018
1 parent 826ea14 commit 7d34d0f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
18 changes: 18 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
plugins:
fixme:
enabled: true
eslint:
enabled: true
config:
config: .eslintrc
nodesecurity:
enabled: true
requiresafe:
enabled: true
exclude_patterns:
- node_modules/**/*
- test/**/*
- error/**/*
- danger*
version: "2"
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Contributing

## Versions

In order to contribute make sure that you add a version tag using the following logic:

| Code Status | Stage | Rule | Example # |
Expand All @@ -18,7 +19,7 @@ Commits should follow the following format:
Keyword is mandatory for **CHANGELOG**
If keyword is skipped, commit will not appear in **CHANGELOG**

### Keywords:
### Keywords

| Keyword | When to use |
| --- | --- |
Expand Down
1 change: 1 addition & 0 deletions error/unexpectedStatusCodeError.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class UnexpectedStatusCodeError extends TrembitaError {
}

static _generateErrorMessage (instance) {
if (instance.httpBody === undefined) { instance.httpBody = null }
return `${`Unexpected status code: ${instance.httpStatusCode}, Body: ${util.inspect(instance.httpBody, { depth: 4 })}, Options: `}${util.inspect(instance.options, { depth: 4 })}`
}

Expand Down
52 changes: 25 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,18 @@ const { isURL } = require('validator');

const { UnexpectedStatusCodeError, TrembitaError } = require('./error');


const Trembita = class Trembita {
constructor(options) {
this.raw = options => {
const expectedCodes = options.expectedCodes || [200];
return this.client(options)
.then(res => {
if (!expectedCodes.includes(res.statusCode)) {
const error = new UnexpectedStatusCodeError({
options,
httpStatusCode: res.statusCode,
httpBody: res.body
});
return Promise.reject(error)
}
if (res.body === undefined) {
res.body = null
}
return res.body;
})
}
this.request = options => {
this.log.trace({ options }, 'request')
return this.raw(options)
};
this.raw = options => this.client(options).then(Trembita._validateExpectedCodes.bind(options))
this.request = options => this.raw(options);

Trembita._validateOptions(options);
Trembita._validateEndpoint(options.endpoint);

this.endpoint = options.endpoint;
this.log = options.log || console; // TODO: add more loggers
this.client = request.defaults({
baseUrl: this.endpoint,
json: true,
});
this.log = options.log || console;
this.client = request.defaults({ baseUrl: this.endpoint, json: true });
}

/**
Expand Down Expand Up @@ -71,6 +49,26 @@ const Trembita = class Trembita {
require_host: true, // eslint-disable-line camelcase
})) { throw new TrembitaError('endpoint is not valid url') }
}

/**
* Status code validator
* @method _validateExpectedCodes
* @param {Number} statusCode res.statusCode
* @param {Object} body res.body
* @return {Promise}
*/
static _validateExpectedCodes ({statusCode, body}) {
const options = this;
const defaultStatusCodes = [200, 201];

const expectedCodes = options.expectedCodes || defaultStatusCodes;
if (!expectedCodes.includes(statusCode)) {
const error = new UnexpectedStatusCodeError({ options, httpStatusCode: statusCode, httpBody: body });
return Promise.reject(error)
}

return Promise.resolve(body)
}
};

module.exports = Trembita;
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('Trembita:', () => {
expectedCodes: [200]
})
.catch(UnexpectedStatusCodeError, (err) => {
const message = `Unexpected status code: 404, Body: undefined, Options: { url: \'/profiles/1\', expectedCodes: [ 200 ] }`
const message = `Unexpected status code: 404, Body: null, Options: { url: \'/profiles/1\', expectedCodes: [ 200 ] }`
expect(err.message).to.equal(message)
expect(err.toJSON()).to.deep.equal({ message })
})
Expand Down

0 comments on commit 7d34d0f

Please sign in to comment.