pl-fe: provide preloaded instance to pl-api client

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2025-08-06 22:00:48 +02:00
parent ec27c5917a
commit 31d0e204bd
3 changed files with 13 additions and 5 deletions

View File

@ -67,6 +67,8 @@ interface PreloadAction {
export {
PLEROMA_PRELOAD_IMPORT,
MASTODON_PRELOAD_IMPORT,
pleromaDecoder,
decodeFromMarkup,
preload,
preloadMastodon,
type PreloadAction,

View File

@ -1,9 +1,9 @@
import trim from 'lodash/trim';
import { create, Draft } from 'mutative';
import { applicationSchema, PlApiClient, tokenSchema, type CredentialAccount, type CredentialApplication, type Token } from 'pl-api';
import { applicationSchema, instanceSchema, PlApiClient, tokenSchema, type CredentialAccount, type CredentialApplication, type Token } from 'pl-api';
import * as v from 'valibot';
import { MASTODON_PRELOAD_IMPORT, type PreloadAction } from 'pl-fe/actions/preload';
import { decodeFromMarkup, MASTODON_PRELOAD_IMPORT, pleromaDecoder, type PreloadAction } from 'pl-fe/actions/preload';
import * as BuildConfig from 'pl-fe/build-config';
import { coerceObject } from 'pl-fe/schemas/utils';
import KVStore from 'pl-fe/storage/kv-store';
@ -24,6 +24,10 @@ import { ME_FETCH_SKIP, type MeAction } from '../actions/me';
import type { PlfeResponse } from 'pl-fe/api';
import type { Account as AccountEntity } from 'pl-fe/normalizers/account';
const preloadedInstance = decodeFromMarkup('initial-results', pleromaDecoder)['/api/v1/instance'];
const parsedInstance = v.safeParse(instanceSchema, preloadedInstance);
const instance = parsedInstance.success ? parsedInstance.output : undefined;
type Action = AuthAction | MeAction | PreloadAction;
const backendUrl = (isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '');
@ -89,7 +93,9 @@ const getLocalState = (): State | undefined => {
tokens: Object.fromEntries(Object.entries(state.tokens).map(([key, value]) => [key, v.parse(tokenWithAppSchema, value)])),
users: Object.fromEntries(Object.entries(state.users).map(([key, value]) => [key, v.parse(authUserSchema, value)])),
me: state.me,
client: new PlApiClient(parseBaseURL(state.me) || backendUrl, state.users[state.me]?.access_token),
client: new PlApiClient(parseBaseURL(state.me) || backendUrl, state.users[state.me]?.access_token, {
instance,
}),
});
};
@ -193,7 +199,7 @@ const initialState: State = initialize({
tokens: {},
users: {},
me: null,
client: new PlApiClient(backendUrl),
client: new PlApiClient(backendUrl, undefined, { instance }),
...localState,
});

View File

@ -10,7 +10,7 @@ import ConfigDB from 'pl-fe/utils/config-db';
const initialState: State = { fetched: false, ...v.parse(instanceSchema, {}) };
type State = Instance & { fetched : boolean };
type State = Instance & { fetched: boolean };
const preloadImport = (state: State, action: PreloadAction, path: string): State => {
const instance = action.data[path];