Skip to content

Commit

Permalink
Use get for querying extension data from ovsx
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Oct 26, 2021
1 parent a87c994 commit 1811b5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- renamed `createMenuBar` to `createElectronMenuBar`
- [core] moved `DEFAULT_WINDOW_HASH` to `common/window.ts` [#10291](https://github.com/eclipse-theia/theia/pull/10291)
- [core] moved `NewWindowOptions` to `common/window.ts` [#10291](https://github.com/eclipse-theia/theia/pull/10291)
- [ovsx-client] removed `fetchJson` method from `OVSXClient` [#10325](https://github.com/eclipse-theia/theia/pull/10325)

## v1.18.0 - 9/30/2021

Expand Down
52 changes: 19 additions & 33 deletions dev-packages/ovsx-client/src/ovsx-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ import {

const fetchText = bent('GET', 'string', 200);
const fetchJson = bent('GET', { 'Accept': 'application/json' }, 'json', 200);
const postJson = bent('POST', {
'Content-Type': 'application/json',
'Accept': 'application/json'
}, 'json', 200);

export interface OVSXClientOptions {
apiVersion: string
Expand All @@ -48,43 +44,37 @@ export class OVSXClient {
}

protected async buildSearchUri(param?: VSXSearchParam): Promise<string> {
return this.buildUri('api/-/search', param);
}

protected buildQueryUri(param?: VSXQueryParam): string {
return this.buildUri('api/-/query', param);
}

protected buildUri(url: string, param?: Object): string {
let searchUri = '';
if (param) {
const query: string[] = [];
if (param.query) {
query.push('query=' + encodeURIComponent(param.query));
}
if (param.category) {
query.push('category=' + encodeURIComponent(param.category));
}
if (param.size) {
query.push('size=' + param.size);
}
if (param.offset) {
query.push('offset=' + param.offset);
}
if (param.sortOrder) {
query.push('sortOrder=' + encodeURIComponent(param.sortOrder));
}
if (param.sortBy) {
query.push('sortBy=' + encodeURIComponent(param.sortBy));
}
if (param.includeAllVersions) {
query.push('includeAllVersions=' + param.includeAllVersions);
for (const [key, value] of Object.entries(param)) {
if (typeof value === 'string') {
query.push(`${key}=${encodeURIComponent(value)}`);
} else if (typeof value === 'boolean' || typeof value === 'number') {
query.push(`${key}=${String(value)}`);
}
}
if (query.length > 0) {
searchUri += '?' + query.join('&');
}
}
return new URL(`api/-/search${searchUri}`, this.options!.apiUrl).toString();
return new URL(`${url}${searchUri}`, this.options!.apiUrl).toString();
}

async getExtension(id: string): Promise<VSXExtensionRaw> {
const apiUri = new URL('api/-/query', this.options!.apiUrl);
const param: VSXQueryParam = {
extensionId: id
};
const result = await this.postJson<VSXQueryParam, VSXQueryResult>(apiUri.toString(), param);
const apiUri = this.buildQueryUri(param);
const result = await this.fetchJson<VSXQueryResult>(apiUri);
if (result.extensions && result.extensions.length > 0) {
return result.extensions[0];
}
Expand All @@ -96,12 +86,12 @@ export class OVSXClient {
* @param id the requested extension id.
*/
async getAllVersions(id: string): Promise<VSXExtensionRaw[]> {
const apiUri = new URL('api/-/query', this.options!.apiUrl);
const param: VSXQueryParam = {
extensionId: id,
includeAllVersions: true,
};
const result = await this.postJson<VSXQueryParam, VSXQueryResult>(apiUri.toString(), param);
const apiUri = this.buildQueryUri(param);
const result = await this.fetchJson<VSXQueryResult>(apiUri);
if (result.extensions && result.extensions.length > 0) {
return result.extensions;
}
Expand All @@ -112,10 +102,6 @@ export class OVSXClient {
return fetchJson(url) as Promise<R>;
}

protected postJson<P, R>(url: string, payload: P): Promise<R> {
return postJson(url, JSON.stringify(payload)) as Promise<R>;
}

fetchText(url: string): Promise<string> {
return fetchText(url);
}
Expand Down

0 comments on commit 1811b5e

Please sign in to comment.