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

feat[gen1][core] ENG-7238 remove query API and apiEndpoint field #3681

Merged
merged 12 commits into from
Oct 31, 2024
159 changes: 3 additions & 156 deletions packages/core/src/builder.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ describe('flushGetContentQueue', () => {
);
});

test("hits query url when apiEndpoint is undefined and format is 'html'", async () => {
test("hits content url when format is 'html'", async () => {
const expectedFormat = 'html';

const result = await builder['flushGetContentQueue'](true, [
Expand All @@ -510,35 +510,6 @@ describe('flushGetContentQueue', () => {
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'html'", async () => {
const expectedFormat = 'html';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
limit: 10,
},
]);
Expand All @@ -565,7 +536,7 @@ describe('flushGetContentQueue', () => {
);
});

test("hits query url when apiEndpoint is undefined and format is 'amp'", async () => {
test("hits content url when format is 'amp'", async () => {
const expectedFormat = 'amp';

const result = await builder['flushGetContentQueue'](true, [
Expand All @@ -576,35 +547,6 @@ describe('flushGetContentQueue', () => {
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'amp'", async () => {
const expectedFormat = 'amp';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
limit: 10,
},
]);
Expand All @@ -631,7 +573,7 @@ describe('flushGetContentQueue', () => {
);
});

test("hits query url when apiEndpoint is undefined and format is 'email'", async () => {
test("hits content url when format is 'email'", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
Expand All @@ -642,63 +584,6 @@ describe('flushGetContentQueue', () => {
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&apiKey=${API_KEY}&fields=data&format=${expectedFormat}&userAttributes=%7B%22respectScheduling%22%3Atrue%7D&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits query url when apiEndpoint is undefined and format is 'email' and url is passed instead of userAttributes", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
{
model: MODEL,
format: expectedFormat,
key: MODEL,
url: '/test-page',
omit: OMIT,
fields: 'data',
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...codegenOrQueryApiResult[MODEL][0],
variationId: expect.any(String),
});
expect(builder['makeFetchApiCall']).toBeCalledTimes(1);
expect(builder['makeFetchApiCall']).toBeCalledWith(
`https://cdn.builder.io/api/v3/query/${API_KEY}/${MODEL}?omit=${OMIT}&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&options.${MODEL}.model=%22${MODEL}%22`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'email'", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
userAttributes: { respectScheduling: true },
omit: OMIT,
fields: 'data',
limit: 10,
},
]);
Expand All @@ -724,42 +609,4 @@ describe('flushGetContentQueue', () => {
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});

test("hits content url when apiEndpoint is 'content' and format is 'email' and url is passed instead of userAttributes", async () => {
const expectedFormat = 'email';

const result = await builder['flushGetContentQueue'](true, [
{
apiEndpoint: 'content',
model: MODEL,
format: expectedFormat,
key: MODEL,
url: '/test-page',
omit: OMIT,
fields: 'data',
limit: 10,
},
]);

const observerNextMock = builder.observersByKey[MODEL]?.next as jest.Mock;

expect(observerNextMock).toBeCalledTimes(1);
expect(observerNextMock.mock.calls[0][0][0]).toStrictEqual({
...contentApiResult.results[0],
variationId: expect.any(String),
});
expect(observerNextMock.mock.calls[0][0][1]).toStrictEqual({
...contentApiResult.results[1],
});
expect(observerNextMock.mock.calls[0][0][2]).toStrictEqual({
...contentApiResult.results[2],
variationId: expect.any(String),
});

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`,
{ headers: { Authorization: `Bearer ${AUTH_TOKEN}` } }
);
});
});
17 changes: 3 additions & 14 deletions packages/core/src/builder.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,6 @@ type AllowEnrich =
| { apiVersion?: never; enrich?: boolean };

export type GetContentOptions = AllowEnrich & {
/**
* Dictates which API endpoint is used when fetching content. Allows `'content'` and `'query'`.
* Defaults to `'query'`.
*/
apiEndpoint?: 'content' | 'query';

/**
* Optional fetch options to be passed as the second argument to the `fetch` function.
*/
Expand Down Expand Up @@ -2416,8 +2410,6 @@ export class Builder {

const queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];

const apiEndpoint = queue[0].apiEndpoint || 'query';

// TODO: do this on every request send?
this.getOverridesFromQueryString();

Expand Down Expand Up @@ -2549,7 +2541,7 @@ export class Builder {
for (const key of properties) {
const value = options[key];
if (value !== undefined) {
if (apiEndpoint === 'query') {
if (format === 'solid' || format === 'react') {
clyde-builderio marked this conversation as resolved.
Show resolved Hide resolved
queryParams.options = queryParams.options || {};
queryParams.options[options.key!] = queryParams.options[options.key!] || {};
queryParams.options[options.key!][key] = JSON.stringify(value);
Expand All @@ -2576,9 +2568,8 @@ export class Builder {

const format = queryParams.format;
const isApiCallForCodegen = format === 'solid' || format === 'react';
const isApiCallForCodegenOrQuery = isApiCallForCodegen || apiEndpoint === 'query';

if (apiEndpoint === 'content') {
if (!isApiCallForCodegen) {
queryParams.enrich = true;
if (queue[0].query) {
const flattened = this.flattenMongoQuery({ query: queue[0].query });
Expand All @@ -2602,8 +2593,6 @@ export class Builder {
let url;
if (isApiCallForCodegen) {
url = `${host}/api/v1/codegen/${this.apiKey}/${keyNames}`;
} else if (apiEndpoint === 'query') {
url = `${host}/api/v3/query/${this.apiKey}/${keyNames}`;
} else {
url = `${host}/api/v3/content/${queue[0].model}`;
}
Expand All @@ -2629,7 +2618,7 @@ export class Builder {
if (!observer) {
return;
}
const data = isApiCallForCodegenOrQuery ? result[keyName] : result.results;
const data = isApiCallForCodegen ? result[keyName] : result.results;
const sorted = data; // sortBy(data, item => item.priority);
if (data) {
const testModifiedResults = Builder.isServer
Expand Down
23 changes: 0 additions & 23 deletions packages/sdks-tests/src/e2e-tests/hit-query-api.spec.ts

This file was deleted.

Loading
Loading