Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions app/client/src/actions/pageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface FetchPublishedPageActionPayload {
export interface FetchPublishedPageResourcesPayload {
pageId: string;
basePageId: string;
branch: string;
}

export const fetchPublishedPageAction = (
Expand Down Expand Up @@ -298,12 +299,14 @@ export const clonePageSuccess = ({
// In future we can reuse this for fetching other page level resources in published mode
export const fetchPublishedPageResources = ({
basePageId,
branch,
pageId,
}: FetchPublishedPageResourcesPayload): ReduxAction<FetchPublishedPageResourcesPayload> => ({
type: ReduxActionTypes.FETCH_PUBLISHED_PAGE_RESOURCES_INIT,
payload: {
pageId,
basePageId,
branch,
},
});

Expand Down
26 changes: 13 additions & 13 deletions app/client/src/api/services/ConsolidatedPageLoadApi/api.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { api } from "api/core";
import type { InitConsolidatedApi } from "sagas/InitSagas";
import type { ConsolidatedApiParams } from "./types";
import { ConsolidatedApiUtils } from "./url";

const BASE_URL = "v1/consolidated-api";
const VIEW_URL = `${BASE_URL}/view`;
const EDIT_URL = `${BASE_URL}/edit`;
export const getConsolidatedPageLoadDataView = async (
params: ConsolidatedApiParams,
) => {
const viewUrl = ConsolidatedApiUtils.getViewUrl(params);

export const getConsolidatedPageLoadDataView = async (params: {
applicationId?: string;
defaultPageId?: string;
}) => {
return api.get<InitConsolidatedApi>(VIEW_URL, { params });
return api.get<InitConsolidatedApi>(viewUrl);
};

export const getConsolidatedPageLoadDataEdit = async (params: {
applicationId?: string;
defaultPageId?: string;
}) => {
return api.get<InitConsolidatedApi>(EDIT_URL, { params });
export const getConsolidatedPageLoadDataEdit = async (
params: ConsolidatedApiParams,
) => {
const editUrl = ConsolidatedApiUtils.getEditUrl(params);

return api.get<InitConsolidatedApi>(editUrl);
};
5 changes: 5 additions & 0 deletions app/client/src/api/services/ConsolidatedPageLoadApi/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ConsolidatedApiParams {
applicationId?: string;
defaultPageId?: string;
branchName?: string;
}
25 changes: 25 additions & 0 deletions app/client/src/api/services/ConsolidatedPageLoadApi/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pickBy from "lodash/pickBy";
import identity from "lodash/identity";
import type { ConsolidatedApiParams } from "./types";

export class ConsolidatedApiUtils {
private static BASE_URL = "v1/consolidated-api";
private static VIEW_URL = `${this.BASE_URL}/view`;
private static EDIT_URL = `${this.BASE_URL}/edit`;

static getViewUrl = (requestParams: ConsolidatedApiParams) => {
// Remove undefined values from the requestParams object
const queryParamsObject = pickBy(requestParams, identity);
const queryParamsString = new URLSearchParams(queryParamsObject).toString();

return `${this.VIEW_URL}?${queryParamsString}`;
};

static getEditUrl = (requestParams: ConsolidatedApiParams) => {
// Remove undefined values from the requestParams object
const queryParamsObject = pickBy(requestParams, identity);
const queryParamsString = new URLSearchParams(queryParamsObject).toString();

return `${this.EDIT_URL}?${queryParamsString}`;
};
}
Comment thread
dvj1988 marked this conversation as resolved.
5 changes: 3 additions & 2 deletions app/client/src/ce/sagas/PageSagas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ import {
selectCombinedPreviewMode,
selectGitApplicationCurrentBranch,
} from "selectors/gitModSelectors";
import { identity, pickBy } from "lodash";

export const checkIfMigrationIsNeeded = (
fetchPageResponse?: FetchPageResponse,
Expand Down Expand Up @@ -400,9 +401,9 @@ export function* fetchPublishedPageResourcesSaga(
action: ReduxAction<FetchPublishedPageResourcesPayload>,
) {
try {
const { basePageId, pageId } = action.payload;
const { basePageId, branch: branchName, pageId } = action.payload;

const params = { defaultPageId: basePageId };
const params = pickBy({ defaultPageId: basePageId, branchName }, identity);
const initConsolidatedApiResponse: ApiResponse<InitConsolidatedApi> =
yield ConsolidatedPageLoadApi.getConsolidatedPageLoadDataView(params);

Expand Down
38 changes: 11 additions & 27 deletions app/client/src/ce/utils/serviceWorkerUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,9 @@ describe("serviceWorkerUtils", () => {

expect(request).toBeInstanceOf(Request);
expect(request?.url).toBe(
`https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${basePageId}&applicationId=${baseApplicationId}`,
`https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${basePageId}&applicationId=${baseApplicationId}&branchName=${params.branchName}`,
);
expect(request?.method).toBe("GET");
expect(request?.headers.get("Branchname")).toBe("main");
});

it("should create request for PUBLISHED mode with applicationId", () => {
Expand All @@ -369,10 +368,9 @@ describe("serviceWorkerUtils", () => {

expect(request).toBeInstanceOf(Request);
expect(request?.url).toBe(
`https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=${basePageId}&applicationId=${baseApplicationId}`,
`https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=${basePageId}&applicationId=${baseApplicationId}&branchName=${params.branchName}`,
);
expect(request?.method).toBe("GET");
expect(request?.headers.get("Branchname")).toBe("main");
});

it("should create request for EDIT mode without applicationId", () => {
Expand All @@ -387,10 +385,9 @@ describe("serviceWorkerUtils", () => {

expect(request).toBeInstanceOf(Request);
expect(request?.url).toBe(
`https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=page123`,
`https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${params.basePageId}&branchName=${params.branchName}`,
);
expect(request?.method).toBe("GET");
expect(request?.headers.get("Branchname")).toBe("main");
});

it("should create request for PUBLISHED mode without applicationId", () => {
Expand All @@ -405,10 +402,9 @@ describe("serviceWorkerUtils", () => {

expect(request).toBeInstanceOf(Request);
expect(request?.url).toBe(
`https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=page123`,
`https://app.appsmith.com/api/v1/consolidated-api/view?defaultPageId=${params.basePageId}&branchName=${params.branchName}`,
);
expect(request?.method).toBe("GET");
expect(request?.headers.get("Branchname")).toBe("main");
});

it("should return null for an unknown app mode", () => {
Expand Down Expand Up @@ -438,10 +434,9 @@ describe("serviceWorkerUtils", () => {

expect(consolidatedAPIRequest).toBeInstanceOf(Request);
expect(consolidatedAPIRequest?.url).toBe(
`https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=page123`,
`https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=${params.basePageId}&branchName=${params.branchName}`,
);
expect(consolidatedAPIRequest?.method).toBe("GET");
expect(consolidatedAPIRequest?.headers.get("Branchname")).toBe("main");
});
});

Expand Down Expand Up @@ -475,27 +470,16 @@ describe("serviceWorkerUtils", () => {

describe("getRequestKey", () => {
it("should return the correct request key", () => {
const request = new Request("https://app.appsmith.com", {
method: "GET",
});

request.headers.append("branchname", "main");
const key = prefetchApiService.getRequestKey(request);

expect(key).toBe("GET:https://app.appsmith.com/:branchname:main");
});

it("should only append branchname header in request key", () => {
const request = new Request("https://app.appsmith.com", {
method: "GET",
const url =
"https://app.appsmith.com/api/v1/consolidated-api/edit?defaultPageId=page123&applicationId=app123&branchName=main";
const method = "GET";
const request = new Request(url, {
method,
});

request.headers.append("branchname", "main");
request.headers.append("another-header-key", "another-header-value");
request.headers.append("Content-Type", "application/json");
const key = prefetchApiService.getRequestKey(request);

expect(key).toBe("GET:https://app.appsmith.com/:branchname:main");
expect(key).toBe(`${method}:${url}`);
});
});

Expand Down
49 changes: 20 additions & 29 deletions app/client/src/ce/utils/serviceWorkerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BUILDER_PATH_DEPRECATED,
VIEWER_PATH_DEPRECATED,
} from "../constants/routes/appRoutes";
import { ConsolidatedApiUtils } from "api/services/ConsolidatedPageLoadApi/url";

interface TMatchResult {
basePageId?: string;
Expand Down Expand Up @@ -104,36 +105,36 @@ export const getConsolidatedApiPrefetchRequest = (
const { appMode, baseApplicationId, basePageId, branchName, origin } =
applicationProps;

const headers = new Headers();
const searchParams = new URLSearchParams();

if (!basePageId) {
return null;
}

searchParams.append("defaultPageId", basePageId);

if (baseApplicationId) {
searchParams.append("applicationId", baseApplicationId);
}

// Add the branch name to the headers
if (branchName) {
headers.append("Branchname", branchName);
}

// If the URL matches the builder path
if (appMode === APP_MODE.EDIT) {
const requestUrl = `${origin}/api/${"v1/consolidated-api/edit"}?${searchParams.toString()}`;
const request = new Request(requestUrl, { method: "GET", headers });
const requestUrl = ConsolidatedApiUtils.getEditUrl({
defaultPageId: basePageId,
applicationId: baseApplicationId,
branchName,
});

const request = new Request(`${origin}/api/${requestUrl}`, {
method: "GET",
});

return request;
}

// If the URL matches the viewer path
if (appMode === APP_MODE.PUBLISHED) {
const requestUrl = `${origin}/api/v1/consolidated-api/view?${searchParams.toString()}`;
const request = new Request(requestUrl, { method: "GET", headers });
const requestUri = ConsolidatedApiUtils.getViewUrl({
defaultPageId: basePageId,
applicationId: baseApplicationId,
branchName,
});

const request = new Request(`${origin}/api/${requestUri}`, {
method: "GET",
});

return request;
}
Expand Down Expand Up @@ -166,22 +167,12 @@ export class PrefetchApiService {
cacheMaxAge = 2 * 60 * 1000; // 2 minutes in milliseconds
// Mutex to lock the fetch and cache operation
prefetchFetchMutexMap = new Map<string, Mutex>();
// Header keys used to create the unique request key
headerKeys = ["branchname"];

constructor() {}

// Function to get the request key
getRequestKey = (request: Request) => {
let requestKey = `${request.method}:${request.url}`;

this.headerKeys.forEach((headerKey) => {
const headerValue = request.headers.get(headerKey);

if (headerValue) {
requestKey += `:${headerKey}:${headerValue}`;
}
});
const requestKey = `${request.method}:${request.url}`;

return requestKey;
};
Expand Down
1 change: 1 addition & 0 deletions app/client/src/pages/AppViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ function AppViewer(props: Props) {
fetchPublishedPageResources({
basePageId,
pageId,
branch,
}),
);
}
Expand Down
7 changes: 4 additions & 3 deletions app/client/src/sagas/InitSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,21 @@ function* executeActionDuringUserDetailsInitialisation(
export function* getInitResponses({
applicationId,
basePageId,
branch,
mode,
shouldInitialiseUserDetails,
}: {
applicationId?: string;
basePageId?: string;
mode?: APP_MODE;
shouldInitialiseUserDetails?: boolean;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}): any {
branch?: string;
}) {
const params = pickBy(
{
applicationId,
defaultPageId: basePageId,
branchName: branch,
},
identity,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -49,9 +48,9 @@ public ConsolidatedAPIController(
public Mono<ResponseDTO<ConsolidatedAPIResponseDTO>> getAllDataForFirstPageLoadForEditMode(
@RequestParam(name = FieldName.APPLICATION_ID, required = false) String baseApplicationId,
@RequestParam(name = "defaultPageId", required = false) String basePageId,
@RequestHeader(required = false, defaultValue = "branch") RefType refType,
@RequestHeader(required = false) String refName,
@RequestHeader(required = false) String branchName) {
@RequestParam(required = false, defaultValue = "branch") RefType refType,
@RequestParam(required = false) String refName,
@RequestParam(required = false) String branchName) {

if (!StringUtils.hasLength(refName)) {
refName = branchName;
Expand Down Expand Up @@ -82,9 +81,9 @@ public Mono<ResponseDTO<ConsolidatedAPIResponseDTO>> getAllDataForFirstPageLoadF
public Mono<ResponseDTO<ConsolidatedAPIResponseDTO>> getAllDataForFirstPageLoadForViewMode(
@RequestParam(required = false) String applicationId,
@RequestParam(required = false) String defaultPageId,
@RequestHeader(required = false, defaultValue = "branch") RefType refType,
@RequestHeader(required = false) String refName,
@RequestHeader(required = false) String branchName) {
@RequestParam(required = false, defaultValue = "branch") RefType refType,
@RequestParam(required = false) String refName,
@RequestParam(required = false) String branchName) {

if (!StringUtils.hasLength(refName)) {
refName = branchName;
Expand Down