Skip to content

Commit

Permalink
feat: add axios transformRequest and transformResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Aug 29, 2023
1 parent 570a628 commit 74cc0fa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/axios.cjs
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions lib/axios.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function transformRequest(
data: any,
headers: Record<string, string>,
): any
export function transformResponse(data: any): any
12 changes: 12 additions & 0 deletions lib/axios.js
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
},
Expand Down

0 comments on commit 74cc0fa

Please sign in to comment.