New abstraction

This commit is contained in:
Manav Rathi
2024-06-27 15:40:35 +05:30
parent 1b77c899da
commit f543b402f8
2 changed files with 15 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
import { isDevBuild } from "@/next/env";
import { apiOrigin } from "@/next/origins";
import { apiOrigin, apiURL } from "@/next/origins";
import { clientPackageName } from "@/next/types/app";
import { TwoFactorAuthorizationResponse } from "@/next/types/credentials";
import { ensure } from "@/utils/ensure";
@@ -58,7 +58,7 @@ const GetPasskeysResponse = z.object({
* has no passkeys.
*/
export const getPasskeys = async (token: string) => {
const url = `${apiOrigin()}/passkeys`;
const url = await apiURL("passkeys");
const res = await fetch(url, {
headers: accountsAuthenticatedRequestHeaders(token),
});

View File

@@ -11,6 +11,19 @@ import { get, set } from "idb-keyval";
export const apiOrigin = async () =>
(await customAPIOrigin()) ?? "https://api.ente.io";
/**
* A convenience function to construct an endpoint in a one-liner.
*
* This avoids us having to create a temporary variable or otherwise complicate
* the call sites since async functions cannot be used inside template literals.
*
* @returns The equivalent of `${await apiOrigin()}/pathEtc`
*/
export const apiURL = async (pathEtc: string) => {
const origin = await apiOrigin();
return `${origin}/${pathEtc}`;
};
/**
* Return the overridden API origin, if one is defined by either (in priority
* order):