diff --git a/examples/agent.mjs b/examples/agent.mjs index e62a9b8..e7b7812 100644 --- a/examples/agent.mjs +++ b/examples/agent.mjs @@ -1,10 +1,9 @@ import * as httpie from "../dist/index.js"; -// import * as httpie from "@myunisoft/httpie"; const yoda = { customPath: "yoda", domains: new Set([ - "yoda.myunisoft.fr" + "app.dev.myunisoft.tech" ]), agent: new httpie.Agent({ connections: 500 @@ -13,9 +12,9 @@ const yoda = { // Work by reading the NODE_ENV var. prod: "", preprod: "", - dev: "https://yoda.myunisoft.fr:1407" + dev: "https://app.dev.myunisoft.tech" }; httpie.agents.add(yoda); -const { data } = await httpie.get("/yoda/api/v1/ipa/healthz"); +const { data } = await httpie.get("/api/v1/ipa/healthz"); console.log(data); diff --git a/package.json b/package.json index 5f1fe5b..bcdca8c 100644 --- a/package.json +++ b/package.json @@ -1,53 +1,53 @@ -{ - "name": "@myunisoft/httpie", - "version": "4.0.0", - "description": "MyUnisoft Node.js HTTP client that use Undici client", - "main": "dist/index.js", - "types": "dist/index.d.ts", - "scripts": { - "build": "tsc", - "prepublishOnly": "npm run build", - "test": "jest" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/MyUnisoft/httpie.git" - }, - "engines": { - "node": ">=18" - }, - "keywords": [], - "files": [ - "dist" - ], - "publishConfig": { - "@myunisoft:registry": "https://registry.npmjs.org/" - }, - "author": "GENTILHOMME Thomas ", - "license": "MIT", - "bugs": { - "url": "https://github.com/MyUnisoft/httpie/issues" - }, - "homepage": "https://github.com/MyUnisoft/httpie#readme", - "devDependencies": { - "@nodesecure/eslint-config": "^1.9.0", - "@types/content-type": "^1.1.8", - "@types/jest": "^29.5.11", - "@types/lru-cache": "^7.10.10", - "@types/node": "^20.10.4", - "@types/statuses": "^2.0.4", - "dotenv": "^16.3.1", - "fastify": "^4.24.3", - "jest": "^29.7.0", - "p-ratelimit": "^1.0.1", - "ts-jest": "^29.1.1", - "typescript": "^5.3.3" - }, - "dependencies": { - "@openally/result": "^1.2.0", - "content-type": "^1.0.5", - "lru-cache": "^10.1.0", - "statuses": "^2.0.1", - "undici": "^6.0.1" - } -} +{ + "name": "@myunisoft/httpie", + "version": "4.0.0", + "description": "MyUnisoft Node.js HTTP client that use Undici client", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc", + "prepublishOnly": "npm run build", + "test": "jest" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MyUnisoft/httpie.git" + }, + "engines": { + "node": ">=18" + }, + "keywords": [], + "files": [ + "dist" + ], + "publishConfig": { + "@myunisoft:registry": "https://registry.npmjs.org/" + }, + "author": "GENTILHOMME Thomas ", + "license": "MIT", + "bugs": { + "url": "https://github.com/MyUnisoft/httpie/issues" + }, + "homepage": "https://github.com/MyUnisoft/httpie#readme", + "devDependencies": { + "@nodesecure/eslint-config": "^1.9.0", + "@types/content-type": "^1.1.8", + "@types/jest": "^29.5.11", + "@types/lru-cache": "^7.10.10", + "@types/node": "^20.10.4", + "@types/statuses": "^2.0.4", + "dotenv": "^16.3.1", + "fastify": "^4.24.3", + "jest": "^29.7.0", + "p-ratelimit": "^1.0.1", + "ts-jest": "^29.1.1", + "typescript": "^5.3.3" + }, + "dependencies": { + "@openally/result": "^1.2.0", + "content-type": "^1.0.5", + "lru-cache": "^10.1.0", + "statuses": "^2.0.1", + "undici": "^7.2.0" + } +} diff --git a/src/agents.ts b/src/agents.ts index 2b7284f..c7a73e1 100644 --- a/src/agents.ts +++ b/src/agents.ts @@ -43,7 +43,7 @@ export const agents: Set = new Set(); * * @example * const URI = computeAgentPath("/windev/ws_monitoring", windev); - * assert.strictEqual(URI, "https://ws-dev.myunisoft.fr/ws_monitoring"); + * assert.strictEqual(URI, "https://ws.dev.myunisoft.tech/ws_monitoring"); */ export function isAgentPathMatchingURI(uri: string, agent: CustomHttpAgent): URL | null { // Note: we want to match both '/path/xxx...' and 'path/xxx...' @@ -78,7 +78,7 @@ export function computeURIOnAllAgents(uri: string): computedUrlAndAgent { * @see https://nodejs.org/api/url.html#url_url_hostname * * @example - * detectAgentFromURI("https://ws-dev.myunisoft.fr/ws_monitoring"); // windev agent + * detectAgentFromURI("https://ws.dev.myunisoft.tech/ws_monitoring"); // windev agent * detectAgentFromURI("https://www.google.fr/"); // null */ export function detectAgentFromURI(uri: URL): CustomHttpAgent | null { diff --git a/src/class/undiciResponseHandler.ts b/src/class/undiciResponseHandler.ts index da5b7e8..0915c8a 100644 --- a/src/class/undiciResponseHandler.ts +++ b/src/class/undiciResponseHandler.ts @@ -70,7 +70,7 @@ export class HttpieResponseHandler { encodingHeader.reverse() : encodingHeader.split(",").reverse(); - let decompressedBuffer = Buffer.from(buffer); + let decompressedBuffer: Buffer = Buffer.from(buffer); for (const rawEncoding of encodings) { const encoding = rawEncoding.trim() as TypeOfDecompression; const strategy = kDecompress[encoding]; diff --git a/src/index.ts b/src/index.ts index 6d44c88..1e4b0f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,6 @@ import { Headers, HeadersInit, FormData, - File, BodyInit, BodyMixin, MockAgent, @@ -36,7 +35,6 @@ export { Headers, HeadersInit, FormData, - File, BodyInit, BodyMixin, MockAgent, diff --git a/src/request.ts b/src/request.ts index 986b14d..bb51c16 100644 --- a/src/request.ts +++ b/src/request.ts @@ -56,7 +56,7 @@ export interface RequestResponse { * @see https://github.com/nodejs/undici * * @example - * const { statusCode, data } = await request("GET", "https://ws-dev.myunisoft.fr/ws_monitoring"); + * const { statusCode, data } = await request("GET", "https://ws.dev.myunisoft.tech/ws_monitoring"); * console.log(statusCode, data); // 200 "true" */ export async function request( diff --git a/test/__snapshots__/request.spec.ts.snap b/test/__snapshots__/request.spec.ts.snap index 15436eb..c906e83 100644 --- a/test/__snapshots__/request.spec.ts.snap +++ b/test/__snapshots__/request.spec.ts.snap @@ -7,8 +7,6 @@ exports[`http.get should throw a 404 Not Found error because the path is not kno

