mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-25 07:11:50 +01:00
Implement setting api token after response
This commit is contained in:
parent
d998dceb1e
commit
4db6dcca48
@ -1,6 +1,6 @@
|
||||
import { ofetch } from "ofetch";
|
||||
|
||||
import { getApiToken } from "@/backend/helpers/providerApi";
|
||||
import { getApiToken, setApiToken } from "@/backend/helpers/providerApi";
|
||||
import { getLoadbalancedProxyUrl } from "@/utils/providers";
|
||||
|
||||
type P<T> = Parameters<typeof ofetch<T, any>>;
|
||||
@ -66,6 +66,11 @@ export async function singularProxiedFetch<T>(
|
||||
},
|
||||
query: {},
|
||||
headers,
|
||||
onResponse(context) {
|
||||
const tokenHeader = context.response.headers.get("X-Token");
|
||||
if (tokenHeader) setApiToken(tokenHeader);
|
||||
ops.onResponse?.(context);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,10 @@ export function getCachedMetadata(): MetaOutput[] {
|
||||
return metaDataCache ?? [];
|
||||
}
|
||||
|
||||
export function setApiToken(newToken: string) {
|
||||
token = newToken;
|
||||
}
|
||||
|
||||
function getTokenIfValid(): null | string {
|
||||
if (!token) return null;
|
||||
try {
|
||||
@ -116,7 +120,7 @@ export async function connectServerSideEvents<T>(
|
||||
});
|
||||
|
||||
eventSource.addEventListener("token", (e) => {
|
||||
token = JSON.parse(e.data);
|
||||
setApiToken(JSON.parse(e.data));
|
||||
});
|
||||
|
||||
eventSource.addEventListener("error", (err: MessageEvent<any>) => {
|
||||
|
Loading…
Reference in New Issue
Block a user