From d5ff3b70e30290487c4d06c11dd4ae5218b92179 Mon Sep 17 00:00:00 2001 From: Todd Martin Date: Thu, 17 Oct 2024 13:02:59 -0400 Subject: [PATCH 1/2] Added new Is-Beta header --- libs/common/src/platform/misc/flags.ts | 1 + libs/common/src/services/api.service.ts | 82 ++++++++++++++++--------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/libs/common/src/platform/misc/flags.ts b/libs/common/src/platform/misc/flags.ts index b3269c8f4e8..664c0037a2f 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; + betaBuild?: 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..7fb7a1c0c7f 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,58 +1844,79 @@ 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, }; + requestInit.headers = requestHeaders; + requestInit.body = requestBody; + const response = await this.fetch(new Request(requestUrl, requestInit)); + + const responseType = response.headers.get("content-type"); + const responseIsJson = responseType != null && responseType.indexOf("application/json") !== -1; + const responseIsCsv = responseType != null && responseType.indexOf("text/csv") !== -1; + if (hasResponse && response.status === 200 && responseIsJson) { + const responseJson = await response.json(); + return responseJson; + } else if (hasResponse && response.status === 200 && responseIsCsv) { + return await response.text(); + } else if (response.status !== 200) { + const error = await this.handleError(response, false, authed); + return Promise.reject(error); + } + } + 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("betaBuild")) { + headers.set("Is-Beta", "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") { - requestInit.body = body; + requestBody = 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; + requestBody = body; } else { headers.set("Content-Type", "application/json; charset=utf-8"); - requestInit.body = JSON.stringify(body); + requestBody = JSON.stringify(body); } } } - if (hasResponse) { - headers.set("Accept", "application/json"); - } - if (alterHeaders != null) { - alterHeaders(headers); - } - - requestInit.headers = headers; - const response = await this.fetch(new Request(requestUrl, requestInit)); - const responseType = response.headers.get("content-type"); - const responseIsJson = responseType != null && responseType.indexOf("application/json") !== -1; - const responseIsCsv = responseType != null && responseType.indexOf("text/csv") !== -1; - if (hasResponse && response.status === 200 && responseIsJson) { - const responseJson = await response.json(); - return responseJson; - } else if (hasResponse && response.status === 200 && responseIsCsv) { - return await response.text(); - } else if (response.status !== 200) { - const error = await this.handleError(response, false, authed); - return Promise.reject(error); - } + return [headers, requestBody]; } private async handleError( From 3427ea704be3f11d71010195ad058157eab3cc6f Mon Sep 17 00:00:00 2001 From: Todd Martin Date: Mon, 21 Oct 2024 16:31:22 -0400 Subject: [PATCH 2/2] Updated beta to prerelease --- libs/common/src/platform/misc/flags.ts | 2 +- libs/common/src/services/api.service.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/common/src/platform/misc/flags.ts b/libs/common/src/platform/misc/flags.ts index 664c0037a2f..297e616e66b 100644 --- a/libs/common/src/platform/misc/flags.ts +++ b/libs/common/src/platform/misc/flags.ts @@ -3,7 +3,7 @@ export type SharedFlags = { showPasswordless?: boolean; sdk?: boolean; - betaBuild?: 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 7fb7a1c0c7f..f3bd93560b6 100644 --- a/libs/common/src/services/api.service.ts +++ b/libs/common/src/services/api.service.ts @@ -1885,8 +1885,8 @@ export class ApiService implements ApiServiceAbstraction { "Device-Type": this.deviceType, }); - if (flagEnabled("betaBuild")) { - headers.set("Is-Beta", "true"); + if (flagEnabled("prereleaseBuild")) { + headers.set("Is-Prerelease", "true"); } if (this.customUserAgent != null) { headers.set("User-Agent", this.customUserAgent);