Skip to content

Commit

Permalink
add support for multiple proxy methods, fixed in pull request dheraul…
Browse files Browse the repository at this point in the history
…t#299 but not applied in main repo, so had to clone it
  • Loading branch information
patrice gargiolo committed Jan 17, 2018
1 parent fa23128 commit 2d03df8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class Offline {
skipCacheInvalidation: false,
noAuth: false,
corsAllowOrigin: '*',
corsAllowHeaders: 'accept,content-type,x-api-key',
corsAllowHeaders: 'accept,content-type,x-api-key,authorization',
corsAllowCredentials: true,
apiKey: crypto.createHash('md5').digest('hex'),
};
Expand Down Expand Up @@ -841,6 +841,7 @@ class Offline {
if (err) return reject(err);

this.printBlankLine();
this.serverlessLog(`Yolo`);
this.serverlessLog(`Offline listening on http${this.options.httpsProtocol ? 's' : ''}://${this.options.host}:${this.options.port}`);

resolve(this.server);
Expand Down Expand Up @@ -919,26 +920,35 @@ class Offline {
if (!isProxy) {
return this.serverlessLog(`WARNING: Only HTTP_PROXY is supported. Path '${pathResource}' is ignored.`);
}
if (`${method}`.toUpperCase() !== 'GET') {
return this.serverlessLog(`WARNING: ${method} proxy is not supported. Path '${pathResource}' is ignored.`);
}
if (!path) {
return this.serverlessLog(`WARNING: Could not resolve path for '${methodId}'.`);
}

let fullPath = this.options.prefix + (pathResource.startsWith('/') ? pathResource.slice(1) : pathResource);
if (fullPath !== '/' && fullPath.endsWith('/')) fullPath = fullPath.slice(0, -1);
fullPath = fullPath.replace(/\+}/g, '*}');

const proxyUriOverwrite = resourceRoutesOptions[methodId] || {};
const proxyUriInUse = proxyUriOverwrite.Uri || proxyUri;

if (!proxyUriInUse) {
return this.serverlessLog(`WARNING: Could not load Proxy Uri for '${methodId}'`);
}

this.serverlessLog(`${method} ${pathResource} -> ${proxyUriInUse}`);
const routeMethod = method === 'ANY' ? '*' : method;
const routeConfig = {
cors: this.options.corsConfig
};
if (routeMethod !== 'HEAD' && routeMethod !== 'GET') {
routeConfig.payload = { parse: false };
}

this.serverlessLog(`${method} ${fullPath} -> ${proxyUriInUse}`);

this.server.route({
method,
path,
config: { cors: this.options.corsConfig },
method : routeMethod,
path : fullPath,
config : routeConfig,
handler: (request, reply) => {
const params = request.params;
let resultUri = proxyUriInUse;
Expand All @@ -947,7 +957,9 @@ class Offline {
resultUri = resultUri.replace(`{${key}}`, params[key]);
});

reply.proxy({ uri: resultUri });
this.serverlessLog(`PROXY ${request.method} ${request.url.path} -> ${resultUri}`);
reply.proxy({ uri: resultUri, passThrough: true });

},
});
});
Expand Down

0 comments on commit 2d03df8

Please sign in to comment.