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; +};