Skip to content

Commit

Permalink
Added config option to support api prefixing and versioning (fix feat…
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Mar 15, 2017
1 parent 4953e39 commit ee5c42b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,50 @@ Now [localhost:3030/docs/](http://localhost:3030/docs/) will show the documentat

You can also use `uiIndex: true` to use the default [Swagger UI](http://swagger.io/swagger-ui/).

### Prefixed routes

If your are using versioned or prefixed routes for your API like `/api/<version>/users`, you can configure it using the
`prefix` property so all your services don't end up in the same group. The value of the `prefix` property can be either
a string or a RegEx.

```js
const app = feathers()
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(rest())
.configure(swagger({
prefix: /api\/v\d\//,
docsPath: '/docs',
info: {
title: 'A test',
description: 'A description'
}
}))
.use('/api/v1/messages', messageService);

app.listen(3030);
```

To display your API version alongside the service name, you can also define a `versionPrefix` to be extracted:
```js
const app = feathers()
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(rest())
.configure(swagger({
prefix: /api\/v\d\//,
versionPrefix: /v\d/,
docsPath: '/docs',
info: {
title: 'A test',
description: 'A description'
}
}))
.use('/api/v1/messages', messageService);

app.listen(3030);
```

## License

Copyright (c) 2016
Expand Down
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ export default function init (config) {
service.docs = service.docs || {};

// Load documentation from service, if available.
const doc = service.docs;
const group = path.split('/');
const tag = path.indexOf('/') > -1 ? group[0] : path;
const model = path.indexOf('/') > -1 ? group[1] : path;
const security = {};
var doc = service.docs;
var version = path.match(config.versionPrefix);
version = version ? ' ' + version[0] : '';
var apiPath = path.replace(config.prefix, '');
var group = apiPath.split('/');
var tag = (apiPath.indexOf('/') > -1 ? group[0] : apiPath) + version;
var model = apiPath.indexOf('/') > -1 ? group[1] : apiPath;
var security = {};

if (rootDoc.security) {
security[rootDoc.security.name] = [];
Expand Down

0 comments on commit ee5c42b

Please sign in to comment.