@aomex/pretty-json 1.0.0 → 1.0.2

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,9 @@
1
1
  # @aomex/pretty-json
2
2
 
3
+ ## 1.0.2
4
+
5
+ ## 1.0.1
6
+
3
7
  ## 1.0.0
4
8
 
5
9
  ### Minor Changes
package/dist/index.js CHANGED
@@ -9,8 +9,7 @@ var prettyJson = (options = {}) => {
9
9
  const { request, response } = ctx;
10
10
  const { body } = response;
11
11
  const prettify = enable || query && Object.hasOwn(request.query, query);
12
- if (!prettify)
13
- return;
12
+ if (!prettify) return;
14
13
  if (body instanceof Readable && body.readableObjectMode) {
15
14
  response.contentType = "json";
16
15
  response.body = body.pipe(StreamStringify({ space: spaces }));
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 * 启用中间件。默认值:`true`\n */\n enable?: boolean;\n /**\n * 在查询字符串中强制启用中间件\n * - example.com?pretty\n */\n query?: string;\n /**\n * 每行缩进,默认值:`2`\n * ```typescript\n * JSON.stringify({}, null, spaces)\n * ```\n */\n spaces?: number;\n}\n\n/**\n * 美化输出的JSON数据\n */\nexport const prettyJson = (options: PrettyJsonOptions = {}): WebMiddleware => {\n const { enable = true, spaces = 2, query } = options;\n\n return middleware.web(async (ctx, next) => {\n await next();\n\n const { request, response } = ctx;\n const { body } = response;\n const prettify = enable || (query && Object.hasOwn(request.query, query));\n\n if (!prettify) return;\n\n if (body instanceof Readable && body.readableObjectMode) {\n response.contentType = 'json';\n response.body = body.pipe(StreamStringify({ space: spaces }));\n } else if (response.isJSON(body)) {\n response.contentType = 'json';\n response.body = JSON.stringify(body, null, spaces);\n }\n });\n};\n"],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,SAAS,gBAAgB;AACzB,OAAO,qBAAqB;AAwBrB,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;AAEX,UAAM,EAAE,SAAS,SAAS,IAAI;AAC9B,UAAM,EAAE,KAAK,IAAI;AACjB,UAAM,WAAW,UAAW,SAAS,OAAO,OAAO,QAAQ,OAAO,KAAK;AAEvE,QAAI,CAAC;AAAU;AAEf,QAAI,gBAAgB,YAAY,KAAK,oBAAoB;AACvD,eAAS,cAAc;AACvB,eAAS,OAAO,KAAK,KAAK,gBAAgB,EAAE,OAAO,OAAO,CAAC,CAAC;AAAA,IAC9D,WAAW,SAAS,OAAO,IAAI,GAAG;AAChC,eAAS,cAAc;AACvB,eAAS,OAAO,KAAK,UAAU,MAAM,MAAM,MAAM;AAAA,IACnD;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 * 启用中间件。默认值:`true`\n */\n enable?: boolean;\n /**\n * 在查询字符串中强制启用中间件\n * - example.com?pretty\n */\n query?: string;\n /**\n * 每行缩进,默认值:`2`\n * ```typescript\n * JSON.stringify({}, null, spaces)\n * ```\n */\n spaces?: number;\n}\n\n/**\n * 美化输出的JSON数据\n */\nexport const prettyJson = (options: PrettyJsonOptions = {}): WebMiddleware => {\n const { enable = true, spaces = 2, query } = options;\n\n return middleware.web(async (ctx, next) => {\n await next();\n\n const { request, response } = ctx;\n const { body } = response;\n const prettify = enable || (query && Object.hasOwn(request.query, query));\n\n if (!prettify) return;\n\n if (body instanceof Readable && body.readableObjectMode) {\n response.contentType = 'json';\n response.body = body.pipe(StreamStringify({ space: spaces }));\n } else if (response.isJSON(body)) {\n response.contentType = 'json';\n response.body = JSON.stringify(body, null, spaces);\n }\n });\n};\n"],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,SAAS,gBAAgB;AACzB,OAAO,qBAAqB;AAwBrB,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;AAEX,UAAM,EAAE,SAAS,SAAS,IAAI;AAC9B,UAAM,EAAE,KAAK,IAAI;AACjB,UAAM,WAAW,UAAW,SAAS,OAAO,OAAO,QAAQ,OAAO,KAAK;AAEvE,QAAI,CAAC,SAAU;AAEf,QAAI,gBAAgB,YAAY,KAAK,oBAAoB;AACvD,eAAS,cAAc;AACvB,eAAS,OAAO,KAAK,KAAK,gBAAgB,EAAE,OAAO,OAAO,CAAC,CAAC;AAAA,IAC9D,WAAW,SAAS,OAAO,IAAI,GAAG;AAChC,eAAS,cAAc;AACvB,eAAS,OAAO,KAAK,UAAU,MAAM,MAAM,MAAM;AAAA,IACnD;AAAA,EACF,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aomex/pretty-json",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "格式化JSON数据",
5
5
  "keywords": [
6
6
  "pretty",
@@ -38,8 +38,8 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/streaming-json-stringify": "^3.1.1",
41
- "@aomex/core": "^1.0.0",
42
- "@aomex/web": "^1.0.0"
41
+ "@aomex/core": "^1.0.2",
42
+ "@aomex/web": "^1.0.2"
43
43
  },
44
44
  "scripts": {}
45
45
  }