Add HTTP endpoint helpers to compliance-portal
Introduce a lib/http module with buildEndpoint and getPathPrefix, extracted from the trust app so the compliance portal can resolve its GraphQL endpoint with the same path-prefix handling. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
42
apps/compliance-portal/src/lib/http/endpoint.ts
Normal file
42
apps/compliance-portal/src/lib/http/endpoint.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||||
|
//
|
||||||
|
// Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
// purpose with or without fee is hereby granted, provided that the above
|
||||||
|
// copyright notice and this permission notice appear in all copies.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||||
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||||
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||||
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||||
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
|
// PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
import { getPathPrefix } from "#/lib/http/pathPrefix";
|
||||||
|
|
||||||
|
export function buildEndpoint(): string {
|
||||||
|
let host = import.meta.env.VITE_API_URL;
|
||||||
|
|
||||||
|
if (!host) {
|
||||||
|
host = window.location.origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
const formattedHost
|
||||||
|
= host.startsWith("http://") || host.startsWith("https://")
|
||||||
|
? host
|
||||||
|
: `https://${host}`;
|
||||||
|
|
||||||
|
const url = new URL(formattedHost);
|
||||||
|
|
||||||
|
const prefix = getPathPrefix();
|
||||||
|
let path: string;
|
||||||
|
if (prefix) {
|
||||||
|
path = `${prefix}/api/trust/v1/graphql`;
|
||||||
|
} else {
|
||||||
|
path = `/api/trust/v1/graphql`;
|
||||||
|
}
|
||||||
|
|
||||||
|
url.pathname = path;
|
||||||
|
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
29
apps/compliance-portal/src/lib/http/pathPrefix.ts
Normal file
29
apps/compliance-portal/src/lib/http/pathPrefix.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||||
|
//
|
||||||
|
// Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
// purpose with or without fee is hereby granted, provided that the above
|
||||||
|
// copyright notice and this permission notice appear in all copies.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||||
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||||
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||||
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||||
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
|
// PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
import { matchPath } from "react-router";
|
||||||
|
|
||||||
|
export function getPathPrefix() {
|
||||||
|
const match = matchPath(
|
||||||
|
{ path: "/trust/:id", caseSensitive: false, end: false },
|
||||||
|
window.location.pathname,
|
||||||
|
);
|
||||||
|
|
||||||
|
let prefix = "";
|
||||||
|
if (match) {
|
||||||
|
prefix = `/trust/${match.params.id}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user