diff --git a/CHANGELOG.md b/CHANGELOG.md index aabfc5d2a5004..c5a1e44171b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/dev-packages/ovsx-client/src/ovsx-client.ts b/dev-packages/ovsx-client/src/ovsx-client.ts index a9ba94225fcaa..685316a6bcd46 100644 --- a/dev-packages/ovsx-client/src/ovsx-client.ts +++ b/dev-packages/ovsx-client/src/ovsx-client.ts @@ -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 @@ -48,43 +44,37 @@ export class OVSXClient { } protected async buildSearchUri(param?: VSXSearchParam): Promise { + 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 { - const apiUri = new URL('api/-/query', this.options!.apiUrl); const param: VSXQueryParam = { extensionId: id }; - const result = await this.postJson(apiUri.toString(), param); + const apiUri = this.buildQueryUri(param); + const result = await this.fetchJson(apiUri); if (result.extensions && result.extensions.length > 0) { return result.extensions[0]; } @@ -96,12 +86,12 @@ export class OVSXClient { * @param id the requested extension id. */ async getAllVersions(id: string): Promise { - const apiUri = new URL('api/-/query', this.options!.apiUrl); const param: VSXQueryParam = { extensionId: id, includeAllVersions: true, }; - const result = await this.postJson(apiUri.toString(), param); + const apiUri = this.buildQueryUri(param); + const result = await this.fetchJson(apiUri); if (result.extensions && result.extensions.length > 0) { return result.extensions; } @@ -112,10 +102,6 @@ export class OVSXClient { return fetchJson(url) as Promise; } - protected postJson(url: string, payload: P): Promise { - return postJson(url, JSON.stringify(payload)) as Promise; - } - fetchText(url: string): Promise { return fetchText(url); }