@fatcherjs/middleware-json 3.0.0-alpha-4 → 3.0.0-alpha-6

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/README.md CHANGED
@@ -1,4 +1,8 @@
1
- # @fatcher/middleware-json
1
+ # @fatcherjs/middleware-json
2
+
3
+ <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>
4
+ [![install size](https://packagephobia.com/badge?p=@fatcherjs/middleware-json)](https://packagephobia.com/result?p=@fatcherjs/middleware-json)
5
+ <a href="https://unpkg.com/@fatcherjs/middleware-json"><img alt="Size" src="https://img.badgesize.io/https://unpkg.com/@fatcherjs/middleware-json"></a>
2
6
 
3
7
  ## Install
4
8
 
@@ -20,9 +24,13 @@
20
24
  import { fatcher } from 'fatcher';
21
25
  import { json } from '@fatcherjs/middleware-json';
22
26
 
23
- fatcher('https://foo.bar', {
27
+ const res = await fatcher('https://foo.bar/get', {
24
28
  middlewares: [json()],
25
29
  });
30
+
31
+ const streamingJson = await res.readStreamAsJson((string: string, buffer: Uint8Array) => {
32
+ console.log(string, buffer); // chunks for streaming string
33
+ }); // full result
26
34
  ```
27
35
 
28
36
  ## License
@@ -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
@@ -13,15 +13,15 @@
13
13
  "dist"
14
14
  ],
15
15
  "peerDependencies": {
16
- "fatcher": "3.0.0-alpha-4"
16
+ "fatcher": "3.0.0-alpha-6"
17
17
  },
18
18
  "dependencies": {
19
- "@fatcherjs/utils-shared": "3.0.0-alpha-4"
19
+ "@fatcherjs/utils-shared": "3.0.0-alpha-6"
20
20
  },
21
21
  "devDependencies": {
22
- "fatcher": "3.0.0-alpha-4"
22
+ "fatcher": "3.0.0-alpha-6"
23
23
  },
24
- "version": "3.0.0-alpha-4",
24
+ "version": "3.0.0-alpha-6",
25
25
  "scripts": {
26
26
  "clean": "rimraf dist",
27
27
  "build": "npm run clean && rollup -c rollup.config.ts",