Skip to content

Commit

Permalink
refactor(resolver): refactor code to allow using strategy pattern
Browse files Browse the repository at this point in the history
Refs #2744
  • Loading branch information
char0n committed Jan 3, 2023
1 parent 91a15ef commit 62209bc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Url from 'url';

import Http, { makeHttp, serializeRes, serializeHeaders } from './http/index.js';
import Resolver, { clearCache } from './resolver.js';
import Resolver from './resolver/index.js';
import { clearCache } from './resolver/strategies/openapi-2--3-0.js';
import resolveSubtree from './subtree-resolver/index.js';
import { makeApisTagOperation } from './interfaces.js';
import { execute, buildRequest, baseUrl } from './execute/index.js';
Expand Down
18 changes: 18 additions & 0 deletions src/resolver/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// eslint-disable-next-line camelcase
import resolveOpenAPI2_30Strategy from './strategies/openapi-2--3-0.js';
import { makeFetchJSON } from './utils/index.js';
import * as optionsUtil from './utils/options.js';

const resolve = async (options) => {
const { spec, requestInterceptor, responseInterceptor } = options;

const retrievalURI = optionsUtil.retrievalURI(options);
const httpClient = optionsUtil.httpClient(options);
const retrievedSpec =
spec ||
(await makeFetchJSON(httpClient, { requestInterceptor, responseInterceptor })(retrievalURI));

return resolveOpenAPI2_30Strategy({ ...options, spec: retrievedSpec });
};

export default resolve;
46 changes: 8 additions & 38 deletions src/resolver.js → src/resolver/strategies/openapi-2--3-0.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import Http from './http/index.js';
import mapSpec, { plugins } from './specmap/index.js';
import mapSpec, { plugins } from '../../specmap/index.js';
// eslint-disable-next-line camelcase
import normalizeOpenAPI2__30 from './helpers/normalize/openapi-2--3-0.js';
import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from './constants.js';

export function makeFetchJSON(http, opts = {}) {
const { requestInterceptor, responseInterceptor } = opts;
// Set credentials with 'http.withCredentials' value
const credentials = http.withCredentials ? 'include' : 'same-origin';
return (docPath) =>
http({
url: docPath,
loadSpec: true,
requestInterceptor,
responseInterceptor,
headers: {
Accept: ACCEPT_HEADER_VALUE_FOR_DOCUMENTS,
},
credentials,
}).then((res) => res.body);
}
import normalizeOpenAPI2__30 from '../../helpers/normalize/openapi-2--3-0.js';
import { makeFetchJSON } from '../utils/index.js';
import * as optionsUtil from '../utils/options.js';

// Wipe out the http cache
export function clearCache() {
plugins.refs.clearCache();
}

export default function resolve(obj) {
// eslint-disable-next-line camelcase
export default function resolveOpenAPI2_30Strategy(obj) {
const {
fetch,
spec,
url,
mode,
allowMetaPatches = true,
pathDiscriminator,
Expand All @@ -43,20 +25,8 @@ export default function resolve(obj) {
} = obj;

let { http, baseDoc } = obj;

// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
// need to fix and pick one.
baseDoc = baseDoc || url;

// Provide a default fetch implementation
// TODO fetch should be removed, and http used instead
http = fetch || http || Http;

if (!spec) {
return makeFetchJSON(http, { requestInterceptor, responseInterceptor })(baseDoc).then(
doResolve
);
}
baseDoc = optionsUtil.retrievalURI(obj);
http = optionsUtil.httpClient(obj);

return doResolve(spec);

Expand Down
19 changes: 19 additions & 0 deletions src/resolver/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from '../../constants.js';

// eslint-disable-next-line import/prefer-default-export
export function makeFetchJSON(http, opts = {}) {
const { requestInterceptor, responseInterceptor } = opts;
// Set credentials with 'http.withCredentials' value
const credentials = http.withCredentials ? 'include' : 'same-origin';
return (docPath) =>
http({
url: docPath,
loadSpec: true,
requestInterceptor,
responseInterceptor,
headers: {
Accept: ACCEPT_HEADER_VALUE_FOR_DOCUMENTS,
},
credentials,
}).then((res) => res.body);
}
17 changes: 17 additions & 0 deletions src/resolver/utils/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Http from '../../http/index.js';

export const retrievalURI = (options) => {
const { baseDoc, url } = options;

// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
// need to fix and pick one.
return baseDoc || url;
};

export const httpClient = (options) => {
const { fetch, http } = options;

// @TODO fetch should be removed, and http used instead
// provide a default fetch implementation
return fetch || http || Http;
};
2 changes: 1 addition & 1 deletion src/subtree-resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import get from 'lodash/get';

import resolve from '../resolver.js';
import resolve from '../resolver/index.js';
// eslint-disable-next-line camelcase
import normalizeOpenAPI2__30 from '../helpers/normalize/openapi-2--3-0.js';

Expand Down
4 changes: 2 additions & 2 deletions test/resolver.js → test/resolver/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import fs from 'fs';
import jsYaml from 'js-yaml';

import Swagger from '../src/index.js';
import Swagger from '../../src/index.js';

describe('resolver', () => {
afterEach(() => {
Expand Down Expand Up @@ -721,7 +721,7 @@ describe('resolver', () => {
test('should not throw errors on resvered-keywords in freely-named-fields', () => {
// Given
const ReservedKeywordSpec = jsYaml.load(
fs.readFileSync(path.resolve(__dirname, './data/reserved-keywords.yaml'), 'utf8')
fs.readFileSync(path.resolve(__dirname, '../data/reserved-keywords.yaml'), 'utf8')
);

// When
Expand Down

0 comments on commit 62209bc

Please sign in to comment.