Skip to content

Instantly share code, notes, and snippets.

@sampaiodiego
Created September 26, 2019 15:03
Show Gist options
  • Save sampaiodiego/4179bd1b9b4094dcf66e762be76db446 to your computer and use it in GitHub Desktop.
Save sampaiodiego/4179bd1b9b4094dcf66e762be76db446 to your computer and use it in GitHub Desktop.
import express from 'express';
import { isObject } from 'underscore';
import { WebApp } from 'meteor/webapp';
import { Meteor } from 'meteor/meteor';
const apiServer = express();
apiServer.disable('x-powered-by');
// WebApp.connectHandlers.use(apiServer);
apiServer.listen(4000);
const APIcontext = {
getLoggedInUser() {
console.log('getLoggedInUser');
},
deprecationWarning() {
console.log('deprecationWarning');
},
};
const addAction = (handler) => (req, res) =>
res.json(handler.call(APIcontext))
;
export class APIClass {
constructor(options) {
this.basePath = '/api';
if (options.version) {
this.basePath += `/${ options.version }`;
}
this._config = {
enableCors: false,
};
}
success(result = {}) {
if (isObject(result)) {
result.success = true;
}
result = {
statusCode: 200,
body: result,
};
// logger.debug('Success', result);
return result;
}
reloadRoutesToRefreshRateLimiter() {
console.log('reloadRoutesToRefreshRateLimiter');
}
addAuthMethod() {
console.log('addAuthMethod');
}
addRoute(routes, options, endpoints) {
// console.log('endpoints ->', endpoints);
const methods = typeof endpoints !== 'undefined' ? endpoints : options;
Object.entries(methods)
.forEach(([method, handler]) => {
// addRoutes(method, routes, handler)
[].concat(routes).forEach((endpoint) => {
const url = `${ this.basePath }/${ endpoint }`;
// console.log('url ->', method, url);
apiServer[method](url, addAction(handler.action || handler));
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment