Skip to content

Commit

Permalink
feat(internal): Add support for missing HTTP methods (#5899)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 7, 2020
1 parent 0622c81 commit 9a68c13
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
36 changes: 36 additions & 0 deletions 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": "",
Expand All @@ -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 {},
Expand All @@ -24,3 +51,12 @@ Object {
},
}
`;

exports[`util/http/index putJson 1`] = `
Object {
"body": Object {},
"headers": Object {
"content-type": "application/json",
},
}
`;
36 changes: 36 additions & 0 deletions lib/util/http/index.spec.ts
Expand Up @@ -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)
Expand Down
30 changes: 29 additions & 1 deletion lib/util/http/index.ts
Expand Up @@ -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> {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 9a68c13

Please sign in to comment.