0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00
logto/packages/demo-app/src/utils.ts

83 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-07-01 00:18:04 -05:00
import { Prompt, UserScope } from '@logto/react';
import { z } from 'zod';
2024-07-07 08:40:26 -05:00
type ToZodObject<T> = z.ZodObject<{
[K in keyof T]-?: z.ZodType<T[K]>;
}>;
2024-07-01 00:18:04 -05:00
type LocalLogtoConfig = {
2024-07-07 08:40:26 -05:00
signInExtraParams?: string;
2024-07-01 00:18:04 -05:00
prompt?: string;
scope?: string;
resource?: string;
2024-07-08 03:52:15 -05:00
appId?: string;
2024-07-01 00:18:04 -05:00
};
2024-07-07 08:40:26 -05:00
const localLogtoConfigGuard = z
.object({
signInExtraParams: z.string(),
prompt: z.string(),
scope: z.string(),
resource: z.string(),
2024-07-08 03:52:15 -05:00
appId: z.string(),
2024-07-07 08:40:26 -05:00
})
.partial() satisfies ToZodObject<LocalLogtoConfig>;
2024-07-01 00:18:04 -05:00
type LocalUiConfig = {
showDevPanel?: boolean;
};
2024-07-07 08:40:26 -05:00
const localUiConfigGuard = z
.object({
showDevPanel: z.boolean(),
})
.partial() satisfies ToZodObject<LocalUiConfig>;
2024-07-01 00:18:04 -05:00
type Key = 'config' | 'ui';
const keyPrefix = 'logto:demo-app:dev:';
type KeyToType = {
config: LocalLogtoConfig;
ui: LocalUiConfig;
};
const keyToGuard: Readonly<{
[K in Key]: z.ZodType<KeyToType[K]>;
}> = Object.freeze({
config: localLogtoConfigGuard,
ui: localUiConfigGuard,
});
const keyToDefault = Object.freeze({
config: {
prompt: [Prompt.Login, Prompt.Consent].join(' '),
scope: [UserScope.Organizations, UserScope.OrganizationRoles].join(' '),
},
ui: {},
} satisfies Record<Key, unknown>);
const safeJsonParse = (value: string): unknown => {
try {
return JSON.parse(value);
} catch {
return null;
}
};
const safeZodParse = (guard: z.ZodType<unknown>, value: unknown) => {
const result = guard.safeParse(value);
return result.success ? result.data : {};
};
export const getLocalData = <K extends Key>(key: K): KeyToType[K] => {
const result = keyToGuard[key].safeParse(
safeJsonParse(localStorage.getItem(`${keyPrefix}${key}`) ?? '')
);
return result.success ? result.data : keyToDefault[key];
};
export const setLocalData = (key: Key, value: unknown) => {
localStorage.setItem(`${keyPrefix}${key}`, JSON.stringify(safeZodParse(keyToGuard[key], value)));
};