Skip to content

Commit

Permalink
fix: dont automatically set content type for fetcher()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `headersMdw` is now opt-in
  • Loading branch information
neurosnap committed Sep 12, 2023
1 parent fe762d6 commit a9c7307
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import nock from 'nock';
import { fetcher, fetchRetry } from './fetch';
import { fetcher, fetchRetry, headersMdw } from './fetch';
import { createApi } from './api';
import { setupStore } from './util';
import { requestMonitor } from './middleware';
Expand All @@ -21,6 +21,7 @@ test('fetch - should be able to fetch a resource and save automatically', async
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.get('/users', function* (ctx, next) {
Expand Down Expand Up @@ -56,6 +57,7 @@ test('fetch - should be able to fetch a resource and parse as text instead of js
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.get('/users', function* (ctx, next) {
Expand Down Expand Up @@ -86,6 +88,7 @@ test('fetch - error handling', async (t) => {
ctx.request = ctx.req({ url: `${baseUrl}${url}` });
yield next();
});
api.use(headersMdw);
api.use(fetcher());

const fetchUsers = api.get('/users', function* (ctx, next) {
Expand Down Expand Up @@ -117,6 +120,7 @@ test('fetch - status 204', async (t) => {
ctx.request = ctx.req({ url: `${baseUrl}${url}` });
yield next();
});
api.use(headersMdw);
api.use(fetcher());

const fetchUsers = api.get('/users', function* (ctx, next) {
Expand Down Expand Up @@ -148,6 +152,7 @@ test('fetch - malformed json', async (t) => {
ctx.request = ctx.req({ url: `${baseUrl}${url}` });
yield next();
});
api.use(headersMdw);
api.use(fetcher());

const fetchUsers = api.get('/users', function* (ctx, next) {
Expand Down Expand Up @@ -177,6 +182,7 @@ test('fetch - POST', async (t) => {
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.post('/users', function* (ctx, next) {
Expand Down Expand Up @@ -210,6 +216,7 @@ test('fetch - POST multiple endpoints with same uri', async (t) => {
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.post<{ id: string }>(
Expand Down Expand Up @@ -265,6 +272,7 @@ test('fetch - slug in url but payload has empty string for slug value', async (t
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.post<{ id: string }>(
Expand Down Expand Up @@ -301,6 +309,7 @@ test('fetch retry - with success - should keep retrying fetch request', async (t
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.get('/users', [
Expand Down Expand Up @@ -337,6 +346,7 @@ test('fetch retry - with failure - should keep retrying and then quit', async (t
const api = createApi();
api.use(requestMonitor());
api.use(api.routes());
api.use(headersMdw);
api.use(fetcher({ baseUrl }));

const fetchUsers = api.get('/users', [
Expand Down
8 changes: 1 addition & 7 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,5 @@ export function fetcher<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
baseUrl?: string;
} = { baseUrl: '' },
) {
return compose<CurCtx>([
headersMdw,
apiUrlMdw(baseUrl),
payloadMdw,
fetchMdw,
jsonMdw,
]);
return compose<CurCtx>([apiUrlMdw(baseUrl), payloadMdw, fetchMdw, jsonMdw]);
}

0 comments on commit a9c7307

Please sign in to comment.