Skip to content

Commit

Permalink
Swagger basePath (#8)
Browse files Browse the repository at this point in the history
```js
openApi.addRoute(info, { basePath: '/api' });
```
  • Loading branch information
kamilkisiela committed Jan 17, 2019
1 parent c32680e commit bdcf473
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .gitignore
@@ -1,3 +1,6 @@
node_modules/
dist/
yarn.lock
yarn.lock
*.log
npm-debug.log*
lerna-debug.log
3 changes: 2 additions & 1 deletion .npmignore
Expand Up @@ -8,4 +8,5 @@ example/
jest.config.js
tsconfig.json
tsconfig.build.json
webpack.config.js
webpack.config.js
yarn.lock
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -158,7 +158,9 @@ app.use(
sofa({
schema,
onRoute(info) {
openApi.addRoute(info);
openApi.addRoute(info, {
basePath: '/api',
});
},
})
);
Expand Down
4 changes: 3 additions & 1 deletion example/index.ts
Expand Up @@ -34,7 +34,9 @@ app.use(
schema,
ignore: ['User.favoriteBook'],
onRoute(info) {
openApi.addRoute(info);
openApi.addRoute(info, {
basePath: '/api',
});
},
})
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,9 +1,9 @@
{
"name": "sofa-api",
"description": "Create REST APIs with GraphQL",
"version": "0.0.1",
"version": "0.1.0",
"main": "dist/index.js",
"typings": "dest/index.d.ts",
"typings": "dist/index.d.ts",
"sideEffects": false,
"license": "MIT",
"keywords": [
Expand Down
18 changes: 12 additions & 6 deletions src/open-api/index.ts
Expand Up @@ -33,14 +33,20 @@ export function OpenAPI({
}

return {
addRoute(info: RouteInfo) {
if (!swagger.paths[info.path]) {
swagger.paths[info.path] = {};
addRoute(
info: RouteInfo,
config?: {
basePath?: string;
}
) {
const basePath = (config && config.basePath) || '';
const path = basePath + info.path;

swagger.paths[info.path][
info.method.toLowerCase()
] = buildPathFromOperation({
if (!swagger.paths[path]) {
swagger.paths[path] = {};
}

swagger.paths[path][info.method.toLowerCase()] = buildPathFromOperation({
operation: info.document,
schema,
});
Expand Down

0 comments on commit bdcf473

Please sign in to comment.