Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk): read the request method before the JS timer is armed
Yudhi Armyndharis46c763f31236
diff --git a/sdk/javascript/src/http.ts b/sdk/javascript/src/http.tsindex 306e75530..4a7eafb75 100644--- a/sdk/javascript/src/http.ts+++ b/sdk/javascript/src/http.ts@@ -184,6 +184,11 @@ async function send<T>(throw new TypeError(`OpenWA: dot path segment in ${JSON.stringify(options.path)}`);}}+ // fetch upper-cases GET, POST, PUT and DELETE itself but sends PATCH and any other verb as written, and the+ // gateway answers 400 to a lowercase method; a caller without the HttpMethod type can pass one. A missing+ // method stays undefined, which fetch sends as GET. Read before the timer is armed: a value that is not a+ // string throws here, and nothing would clear the timer.+ const method = options.method?.toUpperCase();const timeoutMs = toTimeoutMs(options.timeoutMs ?? config.timeoutMs);const controller = new AbortController();@@ -208,11 +213,6 @@ async function send<T>(headers['Content-Type'] = 'application/json';headers['X-API-Key'] = config.apiKey;- // fetch upper-cases GET, POST, PUT and DELETE itself but sends PATCH and any other verb as written, and the- // gateway answers 400 to a lowercase method; a caller without the HttpMethod type can pass one. A missing- // method stays undefined, which fetch sends as GET.- const method = options.method?.toUpperCase();-// Called without a receiver: a platform fetch passed in as `fetch: globalThis.fetch` throws// "Illegal invocation" in browsers and Workers when invoked as a method of `config`.const doFetch = config.fetch;diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.tsindex d552e88ed..26264c565 100644--- a/sdk/javascript/test/client.test.ts+++ b/sdk/javascript/test/client.test.ts@@ -666,6 +666,21 @@ describe('OpenWAClient', () => {expect(sent).toEqual(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'PATCH', 'PATCH', 'PATCH']);});+ it('refuses a method that is not a string without leaving the request timer running', async () => {+ // A timer left pending keeps a short script's process alive until the whole timeout has passed.+ vi.useFakeTimers();+ try {+ const fetchSpy = vi.fn<FetchLike>(async () => new Response('[]', { status: 200 }));+ const c = new OpenWAClient({ baseUrl: 'http://localhost', apiKey: 'k', timeoutMs: 5000, fetch: fetchSpy });+ await expect(c.request({ method: 5 as never, path: '/api/sessions' })).rejects.toBeInstanceOf(TypeError);+ await expect(c.requestBytes({ method: {} as never, path: '/api/sessions' })).rejects.toBeInstanceOf(TypeError);+ expect(vi.getTimerCount()).toBe(0);+ expect(fetchSpy).not.toHaveBeenCalled();+ } finally {+ vi.useRealTimers();+ }+ });+it('calls the global fetch unbound from the client config when none is injected', async () => {// Browsers and Workers reject a fetch invoked as a method of another object ("Illegal invocation").vi.stubGlobal('fetch', function (this: unknown) {