Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Apr 24, 2024
1 parent 9d5f945 commit c821c36
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 42 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# CHANGELOG 📝

## v0.4.0 2024/04/24

**Breaking Change**

This version is about Axios compatible issue in some cases. Fixing https://github.com/suhaotian/xior/issues/12 and https://github.com/suhaotian/xior/issues/15.

- Feat(core): when `responseType: 'blob' | 'arrarybuffer'` then the `resposne.data` is `Blob` and `ArrayBuffer`, no need `response.blob()` or `response.arraybuffer()` anymore.
- Fix(interceptors): make sure the multiple response interceptors chain behaviour same as axios's interceptors.

## v0.3.13 2024/04/21

- Feat(plugin): add custom paramaters of LRU in plugins: cache, error-cache, throttle
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.3.13/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.13/plugins/mock.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.4.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.4.0/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.3.13/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.13/plugins/mock.umd.js"></script>
<script src="https://unpkg.com/xior@0.4.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.4.0/plugins/mock.umd.js"></script>

<!-- Usage -->
<script>
Expand Down
87 changes: 58 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ yarn add xior
Use jsDelivr CDN:

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

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

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

<!-- Usage -->
<script>
Expand Down Expand Up @@ -316,6 +316,7 @@ http.interceptors.response.use(
if (error?.response?.status === 401) {
localStorage.removeItem('REQUEST_TOKEN');
}
return Promise.reject(error);
}
);
```
Expand Down Expand Up @@ -659,9 +660,9 @@ Use CDN:
Using jsDelivr CDN:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<!-- Usage -->
<script>
Expand Down Expand Up @@ -1336,16 +1337,44 @@ Even if the environment doesn't support `fetch`, you can use a `fetch` polyfill

### 3. How do I handle responses with types like `'stream'`, `'document'`, `'arraybuffer'`, or `'blob'`?

> Currently in `xior`, when `responseType` is set to `'stream', 'document', 'arraybuffer', 'blob', or 'original'`, Xior will return the original fetch response.
When `{responseType: 'blob'| 'arraybuffer'}`:

```ts
xior.get('https://exmaple.com/some/api', { responseType: 'blob' }).then((response) => {
console.log(response.data); // response.data is a Blob
});

// Same with
fetch('https://exmaple.com/some/api')
.then((response) => response.blob())
.then((data) => {
console.log(data); // is a Blob
});
```

```ts
xior.get('https://exmaple.com/some/api', { responseType: 'arraybuffer' }).then((response) => {
console.log(response.data); // response.data is a ArrayBuffer
});

// Same with
fetch('https://exmaple.com/some/api')
.then((response) => response.arraybuffer())
.then((data) => {
console.log(data); // is a ArrayBuffer
});
```

**But when `responseType` set to `'stream', 'document' or 'original'`, Xior will return the original fetch response:**

```ts
fetch('https://exmaple.com/some/api').then((response) => {
console.log(response);
});

// same with
xior.get('https://exmaple.com/some/api', { responseType: 'blob' }).then((res) => {
console.log(res.response); // res.data will be undefined
xior.get('https://exmaple.com/some/api', { responseType: 'stream' }).then((res) => {
console.log(res.response); // But res.data will be undefined
});
```

Expand Down
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.3.13",
"version": "0.4.0",
"description": "A lite request lib based on fetch with plugin support and similar API to axios.",
"repository": "suhaotian/xior",
"bugs": "https://github.com/suhaotian/xior/issues",
Expand Down
4 changes: 2 additions & 2 deletions src/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export default async function defaultRequestInterceptor(req: XiorInterceptorRequ

return {
...req,
data,
_data,
url,
_url,
data,
url,
method,
headers,
isGet,
Expand Down
8 changes: 5 additions & 3 deletions src/xior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class xior {
static create(options?: XiorRequestConfig): XiorInstance {
return new xior(options);
}
static VERSION = '0.3.13';
static VERSION = '0.4.0';

config?: XiorRequestConfig;
defaults: XiorInterceptorRequestConfig;
Expand All @@ -71,7 +71,8 @@ export class xior {
fn: (
config: XiorResponseInterceptorConfig
) => Promise<XiorResponseInterceptorConfig> | XiorResponseInterceptorConfig;
onRejected?: null | ((error: XiorError | Error | TypeError) => any);
/** error: XiorError | Error | TypeError */
onRejected?: null | ((error: XiorError) => any);
}[] = [];

get interceptors() {
Expand Down Expand Up @@ -104,7 +105,8 @@ export class xior {
fn: (
config: XiorResponseInterceptorConfig
) => Promise<XiorResponseInterceptorConfig> | XiorResponseInterceptorConfig,
onRejected?: null | ((error: XiorError | Error | TypeError) => any)
/** error: XiorError | Error | TypeError */
onRejected?: null | ((error: XiorError) => any)
) => {
this.RESI.push({ fn, onRejected });
return fn;
Expand Down
4 changes: 2 additions & 2 deletions tests/src/tests/axios-compatible.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe('axios compatible tests', () => {
//
});
assert.strictEqual(xiorHasError, true);
assert.strictEqual(xiorError.request?.url, '/400');
assert.strictEqual(xiorError.response?.config.url, '/400');
assert.strictEqual(xiorError.request?.url, baseURL + '/400');
assert.strictEqual(xiorError.response?.config.url, baseURL + '/400');
});
});
2 changes: 1 addition & 1 deletion tests/src/tests/interceptors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe('interceptors', function () {

await instance.get('/get').then(({ config: request }) => {
assert.equal(request.baseURL, baseURL);
assert.equal(request.url, '/get');
assert.equal(request.url, baseURL + '/get');
});
});

Expand Down

0 comments on commit c821c36

Please sign in to comment.