Skip to content

Commit

Permalink
Merge pull request #3 from touch4it/endpoint-type
Browse files Browse the repository at this point in the history
add error parser for no-feathers endpoint
  • Loading branch information
matejka9 authored Nov 15, 2018
2 parents d8d24c2 + 20b224c commit 8493e88
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 49 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ Configure [Feathers application](https://feathersjs.com) to forward all unknown
__Options:__

- `uri` (**required**) - Uri to remote server
- `reqHeaders` - Headers to forward from REST req object
- `resHeaders` - Headers to be added to REST res object from forwarded response
- `endpoint` - Type of endpoint, for now support `feathers` (not specified === `feathers`) and everything else is default behaviour of request parsing
- `reqHeaders` - Headers to forward from REST req object (case sensitive)
- `resHeaders` - Headers to be added to REST res object from forwarded response (case sensitive)


## Example
Expand Down Expand Up @@ -57,7 +58,8 @@ app.configure(services);
// Forward everything else
app.configure(forward({
uri: 'https://example.com',
reqHeaders: ['Authorization'],
endpoint: 'feathers',
reqHeaders: ['authorization'],
resHeaders: ['custom-header']
}));

Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function requiredParameterHandler(parameterName) {
*/
function configure({
uri = requiredParameterHandler('uri'),
endpoint = 'feathers',
reqHeaders = [],
resHeaders = []
}) {
return function () {
const app = this;

app.set('FEATHERS_FORWARD__REMOTE_FORWARDING_URL', uri);
app.set('FEATHERS_FORWARD__ENDPOINT', endpoint);
app.set('FEATHERS_FORWARD__REQ_HEADERS', reqHeaders);
app.set('FEATHERS_FORWARD__RES_HEADERS', resHeaders);

Expand Down
131 changes: 87 additions & 44 deletions lib/services/forward/forward.hooks.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,102 @@
const feathersErrors = require('@feathersjs/errors');

/**
* Forward error
* Parse feathers error
*
* @param {object} ctx Context
* @param {object} ctx Hook context
* @param {Error} error Error thrown
*/
function forwardError(ctx) {
const {error} = ctx;
const parseFeathersError = function (ctx, error) {
const {message} = error.response.data;

if (error.response) {
const {message} = error.response.data;
const options = error.response.data.data || {};
options.errors = error.response.data.errors;

const options = error.response.data.data || {};
options.errors = error.response.data.errors;
if (feathersErrors[error.response.data.name]) {
ctx.error = new feathersErrors[error.response.data.name](message, options);
return;
}

if (feathersErrors[error.response.data.name]) {
ctx.error = new feathersErrors[error.response.data.name](message, options);
return;
}
ctx.error = new feathersErrors.GeneralError(message, options);
};

ctx.error = new feathersErrors.GeneralError(message, options);
/**
* Parse default error
*
* @param {object} ctx Hook context
* @param {Error} error Error thrown
*/
const parseDefaultError = function (ctx, error) {
const {status, data} = error.response;

const options = {};

if (feathersErrors[status]) {
ctx.error = new feathersErrors[status](data, options);
return;
}

ctx.error = new feathersErrors.Unavailable(error.message);
ctx.error = new feathersErrors.GeneralError(data, options);
};

/**
* Forward error
*
* @param {object} app App
* @return {function} Hook forward function
*/
function forwardError(app) {
const type = app.get('FEATHERS_FORWARD__ENDPOINT');

return ctx => {
const {error} = ctx;

if (error.response) {
switch (type) {
case 'feathers':
parseFeathersError(ctx, error);
break;
default:
parseDefaultError(ctx, error);
break;
}
return;
}

ctx.error = new feathersErrors.Unavailable(error.message);
};
}

module.exports = {
before: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

error: {
all: [forwardError],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
module.exports = function (app) {
return {
before: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

error: {
all: [forwardError(app)],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
};
2 changes: 1 addition & 1 deletion lib/services/forward/forward.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ module.exports = function (app) {
const matchAll = '[^]*';
app.use(matchAll, new ForwardService());
const forward = app.service(matchAll);
forward.hooks(hooks);
forward.hooks(hooks(app));
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "feathers-forward",
"version": "1.1.1",
"version": "1.2.0",
"description": "A Feathers service addapter for forwarding messages to another feathers application",
"license": "MIT",
"engines": {
Expand Down

0 comments on commit 8493e88

Please sign in to comment.