From 05987bd0af73aab86f1bc0ea06f83330df1d7560 Mon Sep 17 00:00:00 2001 From: Anthony Alaribe Date: Fri, 13 Oct 2023 15:50:15 +0200 Subject: [PATCH] add a client.close call to the tests --- axios.test.js | 2 ++ axios.test.ts | 4 ++++ index.js | 4 ++++ index.test.js | 8 ++++++++ index.test.ts | 16 ++++++++++++++++ index.ts | 5 +++++ package.json | 2 +- 7 files changed, 40 insertions(+), 1 deletion(-) diff --git a/axios.test.js b/axios.test.js index bb5de77..0705429 100644 --- a/axios.test.js +++ b/axios.test.js @@ -82,6 +82,7 @@ describe("Axios Interceptors", () => { expect(JSON.stringify(response.body)).toBe(JSON.stringify({ ping: "pong" })); expect(published).toBe(true); expect(pingCalled).toBe(true); + await client.close(); }); it("should intercept axios via responseError", async () => { let published = false; @@ -124,5 +125,6 @@ describe("Axios Interceptors", () => { expect(JSON.stringify(response.body)).toBe(JSON.stringify({ hello: "error" })); // expect(published).toBe(true) // expect(pingCalled).toBe(true) + await client.close(); }); }); diff --git a/axios.test.ts b/axios.test.ts index b1ebf0e..d587eac 100644 --- a/axios.test.ts +++ b/axios.test.ts @@ -62,6 +62,8 @@ describe("Axios Interceptors", () => { expect(JSON.stringify(response.body)).toBe(JSON.stringify({ ping: "pong" })); expect(published).toBe(true); expect(pingCalled).toBe(true); + + await client.close(); }); it("should intercept axios via responseError", async () => { @@ -113,5 +115,7 @@ describe("Axios Interceptors", () => { expect(JSON.stringify(response.body)).toBe(JSON.stringify({ hello: "error" })); // expect(published).toBe(true) // expect(pingCalled).toBe(true) + + await client.close(); }); }); diff --git a/index.js b/index.js index bb7b0b3..fdabeda 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,10 @@ class APIToolkit { } return new APIToolkit(pubsubClient, topic_id, project_id, config); } + async close() { + await __classPrivateFieldGet(this, _APIToolkit_topic, "f")?.flush(); + await __classPrivateFieldGet(this, _APIToolkit_pubsub, "f")?.close(); + } static async getClientMetadata(rootURL, apiKey) { const resp = await (0, node_fetch_1.default)(rootURL + "/api/client_metadata", { method: "GET", diff --git a/index.test.js b/index.test.js index 9303ecd..230f8a8 100644 --- a/index.test.js +++ b/index.test.js @@ -96,6 +96,7 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(response.body.status).toBe("success"); expect(published).toBe(true); + await client.close(); }); it("should get data", async () => { const app = (0, express_1.default)(); @@ -138,6 +139,7 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData)); expect(published).toBe(true); + await client.close(); }); it("should check sub routes", async () => { const app = (0, express_1.default)(); @@ -174,6 +176,7 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData)); expect(published).toBe(true); + await client.close(); }); it("should check sub sub sub routes", async () => { const app = (0, express_1.default)(); @@ -214,6 +217,7 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData)); expect(published).toBe(true); + await client.close(); }); it("should ignore path for endpoins with OPTION", async () => { const app = (0, express_1.default)(); @@ -250,6 +254,7 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("OPTIONS ignored"); expect(published).toBe(true); + await client.close(); }); }); describe("File Upload Endpoint", () => { @@ -311,6 +316,7 @@ describe("File Upload Endpoint", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("File upload successful."); expect(published).toBe(true); + await client.close(); }); it("should upload files (formidable)", async () => { const app = (0, express_1.default)(); @@ -362,6 +368,7 @@ describe("File Upload Endpoint", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("Uploaded successfully"); expect(published).toBe(true); + await client.close(); }); it("should upload files (busboy)", async () => { const app = (0, express_1.default)(); @@ -422,6 +429,7 @@ describe("File Upload Endpoint", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("Uploaded successfully"); expect(published).toBe(true); + await client.close(); }); }); describe("testing headers and jsonpath redaction", () => { diff --git a/index.test.ts b/index.test.ts index d6caee9..c311640 100644 --- a/index.test.ts +++ b/index.test.ts @@ -78,6 +78,8 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(response.body.status).toBe("success"); expect(published).toBe(true); + + await client.close() }); it("should get data", async () => { @@ -126,6 +128,8 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData)); expect(published).toBe(true); + + await client.close() }); it("should check sub routes", async () => { @@ -167,6 +171,8 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData)); expect(published).toBe(true); + + await client.close() }); it("should check sub sub sub routes", async () => { @@ -212,6 +218,8 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(JSON.stringify(response.body)).toBe(JSON.stringify(exampleRequestData)); expect(published).toBe(true); + + await client.close() }); it("should ignore path for endpoins with OPTION", async () => { @@ -251,6 +259,8 @@ describe("Express SDK API Tests", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("OPTIONS ignored"); expect(published).toBe(true); + + await client.close() }); }); @@ -325,6 +335,8 @@ describe("File Upload Endpoint", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("File upload successful."); expect(published).toBe(true); + + await client.close() }); it("should upload files (formidable)", async () => { @@ -379,6 +391,8 @@ describe("File Upload Endpoint", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("Uploaded successfully"); expect(published).toBe(true); + + await client.close() }); it("should upload files (busboy)", async () => { @@ -443,6 +457,8 @@ describe("File Upload Endpoint", () => { expect(response.status).toBe(200); expect(response.body.message).toBe("Uploaded successfully"); expect(published).toBe(true); + + await client.close() }); }); diff --git a/index.ts b/index.ts index 76ab92d..d395b17 100644 --- a/index.ts +++ b/index.ts @@ -90,6 +90,11 @@ export class APIToolkit { return new APIToolkit(pubsubClient, topic_id, project_id, config); } + public async close(){ + await this.#topic?.flush(); + await this.#pubsub?.close(); + } + static async getClientMetadata(rootURL: string, apiKey: string) { const resp = await fetch(rootURL + "/api/client_metadata", { method: "GET", diff --git a/package.json b/package.json index 5d738d5..bc1b8e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apitoolkit-express", - "version": "1.1.20", + "version": "1.1.21", "description": "", "main": "index.js", "scripts": {