Skip to content
Open
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 packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @builder.io/sdk

## 4.0.4

### Patch Changes

- Backported fix from v6.1.1: Corrected the conversion of query-objects with $-mongo-operators which are passed to builder.get() with apiEndpoint is "content"

## 4.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@builder.io/sdk",
"version": "4.0.3",
"version": "4.0.4",
"unpkg": "./dist/index.browser.js",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
Expand Down
129 changes: 129 additions & 0 deletions packages/core/src/builder.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,133 @@ describe('flushGetContentQueue', () => {
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with query.id when id is passed in options.query', async () => {
const expectedModel = 'symbol';
const expectedFormat = 'email';
const expectedEntryId = '123';

const result = await builder['flushGetContentQueue'](true, [
{
model: expectedModel,
format: expectedFormat,
key: expectedModel,
omit: OMIT,
fields: 'data',
limit: 10,
entry: expectedEntryId,
query: {
id: expectedEntryId,
},
},
]);

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&limit=10&model=%22${expectedModel}%22&entry=%22123%22&enrich=true&query.id=%22${expectedEntryId}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with query as the same object as the one passed in options.query if query contains $ mongo-operator', async () => {
const expectedModel = 'symbol';
const expectedFormat = 'email';
const expectedEntryId = '123';

const result = await builder['flushGetContentQueue'](true, [
{
model: expectedModel,
format: expectedFormat,
key: expectedModel,
omit: OMIT,
fields: 'data',
limit: 10,
entry: expectedEntryId,
query: {
data: {
id: '123',
},
$or: [
{
data: {
sourceUrl: '/c/docs/develop',
},
},
{
data: {
sourceUrl: 'https://www.builder.io' + '/c/docs/develop',
},
},
],
},
},
]);

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/symbol?omit=data.blocks&apiKey=25608a566fbb654ea959c1b1729e370d&fields=data&format=email&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&limit=10&model=%22symbol%22&entry=%22123%22&enrich=true&query=%7B%22data%22%3A%7B%22id%22%3A%22123%22%7D%2C%22%24or%22%3A%5B%7B%22data%22%3A%7B%22sourceUrl%22%3A%22%2Fc%2Fdocs%2Fdevelop%22%7D%7D%2C%7B%22data%22%3A%7B%22sourceUrl%22%3A%22https%3A%2F%2Fwww.builder.io%2Fc%2Fdocs%2Fdevelop%22%7D%7D%5D%7D`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with query as the same object as the one passed in options.query if query contains nested $ mongo-operator', async () => {
const expectedModel = 'symbol';
const expectedFormat = 'email';
const expectedEntryId = '123';

const result = await builder['flushGetContentQueue'](true, [
{
model: expectedModel,
format: expectedFormat,
key: expectedModel,
omit: OMIT,
fields: 'data',
limit: 10,
entry: expectedEntryId,
query: {
data: {
sourceUrl: { $eq: '/c/docs/develop' },
},
},
},
]);

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/symbol?omit=data.blocks&apiKey=25608a566fbb654ea959c1b1729e370d&fields=data&format=email&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&limit=10&model=%22symbol%22&entry=%22123%22&enrich=true&query.data.sourceUrl=%7B%22%24eq%22%3A%22%2Fc%2Fdocs%2Fdevelop%22%7D`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with query as the flattened object as the one passed in options.query if query does not contain $ mongo-operator', async () => {
const expectedModel = 'symbol';
const expectedFormat = 'email';
const expectedEntryId = '123';

const result = await builder['flushGetContentQueue'](true, [
{
model: expectedModel,
format: expectedFormat,
key: expectedModel,
omit: OMIT,
fields: 'data',
limit: 10,
entry: expectedEntryId,
query: {
data: {
sourceUrl: '/c/docs/develop',
},
name: {
fullName: 'John Doe',
},
},
},
]);

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/symbol?omit=data.blocks&apiKey=25608a566fbb654ea959c1b1729e370d&fields=data&format=email&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&limit=10&model=%22symbol%22&entry=%22123%22&enrich=true&query.data.sourceUrl=%22%2Fc%2Fdocs%2Fdevelop%22&query.name.fullName=%22John%20Doe%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
});
7 changes: 4 additions & 3 deletions packages/core/src/builder.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2618,11 +2618,12 @@ export class Builder {
if (!isApiCallForCodegen) {
queryParams.enrich = true;
if (queue[0].query) {
const flattened = this.flattenMongoQuery({ query: queue[0].query });
delete queryParams.query;
const objectToFlatten = { query: queue[0].query };
const flattened = this.flattenMongoQuery(objectToFlatten);
for (const key in flattened) {
queryParams[key] = flattened[key];
queryParams[key] = JSON.stringify(flattened[key]);
}
delete queryParams.query;
}
}

Expand Down