@aomex/pretty-json 0.0.4 → 0.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @aomex/pretty-json
2
2
 
3
+ ## 0.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [`922b033`](https://github.com/aomex/aomex/commit/922b033915d39205454ebce15540b43b09ac9e8c) Thanks [@geekact](https://github.com/geekact)! - feat(pretty-json): 禁用美化功能时直接跳过该中间件
8
+
9
+ - Updated dependencies [[`d3a2757`](https://github.com/aomex/aomex/commit/d3a2757b38391ad70a206d95eac09520c64e9b85), [`07c8575`](https://github.com/aomex/aomex/commit/07c857565ede4eca7e88de4e8c5e5f244f9d16cb), [`e80844a`](https://github.com/aomex/aomex/commit/e80844a6808fd0f3b9d613ec3163b06357316cae), [`146b6b5`](https://github.com/aomex/aomex/commit/146b6b5f09d4b9f557c1771686f3585bebf4cfc9), [`56f65c8`](https://github.com/aomex/aomex/commit/56f65c8509b40679272ed58f63e12d98e1818503), [`c6df4de`](https://github.com/aomex/aomex/commit/c6df4de61fc28f452f8a202c9a485e1a061b3eb6), [`79e19c1`](https://github.com/aomex/aomex/commit/79e19c1ae2a35b38580a4d2a27b6dde634345126), [`9adefaa`](https://github.com/aomex/aomex/commit/9adefaa8baaaafb7be610c7a98920c9830d10a3c), [`a8bb601`](https://github.com/aomex/aomex/commit/a8bb601acaf1e1d580511ae886a4ae99bf19f699), [`902597b`](https://github.com/aomex/aomex/commit/902597b2f98fda7e63f7419a5ef62ab2acb7c863), [`e8dee7c`](https://github.com/aomex/aomex/commit/e8dee7c3f064587cd2afb9cb95ab3341f63ccd27)]:
10
+ - @aomex/web@0.0.6
11
+ - @aomex/core@0.0.5
12
+
3
13
  ## 0.0.4
4
14
 
5
15
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -10,7 +10,7 @@ interface PrettyJsonOptions {
10
10
  */
11
11
  param?: string;
12
12
  /**
13
- * JSON每行锁进空格,默认:`2`
13
+ * JSON每行缩进空格,默认:`2`
14
14
  */
15
15
  spaces?: number;
16
16
  }
package/dist/index.js CHANGED
@@ -6,22 +6,18 @@ var prettyJson = (options = {}) => {
6
6
  const { pretty = true, spaces = 2, param } = options;
7
7
  return middleware.web(async (ctx, next) => {
8
8
  await next();
9
- const { body } = ctx.response;
10
- if (body === null)
9
+ const {
10
+ request,
11
+ response,
12
+ response: { body }
13
+ } = ctx;
14
+ const prettify = pretty || param && Object.hasOwn(request.query, param);
15
+ if (!prettify)
11
16
  return;
12
- const isStream = body instanceof Readable && body.readableObjectMode;
13
- const isJson = typeof body === "object";
14
- if (!isJson && !isStream)
15
- return;
16
- const prettify = pretty || param && ctx.request.query.hasOwnProperty(param);
17
- if (isStream) {
18
- ctx.response.contentType = "json";
19
- const stringify = StreamStringify();
20
- if (prettify) {
21
- stringify.space = spaces;
22
- }
23
- ctx.send(body.pipe(stringify));
24
- } else if (prettify) {
17
+ if (body instanceof Readable && body.readableObjectMode) {
18
+ response.contentType = "json";
19
+ ctx.send(body.pipe(StreamStringify({ space: spaces })));
20
+ } else if (response.isJSON(body)) {
25
21
  ctx.send(JSON.stringify(body, null, spaces));
26
22
  }
27
23
  });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { middleware } from '@aomex/core';\nimport type { WebMiddleware } from '@aomex/web';\nimport { Readable } from 'node:stream';\nimport StreamStringify from 'streaming-json-stringify';\n\nexport interface PrettyJsonOptions {\n /**\n * 是否格式化JSON数据,默认:`true`\n */\n pretty?: boolean;\n\n /**\n * 在查询字符串(querystring)中使用指定参数来触发JSON格式化\n */\n param?: string;\n\n /**\n * JSON每行锁进空格,默认:`2`\n */\n spaces?: number;\n}\n\nexport const prettyJson = (options: PrettyJsonOptions = {}): WebMiddleware => {\n const { pretty = true, spaces = 2, param } = options;\n\n return middleware.web(async (ctx, next) => {\n await next();\n const { body } = ctx.response;\n\n if (body === null) return;\n\n const isStream = body instanceof Readable && body.readableObjectMode;\n const isJson = typeof body === 'object';\n\n if (!isJson && !isStream) return;\n\n const prettify =\n pretty || (param && ctx.request.query.hasOwnProperty(param));\n\n if (isStream) {\n ctx.response.contentType = 'json';\n const stringify = StreamStringify();\n if (prettify) {\n stringify.space = spaces;\n }\n ctx.send(body.pipe(stringify));\n } else if (prettify) {\n ctx.send(JSON.stringify(body, null, spaces));\n }\n });\n};\n"],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,SAAS,gBAAgB;AACzB,OAAO,qBAAqB;AAmBrB,IAAM,aAAa,CAAC,UAA6B,CAAC,MAAqB;AAC5E,QAAM,EAAE,SAAS,MAAM,SAAS,GAAG,MAAM,IAAI;AAE7C,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,KAAK;AACX,UAAM,EAAE,KAAK,IAAI,IAAI;AAErB,QAAI,SAAS;AAAM;AAEnB,UAAM,WAAW,gBAAgB,YAAY,KAAK;AAClD,UAAM,SAAS,OAAO,SAAS;AAE/B,QAAI,CAAC,UAAU,CAAC;AAAU;AAE1B,UAAM,WACJ,UAAW,SAAS,IAAI,QAAQ,MAAM,eAAe,KAAK;AAE5D,QAAI,UAAU;AACZ,UAAI,SAAS,cAAc;AAC3B,YAAM,YAAY,gBAAgB;AAClC,UAAI,UAAU;AACZ,kBAAU,QAAQ;AAAA,MACpB;AACA,UAAI,KAAK,KAAK,KAAK,SAAS,CAAC;AAAA,IAC/B,WAAW,UAAU;AACnB,UAAI,KAAK,KAAK,UAAU,MAAM,MAAM,MAAM,CAAC;AAAA,IAC7C;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { middleware } from '@aomex/core';\nimport type { WebMiddleware } from '@aomex/web';\nimport { Readable } from 'node:stream';\nimport StreamStringify from 'streaming-json-stringify';\n\nexport interface PrettyJsonOptions {\n /**\n * 是否格式化JSON数据,默认:`true`\n */\n pretty?: boolean;\n\n /**\n * 在查询字符串(querystring)中使用指定参数来触发JSON格式化\n */\n param?: string;\n\n /**\n * JSON每行缩进空格,默认:`2`\n */\n spaces?: number;\n}\n\nexport const prettyJson = (options: PrettyJsonOptions = {}): WebMiddleware => {\n const { pretty = true, spaces = 2, param } = options;\n\n return middleware.web(async (ctx, next) => {\n await next();\n const {\n request,\n response,\n response: { body },\n } = ctx;\n const prettify = pretty || (param && Object.hasOwn(request.query, param));\n\n if (!prettify) return;\n\n if (body instanceof Readable && body.readableObjectMode) {\n response.contentType = 'json';\n ctx.send(body.pipe(StreamStringify({ space: spaces })));\n } else if (response.isJSON(body)) {\n ctx.send(JSON.stringify(body, null, spaces));\n }\n });\n};\n"],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,SAAS,gBAAgB;AACzB,OAAO,qBAAqB;AAmBrB,IAAM,aAAa,CAAC,UAA6B,CAAC,MAAqB;AAC5E,QAAM,EAAE,SAAS,MAAM,SAAS,GAAG,MAAM,IAAI;AAE7C,SAAO,WAAW,IAAI,OAAO,KAAK,SAAS;AACzC,UAAM,KAAK;AACX,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA,UAAU,EAAE,KAAK;AAAA,IACnB,IAAI;AACJ,UAAM,WAAW,UAAW,SAAS,OAAO,OAAO,QAAQ,OAAO,KAAK;AAEvE,QAAI,CAAC;AAAU;AAEf,QAAI,gBAAgB,YAAY,KAAK,oBAAoB;AACvD,eAAS,cAAc;AACvB,UAAI,KAAK,KAAK,KAAK,gBAAgB,EAAE,OAAO,OAAO,CAAC,CAAC,CAAC;AAAA,IACxD,WAAW,SAAS,OAAO,IAAI,GAAG;AAChC,UAAI,KAAK,KAAK,UAAU,MAAM,MAAM,MAAM,CAAC;AAAA,IAC7C;AAAA,EACF,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aomex/pretty-json",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "格式化json数据中间件",
5
5
  "keywords": [
6
6
  "pretty",
@@ -30,15 +30,15 @@
30
30
  "access": "public"
31
31
  },
32
32
  "peerDependencies": {
33
- "@aomex/core": "^0.0.4",
34
- "@aomex/web": "^0.0.5"
33
+ "@aomex/core": "^0.0.5",
34
+ "@aomex/web": "^0.0.6"
35
35
  },
36
36
  "dependencies": {
37
37
  "streaming-json-stringify": "^3.1.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@aomex/core": "^0.0.4",
41
- "@aomex/web": "^0.0.5",
40
+ "@aomex/core": "^0.0.5",
41
+ "@aomex/web": "^0.0.6",
42
42
  "@types/streaming-json-stringify": "^3.1.1"
43
43
  },
44
44
  "scripts": {}