Skip to content

Commit

Permalink
feat(resolve): add ApiDOM resolver based on SwaggerClient HTTP client
Browse files Browse the repository at this point in the history
Refs #2717
  • Loading branch information
char0n committed Dec 29, 2022
1 parent 00cb06d commit 33b892c
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"globals": {
"File": true,
"Blob": true
"Blob": true,
"globalThis": true
},
"parserOptions": {
"sourceType": "module",
Expand Down
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;
3 changes: 2 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"global-require": 0, // needs to be eliminated in future
"import/no-dynamic-require": 0,
"max-classes-per-file": 0,
"no-underscore-dangle": 0
"no-underscore-dangle": 0,
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
}
}
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
}
]
}
}
}
Loading

0 comments on commit 33b892c

Please sign in to comment.