mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-21 13:43:20 +00:00
Add a way to get the base url of an endpoint
This commit is contained in:
parent
86bbc4cf6e
commit
849177562d
@ -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<T> {
|
||||
count: number;
|
||||
@ -46,3 +47,35 @@ export const paginated = async <T>(
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
interface Request {
|
||||
client?: Client;
|
||||
}
|
||||
|
||||
interface InterceptorOptions {
|
||||
url: string;
|
||||
}
|
||||
|
||||
type GenericEndpoint = <ThrowOnError extends boolean = false>(
|
||||
options?: Options<Request, ThrowOnError>,
|
||||
) => RequestResult<unknown, unknown, ThrowOnError>;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user