1 | import { ResolverError, HttpResolver } from '@swagger-api/apidom-reference/configuration/empty';
|
---|
2 | import '../../../../../../helpers/fetch-polyfill.node.js';
|
---|
3 | import '../../../../../../helpers/abortcontroller-polyfill.node.js';
|
---|
4 | import Http from '../../../../../../http/index.js';
|
---|
5 | const HttpResolverSwaggerClient = HttpResolver.compose({
|
---|
6 | props: {
|
---|
7 | name: 'http-swagger-client',
|
---|
8 | swaggerHTTPClient: Http,
|
---|
9 | swaggerHTTPClientConfig: {}
|
---|
10 | },
|
---|
11 | init({
|
---|
12 | swaggerHTTPClient = this.swaggerHTTPClient
|
---|
13 | } = {}) {
|
---|
14 | this.swaggerHTTPClient = swaggerHTTPClient;
|
---|
15 | },
|
---|
16 | methods: {
|
---|
17 | getHttpClient() {
|
---|
18 | return this.swaggerHTTPClient;
|
---|
19 | },
|
---|
20 | async read(file) {
|
---|
21 | const client = this.getHttpClient();
|
---|
22 | const controller = new AbortController();
|
---|
23 | const {
|
---|
24 | signal
|
---|
25 | } = controller;
|
---|
26 | const timeoutID = setTimeout(() => {
|
---|
27 | controller.abort();
|
---|
28 | }, this.timeout);
|
---|
29 | const credentials = this.getHttpClient().withCredentials || this.withCredentials ? 'include' : 'same-origin';
|
---|
30 | const redirect = this.redirects === 0 ? 'error' : 'follow';
|
---|
31 | const follow = this.redirects > 0 ? this.redirects : undefined;
|
---|
32 | try {
|
---|
33 | const response = await client({
|
---|
34 | url: file.uri,
|
---|
35 | signal,
|
---|
36 | userFetch: async (resource, options) => {
|
---|
37 | let res = await fetch(resource, options);
|
---|
38 | try {
|
---|
39 | // undici supports mutations
|
---|
40 | res.headers.delete('Content-Type');
|
---|
41 | } catch {
|
---|
42 | // Fetch API has guards which prevent mutations
|
---|
43 | res = new Response(res.body, {
|
---|
44 | ...res,
|
---|
45 | headers: new Headers(res.headers)
|
---|
46 | });
|
---|
47 | res.headers.delete('Content-Type');
|
---|
48 | }
|
---|
49 | return res;
|
---|
50 | },
|
---|
51 | credentials,
|
---|
52 | redirect,
|
---|
53 | follow,
|
---|
54 | ...this.swaggerHTTPClientConfig
|
---|
55 | });
|
---|
56 | return response.text.arrayBuffer();
|
---|
57 | } catch (error) {
|
---|
58 | throw new ResolverError(`Error downloading "${file.uri}"`, {
|
---|
59 | cause: error
|
---|
60 | });
|
---|
61 | } finally {
|
---|
62 | clearTimeout(timeoutID);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | });
|
---|
67 | export default HttpResolverSwaggerClient; |
---|