Skip to content

Commit

Permalink
chore: Re-name package, gitignore dist.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Miller authored and Adam Miller committed Jan 4, 2020
1 parent 6ec0f45 commit 550d004
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 256 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
dist
9 changes: 0 additions & 9 deletions dist/index.d.ts

This file was deleted.

230 changes: 0 additions & 230 deletions dist/index.js

This file was deleted.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "rebound-api",
"version": "2.0.0-beta.0",
"description": "An Express middleware for elegant API creation",
"main": "index.js",
"name": "loll",
"version": "0.0.1",
"description": "REST apps for the lazy developer.",
"main": "dist/index.js",
"scripts": {
"build": "rm -rf dist && tsc --jsx preserve -p tsconfig.json",
"pretest": "yarn run build",
Expand All @@ -12,7 +12,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/reboundjs/rebound-api.git"
"url": "git+https://github.com/amiller-gh/loll.git"
},
"keywords": [
"express",
Expand All @@ -24,9 +24,9 @@
"author": "Adam Miller",
"license": "MIT",
"bugs": {
"url": "https://github.com/reboundjs/rebound-api/issues"
"url": "https://github.com/amiller-gh/loll/issues"
},
"homepage": "https://github.com/reboundjs/rebound-api#readme",
"homepage": "https://github.com/amiller-gh/loll#readme",
"dependencies": {
"chalk": "^3.0.0",
"walk": "^2.3.9"
Expand Down
26 changes: 16 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ const evalAPI = function(func: Express.RequestHandler) {
// Evaluate API function
try {
const result = await func(req, res, next);
if(typeof result === 'object'){
return res.status((result.code || 200)).json(result);
}

// If they've returned the response object, assume we've sent the result already.
if (result === res) { return result; }

// If it appears to be a JSON response, send it down.
if (typeof result === 'object') { return res.status((result.code || 200)).json(result); }

// Otherwise, we're rather confused... alert the world.
console.error('✘ API endpoint returned something other than JSON or a Promise:', result, func);
return res.status(500).json({status: 'error', message: 'Invalid Response'});

Expand Down Expand Up @@ -65,23 +69,23 @@ function loadAPI(router: Express.Router, filePath: string, apiPath: string){
try {
const handler = require(filePath) as IApiHandler;
// If handler is a function, register it as a get callback
if(typeof handler === 'function' && (methods += ' GET')) router.get(apiPath, evalAPI(handler));
if (typeof handler === 'function' && (methods += ' GET')) router.get(apiPath, evalAPI(handler));
// If handler is an object with any valid http method, register them
else if(hasValidMethod(handler)){
else if (hasValidMethod(handler)) {
if(typeof handler.ALL === 'function' && (methods += ' ALL')) router.all(apiPath, evalAPI(handler.ALL));
if(typeof handler.GET === 'function' && (methods += ' GET')) router.get(apiPath, evalAPI(handler.GET));
if(typeof handler.POST === 'function' && (methods += ' POST')) router.post(apiPath, evalAPI(handler.POST));
if(typeof handler.PUT === 'function' && (methods += ' PUT')) router.put(apiPath, evalAPI(handler.PUT));
if(typeof handler.DELETE === 'function' && (methods += ' DELETE')) router.delete(apiPath, evalAPI(handler.DELETE));
}
// Otherwise, this is an invalid export. Error.
else{
return console.error(chalk.bold.red(' ✘ Error in API:'), chalk.bold.black(apiPath), chalk.gray(' - no valid HTTP method exported'));
else {
return console.error(chalk.bold.red(' ✘ Error in API:'), chalk.bold(apiPath), chalk.gray(' - no valid HTTP method exported'));
}
console.log(chalk.green(' • Registered:'), (apiPath ? apiPath : '/'), chalk.yellow('('+methods.trim()+')'));
} catch(err) {
// If require() failed, error
console.error(chalk.bold.red(' ✘ Error in API:'), chalk.bold.black(apiPath), chalk.gray(' - error in the API file'));
console.error(chalk.bold.red(' ✘ Error in API:'), chalk.bold(apiPath), chalk.gray(' - error in the API file'));
console.error(' ', chalk.underline(filePath));
console.error(' ', err.toString().replace(/(\r\n|\r|\n)/gm, '$1 '))
}
Expand All @@ -98,7 +102,7 @@ function discoverAPI(router: Express.Router, apiDir: string){
listeners: {
file: function (root: string, fileStats: walk.WalkStats, next: walk.WalkNext) {
// Ignore hidden files
if(fileStats.name[0] === '.') return next();
if(fileStats.name[0] === '.' || !~fileStats.name.indexOf('.js')) return next();

// Construct both the absolute file path, and public facing API path
var filePath = path.join(root, fileStats.name),
Expand Down Expand Up @@ -206,7 +210,7 @@ export default function api(express: any, apiPath: string = DEFAULT_API_DIR) {
const setupRouter = express() as Express.Express;
const router = express.Router() as Express.Router;

// Hacky. Force the parent router to attach the locals.api interface at the begining of each request
// Hacky. Force the parent router to attach the locals.api interface at the beginning of each request
setupRouter.on('mount', function(parent){
parent.use((req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
res.locals.api = {
Expand All @@ -225,6 +229,7 @@ export default function api(express: any, apiPath: string = DEFAULT_API_DIR) {
};
next();
});

parent._router.stack.splice(2, 0, parent._router.stack.pop());
});

Expand All @@ -242,6 +247,7 @@ export default function api(express: any, apiPath: string = DEFAULT_API_DIR) {

console.log(chalk.bold.green('• Discovering API:'));
discoverAPI(router, apiPath);
console.log(router);
setupRouter.use(router);
console.log(chalk.bold.green('✔ API Discovery Complete'));
return setupRouter;
Expand Down

0 comments on commit 550d004

Please sign in to comment.