Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## 2.4.0

### Fixed

- **Illegal invocation on first API call when bundled.** `fetch` is now invoked
with `globalThis` as its receiver (`fetch.call(globalThis, …)`), preventing the
`TypeError: Illegal invocation` thrown by minified production builds on
Authenticate/login.
- **`signal` serialized into `ExecuteMultiCall`.** The batched-call payload no
longer copies queue-entry internals (`signal`, `resolve`, `reject`) into the
JSON-RPC params; only `method` and `params` are sent per call.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "./esm/index.js",
"author": "Fairfleet Gmbh",
"license": "MIT",
"version": "2.3.0",
"version": "2.4.0",
"keywords": [
"geotab",
"api",
Expand Down
15 changes: 15 additions & 0 deletions src/internal/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ test("Should throw on bad HTTP status", async () => {

await expect(call({ method: "Test" })).rejects.toThrow("HTTP status 500");
});

test("Should invoke fetch bound to globalThis (guards against Illegal invocation when bundled)", async () => {
let receivedThis: unknown = "unset";
vi.mocked(fetch).mockImplementation(function (this: unknown) {
receivedThis = this;
return Promise.resolve({
ok: true,
text: async () => JSON.stringify({ result: "ok" }),
}) as never;
});

await call({ method: "Test" });

expect(receivedThis).toBe(globalThis);
});
2 changes: 1 addition & 1 deletion src/internal/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getCall(options: GeotabOptions) {
const parseJSON = options.parseJSON ?? parseJsonWithDates;

return async function call({ method, params, signal }: Call) {
const httpResponse = await fetch(url, {
const httpResponse = await fetch.call(globalThis, url, {
body: JSON.stringify({
id: nanoid(),
method,
Expand Down
21 changes: 21 additions & 0 deletions src/internal/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ test("First request should define timeout", async () => {
await expect(call2).resolves.toBe("test2");
});

test("Should serialize only method+params into ExecuteMultiCall (no signal/resolve/reject)", async () => {
getResult.mockResolvedValueOnce(["a", "b"]);
const controller = new AbortController();

const c1 = callQueued({ method: "Test", params: { x: 1 }, signal: controller.signal });
const c2 = callQueued({ method: "Test", params: { y: 2 } });

vi.advanceTimersToNextTimer();
await Promise.all([c1, c2]);

expect(getResult).toHaveBeenCalledWith({
method: "ExecuteMultiCall",
params: {
calls: [
{ method: "Test", params: { x: 1 } },
{ method: "Test", params: { y: 2 } },
],
},
});
});

function noop() {
// ignore
}
5 changes: 4 additions & 1 deletion src/internal/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export function queue(options: GeotabOptions) {
return [await next(calls[0])];
}

return await next({ method: "ExecuteMultiCall", params: { calls } });
return await next({
method: "ExecuteMultiCall",
params: { calls: calls.map(({ method, params }) => ({ method, params })) },
});
}

return async function middleware(call: Call) {
Expand Down
Loading