Skip to content

Commit

Permalink
option prefix added
Browse files Browse the repository at this point in the history
  • Loading branch information
webNeat committed Oct 27, 2017
1 parent d984d22 commit b9d9f67
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ The full list of the `resource` options is as follows:

```js
test(app).resource(MongooseModel, {
prefix: '/api', // optional, default ''
routes: { // optional
collection: '/users', // the collection route, used to get list of resources and add new ones. Default values is the model name in pluralized dashes case. (ChannelCategory => channel-categories)
resource: '/users/:name', // the route used to get, update or delete single route. Default is `${routes.collection}/:id`
Expand Down Expand Up @@ -267,6 +268,8 @@ When `children` are specified, the following steps are added to the testing sena

# Changelog

- **Version 1.0.5**: option `prefix` added.

- **Version 1.0.4**: option `headers` added to methods `get`, `post`, `put` and `delete`.

- **Version 1.0.3**: README updated.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wajez-api-test",
"version": "1.0.4",
"version": "1.0.5",
"description": "Testing REST API made easy.",
"main": "src/index.js",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions src/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ const pluralize = word => {
return words.join('-')
}

const defaultRoutes = Model => {
const defaultRoutes = (Model, prefix) => {
const names = pluralize(Model.modelName)
prefix = prefix || ''
return {
collection: `/${names}`,
resource: `/${names}/:id`
collection: `${prefix}/${names}`,
resource: `${prefix}/${names}/:id`
}
}

Expand Down Expand Up @@ -55,8 +56,8 @@ const dataFor = (Model, fields) => {
})
}

const make = (Model, {json, routes, create, children} = {}) => {
routes = R.merge(defaultRoutes(Model), routes || {})
const make = (Model, {json, routes, prefix, create, children} = {}) => {
routes = R.merge(defaultRoutes(Model, prefix), routes || {})
create = create || defaultCreate(Model)
children = (children || []).map(child =>
R.mergeDeepRight(defaultChild(Model, routes.resource, child.field), child)
Expand Down
10 changes: 5 additions & 5 deletions test/simple-resources/app.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const asJSON = transform({
token: 'token'
})

app.get('/users', (req, res) => {
app.get('/api/users', (req, res) => {
User.find({})
.then(items => res.json(items.map(asJSON)))
.catch(err => {
Expand All @@ -38,7 +38,7 @@ app.get('/users', (req, res) => {
})
})

app.get('/users/:id', (req, res) => {
app.get('/api/users/:id', (req, res) => {
const id = req.params.id
if (! id.match(/^[0-9a-fA-F]{24}$/)) {
res.status(404)
Expand All @@ -59,7 +59,7 @@ app.get('/users/:id', (req, res) => {
})
})

app.post('/users', (req, res) => {
app.post('/api/users', (req, res) => {
User.create(R.merge(req.body, {token: '1234567890abcde1234567890abcde1234567890abcde1234567890abcdewxyz'}))
.then(item => res.status(201).json(asJSON(item)))
.catch(err => {
Expand All @@ -68,7 +68,7 @@ app.post('/users', (req, res) => {
})
})

app.put('/users/:id', (req, res) => {
app.put('/api/users/:id', (req, res) => {
const id = req.params.id
if (! id.match(/^[0-9a-fA-F]{24}$/)) {
res.status(400)
Expand All @@ -89,7 +89,7 @@ app.put('/users/:id', (req, res) => {
})
})

app.delete('/users/:id', (req, res) => {
app.delete('/api/users/:id', (req, res) => {
const id = req.params.id
if (! id.match(/^[0-9a-fA-F]{24}$/)) {
return res.json({})
Expand Down
1 change: 1 addition & 0 deletions test/simple-resources/app.user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const json = transform({
})

test(app).resource(User, {
prefix: '/api',
create: ['name', 'email', 'password'],
json: { // required
resource: json,
Expand Down

0 comments on commit b9d9f67

Please sign in to comment.