-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitry Dutikov
committed
Feb 13, 2022
1 parent
7093ecb
commit cfbb93d
Showing
11 changed files
with
2,281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages | ||
|
||
name: Node.js Package | ||
|
||
on: | ||
workflow_dispatch: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: 16 | ||
- run: npm ci | ||
- run: npm test | ||
|
||
publish-npm: | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: 16 | ||
registry-url: https://registry.npmjs.org/ | ||
- run: npm ci | ||
- run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.npm_token}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
test | ||
.github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# JSON22-Axios | ||
Axios interceptor providing support to [JSON22](https://github.com/dancecoder/json22#readme) data format in your applications. | ||
|
||
## Features | ||
* Ready to use [Axios](https://axios-http.com/) interceptor | ||
* Parse [JSON22](https://github.com/dancecoder/json22#readme) body content | ||
* Serialize data to JSON22 | ||
* Support for global interceptor as well as request level transformation | ||
* Both CJS/ESM modules support | ||
|
||
## Installation | ||
```shell | ||
npm install json22-axios | ||
``` | ||
|
||
Add interceptor at your client setup | ||
```javascript | ||
import axios from 'axios'; | ||
import { Json22RequestInterceptor } from 'json22-axios'; | ||
|
||
axios.interceptors.request.use(Json22RequestInterceptor()); | ||
``` | ||
|
||
For old-fashioned javascript | ||
|
||
```javascript | ||
const axios = require('axios'); | ||
const { Json22RequestInterceptor } = require('json22-axios'); | ||
|
||
axios.interceptors.request.use(Json22RequestInterceptor()); | ||
``` | ||
|
||
## Options | ||
|
||
Both stringify and parse methods of JSON22 accepts options. You may be interested to define such options at global level as well as with isolated client instance. | ||
|
||
`Json22RequestInterceptor` accepts the next options structure | ||
|
||
```typescript | ||
interface Json22AxiosOptions { | ||
json22ParseOptions?: Json22ParseOptions; | ||
json22StringifyOptions?: Json22StringifyOptions; | ||
} | ||
``` | ||
See also `Json22ParseOptions` and `Json22StringifyOptions` at [JSON22 API description](https://github.com/dancecoder/json22#api) | ||
|
||
### Define global level options | ||
```javascript | ||
import axios from 'axios'; | ||
import { Json22RequestInterceptor } from 'json22-axios'; | ||
import { TypedModel } from './models/typed-model.js'; | ||
|
||
axios.interceptors.request.use(Json22RequestInterceptor({ | ||
json22ParseOptions: { context: { TypedModel } }, | ||
})); | ||
``` | ||
|
||
### Define isolated client options | ||
```javascript | ||
import axios from 'axios'; | ||
import { Json22RequestInterceptor } from 'json22-axios'; | ||
import { TypedModel } from './models/typed-model.js'; | ||
|
||
const client = axios.create(); | ||
client.interceptors.request.use(Json22RequestInterceptor({ | ||
json22ParseOptions: { context: { TypedModel } }, | ||
})); | ||
``` | ||
|
||
## Request level data transformation | ||
In same rare cases you might be interested to set up data transformation for a specific query. | ||
This case you shall not use the interceptor. Instead, you'll have to use data transformers functions. | ||
Data transformers do not accept options, so you'll need to define it on query configuration at `json22Options`. | ||
```javascript | ||
import axios from 'axios'; | ||
import { transformJson22StringToData, transformDataToJson22String } from 'json22-axios'; | ||
import { TypedModel } from './models/typed-model.js'; | ||
|
||
export async function postData(data) { | ||
const resp = await axios.request({ | ||
method: 'POST', | ||
baseURL: 'https://example.com', | ||
url: '/api/data', | ||
transformResponse: transformJson22StringToData, | ||
transformRequest: transformDataToJson22String, | ||
json22Options: { json22ParseOptions: { context: { TypedModel } } }, | ||
data | ||
}); | ||
return resp.data; | ||
} | ||
``` | ||
__Note: `json22Options` configuration field is not defined by axios.__ | ||
That is the reason we do not recommend to use json22 data transformers directly. | ||
Please, use interceptor instead. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2022 Dmitry Dutikov | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
const { JSON22 } = require('json22'); | ||
const utils = require('axios/lib/utils'); | ||
|
||
const PARSE_OPTIONS = Symbol('Json22ParseOptions'); | ||
const STRINGIFY_OPTIONS = Symbol('Json22StringifyOptions'); | ||
|
||
/** | ||
* @param {any} data | ||
* @param {AxiosRequestHeaders} headers | ||
* */ | ||
function transformDataToJson22String(data, headers) { | ||
const unsupported = [ | ||
utils.isFormData, | ||
utils.isArrayBuffer, | ||
utils.isBuffer, | ||
utils.isStream, | ||
utils.isFile, | ||
utils.isBlob, | ||
utils.isArrayBufferView, | ||
utils.isURLSearchParams, | ||
]; | ||
|
||
if (!unsupported.some(fn => fn(data))) { | ||
const contentType = headers['Content-Type'] ?? headers['content-type']; | ||
if (contentType !== 'multipart/form-data' && contentType !== 'application/json') { | ||
const isObjectPayload = utils.isObject(data); | ||
if (isObjectPayload) { | ||
/** @type {AxiosRequestConfig} */ | ||
const config = this; | ||
headers['Content-Type'] = JSON22.mimeType; | ||
// TODO: combine both stringify options objects | ||
return JSON22.stringify(data, config[STRINGIFY_OPTIONS] ?? config.json22Options?.json22StringifyOptions); | ||
} | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
/** | ||
* @param {any} data | ||
* @param {AxiosResponseHeaders} [headers] | ||
* */ | ||
function transformJson22StringToData(data, headers) { | ||
/** @type {AxiosRequestConfig} */ | ||
const config = this; | ||
|
||
if ((headers?.['content-type'] ?? headers?.['Content-Type']) === JSON22.mimeType) { | ||
// TODO: combine both parse options objects | ||
return JSON22.parse(data, config[PARSE_OPTIONS] ?? config.json22Options?.json22ParseOptions); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/** | ||
* @param {Json22AxiosOptions} [options={}] | ||
* */ | ||
function Json22RequestInterceptor(options = {}) { | ||
/** | ||
* @param {AxiosRequestConfig} config | ||
* */ | ||
function json22RqIntercept(config) { | ||
config[PARSE_OPTIONS] = options.json22ParseOptions; | ||
config[STRINGIFY_OPTIONS] = options.json22StringifyOptions; | ||
config.transformRequest.unshift(transformDataToJson22String); | ||
config.transformResponse.unshift(transformJson22StringToData); | ||
return config; | ||
} | ||
return json22RqIntercept; | ||
} | ||
|
||
module.exports = { transformDataToJson22String, transformJson22StringToData, Json22RequestInterceptor }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2022 Dmitry Dutikov | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
import { AxiosRequestConfig, AxiosRequestHeaders, AxiosResponseHeaders } from 'axios'; | ||
import { Json22ParseOptions, Json22StringifyOptions } from 'json22'; | ||
|
||
export interface Json22AxiosOptions { | ||
json22ParseOptions?: Json22ParseOptions; | ||
json22StringifyOptions?: Json22StringifyOptions; | ||
} | ||
|
||
export function transformDataToJson22String(data: any, headers: AxiosRequestHeaders): any; | ||
|
||
export function transformJson22StringToData(data: any, headers: AxiosResponseHeaders): any; | ||
|
||
export function Json22RequestInterceptor(options?: Json22AxiosOptions): (config: AxiosRequestConfig) => AxiosRequestConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { JSON22 } from 'json22'; | ||
import utils from 'axios/lib/utils.js'; | ||
|
||
const PARSE_OPTIONS = Symbol('Json22ParseOptions'); | ||
const STRINGIFY_OPTIONS = Symbol('Json22StringifyOptions'); | ||
|
||
/** | ||
* @param {any} data | ||
* @param {AxiosRequestHeaders} headers | ||
* */ | ||
export function transformDataToJson22String(data, headers) { | ||
const unsupported = [ | ||
utils.isFormData, | ||
utils.isArrayBuffer, | ||
utils.isBuffer, | ||
utils.isStream, | ||
utils.isFile, | ||
utils.isBlob, | ||
utils.isArrayBufferView, | ||
utils.isURLSearchParams, | ||
]; | ||
|
||
if (!unsupported.some(fn => fn(data))) { | ||
const contentType = headers['Content-Type'] ?? headers['content-type']; | ||
if (contentType !== 'multipart/form-data' && contentType !== 'application/json') { | ||
const isObjectPayload = utils.isObject(data); | ||
if (isObjectPayload) { | ||
/** @type {AxiosRequestConfig} */ | ||
const config = this; | ||
headers['Content-Type'] = JSON22.mimeType; | ||
// TODO: combine both stringify options objects | ||
return JSON22.stringify(data, config[STRINGIFY_OPTIONS] ?? config.json22Options?.json22StringifyOptions); | ||
} | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
/** | ||
* @param {any} data | ||
* @param {AxiosResponseHeaders} [headers] | ||
* */ | ||
export function transformJson22StringToData(data, headers) { | ||
/** @type {AxiosRequestConfig} */ | ||
const config = this; | ||
|
||
if ((headers?.['content-type'] ?? headers?.['Content-Type']) === JSON22.mimeType) { | ||
// TODO: combine both parse options objects | ||
return JSON22.parse(data, config[PARSE_OPTIONS] ?? config.json22Options?.json22ParseOptions); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/** | ||
* @param {Json22AxiosOptions} [options={}] | ||
* */ | ||
export function Json22RequestInterceptor(options = {}) { | ||
/** | ||
* @param {AxiosRequestConfig} config | ||
* */ | ||
function json22RqIntercept(config) { | ||
config[PARSE_OPTIONS] = options.json22ParseOptions; | ||
config[STRINGIFY_OPTIONS] = options.json22StringifyOptions; | ||
config.transformRequest.unshift(transformDataToJson22String); | ||
config.transformResponse.unshift(transformJson22StringToData); | ||
return config; | ||
} | ||
return json22RqIntercept; | ||
} |
Oops, something went wrong.