diff --git a/lib/util/http/__snapshots__/index.spec.ts.snap b/lib/util/http/__snapshots__/index.spec.ts.snap index 8d5d6afa8098675b8e6ac8402bbbfdb2f890e580..b8031b689a8b468ccecee2baee89585aafe655c8 100644 --- a/lib/util/http/__snapshots__/index.spec.ts.snap +++ b/lib/util/http/__snapshots__/index.spec.ts.snap @@ -1,5 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`util/http/index deleteJson 1`] = ` +Object { + "body": Object {}, + "headers": Object { + "content-type": "application/json", + }, +} +`; + exports[`util/http/index get 1`] = ` Object { "body": "", @@ -16,6 +25,24 @@ Object { } `; +exports[`util/http/index headJson 1`] = ` +Object { + "body": Object {}, + "headers": Object { + "content-type": "application/json", + }, +} +`; + +exports[`util/http/index patchJson 1`] = ` +Object { + "body": Object {}, + "headers": Object { + "content-type": "application/json", + }, +} +`; + exports[`util/http/index postJson 1`] = ` Object { "body": Object {}, @@ -24,3 +51,12 @@ Object { }, } `; + +exports[`util/http/index putJson 1`] = ` +Object { + "body": Object {}, + "headers": Object { + "content-type": "application/json", + }, +} +`; diff --git a/lib/util/http/index.spec.ts b/lib/util/http/index.spec.ts index d58b3c527d75210df7b844e9cba5990f6fbb89cd..27ea216f3463270d2a801e591b428836d658f217 100644 --- a/lib/util/http/index.spec.ts +++ b/lib/util/http/index.spec.ts @@ -33,6 +33,42 @@ describe(getName(__filename), () => { ).toMatchSnapshot(); expect(nock.isDone()).toBe(true); }); + it('putJson', async () => { + nock(baseUrl) + .put('/') + .reply(200, {}); + expect( + await http.putJson('http://renovate.com', { body: {}, baseUrl }) + ).toMatchSnapshot(); + expect(nock.isDone()).toBe(true); + }); + it('patchJson', async () => { + nock(baseUrl) + .patch('/') + .reply(200, {}); + expect( + await http.patchJson('http://renovate.com', { body: {}, baseUrl }) + ).toMatchSnapshot(); + expect(nock.isDone()).toBe(true); + }); + it('deleteJson', async () => { + nock(baseUrl) + .delete('/') + .reply(200, {}); + expect( + await http.deleteJson('http://renovate.com', { body: {}, baseUrl }) + ).toMatchSnapshot(); + expect(nock.isDone()).toBe(true); + }); + it('headJson', async () => { + nock(baseUrl) + .head('/') + .reply(200, {}); + expect( + await http.headJson('http://renovate.com', { body: {}, baseUrl }) + ).toMatchSnapshot(); + expect(nock.isDone()).toBe(true); + }); it('stream', async () => { nock(baseUrl) diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts index c4a6201a0802dd96132a05560e86c3538636676c..0cd6819b488733dc431fc70abefeb5a60ab364ad 100644 --- a/lib/util/http/index.ts +++ b/lib/util/http/index.ts @@ -20,7 +20,7 @@ export interface HttpPostOptions extends HttpOptions { interface InternalHttpOptions extends HttpOptions { json?: boolean; - method?: 'get' | 'post'; + method?: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head'; } export interface HttpResponse<T = string> { @@ -97,6 +97,34 @@ export class Http { return this.requestJson<T>(url, { ...options, method: 'post' }); } + async putJson<T = unknown>( + url: string, + options: HttpPostOptions + ): Promise<HttpResponse<T>> { + return this.requestJson<T>(url, { ...options, method: 'put' }); + } + + async patchJson<T = unknown>( + url: string, + options: HttpPostOptions + ): Promise<HttpResponse<T>> { + return this.requestJson<T>(url, { ...options, method: 'patch' }); + } + + async deleteJson<T = unknown>( + url: string, + options: HttpPostOptions + ): Promise<HttpResponse<T>> { + return this.requestJson<T>(url, { ...options, method: 'delete' }); + } + + async headJson<T = unknown>( + url: string, + options: HttpPostOptions + ): Promise<HttpResponse<T>> { + return this.requestJson<T>(url, { ...options, method: 'head' }); + } + stream(url: string, options?: HttpOptions): NodeJS.ReadableStream { const combinedOptions: any = { method: 'get',