-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolve): add ApiDOM resolver based on SwaggerClient HTTP client
Refs #2717
- Loading branch information
Showing
8 changed files
with
478 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/helpers/apidom/reference/resolve/resolvers/http-swagger-client/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'cross-fetch/polyfill'; | ||
import { ResolverError, HttpResolver } from '@swagger-api/apidom-reference/configuration/empty'; | ||
|
||
import Http from '../../../../../../http/index.js'; | ||
|
||
const HttpResolverSwaggerClient = HttpResolver.compose({ | ||
props: { | ||
name: 'http-swagger-client', | ||
swaggerHTTPClient: Http, | ||
swaggerHTTPClientConfig: {}, | ||
}, | ||
init({ swaggerHTTPClient = this.swaggerHTTPClient } = {}) { | ||
this.swaggerHTTPClient = swaggerHTTPClient; | ||
}, | ||
methods: { | ||
getHttpClient() { | ||
return this.swaggerHTTPClient; | ||
}, | ||
|
||
async read(file) { | ||
const client = this.getHttpClient(); | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
const timeoutID = setTimeout(() => { | ||
controller.abort(); | ||
}, this.timeout); | ||
const credentials = | ||
this.getHttpClient().withCredentials || this.withCredentials ? 'include' : 'same-origin'; | ||
const redirects = this.redirects === 0 ? 'error' : 'follow'; | ||
const follow = this.redirects > 0 ? this.redirects : undefined; | ||
|
||
try { | ||
const response = await client({ | ||
url: file.uri, | ||
signal, | ||
userFetch: async (resource, options) => { | ||
const res = await fetch(resource, options); | ||
res.headers.delete('Content-Type'); | ||
return res; | ||
}, | ||
credentials, | ||
redirects, | ||
follow, | ||
...this.swaggerHTTPClientConfig, | ||
}); | ||
|
||
return response.text.arrayBuffer(); | ||
} catch (error) { | ||
throw new ResolverError(`Error downloading "${file.uri}"`, error); | ||
} finally { | ||
clearTimeout(timeoutID); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default HttpResolverSwaggerClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
206 changes: 206 additions & 0 deletions
206
...idom/reference/resolve/resolvers/http-swagger-client/fixtures/sample-openapi-3-1-api.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"x-top-level": "value", | ||
"info": { | ||
"title": "Sample API", | ||
"unknownFixedField": "value", | ||
"description": "Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.", | ||
"summary": "example summary", | ||
"termsOfService": "Terms of service", | ||
"version": "0.1.9", | ||
"x-version": "0.1.9-beta", | ||
"license": { | ||
"name": "Apache License 2.0", | ||
"x-fullName": "Apache License 2.0", | ||
"identifier": "Apache License 2.0", | ||
"url": "https://www.apache.org/licenses/LICENSE-2.0" | ||
}, | ||
"contact": { | ||
"name": "Vladimir Gorej", | ||
"x-username": "char0n", | ||
"url": "https://www.linkedin.com/in/vladimirgorej/", | ||
"email": "[email protected]" | ||
} | ||
}, | ||
"components": { | ||
"x-extension": "value", | ||
"schemas": { | ||
"x-model": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type:": "integer" | ||
} | ||
} | ||
}, | ||
"User": { | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer" | ||
}, | ||
"name": { | ||
"type": "string" | ||
}, | ||
"profile": { | ||
"$ref": "#/components/schemas/UserProfile", | ||
"summary": "user profile reference summary", | ||
"description": "user profile reference description" | ||
} | ||
} | ||
}, | ||
"UserProfile": { | ||
"type": "object", | ||
"properties": { | ||
"email": { | ||
"type": "string", | ||
"x-nullable": true | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": { | ||
"userId": { | ||
"$ref": "#/components/parameters/userIdRef" | ||
}, | ||
"userIdRef": { | ||
"name": "userId", | ||
"in": "query", | ||
"description": "ID of the user", | ||
"required": true | ||
} | ||
} | ||
}, | ||
"security": [ | ||
{}, | ||
{ | ||
"petstore_auth": [ | ||
"write:pets", | ||
"read:pets" | ||
] | ||
} | ||
], | ||
"servers": [ | ||
{ | ||
"url": "http://api.example.com/v1", | ||
"description": "Optional server description, e.g. Main (production) server" | ||
}, | ||
{ | ||
"url": "http:{port}//staging-api.example.com", | ||
"description": "Optional server description, e.g. Internal staging server for testing", | ||
"variables": { | ||
"port": { | ||
"enum": [ | ||
"8443", | ||
"443" | ||
], | ||
"default": "8443", | ||
"description": "Port description" | ||
} | ||
} | ||
} | ||
], | ||
"paths": { | ||
"/users": { | ||
"summary": "path item summary", | ||
"description": "path item description", | ||
"get": { | ||
"tags": ["tag1", "tag2"], | ||
"summary": "Returns a list of users.", | ||
"description": "Optional extended description in CommonMark or HTML.", | ||
"externalDocs": { | ||
"description": "Find more info here", | ||
"url": "https://example.com" | ||
}, | ||
"operationId": "getUserList", | ||
"parameters": [ | ||
{ | ||
"$ref": "#/components/parameters/userId" | ||
} | ||
], | ||
"requestBody": { | ||
"content": {} | ||
}, | ||
"responses": { | ||
"xxx": {"key": "val"}, | ||
"200": { | ||
"description": "A JSON array of user names", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"201": { | ||
"description": "A response", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/User" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"callbacks": { | ||
"myCallback": { | ||
"{$request.query.queryUrl}": { | ||
"post": { | ||
"requestBody": { | ||
"description": "Callback payload", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/User" | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "callback successfully processed" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"deprecated": true, | ||
"security": [ | ||
{}, | ||
{ | ||
"petstore_auth": [ | ||
"write:pets", | ||
"read:pets" | ||
] | ||
} | ||
], | ||
"servers": [ | ||
{ | ||
"url": "http://api.example.com/v3", | ||
"description": "Redundant server description, e.g. redundant server" | ||
} | ||
] | ||
}, | ||
"servers": [ | ||
{ | ||
"url": "http://api.example.com/v2", | ||
"description": "Redundant server description, e.g. redundant server" | ||
} | ||
], | ||
"parameters": [ | ||
{ | ||
"name": "userId", | ||
"in": "query", | ||
"description": "ID of the user", | ||
"required": true | ||
} | ||
] | ||
} | ||
} | ||
} |
Empty file.
Oops, something went wrong.