From 74cc0fab26f83044251cb68a3a0cbd0b827a75a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Tue, 29 Aug 2023 16:27:21 +0800 Subject: [PATCH] feat: add axios transformRequest and transformResponse --- lib/axios.cjs | 13 +++++++++++++ lib/axios.d.ts | 5 +++++ lib/axios.js | 12 ++++++++++++ package.json | 5 +++++ tsconfig.json | 2 ++ 5 files changed, 37 insertions(+) create mode 100644 lib/axios.cjs create mode 100644 lib/axios.d.ts create mode 100644 lib/axios.js diff --git a/lib/axios.cjs b/lib/axios.cjs new file mode 100644 index 0000000..1691fc6 --- /dev/null +++ b/lib/axios.cjs @@ -0,0 +1,13 @@ +const { parse, stringify } = require('./jsonb.cjs') + +module.exports.transformRequest = function transformRequest(data, headers) { + if (data.constructor === Object) { + headers['Content-Type'] = 'application/json;charset=utf-8' + return stringify(data) + } + return data; +} + +module.exports.transformResponse = function transformResponse(data) { + return typeof data === 'string' ? parse(data) : data +} diff --git a/lib/axios.d.ts b/lib/axios.d.ts new file mode 100644 index 0000000..63fd905 --- /dev/null +++ b/lib/axios.d.ts @@ -0,0 +1,5 @@ +export function transformRequest( + data: any, + headers: Record, +): any +export function transformResponse(data: any): any diff --git a/lib/axios.js b/lib/axios.js new file mode 100644 index 0000000..c0e6727 --- /dev/null +++ b/lib/axios.js @@ -0,0 +1,12 @@ +import { parse, stringify } from './jsonb.js' +export function transformRequest(data, headers) { + if (data.constructor === Object) { + headers['Content-Type'] = 'application/json;charset=utf-8' + return stringify(data) + } + return data +} + +export function transformResponse(data) { + return typeof data === 'string' ? parse(data) : data +} diff --git a/package.json b/package.json index a559aea..986cc0b 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,11 @@ "import": "./lib/jsonb.js", "require": "./lib/jsonb.cjs", "types": "./lib/jsonb.d.ts" + }, + "./axios": { + "import": "./lib/axios.js", + "require": "./lib/axios.cjs", + "types": "./lib/axios.d.ts" } }, "files": [ diff --git a/tsconfig.json b/tsconfig.json index 04bc9d8..10fb8b5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,14 @@ { "extends": "@tsconfig/esm/tsconfig.json", "compilerOptions": { + "outDir": "./dist", "lib": ["DOM", "ES2022"], "target": "es2022", "allowJs": true, "checkJs": true, "noEmit": true, "declaration": true, + "module": "node16", "moduleResolution": "nodenext", "allowSyntheticDefaultImports": true },