Skip to content

Commit

Permalink
fix: OpenAPI name, version and x-methods (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored Dec 15, 2020
1 parent 04446d4 commit cbad7c0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/daf-cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
# $env: API_KEY
schemaPath: /open-api.json
# homePageTemplate: ./homepage.html
apiName: Agent
apiVersion: 1.0.0
apiBasePath: /agent
apiDocsPath: /api-docs
defaultIdentity:
Expand Down
6 changes: 2 additions & 4 deletions packages/daf-cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ program
console.error(e)
process.exit()
}
process.on('SIGINT', function(code) {
console.log('Killing ngrok')
ngrok.kill()
});
app.set('trust proxy', 'loopback')
}
const hostname = parse(baseUrl).hostname
Expand Down Expand Up @@ -103,6 +99,8 @@ program
getAgentForRequest,
exposedMethods,
securityScheme,
apiName: options.apiName,
apiVersion: options.apiVersion,
}),
)

Expand Down
2 changes: 2 additions & 0 deletions packages/daf-express/api/daf-express.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const ApiSchemaRouter: (options: ApiSchemaRouterOptions) => Router;

// @public (undocumented)
export interface ApiSchemaRouterOptions {
apiName?: string;
apiVersion?: string;
basePath: string;
exposedMethods: Array<string>;
getAgentForRequest: (req: Request_2) => Promise<IAgent>;
Expand Down
12 changes: 11 additions & 1 deletion packages/daf-express/src/api-schema-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ export interface ApiSchemaRouterOptions {
* ```
*/
securityScheme?: string

/**
* Name used in OpenAPI schema
*/
apiName?: string

/**
* Version used in OpenAPI schema
*/
apiVersion?: string
}

/**
Expand All @@ -50,7 +60,7 @@ export const ApiSchemaRouter = (options: ApiSchemaRouterOptions): Router => {

router.get('/', (req: RequestWithAgent, res) => {
if (req.agent) {
const openApiSchema = getOpenApiSchema(req.agent, '', options.exposedMethods)
const openApiSchema = getOpenApiSchema(req.agent, '', options.exposedMethods, options.apiName, options.apiVersion)
const url = (req.headers['x-forwarded-proto'] || req.protocol) + '://' + req.hostname + options.basePath
openApiSchema.servers = [{ url }]

Expand Down
2 changes: 1 addition & 1 deletion packages/daf-rest/api/daf-rest.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AgentRestClient implements IAgentPlugin {
}

// @public (undocumented)
export const getOpenApiSchema: (agent: IAgent, basePath: string, exposedMethods: Array<string>) => OpenAPIV3.Document;
export const getOpenApiSchema: (agent: IAgent, basePath: string, exposedMethods: Array<string>, name?: string | undefined, version?: string | undefined) => OpenAPIV3.Document;


```
13 changes: 9 additions & 4 deletions packages/daf-rest/src/openApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAPIV3 } from 'openapi-types'
import { IAgent } from 'daf-core'

export const getOpenApiSchema = (agent: IAgent, basePath: string, exposedMethods: Array<string>): OpenAPIV3.Document => {
export const getOpenApiSchema = (agent: IAgent, basePath: string, exposedMethods: Array<string>, name?: string, version?: string): OpenAPIV3.Document => {
const agentSchema = agent.getSchema()

const paths: OpenAPIV3.PathsObject = {}
Expand Down Expand Up @@ -47,14 +47,19 @@ export const getOpenApiSchema = (agent: IAgent, basePath: string, exposedMethods
const openApi: OpenAPIV3.Document = {
openapi: "3.0.0",
info: {
title: "DID Agent",
version: ""
title: name || "DID Agent",
version: version || ""
},
components:{
schemas: agent.getSchema().components.schemas
schemas: agent.getSchema().components.schemas,
},
paths
}

if (openApi.components?.schemas) {
//@ts-ignore
openApi['x-methods'] = agent.getSchema().components.methods
}

return openApi
}

0 comments on commit cbad7c0

Please sign in to comment.