Skip to content
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

maxEntries support #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ which will be passed into `AxiosHarTracker` constructor:
```js
const axiosTracker = new AxiosHarTracker(axios);
```
The constructor takes an optional second object for creator configuration and an optional third argument ```maxEntries```, which will prevent
the log from growing beyond that size.

In order to perform an actual request use the axios.get/post/delete... call, examples:

Expand Down
13 changes: 12 additions & 1 deletion src/axios-har-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ export class AxiosHarTracker {
private generatedHar: HarFile;
private newEntry: NewEntry;
private creatorConfig: AxiosHarTrackerCreatorConfig;
private maxEntries: number;

constructor(
axiosModule: AxiosInstance,
creatorConfig: AxiosHarTrackerCreatorConfig = {
name: "axios-har-tracker",
version: "0.1.0",
}
},
maxEntries?:number
) {
this.axios = axiosModule;
this.maxEntries = maxEntries;
this.creatorConfig = creatorConfig;
this.resetHar();

Expand All @@ -64,6 +67,7 @@ export class AxiosHarTracker {
if (error.request) {
this.newEntry.request = this.returnRequestObject(error.request);
this.generatedHar.log.entries.push(this.newEntry);
this.pruneLog();
}
return Promise.reject(error);
}
Expand Down Expand Up @@ -163,6 +167,13 @@ export class AxiosHarTracker {
private pushNewEntryResponse(response) {
this.newEntry.response = this.returnResponseObject(response);
this.generatedHar.log.entries.push(this.newEntry);
this.pruneLog();
}

private pruneLog() {
if (this.maxEntries !== undefined && this.generatedHar.log.entries?.length > this.maxEntries) {
this.generatedHar.log.entries.shift();
}
}

private generateNewEntry() {
Expand Down
17 changes: 16 additions & 1 deletion src/tests/multiple-calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ describe("axios-har-tracker e2e tests", () => {
await fse.writeJson("./harfiles/example-500.har", generatedHar);
});

it("Should collect multiple calls", async () => {
it("Should collect multiple calls up to a max of 4", async () => {

axiosTracker = new AxiosHarTracker(axios, undefined, 4);

//This one will get pruned
await axios.get("http://httpstat.us/200", {
params: {
test: 1,
},
});

await axios.get("http://httpstat.us/200");
try {
await axios.get("http://httpstat.us/302");
Expand All @@ -125,8 +135,13 @@ describe("axios-har-tracker e2e tests", () => {
"An expected error appears after call to http://httpstat.us/500"
);
}

const generatedHar = axiosTracker.getGeneratedHar();
const array = generatedHar.log.entries;

//5 requests were made but maxEntries is 4
expect(array.length).toEqual(4);

expect(array[0].request).toMatchObject({
method: "get",
url: "http://httpstat.us/200",
Expand Down