Skip to content

Commit

Permalink
feat(Resource): Added proper camelcasing and decamelizing to paths an…
Browse files Browse the repository at this point in the history
…d resource names
  • Loading branch information
0xfede committed Mar 21, 2017
1 parent 9d9c2f6 commit 49d0778
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
{
"name": "Antonio Pintus",
"email": "apintus@vivocha.com"
},
{
"name": "William Ghelfi",
"email": "trumbitta@gmail.com",
"url": "https://github.com/trumbitta"
}
],
"license": "MIT",
Expand All @@ -58,7 +63,9 @@
"homepage": "https://github.com/vivocha/arrest",
"devDependencies": {
"@types/body-parser": "0.0.34",
"@types/camelcase": "0.0.30",
"@types/debug": "0.0.29",
"@types/decamelize": "0.0.30",
"@types/express": "4.0.35",
"@types/lodash": "4.14.52",
"@types/node": "7.0.5",
Expand All @@ -80,7 +87,9 @@
},
"dependencies": {
"body-parser": "1.16.1",
"camelcase": "4.0.0",
"debug": "2.6.1",
"decamelize": "1.2.0",
"eredita": "1.0.1",
"express": "4.14.1",
"jsonpolice": "4.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/mongo/resource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as decamelize from 'decamelize';
import { MongoClient, Db } from 'mongodb';
import { Routes, Resource, ResourceDefinition } from '../resource';
import { QueryMongoOperation, ReadMongoOperation, CreateMongoOperation, UpdateMongoOperation, RemoveMongoOperation } from './operation';
Expand All @@ -16,7 +17,7 @@ export class MongoResource extends Resource {
constructor(db: string | Db, info:MongoResourceDefinition, routes:Routes = MongoResource.defaultRoutes()) {
super(info, routes);
if (!this.collection) {
this.collection = ('' + this.namePlural).toLocaleLowerCase();
this.collection = decamelize('' + this.namePlural, '_');
}
if (typeof db === 'string') {
this[__db] = MongoClient.connect(db as string);
Expand Down
11 changes: 7 additions & 4 deletions src/resource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as camelcase from 'camelcase';
import * as decamelize from 'decamelize';
import { Router, RouterOptions, NextFunction } from 'express';
import { Swagger } from './swagger';
import { API, APIRequest, APIResponse, APIRequestHandler } from './api';
Expand Down Expand Up @@ -38,6 +40,7 @@ export class Resource implements ResourceDefinition {
description?: string;
externalDocs?: Swagger.ExternalDocs;
namePlural?: string;
path?: string;
id?: string;
title?: string;
summaryFields?: string[];
Expand All @@ -53,7 +56,7 @@ export class Resource implements ResourceDefinition {
}
Object.assign(this, info);
if (!this.name) {
this.name = this.constructor.name;
this.name = Resource.capitalize(camelcase(this.constructor.name));
}
if (!this.namePlural) {
this.namePlural = this.name + 's';
Expand All @@ -70,7 +73,7 @@ export class Resource implements ResourceDefinition {
}

get basePath(): string {
return '/' + Resource.uncapitalize(this.namePlural);
return '/' + (this.path ? this.path : decamelize('' + this.namePlural, '-'));
}
get operations():Operation[] {
return this[__operations];
Expand Down Expand Up @@ -136,7 +139,7 @@ export class Resource implements ResourceDefinition {
});
}

static uncapitalize(s) {
return s.charAt(0).toLowerCase() + s.slice(1);
static capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
}
8 changes: 6 additions & 2 deletions test/resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ describe('Resource', function() {
describe('basePath', function () {

it('should return the correct base path', function () {
let r = new Resource({name: 'Test'});
r.basePath.should.equal('/tests');
let r1 = new Resource({name: 'Test'});
r1.basePath.should.equal('/tests');
let r2 = new Resource({name: 'MyTest'});
r2.basePath.should.equal('/my-tests');
let r3 = new Resource({name: 'Test', path: 'my-path' });
r3.basePath.should.equal('/my-path');
});

});
Expand Down

0 comments on commit 49d0778

Please sign in to comment.