Skip to content

Commit

Permalink
fix: small client bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsenko committed Apr 12, 2024
1 parent 3fc6e28 commit 0b6868b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/apicurioExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class ApicurioExplorerProvider implements vscode.TreeDataProvider<SearchE

// Confirm box
const confirm = await vscode.window.showQuickPick(_.tools.getLists('confirm'), {
title: `Confirm '${artifactType}' : '${groupId}/${artifactId}:${version}'`,
title: `Create ${artifactType} artifact with identifiers '${groupId}:${artifactId}:${version}' ?`,
canPickMany: false,
});
if (confirm != 'yes') {
Expand Down
31 changes: 14 additions & 17 deletions src/registryClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Settings } from './services';
import * as vscode from 'vscode';
import * as https from 'https';
import * as http from 'http';
import * as https from 'https';
import * as vscode from 'vscode';
import { Services, Settings } from './services';
import { isObject } from './utils';

interface SearchedArtifact {
groupId: string | undefined;
Expand Down Expand Up @@ -31,20 +32,14 @@ interface ArtifactSearchResult {
const DEFAULT_GROUP_ID = 'default';

class RegistryClient {
private settings: Settings;

constructor(settings: Settings) {
this.settings = settings;
}

public getArtifacts(): Promise<ArtifactSearchResult> {
return this.searchArtifacts();
}

public searchArtifacts(options?: object): Promise<ArtifactSearchResult> {
const res = this.executeRequest(
this.requestPath(`search/artifacts`, {
...this.settings.limits(),
...Services.get().getSettings().limits(),
...options,
})
) as Promise<ArtifactSearchResult>;
Expand Down Expand Up @@ -79,23 +74,25 @@ class RegistryClient {

private executeRequest(path: string, method?: string, headers?: any, body?: any): Promise<object | string | null> {
return new Promise<object | string>((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,
},
Expand Down
6 changes: 2 additions & 4 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -111,7 +112,14 @@ export class ApicurioTools {
return new Promise<string>((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"
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function isObject(value: unknown): value is object {
return value instanceof Object && value.constructor === Object;
}

export { isObject };

0 comments on commit 0b6868b

Please sign in to comment.