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 17, 2023
1 parent 925bcb7 commit 31f4ca8
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 102 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
98 changes: 0 additions & 98 deletions src/resolver.js

This file was deleted.

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;
67 changes: 67 additions & 0 deletions src/resolver/strategies/openapi-2--3-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import mapSpec, { plugins } from '../../specmap/index.js';
// eslint-disable-next-line camelcase
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();
}

// eslint-disable-next-line camelcase
export default function resolveOpenAPI2_30Strategy(obj) {
const {
spec,
mode,
allowMetaPatches = true,
pathDiscriminator,
modelPropertyMacro,
parameterMacro,
requestInterceptor,
responseInterceptor,
skipNormalization,
useCircularStructures,
} = obj;

const retrievalURI = optionsUtil.retrievalURI(obj);
const httpClient = optionsUtil.httpClient(obj);

return doResolve(spec);

function doResolve(_spec) {
if (retrievalURI) {
plugins.refs.docCache[retrievalURI] = _spec;
}

// Build a json-fetcher ( ie: give it a URL and get json out )
plugins.refs.fetchJSON = makeFetchJSON(httpClient, { requestInterceptor, responseInterceptor });

const plugs = [plugins.refs];

if (typeof parameterMacro === 'function') {
plugs.push(plugins.parameters);
}

if (typeof modelPropertyMacro === 'function') {
plugs.push(plugins.properties);
}

if (mode !== 'strict') {
plugs.push(plugins.allOf);
}

// mapSpec is where the hard work happens
return mapSpec({
spec: _spec,
context: { baseDoc: retrievalURI },
plugins: plugs,
allowMetaPatches, // allows adding .meta patches, which include adding `$$ref`s to the spec
pathDiscriminator, // for lazy resolution
parameterMacro,
modelPropertyMacro,
useCircularStructures,
// eslint-disable-next-line camelcase
}).then(skipNormalization ? async (a) => a : normalizeOpenAPI2__30);
}
}
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 31f4ca8

Please sign in to comment.