Skip to content

Commit

Permalink
Add custom components, security for OpenAPI (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
pthongtaem authored Mar 11, 2020
1 parent 167097c commit 6870fe8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,49 @@ app.use(
openApi.save('./swagger.yml');
```

OpenAPI (Swagger) with Bearer Authentication

```ts
import { useSofa, OpenAPI } from 'sofa-api';

const openApi = OpenAPI({
schema,
info: {
title: 'Example API',
version: '3.0.0',
},
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [
{
bearerAuth: [],
},
],
});

app.use(
'/api',
useSofa({
schema,
onRoute(info) {
openApi.addRoute(info, {
basePath: '/api',
});
},
})
);

// writes every recorder route
openApi.save('./swagger.yml');
```

## License

[MIT](https://github.com/Urigo/sofa/blob/master/LICENSE) © Uri Goldshtein
12 changes: 12 additions & 0 deletions src/open-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import { OpenAPI } from './interfaces';
export function OpenAPI({
schema,
info,
components,
security,
}: {
schema: GraphQLSchema;
info: Record<string, any>;
components?: Record<string, any>;
security?: Record<string, any>[];
}) {
const types = schema.getTypeMap();
const swagger: any = {
Expand All @@ -40,6 +44,14 @@ export function OpenAPI({
}
}

if (components) {
swagger.components = { ...components, ...swagger.components };
}

if (security) {
swagger.security = security;
}

return {
addRoute(
info: RouteInfo,
Expand Down

0 comments on commit 6870fe8

Please sign in to comment.