Not Found

The requested URL was not found on this server.

-
-
Apache/2.4.56 (Debian) Server at ws-dev.myunisoft.fr Port 443
" `; @@ -20,8 +18,6 @@ exports[`http.safeGet should throw a 404 Not Found error because the path is not

Not Found

The requested URL was not found on this server.

-
-
Apache/2.4.56 (Debian) Server at ws-dev.myunisoft.fr Port 443
" `; diff --git a/test/agents.spec.ts b/test/agents.spec.ts index 514a78c..b7ce8e3 100644 --- a/test/agents.spec.ts +++ b/test/agents.spec.ts @@ -3,7 +3,7 @@ import { windev } from "./helpers"; import * as Agents from "../src/agents"; // CONSTANTS -const kWindevMonitoringURL = "https://ws-dev.myunisoft.fr/ws_monitoring"; +const kWindevMonitoringURL = "https://ws.dev.myunisoft.tech/ws_monitoring"; describe("agents", () => { it("should be an Array of CustomHttpAgent and must remain extensible", () => { @@ -50,7 +50,7 @@ describe("computeURIOnAllAgents", () => { describe("detectAgentFromURI", () => { it("should detect windev agent with URI hostname", () => { - const returnedAgent = Agents.detectAgentFromURI(new URL("https://ws-dev.myunisoft.fr")); + const returnedAgent = Agents.detectAgentFromURI(new URL("https://ws.dev.myunisoft.tech")); expect(returnedAgent).toStrictEqual(windev); }); diff --git a/test/helpers.ts b/test/helpers.ts index 656304c..f840f33 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -7,14 +7,14 @@ import { CustomHttpAgent, agents } from "../src/agents"; const windev: CustomHttpAgent = { customPath: "windev", domains: new Set([ - "ws-dev.myunisoft.fr" + "ws.dev.myunisoft.tech" ]), agent: new undici.Agent({ connections: 500 }), - prod: "https://ws-dev.myunisoft.fr", - preprod: "https://ws-dev.myunisoft.fr", - dev: "https://ws-dev.myunisoft.fr" + prod: "https://ws.dev.myunisoft.tech", + preprod: "https://ws.sta.myunisoft.tech", + dev: "https://ws.dev.myunisoft.tech" }; agents.add(windev); diff --git a/test/request.spec.ts b/test/request.spec.ts index 2d06a84..980f56a 100644 --- a/test/request.spec.ts +++ b/test/request.spec.ts @@ -73,7 +73,7 @@ describe("http.get", () => { }); it("should GET json response from node.js health endpoint", async() => { - const { data } = await get("https://yoda.myunisoft.fr:1407/api/v1/ipa/healthz"); + const { data } = await get("https://app.dev.myunisoft.tech/api/v1/ipa/healthz"); expect(Object.keys(data).sort()).toMatchObject([ "status", "version", "description", "checks" @@ -182,6 +182,7 @@ describe("http.safeGet", () => { if (result.err) { const error = result.val; + console.log(error); expect(error.name).toStrictEqual("HttpieOnHttpError"); expect(error.statusCode).toStrictEqual(404); diff --git a/test/safeRequest.spec.ts b/test/safeRequest.spec.ts index 027bec2..9aeac31 100644 --- a/test/safeRequest.spec.ts +++ b/test/safeRequest.spec.ts @@ -48,8 +48,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -99,8 +98,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -150,8 +148,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -201,8 +198,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -508,8 +504,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -539,8 +534,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -739,8 +733,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -770,8 +763,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -970,8 +962,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -1001,8 +992,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -1201,8 +1191,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -1232,8 +1221,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -1369,8 +1357,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1380,7 +1367,7 @@ describe("Httpie.safeRequest", () => { }); it("should throw when 'content-encoding' header is invalid", async() => { - expect.assertions(10); + expect.assertions(9); const target = { method: "GET", @@ -1398,9 +1385,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." @@ -1535,8 +1520,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1546,7 +1530,7 @@ describe("Httpie.safeRequest", () => { }); it("should throw when 'content-encoding' header is invalid", async() => { - expect.assertions(10); + expect.assertions(9); const target = { method: "POST", @@ -1564,9 +1548,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." @@ -1701,8 +1683,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1712,7 +1693,7 @@ describe("Httpie.safeRequest", () => { }); it("should throw when 'content-encoding' header is invalid", async() => { - expect.assertions(10); + expect.assertions(9); const target = { method: "PUT", @@ -1730,9 +1711,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." @@ -1867,8 +1846,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1878,7 +1856,7 @@ describe("Httpie.safeRequest", () => { }); it("should throw when 'content-encoding' header is invalid", async() => { - expect.assertions(10); + expect.assertions(9); const target = { method: "DELETE", @@ -1896,9 +1874,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." diff --git a/test/stream.spec.ts b/test/stream.spec.ts index d1e093f..d9fbd51 100644 --- a/test/stream.spec.ts +++ b/test/stream.spec.ts @@ -28,8 +28,8 @@ afterAll(async() => { describe("stream", () => { it("should use callback dispatcher to init headers/statusCode etc.", async() => { - const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz"); - const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL); + const fileDestination = path.join(kDownloadPath, "flags-main.tar.gz"); + const repositoryURL = new URL("NodeSecure/flags/archive/main.tar.gz", kGithubURL); const cursor = httpie.stream("GET", repositoryURL, { headers: { @@ -54,8 +54,8 @@ describe("stream", () => { }); it("should fetch a .tar.gz of a given github repository", async() => { - const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz"); - const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL); + const fileDestination = path.join(kDownloadPath, "flags-main.tar.gz"); + const repositoryURL = new URL("NodeSecure/flags/archive/main.tar.gz", kGithubURL); await httpie.stream("GET", repositoryURL, { headers: {