|
| 1 | +# Axios Large Response |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +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 disabled 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. |
| 6 | + |
| 7 | +The interceptor is **disabled by default**, so you need to explicitly enable it. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +pnpm add @epilot/axios-large-response |
| 13 | +npm install @epilot/axios-large-response |
| 14 | +yarn add @epilot/axios-large-response |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```ts |
| 20 | +import { axiosLargeResponse } from '@epilot/axios-large-response'; |
| 21 | +import axios from 'axios'; |
| 22 | + |
| 23 | +// Axios instance |
| 24 | +const axiosInstance = axios.create(); |
| 25 | + |
| 26 | +// Example 1: disable interceptor globally so we enable it per-request |
| 27 | +axiosLargeResponse(axiosInstance, { |
| 28 | + // enabled: false, -> disabled by default |
| 29 | + // ... other global options |
| 30 | +}); |
| 31 | +... |
| 32 | +const response = await axiosInstance.get('https://api.example.com/data', { |
| 33 | + 'axios-large-response': { |
| 34 | + enabled: true, |
| 35 | + headerFlag: 'application/custom-large-response.vnd+json', |
| 36 | + refProperty: '$customRef', |
| 37 | + debug: true, |
| 38 | + onFetchLargePayloadFromRef: async (refUrl) => { |
| 39 | + // Custom handling for this specific request |
| 40 | + const response = await axios.get(refUrl); |
| 41 | + return response.data; |
| 42 | + } |
| 43 | + } |
| 44 | +}); |
| 45 | + |
| 46 | +// Example 2: enable interceptor globally so we disable it per-request |
| 47 | +axiosLargeResponse(axiosInstance, { |
| 48 | + enabled: true, |
| 49 | + // ... other global options |
| 50 | +}); |
| 51 | +... |
| 52 | +const response = await axiosInstance.get('https://api.example.com/data', { |
| 53 | + 'axios-large-response': { |
| 54 | + enabled: false |
| 55 | + } |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +## Options |
| 60 | + |
| 61 | +| Name | Type | Default | Description | |
| 62 | +|------|------|---------|-------------| |
| 63 | +| enabled | Boolean | false | Enable/disable the interceptor | |
| 64 | +| headerFlag | String | 'application/large-response.vnd+json' | Content type header indicating a large payload reference response | |
| 65 | +| refProperty | String | '$payloadRef' | Property name containing the reference URL in the response | |
| 66 | +| debug | Boolean | false | Enable debug logging | |
| 67 | +| logger | Object | console | Logger object with debug() and error() methods | |
| 68 | +| onFetchLargePayloadFromRef | Function | Fetches the reference URL and returns the full payload | Callback function to fetch the full payload from the reference URL | |
| 69 | +| 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 | |
| 70 | +| disableWarnings | Boolean | false | Disable warnings, only available globally in the options | |
| 71 | + |
| 72 | +For debug purposes, you can also set the `AXIOS_INTERCEPTOR_LARGE_RESPONSE_DEBUG` environment variable to `true` or `1` to enable debug logging. |
| 73 | + |
| 74 | +## How it works |
| 75 | + |
| 76 | +1. Adds the appropriate Accept header to requests to indicate large payload support; |
| 77 | +2. Detects responses with the configured header content type; |
| 78 | +3. If the response contains a reference in the specified refProperty, automatically fetches the full payload; |
| 79 | +4. Returns the complete data in the response. |
| 80 | + |
| 81 | +Example server response for a large payload: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "$payloadRef": "https://api.example.com/large-payloads/123" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +After interceptor processing, the response becomes: |
| 90 | + |
| 91 | +```json |
| 92 | +{ |
| 93 | + "huge": "data", |
| 94 | + "nested": { |
| 95 | + "complex": "structure" |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
0 commit comments