Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[gen1][core] ENG-7841 change to queryParams #3787

Merged
merged 12 commits into from
Dec 23, 2024
6 changes: 6 additions & 0 deletions .changeset/famous-pants-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@builder.io/sdk': patch
'@builder.io/react': patch
---

Fix: Content API invocations will have `includeRefs` set to `true` by default.
148 changes: 143 additions & 5 deletions packages/core/src/builder.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ describe('flushGetContentQueue', () => {

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&limit=10&model=%22${MODEL}%22&enrich=true`,
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&includeRefs=true&limit=10&model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
Expand Down Expand Up @@ -755,7 +755,7 @@ describe('flushGetContentQueue', () => {

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&limit=10&model=%22${MODEL}%22&enrich=true`,
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&includeRefs=true&limit=10&model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
Expand Down Expand Up @@ -849,7 +849,7 @@ describe('flushGetContentQueue', () => {

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&limit=10&model=%22${MODEL}%22&enrich=true`,
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&includeRefs=true&limit=10&model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
Expand Down Expand Up @@ -887,7 +887,7 @@ describe('flushGetContentQueue', () => {

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22urlPath%22%3A%22%2Ftest-page%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&limit=10&model=%22${MODEL}%22&enrich=true`,
`https://cdn.builder.io/api/v3/content/${MODEL}?omit=data.blocks&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22urlPath%22%3A%22%2Ftest-page%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&includeRefs=true&limit=10&model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
Expand Down Expand Up @@ -915,7 +915,145 @@ describe('flushGetContentQueue', () => {

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=%22${expectedEntryId}%22&enrich=true&query.id=${expectedEntryId}`,
`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&includeRefs=true&limit=10&model=%22${expectedModel}%22&entry=%22${expectedEntryId}%22&query.id=${expectedEntryId}`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
});

describe('get', () => {
const API_KEY = '25608a566fbb654ea959c1b1729e370d';
const MODEL = 'page';
const AUTH_TOKEN = '82202e99f9fb4ed1da5940f7fa191e72';

let builder: Builder;

beforeEach(() => {
builder = new Builder(API_KEY, undefined, undefined, false, AUTH_TOKEN, 'v3');
const builderSubject = new BehaviorSubject<BuilderContent[]>([]);
builderSubject.next = jest.fn(() => {});
builder.observersByKey[MODEL] = builderSubject;
builder['makeFetchApiCall'] = jest.fn((url: string) => {
return Promise.resolve({
json: () => {
return Promise.resolve({});
},
});
});
});

test('hits content url with includeRefs=false when passed in params and noTraverse=false', async () => {
const expectedModel = 'page';

const includeRefs = false;

builder.apiEndpoint = 'content';
builder.getLocation = jest.fn(() => ({
search: `?builder.params=includeRefs%3D${includeRefs}`,
}));

await builder.get(expectedModel, {});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=meta.componentsUsed&apiKey=${API_KEY}&noTraverse=false&userAttributes=%7B%22device%22%3A%22desktop%22%7D&includeRefs=${includeRefs}&model=%22${expectedModel}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with includeRefs=true and noTraverse=false by default', async () => {
const expectedModel = 'page';

builder.apiEndpoint = 'content';
await builder.get(expectedModel, {});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=meta.componentsUsed&apiKey=${API_KEY}&noTraverse=false&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&includeRefs=true&model=%22${expectedModel}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with includeRefs=false when passed in options and noTraverse=false by default', async () => {
const expectedModel = 'page';

builder.apiEndpoint = 'content';
await builder.get(expectedModel, {
includeRefs: false,
});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=meta.componentsUsed&apiKey=${API_KEY}&noTraverse=false&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&includeRefs=false&model=%22${expectedModel}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
});

describe('getAll', () => {
const API_KEY = '25608a566fbb654ea959c1b1729e370d';
const MODEL = 'page';
const AUTH_TOKEN = '82202e99f9fb4ed1da5940f7fa191e72';

let builder: Builder;

beforeEach(() => {
builder = new Builder(API_KEY, undefined, undefined, false, AUTH_TOKEN, 'v3');
const builderSubject = new BehaviorSubject<BuilderContent[]>([]);
builderSubject.next = jest.fn(() => {});
builder.observersByKey[MODEL] = builderSubject;
builder['makeFetchApiCall'] = jest.fn((url: string) => {
return Promise.resolve({
json: () => {
return Promise.resolve({});
},
});
});
});

test('hits content url with includeRefs=false when passed in params and noTraverse=true by default', async () => {
const expectedModel = 'page';

const includeRefs = false;

builder.apiEndpoint = 'content';
builder.getLocation = jest.fn(() => ({
search: `?builder.params=includeRefs%3D${includeRefs}`,
}));

await builder.getAll(expectedModel, {});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=meta.componentsUsed&apiKey=${API_KEY}&noTraverse=true&userAttributes=%7B%22device%22%3A%22desktop%22%7D&includeRefs=${includeRefs}&limit=30&model=%22${expectedModel}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with includeRefs=true and noTraverse=true by default', async () => {
const expectedModel = 'page';

builder.apiEndpoint = 'content';
await builder.getAll(expectedModel, {});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=meta.componentsUsed&apiKey=${API_KEY}&noTraverse=true&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&includeRefs=true&limit=30&model=%22${expectedModel}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test('hits content url with includeRefs=false when passed in options and noTraverse=true by default', async () => {
const expectedModel = 'page';

builder.apiEndpoint = 'content';
await builder.getAll(expectedModel, {
includeRefs: false,
});

expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/content/${expectedModel}?omit=meta.componentsUsed&apiKey=${API_KEY}&noTraverse=true&userAttributes=%7B%22urlPath%22%3A%22%2F%22%2C%22host%22%3A%22localhost%22%2C%22device%22%3A%22desktop%22%7D&includeRefs=false&limit=30&model=%22${expectedModel}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/builder.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,11 @@ export class Builder {
let instance: Builder = this;
let finalLocale =
options.locale || options.userAttributes?.locale || this.getUserAttributes().locale;

if (!('noTraverse' in options)) {
sidmohanty11 marked this conversation as resolved.
Show resolved Hide resolved
options.noTraverse = false;
}

let finalOptions = {
...options,
...(finalLocale && {
Expand Down Expand Up @@ -2594,6 +2599,10 @@ export class Builder {
queryParams.staleCacheSeconds = options.staleCacheSeconds;
}

if (this.apiEndpoint === 'content') {
queryParams.includeRefs = true;
}

const properties: (keyof GetContentOptions)[] = [
'prerender',
'extractCss',
Expand All @@ -2605,6 +2614,7 @@ export class Builder {
'entry',
'rev',
'static',
'includeRefs',
];

for (const key of properties) {
Expand Down Expand Up @@ -2640,7 +2650,6 @@ export class Builder {
const isApiCallForCodegenOrQuery = isApiCallForCodegen || this.apiEndpoint === 'query';

if (this.apiEndpoint === 'content') {
queryParams.enrich = true;
if (queue[0].query) {
const flattened = this.flattenMongoQuery({ query: queue[0].query });
for (const key in flattened) {
Expand Down
Loading