0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(core,schemas): refactor log types with zod (#2034)

This commit is contained in:
Darcy Ye 2022-09-30 11:48:12 +08:00 committed by GitHub
parent f1ca49c892
commit 753e8ebdfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 191 additions and 138 deletions

View file

@ -1,6 +1,7 @@
import { merge, Config } from '@silverhand/jest-config'; import { merge, Config } from '@silverhand/jest-config';
const config: Config.InitialOptions = merge({ const config: Config.InitialOptions = merge({
testPathIgnorePatterns: ['/core/connectors/'],
setupFilesAfterEnv: ['jest-matcher-specific-error', './jest.setup.ts'], setupFilesAfterEnv: ['jest-matcher-specific-error', './jest.setup.ts'],
globalSetup: './jest.global-setup.ts', globalSetup: './jest.global-setup.ts',
}); });

View file

@ -17,11 +17,13 @@ type SessionPayload = {
type AddLogContext = (sessionPayload: SessionPayload) => void; type AddLogContext = (sessionPayload: SessionPayload) => void;
export type WithLogContext<ContextT extends IRouterParamContext = IRouterParamContext> = export type LogContext = {
ContextT & { addLogContext: AddLogContext;
addLogContext: AddLogContext; log: MergeLog;
log: MergeLog; };
};
export type WithLogContext<ContextT extends IRouterParamContext = IRouterParamContext> = ContextT &
LogContext;
type Logger = { type Logger = {
type?: LogType; type?: LogType;

View file

@ -1,3 +1,5 @@
import { z } from 'zod';
import { Log } from '../db-entries'; import { Log } from '../db-entries';
export enum LogResult { export enum LogResult {
@ -5,122 +7,160 @@ export enum LogResult {
Error = 'Error', Error = 'Error',
} }
export type BaseLogPayload = { export const logResultGuard = z.nativeEnum(LogResult);
result?: LogResult;
error?: Record<string, unknown>;
ip?: string;
userAgent?: string;
applicationId?: string;
sessionId?: string;
};
type ArbitraryLogPayload = Record<string, unknown>; export const baseLogPayloadGuard = z.object({
result: logResultGuard.optional(),
error: z.record(z.string(), z.unknown()).optional(),
ip: z.string().optional(),
userAgent: z.string().optional(),
applicationId: z.string().optional(),
sessionId: z.string().optional(),
});
type RegisterUsernamePasswordLogPayload = ArbitraryLogPayload & { export type BaseLogPayload = z.infer<typeof baseLogPayloadGuard>;
userId?: string;
username?: string;
};
type RegisterEmailSendPasscodeLogPayload = ArbitraryLogPayload & { const arbitraryLogPayloadGuard = z.record(z.string(), z.unknown());
email?: string;
connectorId?: string;
};
type RegisterEmailLogPayload = ArbitraryLogPayload & { export type ArbitraryLogPayload = z.infer<typeof arbitraryLogPayloadGuard>;
email?: string;
code?: string;
userId?: string;
};
type RegisterSmsSendPasscodeLogPayload = { const registerUsernamePasswordLogPayloadGuard = arbitraryLogPayloadGuard.and(
phone?: string; z.object({ userId: z.string().optional(), username: z.string().optional() })
connectorId?: string; );
} & ArbitraryLogPayload;
type RegisterSmsLogPayload = ArbitraryLogPayload & { const registerEmailSendPasscodeLogPayloadGuard = arbitraryLogPayloadGuard.and(
phone?: string; z.object({ email: z.string().optional(), connectorId: z.string().optional() })
code?: string; );
userId?: string;
};
type RegisterSocialBindLogPayload = ArbitraryLogPayload & { const registerEmailLogPayloadGuard = arbitraryLogPayloadGuard.and(
connectorId?: string; z.object({
userInfo?: object; email: z.string().optional(),
userId?: string; code: z.string().optional(),
}; userId: z.string().optional(),
})
);
type RegisterSocialLogPayload = RegisterSocialBindLogPayload & { const registerSmsSendPasscodeLogPayloadGuard = arbitraryLogPayloadGuard.and(
code?: string; z.object({
state?: string; phone: z.string().optional(),
redirectUri?: string; connectorId: z.string().optional(),
redirectTo?: string; })
}; );
type SignInUsernamePasswordLogPayload = ArbitraryLogPayload & { const registerSmsLogPayloadGuard = arbitraryLogPayloadGuard.and(
userId?: string; z.object({
username?: string; phone: z.string().optional(),
}; code: z.string().optional(),
userId: z.string().optional(),
})
);
type SignInEmailSendPasscodeLogPayload = ArbitraryLogPayload & { const registerSocialBindLogPayloadGuard = arbitraryLogPayloadGuard.and(
email?: string; z.object({
connectorId?: string; connectorId: z.string().optional(),
}; userId: z.string().optional(),
userInfo: z.unknown().optional(),
})
);
type SignInEmailLogPayload = ArbitraryLogPayload & { const registerSocialLogPayloadGuard = registerSocialBindLogPayloadGuard.and(
email?: string; z.object({
code?: string; code: z.string().optional(),
userId?: string; state: z.string().optional(),
}; redirectUri: z.string().optional(),
redirectTo: z.string().optional(),
})
);
type SignInSmsSendPasscodeLogPayload = ArbitraryLogPayload & { const signInUsernamePasswordLogPayloadGuard = arbitraryLogPayloadGuard.and(
phone?: string; z.object({
connectorId?: string; userId: z.string().optional(),
}; username: z.string().optional(),
})
);
type SignInSmsLogPayload = ArbitraryLogPayload & { const signInEmailSendPasscodeLogPayloadGuard = arbitraryLogPayloadGuard.and(
phone?: string; z.object({
code?: string; email: z.string().optional(),
userId?: string; connectorId: z.string().optional(),
}; })
);
type SignInSocialBindLogPayload = ArbitraryLogPayload & { const signInEmailLogPayloadGuard = arbitraryLogPayloadGuard.and(
connectorId?: string; z.object({
userInfo?: object; email: z.string().optional(),
userId?: string; code: z.string().optional(),
}; userId: z.string().optional(),
})
);
type SignInSocialLogPayload = SignInSocialBindLogPayload & { const signInSmsSendPasscodeLogPayloadGuard = arbitraryLogPayloadGuard.and(
code?: string; z.object({
state?: string; phone: z.string().optional(),
redirectUri?: string; connectorId: z.string().optional(),
redirectTo?: string; })
}; );
type ForgotPasswordSmsSendPasscodeLogPayload = ArbitraryLogPayload & { const signInSmsLogPayloadGuard = arbitraryLogPayloadGuard.and(
phone?: string; z.object({
connectorId?: string; phone: z.string().optional(),
}; code: z.string().optional(),
userId: z.string().optional(),
})
);
type ForgotPasswordSmsLogPayload = ArbitraryLogPayload & { const signInSocialBindLogPayloadGuard = arbitraryLogPayloadGuard.and(
phone?: string; z.object({
code?: string; connectorId: z.string().optional(),
userId?: string; userId: z.string().optional(),
}; userInfo: z.unknown().optional(),
})
);
type ForgotPasswordEmailSendPasscodeLogPayload = ArbitraryLogPayload & { const signInSocialLogPayloadGuard = signInSocialBindLogPayloadGuard.and(
email?: string; z.object({
connectorId?: string; code: z.string().optional(),
}; state: z.string().optional(),
redirectUri: z.string().optional(),
redirectTo: z.string().optional(),
})
);
type ForgotPasswordEmailLogPayload = ArbitraryLogPayload & { const forgotPasswordSmsSendPasscodeLogPayloadGuard = arbitraryLogPayloadGuard.and(
email?: string; z.object({
code?: string; phone: z.string().optional(),
userId?: string; connectorId: z.string().optional(),
}; })
);
type ForgotPasswordResetLogPayload = ArbitraryLogPayload & { const forgotPasswordSmsLogPayloadGuard = arbitraryLogPayloadGuard.and(
userId?: string; z.object({
}; phone: z.string().optional(),
code: z.string().optional(),
userId: z.string().optional(),
})
);
const forgotPasswordEmailSendPasscodeLogPayloadGuard = arbitraryLogPayloadGuard.and(
z.object({
email: z.string().optional(),
connectorId: z.string().optional(),
})
);
const forgotPasswordEmailLogPayloadGuard = arbitraryLogPayloadGuard.and(
z.object({
email: z.string().optional(),
code: z.string().optional(),
userId: z.string().optional(),
})
);
const forgotPasswordResetLogPayloadGuard = arbitraryLogPayloadGuard.and(
z.object({
userId: z.string().optional(),
})
);
export enum TokenType { export enum TokenType {
AccessToken = 'AccessToken', AccessToken = 'AccessToken',
@ -128,46 +168,56 @@ export enum TokenType {
IdToken = 'IdToken', IdToken = 'IdToken',
} }
type ExchangeTokenLogPayload = ArbitraryLogPayload & { export const tokenTypeGuard = z.nativeEnum(TokenType);
userId?: string;
params?: Record<string, unknown>;
issued?: TokenType[];
scope?: string;
};
type RevokeTokenLogPayload = ArbitraryLogPayload & { const exchangeTokenLogPayloadGuard = arbitraryLogPayloadGuard.and(
userId?: string; z.object({
params?: Record<string, unknown>; userId: z.string().optional(),
grantId?: string; params: z.record(z.string(), z.unknown()).optional(),
tokenType?: TokenType; issued: tokenTypeGuard.array().optional(),
}; scope: z.string().optional(),
})
);
export type LogPayloads = { const revokeTokenLogPayloadGuard = arbitraryLogPayloadGuard.and(
RegisterUsernamePassword: RegisterUsernamePasswordLogPayload; z.object({
RegisterEmailSendPasscode: RegisterEmailSendPasscodeLogPayload; userId: z.string().optional(),
RegisterEmail: RegisterEmailLogPayload; params: z.record(z.string(), z.unknown()).optional(),
RegisterSmsSendPasscode: RegisterSmsSendPasscodeLogPayload; tokenType: tokenTypeGuard.optional(),
RegisterSms: RegisterSmsLogPayload; grantId: z.string().optional(),
RegisterSocialBind: RegisterSocialBindLogPayload; })
RegisterSocial: RegisterSocialLogPayload; );
SignInUsernamePassword: SignInUsernamePasswordLogPayload;
SignInEmailSendPasscode: SignInEmailSendPasscodeLogPayload;
SignInEmail: SignInEmailLogPayload;
SignInSmsSendPasscode: SignInSmsSendPasscodeLogPayload;
SignInSms: SignInSmsLogPayload;
SignInSocialBind: SignInSocialBindLogPayload;
SignInSocial: SignInSocialLogPayload;
ForgotPasswordSmsSendPasscode: ForgotPasswordSmsSendPasscodeLogPayload;
ForgotPasswordSms: ForgotPasswordSmsLogPayload;
ForgotPasswordEmailSendPasscode: ForgotPasswordEmailSendPasscodeLogPayload;
ForgotPasswordEmail: ForgotPasswordEmailLogPayload;
ForgotPasswordReset: ForgotPasswordResetLogPayload;
CodeExchangeToken: ExchangeTokenLogPayload;
RefreshTokenExchangeToken: ExchangeTokenLogPayload;
RevokeToken: RevokeTokenLogPayload;
};
export type LogType = keyof LogPayloads; const logPayloadsGuard = z.object({
RegisterUsernamePassword: registerUsernamePasswordLogPayloadGuard,
RegisterEmailSendPasscode: registerEmailSendPasscodeLogPayloadGuard,
RegisterEmail: registerEmailLogPayloadGuard,
RegisterSmsSendPasscode: registerSmsSendPasscodeLogPayloadGuard,
RegisterSms: registerSmsLogPayloadGuard,
RegisterSocialBind: registerSocialBindLogPayloadGuard,
RegisterSocial: registerSocialLogPayloadGuard,
SignInUsernamePassword: signInUsernamePasswordLogPayloadGuard,
SignInEmailSendPasscode: signInEmailSendPasscodeLogPayloadGuard,
SignInEmail: signInEmailLogPayloadGuard,
SignInSmsSendPasscode: signInSmsSendPasscodeLogPayloadGuard,
SignInSms: signInSmsLogPayloadGuard,
SignInSocialBind: signInSocialBindLogPayloadGuard,
SignInSocial: signInSocialLogPayloadGuard,
ForgotPasswordSmsSendPasscode: forgotPasswordSmsSendPasscodeLogPayloadGuard,
ForgotPasswordSms: forgotPasswordSmsLogPayloadGuard,
ForgotPasswordEmailSendPasscode: forgotPasswordEmailSendPasscodeLogPayloadGuard,
ForgotPasswordEmail: forgotPasswordEmailLogPayloadGuard,
ForgotPasswordReset: forgotPasswordResetLogPayloadGuard,
CodeExchangeToken: exchangeTokenLogPayloadGuard,
RefreshTokenExchangeToken: exchangeTokenLogPayloadGuard,
RevokeToken: revokeTokenLogPayloadGuard,
});
export type LogPayloads = z.infer<typeof logPayloadsGuard>;
export const logTypeGuard = logPayloadsGuard.keyof();
export type LogType = z.infer<typeof logTypeGuard>;
export type LogPayload = LogPayloads[LogType]; export type LogPayload = LogPayloads[LogType];