From d7f88e9ca449fac02644ba9dd91eb83e333eebec Mon Sep 17 00:00:00 2001 From: heilmela Date: Wed, 30 Aug 2023 17:07:52 +0200 Subject: [PATCH 1/2] feat: add custom endpoint queries and headers --- src/Client.ts | 18 ++++++++++++++++-- test/client_test.ts | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index 098af45..7d73fef 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -26,6 +26,8 @@ export interface EndpointSettings { secure: boolean, port?: number, pathname?: string, + query?: URLSearchParams + headers?: Record } export class Client { @@ -39,10 +41,13 @@ export class Client { const url = new URL(settings); const secure = (url.protocol === "https:" || url.protocol === "wss:"); const port = Number(url.port || (secure ? 443 : 80)); + const query = new URLSearchParams(url.searchParams); + url.search = ''; this.settings = { hostname: url.hostname, pathname: url.pathname !== "/" ? url.pathname : "", + ...(query.size > 0 ? { query }: {}), port, secure }; @@ -57,6 +62,7 @@ export class Client { if (settings.pathname === undefined) { settings.pathname = ""; } + this.settings = settings; } } @@ -96,7 +102,8 @@ export class Client { return ( await get(this.getHttpEndpoint(`${roomName}`), { headers: { - 'Accept': 'application/json' + 'Accept': 'application/json', + ...(this.settings.headers ? this.settings.headers : {}) } }) ).data; @@ -168,7 +175,8 @@ export class Client { await post(this.getHttpEndpoint(`${method}/${roomName}`), { headers: { 'Accept': 'application/json', - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + ...(this.settings.headers ? this.settings.headers : {}) }, body: JSON.stringify(options) }) @@ -200,6 +208,12 @@ export class Client { params.push(`${name}=${options[name]}`); } + if(this.settings.query) { + for (const [name, value] of [...this.settings.query.entries()]) { + params.push(`${name}=${value}`); + } + } + let endpoint = (this.settings.secure) ? "wss://" : "ws://" diff --git a/test/client_test.ts b/test/client_test.ts index a1dc7f5..4400295 100644 --- a/test/client_test.ts +++ b/test/client_test.ts @@ -40,6 +40,18 @@ describe("Client", function () { wsEndpoint: "wss://localhost/custom/path/processId/roomId?", wsEndpointPublicAddress: "wss://node-1.colyseus.cloud/processId/roomId?" }, + 'https://localhost/with/header': { + settings: { hostname: "localhost", port: 443, secure: true, pathname: "/with/header", headers: { "Authorization": "Bearer ey21xx"} }, + httpEndpoint: "https://localhost/with/header", + wsEndpoint: "wss://localhost/with/header/processId/roomId?", + wsEndpointPublicAddress: "wss://node-1.colyseus.cloud/processId/roomId?" + }, + 'https://localhost/custom/path?query=value': { + settings: { hostname: "localhost", port: 443, secure: true, pathname: "/custom/path", query: new URLSearchParams({"query": "value"}) }, + httpEndpoint: "https://localhost/custom/path", + wsEndpoint: "wss://localhost/custom/path/processId/roomId?query=value", + wsEndpointPublicAddress: "wss://node-1.colyseus.cloud/processId/roomId?query=value" + }, }; for (const url in settingsByUrl) { @@ -49,6 +61,7 @@ describe("Client", function () { assert.strictEqual(expected.settings.hostname, settings.hostname); assert.strictEqual(expected.settings.port, settings.port); assert.strictEqual(expected.settings.secure, settings.secure); + assert.strictEqual(expected.settings.query?.toString(), settings.query?.toString()); assert.strictEqual(expected.httpEndpoint + "/matchmake/", client['getHttpEndpoint']()); assert.strictEqual(expected.wsEndpoint, client['buildEndpoint'](room)); assert.strictEqual(expected.wsEndpointPublicAddress, client['buildEndpoint'](roomWithPublicAddress)); From 6838bde0f3f1f29a78e050a3661bd915a79050cd Mon Sep 17 00:00:00 2001 From: heilmela Date: Wed, 30 Aug 2023 17:35:23 +0200 Subject: [PATCH 2/2] refactor: test --- test/client_test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/client_test.ts b/test/client_test.ts index 4400295..4f135d6 100644 --- a/test/client_test.ts +++ b/test/client_test.ts @@ -11,7 +11,7 @@ describe("Client", function () { }) describe("constructor settings", () => { - it("url string", () => { + const room = { roomId: "roomId", processId: "processId", sessionId: "sessionId", }; const roomWithPublicAddress = { publicAddress: "node-1.colyseus.cloud", roomId: "roomId", processId: "processId", sessionId: "sessionId", }; @@ -55,6 +55,7 @@ describe("Client", function () { }; for (const url in settingsByUrl) { + it(`testing settings by url ${url}`, () => { const expected = settingsByUrl[url] const client = new Client(url); const settings = client['settings']; @@ -73,8 +74,9 @@ describe("Client", function () { assert.strictEqual(expected.httpEndpoint + "/matchmake/", clientWithSettings['getHttpEndpoint']()); assert.strictEqual(expected.wsEndpoint, clientWithSettings['buildEndpoint'](room)); assert.strictEqual(expected.wsEndpointPublicAddress, clientWithSettings['buildEndpoint'](roomWithPublicAddress)); + }); } - }); + }); xit("join", function () {