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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to the Fleetbase JavaScript SDK are documented here. This project follows semantic versioning.

## 2.1.1

### Fixed

- Query lists and nested values are sent in bracket form (`ids[]=1&ids[]=2`, `filter[active]=true`) so PHP and Laravel read every value; v2.0 and v2.1.0 repeated the key, which the server reads as the last value only.

## 2.1.0

### Added
Expand Down
10 changes: 4 additions & 6 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
> v2.1.0 ~ "Driver workflows, realtime socket tokens and a working Node transport"
> v2.1.1 ~ "Query strings Fleetbase APIs can read"

## Highlights

- Fleet-Ops driver workflow stores: `manifests` (with `optimize` and `drivers.manifests`), `manifestStops`, `trailers` (CRUD, `attach`, `detach`, `connections`, `track`, `vehicles.trailers`), `fuelReports`, `issues` and `workOrders` (with `send`), plus the driver password actions `changePassword`, `forgotPassword` and `resetPassword`.
- `fleetbase.socket.token()` mints short-lived realtime socket tokens (`POST socket/token`) for server-side code that holds a secret key or user token.
- `NodeAdapter` requests work: GET requests reach the right path, and headers set after construction with `setHeaders` are sent. Node consumers of v2.0.0, including the Storefront SDK under Node, should upgrade.
- Query lists and nested values are sent in bracket form (`ids[]=1&ids[]=2`, `filter[active]=true`, `stops[0][id]=s1`), as SDK v1 sent them on native. v2.0 and v2.1.0 repeated the key (`ids=1&ids=2`), which PHP and Laravel read as only the last value, so multi-value filters such as store tags or several cart origins reached the API as one value.

## Compatibility

All additions are additive; no export, store or method changes. Node 20.19.4 remains a legacy runtime target; Node 22/24 are recommended.
A patch release with no API changes. Nested query objects were JSON-encoded in v2.0 and v2.1.0 and are now bracketed.

This is the release branch's metadata, not evidence that v2.1.0 has been published. Merging this release PR into `main` tags and publishes it.
This is the release branch's metadata, not evidence that v2.1.1 has been published. Merging this release PR into `main` tags and publishes it.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/sdk",
"version": "2.1.0",
"version": "2.1.1",
"description": "Official JavaScript and TypeScript SDK for the Fleetbase API",
"type": "module",
"main": "./dist/index.cjs",
Expand Down
48 changes: 29 additions & 19 deletions src/adapters/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,16 @@ export function buildUrl(config: AdapterConfig, path: string, override?: string)
return [host, namespace, cleanPath].filter(Boolean).join('/');
}

/**
* Append query values the way PHP and Laravel read them, as SDK v1 did on native
* (axios): a list is `key[]=a&key[]=b`, a nested object `key[field]=value`, a list of
* objects `key[0][field]=value`. Repeating a plain key (`key=a&key=b`) would leave the
* server with only the last value.
*/
export function appendQuery(url: string, query: ResourceAttributes = {}): string {
const entries = Object.entries(query);
if (entries.length === 0) {
return url;
}

const params = new URLSearchParams();
for (const [key, rawValue] of entries) {
const values = Array.isArray(rawValue) ? rawValue : [rawValue];
for (const value of values) {
if (value === null || value === undefined) {
continue;
}
if (value instanceof Date) {
params.append(key, value.toISOString());
} else if (typeof value === 'object') {
params.append(key, JSON.stringify(value));
} else {
params.append(key, String(value));
}
}
for (const [key, value] of Object.entries(query)) {
appendValue(params, key, value);
}

const serialized = params.toString();
Expand All @@ -50,6 +39,27 @@ export function appendQuery(url: string, query: ResourceAttributes = {}): string
return `${url}${url.includes('?') ? '&' : '?'}${serialized}`;
}

function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date);
}

function appendValue(params: URLSearchParams, key: string, value: unknown): void {
if (value === null || value === undefined) {
return;
}
if (value instanceof Date) {
params.append(key, value.toISOString());
} else if (Array.isArray(value)) {
value.forEach((item, index) => appendValue(params, isPlainObject(item) || Array.isArray(item) ? `${key}[${index}]` : `${key}[]`, item));
} else if (isPlainObject(value)) {
for (const [field, nested] of Object.entries(value)) {
appendValue(params, `${key}[${field}]`, nested);
}
} else {
params.append(key, String(value));
}
}

function errorMessage(payload: unknown, fallback: string): string {
if (payload && typeof payload === 'object') {
const body = payload as Record<string, unknown>;
Expand Down
2 changes: 1 addition & 1 deletion tests/consumer-compatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('first-party consumer contracts', () => {
await adapter.delete('orders/2', {}, { cache: 'no-store' });
expect(adapter.calls).toEqual([
...['POST', 'PUT', 'PATCH'].map((method) => ['messages', method, { body: JSON.stringify(payload) }, {}]),
['orders?active=true&page=2&ids=1&ids=2', 'GET', {}, {}],
['orders?active=true&page=2&ids%5B%5D=1&ids%5B%5D=2', 'GET', {}, {}],
['orders?page=3', 'GET', {}, { url: 'https://other.test/orders?active=true&page=3' }],
['orders/1', 'DELETE', {}, { cache: 'reload' }],
['orders/2', 'DELETE', {}, { cache: 'no-store' }],
Expand Down
16 changes: 12 additions & 4 deletions tests/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,18 @@ describe('fetch adapters', () => {
it('constructs URLs and serializes query values', () => {
expect(buildUrl({ host: 'https://api.test/', namespace: '/v1/' }, '/orders')).toBe('https://api.test/v1/orders');
expect(buildUrl({}, 'orders', 'https://override.test')).toBe('https://override.test');
const url = appendQuery('https://api.test/orders?first=1', { ids: [1, 2], at: new Date('2026-01-01T00:00:00.000Z'), filter: { active: true }, skip: null });
expect(url).toContain('ids=1');
expect(url).toContain('ids=2');
expect(url).toContain('filter=%7B%22active%22%3Atrue%7D');
const url = appendQuery('https://api.test/orders?first=1', {
ids: [1, 2, null],
at: new Date('2026-01-01T00:00:00.000Z'),
filter: { active: true, tags: ['a', 'b'], since: new Date('2026-01-02T00:00:00.000Z'), none: undefined },
stops: [{ id: 's1' }, ['x']],
empty: [],
skip: null,
});
// PHP and Laravel read lists and nested values only in bracket form.
expect(decodeURIComponent(url)).toBe(
'https://api.test/orders?first=1&ids[]=1&ids[]=2&at=2026-01-01T00:00:00.000Z&filter[active]=true&filter[tags][]=a&filter[tags][]=b&filter[since]=2026-01-02T00:00:00.000Z&stops[0][id]=s1&stops[1][]=x'
);
expect(appendQuery('x', {})).toBe('x');
expect(appendQuery('x', { skip: undefined })).toBe('x');
});
Expand Down
Loading