Files
ncd-fe/packages/pl-fe/src/api/index.ts
nicole mikołajczyk 9f98b5b07d nicolium: oxlint and oxfmt migration, remove eslint
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
2026-02-15 13:30:55 +01:00

57 lines
1.3 KiB
TypeScript

/**
* API: HTTP client and utilities.
* @module pl-fe/api
*/
import * as BuildConfig from '@/build-config';
import { buildFullPath } from '@/utils/url';
import type { RootState, Store } from '@/store';
let store: Store;
import('@/store').then((value) => (store = value.store)).catch(() => {});
type PlfeResponse<T = any> = Response & { data: string; json: T };
/**
* Dumb client for grabbing static files.
* It uses FE_SUBDIRECTORY and parses JSON if possible.
* No authorization is needed.
*/
const staticFetch = async (input: URL | RequestInfo, init?: RequestInit) => {
const fullPath = buildFullPath(input.toString(), BuildConfig.BACKEND_URL);
const response = await fetch(fullPath, init);
if (!response.ok) throw { response };
const data = await response.text();
let json: any = undefined!;
try {
json = JSON.parse(data);
} catch (e) {
//
}
const { headers, ok, redirected, status, statusText, type, url } = response;
return {
headers,
ok,
redirected,
status,
statusText,
type,
url,
data,
json,
} as any as PlfeResponse;
};
const getClient = (state: RootState | (() => RootState) = store?.getState()) => {
if (typeof state === 'function') state = state();
return state.auth.client;
};
export { type PlfeResponse, staticFetch, getClient };