Skip to content

Commit

Permalink
add fix to account for limitations of qs.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
tintinthong committed Nov 4, 2024
1 parent a3ea344 commit 148551a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
31 changes: 31 additions & 0 deletions packages/runtime-common/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,34 @@ function assertRangeFilter(
});
});
}

const removeBrackets = (obj: any): any => {
if (!obj || typeof obj !== 'object') return obj;

// Handle arrays
if (Array.isArray(obj)) {
return obj.map((item) => removeBrackets(item));
}

return Object.entries(obj).reduce((acc, [key, value]) => {
// Remove surrounding brackets if they exist
const newKey = key.replace(/^\[(.*)\]$/, '$1');

// Handle arrays in values
if (Array.isArray(value)) {
acc[newKey] = value.map((item) => removeBrackets(item));
} else if (typeof value === 'object' && value !== null) {
// Recursively removeBrackets nested objects
acc[newKey] = removeBrackets(value);
} else {
// Handle primitive values
acc[newKey] = value;
}

return acc;
}, {} as any);
};

export const parseQuery = (queryString: string) => {
return removeBrackets(qs.parse(queryString));
};
8 changes: 4 additions & 4 deletions packages/runtime-common/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
SupportedMimeType,
lookupRouteTable,
} from './router';
import { assertQuery } from './query';
import { assertQuery, parseQuery } from './query';
import type { Readable } from 'stream';
import { type CardDef } from 'https://cardstack.com/base/card-api';
import type * as CardAPI from 'https://cardstack.com/base/card-api';
Expand All @@ -75,7 +75,6 @@ import { fetcher } from './fetcher';
import { RealmIndexQueryEngine } from './realm-index-query-engine';
import { RealmIndexUpdater } from './realm-index-updater';

import qs from 'qs';
import {
MatrixBackendAuthentication,
Utils,
Expand Down Expand Up @@ -1531,7 +1530,7 @@ export class Realm {
request.headers.get('X-Boxel-Building-Index'),
);

let cardsQuery = qs.parse(new URL(request.url).search.slice(1));
let cardsQuery = parseQuery(new URL(request.url).search.slice(1));
assertQuery(cardsQuery);

let doc = await this.#realmIndexQueryEngine.search(cardsQuery, {
Expand All @@ -1555,7 +1554,8 @@ export class Realm {
request.headers.get('X-Boxel-Building-Index'),
);

let parsedQueryString = qs.parse(new URL(request.url).search.slice(1));
let href = new URL(request.url).search.slice(1);
let parsedQueryString = parseQuery(href);
let htmlFormat = parsedQueryString.prerenderedHtmlFormat as string;
let cardUrls = parsedQueryString.cardUrls as string[];

Expand Down

0 comments on commit 148551a

Please sign in to comment.