diff --git a/libs/common/src/platform/misc/flags.ts b/libs/common/src/platform/misc/flags.ts index b3269c8f4e8..297e616e66b 100644 --- a/libs/common/src/platform/misc/flags.ts +++ b/libs/common/src/platform/misc/flags.ts @@ -3,6 +3,7 @@ export type SharedFlags = { showPasswordless?: boolean; sdk?: boolean; + prereleaseBuild?: boolean; }; // required to avoid linting errors when there are no flags diff --git a/libs/common/src/services/api.service.ts b/libs/common/src/services/api.service.ts index 5f53131719c..f3bd93560b6 100644 --- a/libs/common/src/services/api.service.ts +++ b/libs/common/src/services/api.service.ts @@ -126,6 +126,7 @@ import { AppIdService } from "../platform/abstractions/app-id.service"; import { EnvironmentService } from "../platform/abstractions/environment.service"; import { LogService } from "../platform/abstractions/log.service"; import { PlatformUtilsService } from "../platform/abstractions/platform-utils.service"; +import { flagEnabled } from "../platform/misc/flags"; import { Utils } from "../platform/misc/utils"; import { SyncResponse } from "../platform/sync"; import { UserId } from "../types/guid"; @@ -1843,44 +1844,20 @@ export class ApiService implements ApiServiceAbstraction { const requestUrl = apiUrl + Utils.normalizePath(pathParts[0]) + (pathParts.length > 1 ? `?${pathParts[1]}` : ""); - const headers = new Headers({ - "Device-Type": this.deviceType, - }); - if (this.customUserAgent != null) { - headers.set("User-Agent", this.customUserAgent); - } + const [requestHeaders, requestBody] = await this.buildHeadersAndBody( + authed, + hasResponse, + alterHeaders, + body, + ); const requestInit: RequestInit = { cache: "no-store", credentials: await this.getCredentials(), method: method, }; - - if (authed) { - const authHeader = await this.getActiveBearerToken(); - headers.set("Authorization", "Bearer " + authHeader); - } - if (body != null) { - if (typeof body === "string") { - requestInit.body = body; - headers.set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); - } else if (typeof body === "object") { - if (body instanceof FormData) { - requestInit.body = body; - } else { - headers.set("Content-Type", "application/json; charset=utf-8"); - requestInit.body = JSON.stringify(body); - } - } - } - if (hasResponse) { - headers.set("Accept", "application/json"); - } - if (alterHeaders != null) { - alterHeaders(headers); - } - - requestInit.headers = headers; + requestInit.headers = requestHeaders; + requestInit.body = requestBody; const response = await this.fetch(new Request(requestUrl, requestInit)); const responseType = response.headers.get("content-type"); @@ -1897,6 +1874,51 @@ export class ApiService implements ApiServiceAbstraction { } } + private async buildHeadersAndBody( + authed: boolean, + hasResponse: boolean, + body: any, + alterHeaders: (headers: Headers) => void, + ): Promise<[Headers, any]> { + let requestBody: any = null; + const headers = new Headers({ + "Device-Type": this.deviceType, + }); + + if (flagEnabled("prereleaseBuild")) { + headers.set("Is-Prerelease", "true"); + } + if (this.customUserAgent != null) { + headers.set("User-Agent", this.customUserAgent); + } + if (hasResponse) { + headers.set("Accept", "application/json"); + } + if (alterHeaders != null) { + alterHeaders(headers); + } + if (authed) { + const authHeader = await this.getActiveBearerToken(); + headers.set("Authorization", "Bearer " + authHeader); + } + + if (body != null) { + if (typeof body === "string") { + requestBody = body; + headers.set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); + } else if (typeof body === "object") { + if (body instanceof FormData) { + requestBody = body; + } else { + headers.set("Content-Type", "application/json; charset=utf-8"); + requestBody = JSON.stringify(body); + } + } + } + + return [headers, requestBody]; + } + private async handleError( response: Response, tokenError: boolean,