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

View file

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

View file

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