diff --git a/src/apicurioExplorer.ts b/src/apicurioExplorer.ts index 1c9ba9e..a138997 100644 --- a/src/apicurioExplorer.ts +++ b/src/apicurioExplorer.ts @@ -212,7 +212,7 @@ export class ApicurioExplorerProvider implements vscode.TreeDataProvider { return this.searchArtifacts(); } @@ -44,7 +39,7 @@ class RegistryClient { public searchArtifacts(options?: object): Promise { const res = this.executeRequest( this.requestPath(`search/artifacts`, { - ...this.settings.limits(), + ...Services.get().getSettings().limits(), ...options, }) ) as Promise; @@ -79,23 +74,25 @@ class RegistryClient { private executeRequest(path: string, method?: string, headers?: any, body?: any): Promise { return new Promise((resolve, reject) => { - const client = this.settings.useHttps ? https : http; + const settings = Services.get().getSettings(); + const client = settings.useHttps ? https : http; + if (!isObject(headers)) { + headers = {}; + } headers = { - Accept: '*/*', - 'Content-Type': 'application/json', + ...{ 'Content-Type': 'application/json', Accept: '*/*' }, ...headers, }; - if (headers['Content-Type'].endsWith('yaml') || headers['Content-Type'].endsWith('yml')) { headers['Content-Type'] = 'application/x-yaml'; } const req = client.request( { - hostname: this.settings.hostname, - port: this.settings.port, - path: `${this.settings.path}${path}`, + hostname: settings.hostname, + port: settings.port, + path: `${settings.path}${path}`, method: method ? method : 'GET', headers: headers, }, diff --git a/src/services.ts b/src/services.ts index e5dfa93..1c5516a 100644 --- a/src/services.ts +++ b/src/services.ts @@ -30,20 +30,18 @@ class Services { public static get() { if (this.instance == null) { this.instance = new this(); - this.instance.settings = new Settings(); - this.instance.client = new RegistryClient(this.instance.settings); + this.instance.client = new RegistryClient(); } return this.instance; } - private settings: Settings; private client: RegistryClient; // eslint-disable-next-line @typescript-eslint/no-empty-function private constructor() {} public getSettings() { - return this.settings; + return new Settings(); // TODO: We need to create a new instance in case the settings change. } public getRegistryClient() { diff --git a/src/tools.ts b/src/tools.ts index c676075..2b40f5f 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as http from 'http'; import * as https from 'https'; import { CurrentArtifact } from './interfaces'; +import { isObject } from './utils'; export class ApicurioTools { /** @@ -111,7 +112,14 @@ export class ApicurioTools { return new Promise((resolve, reject) => { const hhttpx = vscode.workspace.getConfiguration('apicurio.http').get('secure') ? https : http; const settings = this.getApicurioHttpSettings(); - headers = !headers ? { 'Content-Type': 'application/json', Accept: '*/*' } : headers; + + if (!isObject(headers)) { + headers = {}; + } + headers = { + ...{ 'Content-Type': 'application/json', Accept: '*/*' }, + ...headers, + }; // FIX Apicurio isso on Yaml mime type (for OAS mostly). // If the type is not recognise, the entity is stored as YAML and not JSON wich is an issue for referencine entities thrue the registry across schamas // ex : $ref: "http://127.0.0.1.nip.io:8080/apis/registry/v2/groups/test/artifacts/test/versions/1#/components/schemas/test" diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..f58baf9 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,5 @@ +function isObject(value: unknown): value is object { + return value instanceof Object && value.constructor === Object; +} + +export { isObject };