Files
ncd-fe/packages/pl-fe/src/api/index.ts
marcin mikołajczak 966b04fdf0 Call it pl-fe internally
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-08-28 13:41:08 +02:00

46 lines
1.2 KiB
TypeScript

/**
* API: HTTP client and utilities.
* @module pl-fe/api
*/
import * as BuildConfig from 'pl-fe/build-config';
import { RootState } from 'pl-fe/store';
import { buildFullPath } from 'pl-fe/utils/url';
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 = (input: URL | RequestInfo, init?: RequestInit | undefined) => {
const fullPath = buildFullPath(input.toString(), BuildConfig.FE_SUBDIRECTORY);
return fetch(fullPath, init).then(async (response) => {
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)) => {
if (typeof state === 'function') state = state();
return state.auth.client;
};
export {
type PlfeResponse,
staticFetch,
getClient,
};