0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

refactor(core): simplify check-and-throw-ConnectorError with assertThat (#333)

This commit is contained in:
IceHe.xyz 2022-03-07 16:08:05 +08:00 committed by GitHub
parent b14c30beca
commit fc113fba99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 13 deletions

View file

@ -1,5 +1,7 @@
import { z } from 'zod';
import assertThat from '@/utils/assert-that';
import {
ConnectorError,
ConnectorErrorCodes,
@ -61,12 +63,13 @@ export const sendMessage: EmailSendMessageFunction = async (address, type, data)
const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config;
const template = templates.find((template) => template.usageType === type);
if (!template) {
throw new ConnectorError(
assertThat(
template,
new ConnectorError(
ConnectorErrorCodes.TemplateNotFound,
`Cannot find template for type: ${type}`
);
}
)
);
return singleSendMail(
{

View file

@ -1,5 +1,7 @@
import { z } from 'zod';
import assertThat from '@/utils/assert-that';
import {
ConnectorError,
ConnectorErrorCodes,
@ -84,9 +86,10 @@ export const sendMessage: SmsSendMessageFunction = async (phone, type, { code })
const { accessKeyId, accessKeySecret, signName, templates } = config;
const template = templates.find(({ usageType }) => usageType === type);
if (!template) {
throw new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`);
}
assertThat(
template,
new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`)
);
return sendSms(
{

View file

@ -2,6 +2,8 @@ import got, { RequestError as GotRequestError } from 'got';
import { stringify } from 'query-string';
import { z } from 'zod';
import assertThat from '@/utils/assert-that';
import {
ConnectorMetadata,
GetAccessToken,
@ -78,9 +80,7 @@ export const getAccessToken: GetAccessToken = async (code) => {
})
.json<AccessTokenResponse>();
if (!accessToken) {
throw new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid);
}
assertThat(accessToken, new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
return accessToken;
};

View file

@ -8,6 +8,8 @@ import got, { RequestError as GotRequestError } from 'got';
import { stringify } from 'query-string';
import { z } from 'zod';
import assertThat from '@/utils/assert-that';
import {
ConnectorError,
ConnectorErrorCodes,
@ -88,9 +90,7 @@ export const getAccessToken: GetAccessToken = async (code, redirectUri) => {
})
.json<AccessTokenResponse>();
if (!accessToken) {
throw new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid);
}
assertThat(accessToken, new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
return accessToken;
};