Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proper fetch-spec related input handling #86

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Response, Request, Headers as FetchHeaders, RequestInfo, RequestInit }
import { AxiosInstance, AxiosRequestConfig } from './types';
import FormData from 'form-data';

export type AxiosTransformer = (config: AxiosRequestConfig, input: RequestInfo, init?: RequestInit) => AxiosRequestConfig;
export type FetchInit = RequestInit & { extra?: any }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was that any extra options (a superset of RequestInit) would be passed to the transformer, not just extra

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's be fine if you did some hacky typing in the test code to make it work


export type AxiosFetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>;
export type AxiosTransformer = (config: AxiosRequestConfig, input: RequestInfo, init?: FetchInit) => AxiosRequestConfig;

export type AxiosFetch = (input: RequestInfo, init?: FetchInit) => Promise<Response>;

/**
* A Fetch WebAPI implementation based on the Axios client
Expand All @@ -18,7 +20,7 @@ async function axiosFetch (
axios: AxiosInstance,
transformer: AxiosTransformer | undefined,
input: RequestInfo,
init?: RequestInit
init?: FetchInit
) {
// Request class handles for us all the input normalisation
const request = new Request(input, init);
Expand Down
18 changes: 9 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import nock from 'nock';
import fetch, { RequestInit } from 'node-fetch';
import { buildAxiosFetch } from '../src';
import fetch from 'node-fetch';
import { buildAxiosFetch, FetchInit } from '../src';
import axios, { AxiosInstance, AxiosPromise, AxiosRequestConfig } from 'axios';
import sinon from 'sinon';
import FormData from 'form-data';
Expand Down Expand Up @@ -50,7 +50,7 @@ test.before(() => {
.replyWithError('simulated failure');
});

async function dualFetch (input: string, init?: RequestInit) {
async function dualFetch (input: string, init?: FetchInit) {
const expectedResponse = await fetch(input, init);
const axiosFetch = buildAxiosFetch(axios);
const axiosResponse = await axiosFetch(input, init);
Expand Down Expand Up @@ -85,7 +85,7 @@ test('returns the expected response on a text body', async (test) => {
});

test('respects the headers init option', async (test) => {
const init: RequestInit = {
const init: FetchInit = {
method: 'POST',
headers: {
'testheader': 'test-value'
Expand All @@ -99,7 +99,7 @@ test('respects the headers init option', async (test) => {
});

test('handles text body init options', async (test) => {
const init: RequestInit = {
const init: FetchInit = {
method: 'POST',
body: 'some text'
};
Expand All @@ -113,7 +113,7 @@ test('handles text body init options', async (test) => {
});

test('handles text body with content-type init options', async (test) => {
const init: RequestInit = {
const init: FetchInit = {
method: 'POST',
body: '{}',
headers: {
Expand All @@ -130,7 +130,7 @@ test('handles text body with content-type init options', async (test) => {
});

test('handles undefined body in init options', async (test) => {
const init: RequestInit = {
const init: FetchInit = {
method: 'POST',
body: undefined
};
Expand All @@ -145,7 +145,7 @@ test('handles undefined body in init options', async (test) => {
test('returns the expected response on a multipart request', async (test) => {
const data = new FormData();
data.append('key', 'value');
const init: RequestInit = {
const init: FetchInit = {
method: 'POST',
body: data
};
Expand Down Expand Up @@ -208,7 +208,7 @@ test('allows transforming request options', async (test) => {

const axiosFetch = buildAxiosFetch(client, transformer);

const init: RequestInit = { method: 'POST' };
const init: FetchInit = { method: 'POST', extra: 'options' };
await axiosFetch(originalUrl, init);

// Make sure the transformer was called with the expected arguments
Expand Down