@fatcherjs/middleware-json 2.0.0 → 3.0.0-alpha-5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +7 -25
- package/dist/index.d.ts +11 -0
- package/dist/index.esm.js +31 -0
- package/dist/index.js +35 -0
- package/dist/index.min.js +1 -0
- package/package.json +19 -42
- package/dist/json.d.ts +0 -12
- package/dist/json.esm.js +0 -18
- package/dist/json.js +0 -22
- package/dist/json.min.js +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
# @
|
|
2
|
-
|
|
3
|
-
A middleware for transform response to JSON
|
|
4
|
-
|
|
5
|
-
[](https://codecov.io/gh/fatcherjs/middleware-json)
|
|
6
|
-
[](https://packagephobia.com/result?p=@fatcherjs/middleware-json)
|
|
7
|
-
<a href="https://unpkg.com/fatcher"><img alt="Size" src="https://img.badgesize.io/https://unpkg.com/fatcher"></a>
|
|
8
|
-
<a href="https://npmjs.com/package/@fatcherjs/middleware-json"><img src="https://img.shields.io/npm/v/@fatcherjs/middleware-json.svg" alt="npm package"></a>
|
|
9
|
-
<a href="https://github.com/fatcherjs/middleware-json/actions/workflows/ci.yml"><img src="https://github.com/fatcherjs/middleware-json/actions/workflows/ci.yml/badge.svg?branch=master" alt="build status"></a>
|
|
1
|
+
# @fatcher/middleware-json
|
|
10
2
|
|
|
11
3
|
## Install
|
|
12
4
|
|
|
@@ -19,30 +11,20 @@ A middleware for transform response to JSON
|
|
|
19
11
|
### CDN
|
|
20
12
|
|
|
21
13
|
```html
|
|
22
|
-
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-json/dist/
|
|
14
|
+
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-json/dist/index.min.js"></script>
|
|
23
15
|
```
|
|
24
16
|
|
|
25
17
|
## Usage
|
|
26
18
|
|
|
27
19
|
```ts
|
|
28
|
-
import { json } from '@fatcherjs/middleware-json';
|
|
29
20
|
import { fatcher } from 'fatcher';
|
|
21
|
+
import { json } from '@fatcherjs/middleware-json';
|
|
30
22
|
|
|
31
|
-
fatcher({
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
body: {
|
|
35
|
-
bar: 'foo',
|
|
36
|
-
},
|
|
37
|
-
})
|
|
38
|
-
.then(res => {
|
|
39
|
-
console.log(res);
|
|
40
|
-
})
|
|
41
|
-
.catch(err => {
|
|
42
|
-
console.error(error);
|
|
43
|
-
});
|
|
23
|
+
fatcher('https://foo.bar', {
|
|
24
|
+
middlewares: [json()],
|
|
25
|
+
});
|
|
44
26
|
```
|
|
45
27
|
|
|
46
28
|
## License
|
|
47
29
|
|
|
48
|
-
[
|
|
30
|
+
[MIT](https://github.com/fanhaoyuan/fatcher/blob/master/LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FatcherMiddleware } from 'fatcher';
|
|
2
|
+
|
|
3
|
+
declare const json: () => FatcherMiddleware;
|
|
4
|
+
|
|
5
|
+
declare module 'fatcher' {
|
|
6
|
+
interface FatcherResponse {
|
|
7
|
+
readStreamAsJson: <T>(onRead?: (chunk: string, buffer: Uint8Array) => void | Promise<void>) => Promise<T | null>;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { json };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { readStreamByChunk } from '@fatcherjs/utils-shared';
|
|
2
|
+
import 'fatcher';
|
|
3
|
+
|
|
4
|
+
const json = () => {
|
|
5
|
+
return async (context, next) => {
|
|
6
|
+
const response = await next();
|
|
7
|
+
if (response.bodyUsed || !response.body) {
|
|
8
|
+
response.readStreamAsJson = async () => null;
|
|
9
|
+
return response;
|
|
10
|
+
}
|
|
11
|
+
const clonedResponse = response.clone();
|
|
12
|
+
response.readStreamAsJson = async (onRead) => {
|
|
13
|
+
const textDecoder = new TextDecoder();
|
|
14
|
+
let string = "";
|
|
15
|
+
await readStreamByChunk(clonedResponse.body, (chunk) => {
|
|
16
|
+
const currentChunkString = textDecoder.decode(chunk, { stream: true });
|
|
17
|
+
string += currentChunkString;
|
|
18
|
+
onRead?.(currentChunkString, chunk);
|
|
19
|
+
});
|
|
20
|
+
string += textDecoder.decode(void 0, { stream: false });
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(string);
|
|
23
|
+
} catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
return response;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { json };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var utilsShared = require('@fatcherjs/utils-shared');
|
|
6
|
+
require('fatcher');
|
|
7
|
+
|
|
8
|
+
const json = () => {
|
|
9
|
+
return async (context, next) => {
|
|
10
|
+
const response = await next();
|
|
11
|
+
if (response.bodyUsed || !response.body) {
|
|
12
|
+
response.readStreamAsJson = async () => null;
|
|
13
|
+
return response;
|
|
14
|
+
}
|
|
15
|
+
const clonedResponse = response.clone();
|
|
16
|
+
response.readStreamAsJson = async (onRead) => {
|
|
17
|
+
const textDecoder = new TextDecoder();
|
|
18
|
+
let string = "";
|
|
19
|
+
await utilsShared.readStreamByChunk(clonedResponse.body, (chunk) => {
|
|
20
|
+
const currentChunkString = textDecoder.decode(chunk, { stream: true });
|
|
21
|
+
string += currentChunkString;
|
|
22
|
+
onRead?.(currentChunkString, chunk);
|
|
23
|
+
});
|
|
24
|
+
string += textDecoder.decode(void 0, { stream: false });
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(string);
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return response;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.json = json;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(t,n){typeof exports=="object"&&typeof module!="undefined"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(t=typeof globalThis!="undefined"?globalThis:t||self,n(t.FatcherMiddlewareJson={}))})(this,function(t){"use strict";async function n(i,a){async function e(s){const{value:r,done:o}=await s.read();o||(await a(r),await e(s))}return e(i.getReader())}const f=()=>async(i,a)=>{const e=await a();if(e.bodyUsed||!e.body)return e.readStreamAsJson=async()=>null,e;const s=e.clone();return e.readStreamAsJson=async r=>{const o=new TextDecoder;let c="";await n(s.body,d=>{const u=o.decode(d,{stream:!0});c+=u,r==null||r(u,d)}),c+=o.decode(void 0,{stream:!1});try{return JSON.parse(c)}catch(d){return null}},e};t.json=f,Object.defineProperty(t,"__esModule",{value:!0})});
|
package/package.json
CHANGED
|
@@ -1,55 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fatcherjs/middleware-json",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"middleware",
|
|
13
|
-
"json"
|
|
14
|
-
],
|
|
3
|
+
"homepage": "https://github.com/fanhaoyuan/fatcher",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/fanhaoyuan/fatcher.git"
|
|
7
|
+
},
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.esm.js",
|
|
10
|
+
"browser": "dist/index.min.js",
|
|
11
|
+
"typings": "dist/index.d.ts",
|
|
15
12
|
"files": [
|
|
16
13
|
"dist"
|
|
17
14
|
],
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
"type": "git",
|
|
21
|
-
"url": "git+https://github.com/fatcherjs/middleware-json"
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"fatcher": "3.0.0-alpha-5"
|
|
22
17
|
},
|
|
23
|
-
"
|
|
24
|
-
"@
|
|
25
|
-
"@fansy/prettier-config": "^1.0.0",
|
|
26
|
-
"@jest/types": "^29.1.2",
|
|
27
|
-
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
28
|
-
"@types/jest": "^29.1.2",
|
|
29
|
-
"@types/node": "^18.11.2",
|
|
30
|
-
"esbuild": "^0.15.10",
|
|
31
|
-
"fatcher": "^2.0.0",
|
|
32
|
-
"jest": "^29.1.2",
|
|
33
|
-
"jest-fetch-mock": "^3.0.3",
|
|
34
|
-
"rimraf": "^3.0.2",
|
|
35
|
-
"rollup": "^2.79.1",
|
|
36
|
-
"rollup-plugin-dts": "^4.2.3",
|
|
37
|
-
"rollup-plugin-esbuild": "^4.10.1",
|
|
38
|
-
"ts-jest": "^29.0.3",
|
|
39
|
-
"ts-node": "^10.9.1",
|
|
40
|
-
"typescript": "^4.8.4"
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@fatcherjs/utils-shared": "3.0.0-alpha-5"
|
|
41
20
|
},
|
|
42
|
-
"
|
|
43
|
-
"fatcher": "
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"fatcher": "3.0.0-alpha-5"
|
|
44
23
|
},
|
|
24
|
+
"version": "3.0.0-alpha-5",
|
|
45
25
|
"scripts": {
|
|
46
|
-
"
|
|
47
|
-
"build": "
|
|
48
|
-
"deploy": "pnpm run build && pnpm publish --no-git-check",
|
|
26
|
+
"clean": "rimraf dist",
|
|
27
|
+
"build": "npm run clean && rollup -c rollup.config.ts",
|
|
49
28
|
"test": "jest",
|
|
50
29
|
"test:cov": "jest --coverage",
|
|
51
|
-
"
|
|
52
|
-
"tsc": "tsc --noEmit",
|
|
53
|
-
"ci": "npm run eslint && npm run tsc && npm run build && npm run test"
|
|
30
|
+
"tsc": "tsc --noEmit"
|
|
54
31
|
}
|
|
55
32
|
}
|
package/dist/json.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Middleware } from 'fatcher';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A middleware for transforming stream into json
|
|
5
|
-
*
|
|
6
|
-
* If response.body never used, will try to transform into json.
|
|
7
|
-
*
|
|
8
|
-
* But if transform error. Will return a origin response.
|
|
9
|
-
*/
|
|
10
|
-
declare function json(): Middleware;
|
|
11
|
-
|
|
12
|
-
export { json };
|
package/dist/json.esm.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { defineMiddleware, canActivate } from 'fatcher';
|
|
2
|
-
|
|
3
|
-
function json() {
|
|
4
|
-
return defineMiddleware(async (context, next) => {
|
|
5
|
-
const result = await next();
|
|
6
|
-
if (canActivate(result.data)) {
|
|
7
|
-
const clonedResponse = result.data.clone();
|
|
8
|
-
try {
|
|
9
|
-
const data = await clonedResponse.json();
|
|
10
|
-
return Object.assign(result, { data });
|
|
11
|
-
} catch (e) {
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return result;
|
|
15
|
-
}, "fatcher-middleware-json");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { json };
|
package/dist/json.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var fatcher = require('fatcher');
|
|
6
|
-
|
|
7
|
-
function json() {
|
|
8
|
-
return fatcher.defineMiddleware(async (context, next) => {
|
|
9
|
-
const result = await next();
|
|
10
|
-
if (fatcher.canActivate(result.data)) {
|
|
11
|
-
const clonedResponse = result.data.clone();
|
|
12
|
-
try {
|
|
13
|
-
const data = await clonedResponse.json();
|
|
14
|
-
return Object.assign(result, { data });
|
|
15
|
-
} catch (e) {
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
}, "fatcher-middleware-json");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
exports.json = json;
|
package/dist/json.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(e,t){typeof exports=="object"&&typeof module!="undefined"?t(exports,require("fatcher")):typeof define=="function"&&define.amd?define(["exports","fatcher"],t):(e=typeof globalThis!="undefined"?globalThis:e||self,t(e.FatcherMiddlewareJson={},e.Fatcher))})(this,function(e,t){"use strict";function o(){return t.defineMiddleware(async(a,s)=>{const n=await s();if(t.canActivate(n.data)){const d=n.data.clone();try{const i=await d.json();return Object.assign(n,{data:i})}catch(i){}}return n},"fatcher-middleware-json")}e.json=o,Object.defineProperty(e,"__esModule",{value:!0})});
|