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:
parent
b14c30beca
commit
fc113fba99
4 changed files with 19 additions and 13 deletions
|
@ -1,5 +1,7 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import assertThat from '@/utils/assert-that';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ConnectorError,
|
ConnectorError,
|
||||||
ConnectorErrorCodes,
|
ConnectorErrorCodes,
|
||||||
|
@ -61,12 +63,13 @@ export const sendMessage: EmailSendMessageFunction = async (address, type, data)
|
||||||
const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config;
|
const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config;
|
||||||
const template = templates.find((template) => template.usageType === type);
|
const template = templates.find((template) => template.usageType === type);
|
||||||
|
|
||||||
if (!template) {
|
assertThat(
|
||||||
throw new ConnectorError(
|
template,
|
||||||
|
new ConnectorError(
|
||||||
ConnectorErrorCodes.TemplateNotFound,
|
ConnectorErrorCodes.TemplateNotFound,
|
||||||
`Cannot find template for type: ${type}`
|
`Cannot find template for type: ${type}`
|
||||||
);
|
)
|
||||||
}
|
);
|
||||||
|
|
||||||
return singleSendMail(
|
return singleSendMail(
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import assertThat from '@/utils/assert-that';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ConnectorError,
|
ConnectorError,
|
||||||
ConnectorErrorCodes,
|
ConnectorErrorCodes,
|
||||||
|
@ -84,9 +86,10 @@ export const sendMessage: SmsSendMessageFunction = async (phone, type, { code })
|
||||||
const { accessKeyId, accessKeySecret, signName, templates } = config;
|
const { accessKeyId, accessKeySecret, signName, templates } = config;
|
||||||
const template = templates.find(({ usageType }) => usageType === type);
|
const template = templates.find(({ usageType }) => usageType === type);
|
||||||
|
|
||||||
if (!template) {
|
assertThat(
|
||||||
throw new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`);
|
template,
|
||||||
}
|
new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`)
|
||||||
|
);
|
||||||
|
|
||||||
return sendSms(
|
return sendSms(
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,6 +2,8 @@ import got, { RequestError as GotRequestError } from 'got';
|
||||||
import { stringify } from 'query-string';
|
import { stringify } from 'query-string';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import assertThat from '@/utils/assert-that';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ConnectorMetadata,
|
ConnectorMetadata,
|
||||||
GetAccessToken,
|
GetAccessToken,
|
||||||
|
@ -78,9 +80,7 @@ export const getAccessToken: GetAccessToken = async (code) => {
|
||||||
})
|
})
|
||||||
.json<AccessTokenResponse>();
|
.json<AccessTokenResponse>();
|
||||||
|
|
||||||
if (!accessToken) {
|
assertThat(accessToken, new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
|
||||||
throw new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid);
|
|
||||||
}
|
|
||||||
|
|
||||||
return accessToken;
|
return accessToken;
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,6 +8,8 @@ import got, { RequestError as GotRequestError } from 'got';
|
||||||
import { stringify } from 'query-string';
|
import { stringify } from 'query-string';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import assertThat from '@/utils/assert-that';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ConnectorError,
|
ConnectorError,
|
||||||
ConnectorErrorCodes,
|
ConnectorErrorCodes,
|
||||||
|
@ -88,9 +90,7 @@ export const getAccessToken: GetAccessToken = async (code, redirectUri) => {
|
||||||
})
|
})
|
||||||
.json<AccessTokenResponse>();
|
.json<AccessTokenResponse>();
|
||||||
|
|
||||||
if (!accessToken) {
|
assertThat(accessToken, new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid));
|
||||||
throw new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid);
|
|
||||||
}
|
|
||||||
|
|
||||||
return accessToken;
|
return accessToken;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue