mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
refactor(schemas): reorg constants
This commit is contained in:
parent
c2c3faa248
commit
06e7d6ea9d
4 changed files with 41 additions and 29 deletions
|
@ -1,5 +1,5 @@
|
|||
import type { AlterationState, System } from '@logto/schemas';
|
||||
import { Systems, logtoConfigGuards, AlterationStateKey } from '@logto/schemas';
|
||||
import { systemGuards, Systems, AlterationStateKey } from '@logto/schemas';
|
||||
import { convertToIdentifiers } from '@logto/shared';
|
||||
import type { Nullable } from '@silverhand/essentials';
|
||||
import type { CommonQueryMethods, DatabaseTransactionConnection } from 'slonik';
|
||||
|
@ -31,7 +31,7 @@ export const getCurrentDatabaseAlterationTimestamp = async (pool: CommonQueryMet
|
|||
const result = await pool.maybeOne<System>(
|
||||
sql`select * from ${table} where ${fields.key}=${AlterationStateKey.AlterationState}`
|
||||
);
|
||||
const parsed = logtoConfigGuards[AlterationStateKey.AlterationState].safeParse(result?.value);
|
||||
const parsed = systemGuards[AlterationStateKey.AlterationState].safeParse(result?.value);
|
||||
|
||||
return (parsed.success && parsed.data.timestamp) || 0;
|
||||
} catch (error: unknown) {
|
||||
|
|
|
@ -10,3 +10,4 @@ export * from './scope.js';
|
|||
export * from './role.js';
|
||||
export * from './verification-code.js';
|
||||
export * from './application.js';
|
||||
export * from './system.js';
|
||||
|
|
|
@ -1,26 +1,6 @@
|
|||
import type { ZodType } from 'zod';
|
||||
import { z } from 'zod';
|
||||
|
||||
// Alteration state
|
||||
export enum AlterationStateKey {
|
||||
AlterationState = 'alterationState',
|
||||
}
|
||||
|
||||
export type AlterationState = { timestamp: number; updatedAt?: string };
|
||||
|
||||
export type AlterationStateType = {
|
||||
[AlterationStateKey.AlterationState]: AlterationState;
|
||||
};
|
||||
|
||||
export const alterationStateGuard: Readonly<{
|
||||
[key in AlterationStateKey]: ZodType<AlterationStateType[key]>;
|
||||
}> = Object.freeze({
|
||||
[AlterationStateKey.AlterationState]: z.object({
|
||||
timestamp: z.number(),
|
||||
updatedAt: z.string().optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
// Logto OIDC config
|
||||
export enum LogtoOidcConfigKey {
|
||||
PrivateKeys = 'oidc.privateKeys',
|
||||
|
@ -67,20 +47,16 @@ export const adminConsoleConfigGuard: Readonly<{
|
|||
});
|
||||
|
||||
// Summary
|
||||
export type LogtoConfigKey = AlterationStateKey | LogtoOidcConfigKey | AdminConsoleConfigKey;
|
||||
export type LogtoConfigType = AlterationStateType | LogtoOidcConfigType | AdminConsoleConfigType;
|
||||
export type LogtoConfigGuard = typeof alterationStateGuard &
|
||||
typeof logtoOidcConfigGuard &
|
||||
typeof adminConsoleConfigGuard;
|
||||
export type LogtoConfigKey = LogtoOidcConfigKey | AdminConsoleConfigKey;
|
||||
export type LogtoConfigType = LogtoOidcConfigType | AdminConsoleConfigType;
|
||||
export type LogtoConfigGuard = typeof logtoOidcConfigGuard & typeof adminConsoleConfigGuard;
|
||||
|
||||
export const logtoConfigKeys: readonly LogtoConfigKey[] = Object.freeze([
|
||||
...Object.values(AlterationStateKey),
|
||||
...Object.values(LogtoOidcConfigKey),
|
||||
...Object.values(AdminConsoleConfigKey),
|
||||
]);
|
||||
|
||||
export const logtoConfigGuards: LogtoConfigGuard = Object.freeze({
|
||||
...alterationStateGuard,
|
||||
...logtoOidcConfigGuard,
|
||||
...adminConsoleConfigGuard,
|
||||
});
|
||||
|
|
35
packages/schemas/src/types/system.ts
Normal file
35
packages/schemas/src/types/system.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import type { ZodType } from 'zod';
|
||||
import { z } from 'zod';
|
||||
|
||||
// Alteration state
|
||||
export enum AlterationStateKey {
|
||||
AlterationState = 'alterationState',
|
||||
}
|
||||
|
||||
export type AlterationState = { timestamp: number; updatedAt?: string };
|
||||
|
||||
export type AlterationStateType = {
|
||||
[AlterationStateKey.AlterationState]: AlterationState;
|
||||
};
|
||||
|
||||
export const alterationStateGuard: Readonly<{
|
||||
[key in AlterationStateKey]: ZodType<AlterationStateType[key]>;
|
||||
}> = Object.freeze({
|
||||
[AlterationStateKey.AlterationState]: z.object({
|
||||
timestamp: z.number(),
|
||||
updatedAt: z.string().optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
// Summary
|
||||
export type SystemKey = AlterationStateKey;
|
||||
export type SystemType = AlterationStateType;
|
||||
export type SystemGuard = typeof alterationStateGuard;
|
||||
|
||||
export const systemKeys: readonly SystemKey[] = Object.freeze([
|
||||
...Object.values(AlterationStateKey),
|
||||
]);
|
||||
|
||||
export const systemGuards: SystemGuard = Object.freeze({
|
||||
...alterationStateGuard,
|
||||
});
|
Loading…
Reference in a new issue