Skip to content

Commit

Permalink
refactor: use a Set instead of Array for agents list
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Aug 10, 2021
1 parent 240900e commit 28e0f13
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const test: CustomHttpAgent = {
};

// Note: push it to the package agents list
agents.push(test);
agents.add(test);
```

The **agent** property is an Undici Agent.
2 changes: 1 addition & 1 deletion examples/agent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const yoda = {
preprod: "",
dev: "https://yoda.myunisoft.fr:1407"
};
httpie.agents.push(yoda);
httpie.agents.add(yoda);

const { data } = await httpie.get("/yoda/api/v1/ipa/healthz");
console.log(data);
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"homepage": "https://github.com/MyUnisoft/httpie#readme",
"devDependencies": {
"@myunisoft/eslint-config": "^1.3.2",
"@slimio/is": "^1.5.1",
"@types/content-type": "^1.1.5",
"@types/jest": "^26.0.24",
"@types/lru-cache": "^5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface CustomHttpAgent {
limit?: InlineCallbackAction;
}

export const agents: CustomHttpAgent[] = [];
export const agents: Set<CustomHttpAgent> = new Set();

/**
* @description Detect if a given string URI is matching a given Agent custom path.
Expand Down
6 changes: 5 additions & 1 deletion test/agents.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Import Third-party Dependencies
import is from "@slimio/is";

// Import Internal Dependencies
import { windev } from "./helpers";
import * as Agents from "../src/agents";
Expand All @@ -7,7 +10,8 @@ const kWindevMonitoringURL = "https://ws-dev.myunisoft.fr/ws_monitoring";

describe("agents", () => {
it("should be an Array of CustomHttpAgent and must remain extensible", () => {
expect(Array.isArray(Agents.agents)).toStrictEqual(true);
expect(is.set(Agents.agents)).toStrictEqual(true);

expect(Object.isExtensible(Agents.agents)).toStrictEqual(true);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ const windev: CustomHttpAgent = {
preprod: "https://ws-dev.myunisoft.fr",
dev: "https://ws-dev.myunisoft.fr"
};
agents.push(windev);
agents.add(windev);

export { windev };
2 changes: 1 addition & 1 deletion test/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function createServer(customPath = "local", port = 3000) {
preprod: `http://localhost:${port}/`,
dev: `http://localhost:${port}/`
};
agents.push(serverAgent);
agents.add(serverAgent);

server.get("/", async() => {
return {
Expand Down

0 comments on commit 28e0f13

Please sign in to comment.