From 849177562d2fb7151fb154d308159b663c531bf8 Mon Sep 17 00:00:00 2001 From: Sli Date: Thu, 10 Oct 2024 02:57:54 +0200 Subject: [PATCH] Add a way to get the base url of an endpoint --- core/static/webpack/utils/api.ts | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/core/static/webpack/utils/api.ts b/core/static/webpack/utils/api.ts index e81be5d4..a2c872c7 100644 --- a/core/static/webpack/utils/api.ts +++ b/core/static/webpack/utils/api.ts @@ -1,4 +1,5 @@ -import type { Options, RequestResult } from "@hey-api/client-fetch"; +import type { Client, Options, RequestResult } from "@hey-api/client-fetch"; +import { client } from "#openapi"; interface PaginatedResponse { count: number; @@ -46,3 +47,35 @@ export const paginated = async ( } return results; }; + +interface Request { + client?: Client; +} + +interface InterceptorOptions { + url: string; +} + +type GenericEndpoint = ( + options?: Options, +) => RequestResult; + +/** + * Return the endpoint url of the endpoint + **/ +export const makeUrl = async (endpoint: GenericEndpoint) => { + let url = ""; + const interceptor = (_request: undefined, options: InterceptorOptions) => { + url = options.url; + throw new Error("We don't want to send the request"); + }; + + client.interceptors.request.use(interceptor); + try { + await endpoint({ client: client }); + } catch (_error) { + /* do nothing */ + } + client.interceptors.request.eject(interceptor); + return url; +};