0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

refactor(core): remove confusing assertThat signature (#1066)

This commit is contained in:
Gao Sun 2022-06-07 16:26:04 +08:00 committed by GitHub
parent f2b44b49f9
commit 69e32cb646
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 15 deletions

View file

@ -8,6 +8,7 @@ import {
import { Optional } from '@silverhand/essentials';
import { ConnectorInstance, ConnectorType } from '@/connectors/types';
import RequestError from '@/errors/RequestError';
import assertThat from '@/utils/assert-that';
export const validateBranding = (branding: Branding) => {
@ -39,24 +40,30 @@ export const validateSignInMethods = (
if (isEnabled(signInMethods.email)) {
assertThat(
enabledConnectorInstances.some((item) => item.metadata.type === ConnectorType.Email),
'sign_in_experiences.enabled_connector_not_found',
{ type: ConnectorType.Email }
new RequestError({
code: 'sign_in_experiences.enabled_connector_not_found',
type: ConnectorType.Email,
})
);
}
if (isEnabled(signInMethods.sms)) {
assertThat(
enabledConnectorInstances.some((item) => item.metadata.type === ConnectorType.SMS),
'sign_in_experiences.enabled_connector_not_found',
{ type: ConnectorType.SMS }
new RequestError({
code: 'sign_in_experiences.enabled_connector_not_found',
type: ConnectorType.SMS,
})
);
}
if (isEnabled(signInMethods.social)) {
assertThat(
enabledConnectorInstances.some((item) => item.metadata.type === ConnectorType.Social),
'sign_in_experiences.enabled_connector_not_found',
{ type: ConnectorType.Social }
new RequestError({
code: 'sign_in_experiences.enabled_connector_not_found',
type: ConnectorType.Social,
})
);
assertThat(

View file

@ -5,15 +5,11 @@ import RequestError from '@/errors/RequestError';
export type AssertThatFunction = <E extends Error>(
value: unknown,
error: E | LogtoErrorCode,
interpolation?: Record<string, unknown>
error: E | LogtoErrorCode
) => asserts value;
const assertThat: AssertThatFunction = (value, error, interpolation): asserts value => {
assert(
value,
error instanceof Error ? error : new RequestError({ code: error, ...interpolation })
);
const assertThat: AssertThatFunction = (value, error): asserts value => {
assert(value, error instanceof Error ? error : new RequestError({ code: error }));
};
export default assertThat;

View file

@ -1,6 +1,7 @@
import { UsersPasswordEncryptionMethod } from '@logto/schemas';
import argon2 from 'argon2';
import RequestError from '@/errors/RequestError';
import assertThat from '@/utils/assert-that';
export const encryptPassword = async (
@ -10,8 +11,7 @@ export const encryptPassword = async (
assertThat(
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
method === UsersPasswordEncryptionMethod.Argon2i,
'password.unsupported_encryption_method',
{ method }
new RequestError({ code: 'password.unsupported_encryption_method', method })
);
return argon2.hash(password, { timeCost: 10 });