Skip to content

Commit 78b87b7

Browse files
authored
Merge pull request #5 from alexmarqs/main
Feat: Create / Add Axios Large Response Interceptor
2 parents 2e6fe58 + 201f5e2 commit 78b87b7

File tree

16 files changed

+1792
-5
lines changed

16 files changed

+1792
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ yarn-error.log*
2424
.DS_Store
2525
*.pem
2626
test-report.xml
27-
**/lib/
27+
**/large-response-middleware/lib/
28+
**/dist/
2829
stats.html
2930
*.tgz

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"editor.defaultFormatter": "biomejs.biome",
33
"editor.codeActionsOnSave": {
4-
"source.organizeImports": "always"
4+
"source.organizeImports": "always",
5+
"source.fixAll": "always"
56
},
67
"[json]": {
78
"editor.defaultFormatter": "biomejs.biome"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ This repository is designed to host a collection of useful AWS Lambda Utilities
99

1010
## Middlewares
1111

12-
- [Large Response Middleware](./packages/large-response-middleware/): AWS Lambda has a known limitation regarding the payload size of responses, which is currently set at 6MB. This middleware allows a service to log and save large responses to an S3 bucket, enabling developers to investigate the causes of such large responses. Furthermore, this middleware accepts a special header that allows the rewriting of the response with a $ref pointing to the large payload stored in S3, enabling clients to recover gracefully.
12+
- [Large Response Middleware](./packages/large-response-middleware/): AWS Lambda has a known limitation regarding the payload size of responses, which is currently set at 6MB. This middleware allows a service to log and save large responses to an S3 bucket, enabling developers to investigate the causes of such large responses. Furthermore, this middleware accepts a special header that allows the rewriting of the response with a $ref pointing to the large payload stored in S3, enabling clients to recover gracefully.
13+
14+
- If you are using `axios` in the client, feel free to try our interceptor [Axios Large Response](./packages/axios-large-response/) allowing you to intercept large responses and easily fetch them from S3 with minimal effort.
1315

1416
- [Lambda Server-Timing Middleware (ext)](https://github.com/NishuGoel/lambda-server-timing/tree/main/src): Enables Lambdas to return responses with Server-Timing Header allowing to to pass request-specific timings from the backend to the browser. Allows a server to communicate performance metrics about the request-response cycle to the user agent. It also standardizes a JavaScript interface to enable applications to collect, process, and act on these metrics to optimize application delivery.
1517

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@epilot/axios-large-response",
3+
"version": "0.0.2",
4+
"main": "dist/index.js",
5+
"types": "dist/index.d.ts",
6+
"module": "dist/index.mjs",
7+
"license": "MIT",
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/epilot-dev/aws-lambda-utility-middlewares.git#main",
11+
"directory": "packages/axios-large-response"
12+
},
13+
"description": "Axios plugin to intercept large responses",
14+
"keywords": ["axios-large-response", "large-response-middleware", "interceptor", "axios", "large-response", "plugin"],
15+
"contributors": ["Alexandre Marques(https://github.com/alexmarqs)"],
16+
"scripts": {
17+
"build": "tsup",
18+
"test": "vitest",
19+
"lint": "biome check --write .",
20+
"prepublishOnly": "pnpm lint && pnpm build",
21+
"typecheck": "tsc --noEmit",
22+
"local:publish": "yalc publish"
23+
},
24+
"exports": {
25+
".": {
26+
"types": "./dist/index.d.ts",
27+
"import": "./dist/index.mjs",
28+
"module": "./dist/index.mjs",
29+
"require": "./dist/index.js"
30+
}
31+
},
32+
"files": ["dist/**", "README.md"],
33+
"devDependencies": {
34+
"typescript": "^5.3.3",
35+
"vitest": "3.0.5",
36+
"axios": "^1.6.2",
37+
"tsup": "6.7.0"
38+
},
39+
"peerDependencies": {
40+
"axios": ">=0.25.0"
41+
},
42+
"engines": {
43+
"node": ">=14"
44+
}
45+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { axiosLargeResponse } from './interceptor/axios-interceptor';
2+
export * from './types';

0 commit comments

Comments
 (0)