Skip to content

Commit

Permalink
fix(core): when post with `headers: { 'content-type': 'application/x-…
Browse files Browse the repository at this point in the history
…www-form-urlencoded' }`, shouldn't post with body
  • Loading branch information
suhaotian committed Mar 22, 2024
1 parent 294535f commit e37c72c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG 📝

## v0.2.6 2024/03/22

- fix(core): when post with `headers: { 'content-type': 'application/x-www-form-urlencoded' }`, shouldn't post with body
- chore(core): shorter naming words

## v0.2.5 2024/03/20

- fix(plugin): fix `error-retry` plugin default options override bugs
Expand Down
8 changes: 4 additions & 4 deletions Mock-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ instance.get('/api/hello').then((res) => {
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/mock.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/mock.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -79,8 +79,8 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.5/plugins/mock.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/mock.umd.js"></script>

<!-- Usage -->
<script>
Expand Down
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ yarn add xior

#### Use CDN

> Since v0.2.1, xior support UMD format now :()
> Since v0.2.1, xior support UMD format
Use jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -131,7 +131,7 @@ Use jsDelivr CDN:
Use unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -442,9 +442,9 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/error-retry.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/error-retry.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -457,10 +457,10 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.5/plugins/error-retry.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/error-retry.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -543,9 +543,9 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/throttle.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/throttle.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -558,10 +558,10 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.5/plugins/throttle.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/throttle.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -609,9 +609,9 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/dedupe.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/dedupe.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -624,10 +624,10 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.5/plugins/dedupe.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/dedupe.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -685,9 +685,9 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/error-cache.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/error-cache.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -700,10 +700,10 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.5/plugins/error-cache.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/error-cache.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -838,9 +838,9 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/progress.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/progress.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -853,10 +853,10 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.5/plugins/progress.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/progress.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -963,9 +963,9 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.5/plugins/mock.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/mock.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -978,10 +978,10 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.2.5/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.5/plugins/mock.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/mock.umd.js"></script>

<!-- Usage -->
<script>
Expand Down
20 changes: 20 additions & 0 deletions next-example/app/api/client/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client';

import { useEffect } from 'react';

import { http } from '@/app/http';

export default function ClientPage() {
useEffect(() => {
http.post(
'/',
{ data: 123456 },
{
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
}
);
}, []);
return <div>hello</div>;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xior",
"version": "0.2.5",
"version": "0.2.6",
"description": "A lite request lib based on fetch with plugins support, and axios similar API",
"repository": "suhaotian/xior",
"bugs": "https://github.com/suhaotian/xior/issues",
Expand Down
8 changes: 5 additions & 3 deletions src/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { XiorInterceptorRequestConfig } from './types';
import { encodeParams, merge } from './utils';

const encodeUrlType = 'application/x-www-form-urlencoded';
export const formUrl = 'application/x-www-form-urlencoded';
const jsonType = 'application/json';
// const formType = 'multipart/form-data';

Expand Down Expand Up @@ -31,14 +31,16 @@ export default async function defaultRequestInterceptor(
});
if (contentTypeKey) {
contentType = req.headers[contentTypeKey];
req.headers['Content-Type'] = contentType;
delete req.headers[contentTypeKey];
}
}
if (!contentType) {
contentType = likeGET(method) ? encodeUrlType : jsonType;
contentType = likeGET(method) ? formUrl : jsonType;
headers['Content-Type'] = contentType;
}

if (contentType === encodeUrlType && (typeof data === 'object' || req.params)) {
if (contentType === formUrl && (typeof data === 'object' || req.params)) {
encodedParams = true;
const encodeUrlData = encode(merge(data || {}, req.params || {}), encodeURI);
if (encodeUrlData) {
Expand Down
9 changes: 6 additions & 3 deletions src/xior.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import defaultRequestInterceptor, { likeGET } from './interceptors';
import defaultRequestInterceptor, { formUrl, likeGET } from './interceptors';
import type {
XiorInterceptorRequestConfig,
XiorPlugin,
Expand All @@ -23,7 +23,7 @@ export class xior {
static create(options?: XiorRequestConfig): XiorInstance {
return new xior(options);
}
static VERSION = '0.2.5';
static VERSION = '0.2.6';

config?: XiorRequestConfig;
defaults: XiorInterceptorRequestConfig;
Expand Down Expand Up @@ -201,7 +201,10 @@ export class xior {
}

const response = await fetch(finalURL, {
body: likeGET(requestConfig.method) ? undefined : _data,
body:
likeGET(requestConfig.method) || requestConfig.headers?.['Content-Type'] === formUrl
? undefined
: _data,
...rest,
signal,
method,
Expand Down
23 changes: 23 additions & 0 deletions tests/src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ describe('xior tests', () => {
assert.strictEqual(data.query.b, '2');
});

it("POST should work without body when header's `content-type: application/x-www-form-urlencoded`", async () => {
const { data } = await xiorInstance.post<{
method: string;
body: Record<string, any>;
query: Record<string, any>;
}>(
'/post',
{ a: 1, b: 2 },
{
params: { c: 3, d: 4 },
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
}
);
assert.strictEqual(data.method, 'post');
assert.strictEqual(Object.keys(data.query).length, 4);
assert.strictEqual(data.query.a, '1');
assert.strictEqual(data.query.b, '2');
assert.strictEqual(data.query.c, '3');
assert.strictEqual(data.query.d, '4');
});

it('DELETE should work', async () => {
const { data } = await xiorInstance.delete<{
method: string;
Expand Down

0 comments on commit e37c72c

Please sign in to comment.