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 },