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

[PM-13804] Add new Is-Prerelease header to requests #11605

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions libs/common/src/platform/misc/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export type SharedFlags = {
showPasswordless?: boolean;
sdk?: boolean;
prereleaseBuild?: boolean;
};

// required to avoid linting errors when there are no flags
Expand Down
82 changes: 52 additions & 30 deletions libs/common/src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
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";

Check warning on line 129 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L129

Added line #L129 was not covered by tests
import { Utils } from "../platform/misc/utils";
import { SyncResponse } from "../platform/sync";
import { UserId } from "../types/guid";
Expand Down Expand Up @@ -1843,58 +1844,79 @@
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(

Check warning on line 1847 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1847

Added line #L1847 was not covered by tests
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));

Check warning on line 1861 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1859-L1861

Added lines #L1859 - L1861 were not covered by tests

const responseType = response.headers.get("content-type");

Check warning on line 1863 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1863

Added line #L1863 was not covered by tests
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;

Check warning on line 1868 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1867-L1868

Added lines #L1867 - L1868 were not covered by tests
} else if (hasResponse && response.status === 200 && responseIsCsv) {
return await response.text();

Check warning on line 1870 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1870

Added line #L1870 was not covered by tests
} else if (response.status !== 200) {
const error = await this.handleError(response, false, authed);
return Promise.reject(error);

Check warning on line 1873 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1872-L1873

Added lines #L1872 - L1873 were not covered by tests
}
}

private async buildHeadersAndBody(
authed: boolean,
hasResponse: boolean,
body: any,
alterHeaders: (headers: Headers) => void,
): Promise<[Headers, any]> {
let requestBody: any = null;
const headers = new Headers({

Check warning on line 1884 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1883-L1884

Added lines #L1883 - L1884 were not covered by tests
"Device-Type": this.deviceType,
});

if (flagEnabled("prereleaseBuild")) {
headers.set("Is-Prerelease", "true");

Check warning on line 1889 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1889

Added line #L1889 was not covered by tests
}
if (this.customUserAgent != null) {
headers.set("User-Agent", this.customUserAgent);

Check warning on line 1892 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1892

Added line #L1892 was not covered by tests
}
if (hasResponse) {
headers.set("Accept", "application/json");

Check warning on line 1895 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1895

Added line #L1895 was not covered by tests
}
if (alterHeaders != null) {
alterHeaders(headers);

Check warning on line 1898 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1898

Added line #L1898 was not covered by tests
}
if (authed) {
const authHeader = await this.getActiveBearerToken();
headers.set("Authorization", "Bearer " + authHeader);
}

if (body != null) {
if (typeof body === "string") {
requestInit.body = body;
requestBody = body;

Check warning on line 1907 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1907

Added line #L1907 was not covered by tests
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;

Check warning on line 1911 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1911

Added line #L1911 was not covered by tests
} else {
headers.set("Content-Type", "application/json; charset=utf-8");
requestInit.body = JSON.stringify(body);
requestBody = JSON.stringify(body);

Check warning on line 1914 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1914

Added line #L1914 was not covered by tests
}
}
}
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];

Check warning on line 1919 in libs/common/src/services/api.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/services/api.service.ts#L1919

Added line #L1919 was not covered by tests
}

private async handleError(
Expand Down
Loading