Skip to content

Commit

Permalink
feat: support for custom RequestInit
Browse files Browse the repository at this point in the history
  • Loading branch information
karol-maciaszek committed Oct 10, 2019
1 parent 9777c09 commit d2ae11a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/__tests__/http.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from 'node-fetch';
import { resolveHttp } from '../http';
import { createResolveHttp, resolveHttp } from '../http';

jest.mock('node-fetch');

Expand All @@ -9,3 +9,15 @@ describe('resolveHttp()', () => {
await expect(resolveHttp({ toString: () => 'http://stoplight.io' } as uri.URI)).resolves.toEqual('test');
});
});

describe('createResolveHttp()', () => {
it('works', async () => {
((fetch as unknown) as jest.Mock).mockImplementation(() => ({ text: jest.fn(() => 'test') }));

await createResolveHttp({ headers: { myLovelyHeader: 'test' } })({
toString: () => 'http://stoplight.io',
} as uri.URI);

expect(fetch).toHaveBeenCalledWith('http://stoplight.io', { headers: { myLovelyHeader: 'test' } });
});
});
10 changes: 7 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import fetch from 'node-fetch';
import fetch, { RequestInit } from 'node-fetch';

export async function resolveHttp(ref: uri.URI) {
const response = await fetch(String(ref));
export async function resolveHttp(ref: uri.URI, opts: RequestInit = {}) {
const response = await fetch(String(ref), opts);
return response.text();
}

export function createResolveHttp(defaultRequestOptions: RequestInit = {}) {
return (ref: uri.URI) => resolveHttp(ref, defaultRequestOptions);
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { resolveHttp } from './http';
export { createResolveHttp, resolveHttp } from './http';
export { resolveFile } from './file';

0 comments on commit d2ae11a

Please sign in to comment.