-
Notifications
You must be signed in to change notification settings - Fork 1
Feat: Create / Add Axios Large Response Interceptor #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e75ba57
chore: commit base structure for axios interceptor
alexmarqs c35fce4
feat: wip: enhance axios interceptor for large response handling
alexmarqs 9d98a51
refactor: improve axios large response interceptor configuration
alexmarqs 68f4bd4
test: add initial test suite for axios large response interceptor
alexmarqs 8a9ffa6
test: configure Vitest to run silently by default again
alexmarqs 2119429
test: expand test coverage for axios large response interceptor
alexmarqs dcdb73c
test: update axios interceptor test assertions for header modification
alexmarqs edaa0a0
test: enhance axios interceptor test coverage with advanced scenarios
alexmarqs 49e241b
docs: add comprehensive README for axios large response interceptor
alexmarqs a6d5a7b
feat: add per-request configuration support for axios large response …
alexmarqs f67c070
chore: update package name and version for axios large response inter…
alexmarqs f85d705
feat: add error payload handling for large response interceptor + bum…
alexmarqs 19bb476
chore: update package keywords and import statement in README
alexmarqs 4b391a6
docs: improve README with clearer description and add debug logging o…
alexmarqs c15eca6
docs: add axios large response interceptor reference to README
alexmarqs 29c2168
chore: improve README.md
alexmarqs 1feecec
docs: update Axios Large Response link text in README
alexmarqs 0383366
feat: update axios large response interceptor to be disabled by default
alexmarqs 4801ccd
Update packages/axios-large-response/README.md
alexmarqs 201f5e2
chore: bump package version to 0.0.2
alexmarqs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,99 @@ | ||
| # Axios Large Response | ||
|
|
||
| An Axios interceptor designed to handle large responses. By default, it assumes that your backend uses `@epilot/large-response-middleware`, as described in the [large-response-middleware README](https://github.com/epilot-dev/aws-lambda-utility-middlewares/blob/main/packages/large-response-middleware/README.md). However, it also supports custom callback function to fetch large payloads from a reference data, customizable reference property names, headers, and other options. See below for details. | ||
|
|
||
| It supports per-request options, so you can enable/disable the interceptor for a specific request (the axios config namespace is `axios-large-response` - please check the [Usage](#usage) section for more details). For now, it is enabled by default, however we can do some combinations based on the use cases, for example, we can disable it globally and enable it per-request if needed. | ||
|
|
||
| The interceptor is **disabled by default**, so you need to explicitly enable it. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pnpm add @epilot/axios-large-response | ||
| npm install @epilot/axios-large-response | ||
| yarn add @epilot/axios-large-response | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { axiosLargeResponse } from '@epilot/axios-large-response'; | ||
| import axios from 'axios'; | ||
|
|
||
| // Axios instance | ||
| const axiosInstance = axios.create(); | ||
|
|
||
| // Example 1: disable interceptor globally so we enable it per-request | ||
| axiosLargeResponse(axiosInstance, { | ||
| // enabled: false, -> disabled by default | ||
| // ... other global options | ||
| }); | ||
| ... | ||
| const response = await axiosInstance.get('https://api.example.com/data', { | ||
| 'axios-large-response': { | ||
| enabled: true, | ||
| headerFlag: 'application/custom-large-response.vnd+json', | ||
| refProperty: '$customRef', | ||
| debug: true, | ||
| onFetchLargePayloadFromRef: async (refUrl) => { | ||
| // Custom handling for this specific request | ||
| const response = await axios.get(refUrl); | ||
| return response.data; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Example 2: enable interceptor globally so we disable it per-request | ||
| axiosLargeResponse(axiosInstance, { | ||
| enabled: true, | ||
| // ... other global options | ||
| }); | ||
| ... | ||
| const response = await axiosInstance.get('https://api.example.com/data', { | ||
| 'axios-large-response': { | ||
| enabled: false | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| | Name | Type | Default | Description | | ||
| |------|------|---------|-------------| | ||
| | enabled | Boolean | false | Enable/disable the interceptor | | ||
| | headerFlag | String | 'application/large-response.vnd+json' | Content type header indicating a large payload reference response | | ||
| | refProperty | String | '$payloadRef' | Property name containing the reference URL in the response | | ||
| | debug | Boolean | false | Enable debug logging | | ||
| | logger | Object | console | Logger object with debug() and error() methods | | ||
| | onFetchLargePayloadFromRef | Function | Fetches the reference URL and returns the full payload | Callback function to fetch the full payload from the reference URL | | ||
| | errorPayload | Unknown/Any | undefined | Error payload to return if the reference URL is not found or something goes wrong - this will be returned in the response data instead of throwing an error | | ||
| | disableWarnings | Boolean | false | Disable warnings, only available globally in the options | | ||
|
|
||
| For debug purposes, you can also set the `AXIOS_INTERCEPTOR_LARGE_RESPONSE_DEBUG` environment variable to `true` or `1` to enable debug logging. | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. Adds the appropriate Accept header to requests to indicate large payload support; | ||
| 2. Detects responses with the configured header content type; | ||
| 3. If the response contains a reference in the specified refProperty, automatically fetches the full payload; | ||
| 4. Returns the complete data in the response. | ||
|
|
||
| Example server response for a large payload: | ||
|
|
||
| ```json | ||
| { | ||
| "$payloadRef": "https://api.example.com/large-payloads/123" | ||
| } | ||
| ``` | ||
|
|
||
| After interceptor processing, the response becomes: | ||
|
|
||
| ```json | ||
| { | ||
| "huge": "data", | ||
| "nested": { | ||
| "complex": "structure" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
This file contains hidden or 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,45 @@ | ||
| { | ||
| "name": "@epilot/axios-large-response", | ||
| "version": "0.0.1", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "module": "dist/index.mjs", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/epilot-dev/aws-lambda-utility-middlewares.git#main", | ||
| "directory": "packages/axios-large-response" | ||
| }, | ||
| "description": "Axios plugin to intercept large responses", | ||
| "keywords": ["axios-large-response", "large-response-middleware", "interceptor", "axios", "large-response", "plugin"], | ||
| "contributors": ["Alexandre Marques(https://github.com/alexmarqs)"], | ||
| "scripts": { | ||
| "build": "tsup", | ||
| "test": "vitest", | ||
| "lint": "biome check --write .", | ||
| "prepublishOnly": "pnpm lint && pnpm build", | ||
| "typecheck": "tsc --noEmit", | ||
| "local:publish": "yalc publish" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.mjs", | ||
| "module": "./dist/index.mjs", | ||
| "require": "./dist/index.js" | ||
| } | ||
| }, | ||
| "files": ["dist/**", "README.md"], | ||
| "devDependencies": { | ||
| "typescript": "^5.3.3", | ||
| "vitest": "3.0.5", | ||
| "axios": "^1.6.2", | ||
| "tsup": "6.7.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "axios": ">=0.25.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">=14" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| export { axiosLargeResponse } from './interceptor/axios-interceptor'; | ||
| export * from './types'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.