Skip to content

Commit

Permalink
feat: introduce new options for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasadiyodi93 committed Mar 5, 2021
1 parent 12c9f1b commit 5c27f3c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
40 changes: 34 additions & 6 deletions plugin/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@ const httpRequestClient = async (method: string, uri: string, options: any) => {
return Wreck.request(method, uri, proxyOptions);
};

const onResponse = (route: any) => async (
err: any,
res: any,
request: Hapi.Request,
h: Hapi.ResponseToolkit
) => {
const payload = await Wreck.read(res, {
json: 'force',
gunzip: true
});

// only return the following key from the response
const responseKeyToReturn = R.pathOr(
'',
['options', 'app', 'proxy', 'responseKeyToReturn'],
route
);
if (!R.hasPath(responseKeyToReturn.split('.'), payload)) {
return h.response(payload);
}

const payloadToReturn = R.pathOr({}, responseKeyToReturn.split('.'), payload);
return h.response(payloadToReturn);
};

export const plugin = {
name: 'proxies',
dependencies: [],
Expand All @@ -26,12 +51,15 @@ export const plugin = {
const routes = generateRoutes(routesContent);

const ROUTES: any[] = routes;
const ROUTES_WITH_PROXY_DATA = ROUTES.map(
R.assocPath(
['handler', 'proxy', 'httpClient', 'request'],
httpRequestClient
)
);
const ROUTES_WITH_PROXY_DATA: any[] = ROUTES.map((route) => {
return R.pipe(
R.assocPath(
['handler', 'proxy', 'httpClient', 'request'],
httpRequestClient
),
R.assocPath(['handler', 'proxy', 'onResponse'], onResponse(route))
)(route);
});

server.route(ROUTES_WITH_PROXY_DATA);
}
Expand Down
3 changes: 2 additions & 1 deletion plugin/proxy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export const generateRoutes = (contents: Array<YAMLRoute> = []) => {
iam: {
permissions: route?.permissions || [],
hooks: route?.hooks || []
}
},
proxy: route?.proxy?.extraOptions
}
}
};
Expand Down

0 comments on commit 5c27f3c

Please sign in to comment.