mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
chore: make connectorInstance.connector non-nullable (#268)
This commit is contained in:
parent
bdee44a6b9
commit
9ffe12ad0d
5 changed files with 30 additions and 11 deletions
|
@ -4,9 +4,9 @@ import { findConnectorById, hasConnector, insertConnector } from '@/queries/conn
|
|||
import * as AliyunDM from './aliyun-dm';
|
||||
import * as AliyunSMS from './aliyun-sms';
|
||||
import * as GitHub from './github';
|
||||
import { ConnectorInstance, ConnectorType, SocialConector } from './types';
|
||||
import { ConnectorInstance, ConnectorType, IConnector, SocialConectorInstance } from './types';
|
||||
|
||||
const allConnectors: ConnectorInstance[] = [AliyunDM, AliyunSMS, GitHub];
|
||||
const allConnectors: IConnector[] = [AliyunDM, AliyunSMS, GitHub];
|
||||
|
||||
export const getConnectorInstances = async (): Promise<ConnectorInstance[]> => {
|
||||
return Promise.all(
|
||||
|
@ -34,11 +34,15 @@ export const getConnectorInstanceById = async (id: string): Promise<ConnectorIns
|
|||
return { connector, ...found };
|
||||
};
|
||||
|
||||
const isSocialConnectorInstance = (connector: ConnectorInstance): connector is SocialConector => {
|
||||
const isSocialConnectorInstance = (
|
||||
connector: ConnectorInstance
|
||||
): connector is SocialConectorInstance => {
|
||||
return connector.metadata.type === ConnectorType.Social;
|
||||
};
|
||||
|
||||
export const getSocialConnectorInstanceById = async (id: string): Promise<SocialConector> => {
|
||||
export const getSocialConnectorInstanceById = async (
|
||||
id: string
|
||||
): Promise<SocialConectorInstance> => {
|
||||
const connector = await getConnectorInstanceById(id);
|
||||
|
||||
if (!isSocialConnectorInstance(connector)) {
|
||||
|
@ -57,7 +61,7 @@ export const getConnectorInstanceByType = async <T extends ConnectorInstance>(
|
|||
): Promise<T> => {
|
||||
const connectors = await getConnectorInstances();
|
||||
const connector = connectors
|
||||
.filter((connector) => connector.connector?.enabled)
|
||||
.filter((connector) => connector.connector.enabled)
|
||||
.find<T>((connector): connector is T => connector.metadata.type === type);
|
||||
|
||||
if (!connector) {
|
||||
|
|
|
@ -16,10 +16,13 @@ export interface ConnectorMetadata {
|
|||
}
|
||||
|
||||
// The name `Connector` is used for database, use `ConnectorInstance` to avoid confusing.
|
||||
export type ConnectorInstance = SmsConnector | EmailConector | SocialConector;
|
||||
export type IConnector = SmsConnector | EmailConector | SocialConector;
|
||||
export type ConnectorInstance =
|
||||
| SmsConnectorInstance
|
||||
| EmailConectorInstance
|
||||
| SocialConectorInstance;
|
||||
|
||||
export interface BaseConnector {
|
||||
connector?: Connector;
|
||||
metadata: ConnectorMetadata;
|
||||
validateConfig: ValidateConfig;
|
||||
}
|
||||
|
@ -28,16 +31,22 @@ export interface SmsConnector extends BaseConnector {
|
|||
sendMessage: SmsSendMessageFunction;
|
||||
}
|
||||
|
||||
export type SmsConnectorInstance = SmsConnector & { connector: Connector };
|
||||
|
||||
export interface EmailConector extends BaseConnector {
|
||||
sendMessage: EmailSendMessageFunction;
|
||||
}
|
||||
|
||||
export type EmailConectorInstance = EmailConector & { connector: Connector };
|
||||
|
||||
export interface SocialConector extends BaseConnector {
|
||||
getAuthorizationUri: GetAuthorizationUri;
|
||||
getAccessToken: GetAccessToken;
|
||||
getUserInfo: GetUserInfo;
|
||||
}
|
||||
|
||||
export type SocialConectorInstance = SocialConector & { connector: Connector };
|
||||
|
||||
type EmailMessageTypes = {
|
||||
SignIn: {
|
||||
code: string;
|
||||
|
|
|
@ -122,6 +122,12 @@ describe('sendPasscode', () => {
|
|||
it('should call sendPasscode with params matching', async () => {
|
||||
const sendMessage = jest.fn();
|
||||
mockedGetConnectorInstanceByType.mockResolvedValue({
|
||||
connector: {
|
||||
id: 'id',
|
||||
enabled: true,
|
||||
config: {},
|
||||
createdAt: Date.now(),
|
||||
},
|
||||
metadata: {
|
||||
id: 'id',
|
||||
type: ConnectorType.SMS,
|
||||
|
|
|
@ -2,7 +2,7 @@ import { Passcode, PasscodeType } from '@logto/schemas';
|
|||
import { customAlphabet, nanoid } from 'nanoid';
|
||||
|
||||
import { getConnectorInstanceByType } from '@/connectors';
|
||||
import { ConnectorType, EmailConector, SmsConnector } from '@/connectors/types';
|
||||
import { ConnectorType, EmailConectorInstance, SmsConnectorInstance } from '@/connectors/types';
|
||||
import RequestError from '@/errors/RequestError';
|
||||
import {
|
||||
deletePasscodesByIds,
|
||||
|
@ -44,8 +44,8 @@ export const sendPasscode = async (passcode: Passcode) => {
|
|||
}
|
||||
|
||||
const connector = passcode.email
|
||||
? await getConnectorInstanceByType<EmailConector>(ConnectorType.Email)
|
||||
: await getConnectorInstanceByType<SmsConnector>(ConnectorType.SMS);
|
||||
? await getConnectorInstanceByType<EmailConectorInstance>(ConnectorType.Email)
|
||||
: await getConnectorInstanceByType<SmsConnectorInstance>(ConnectorType.SMS);
|
||||
|
||||
return connector.sendMessage(emailOrPhone, passcode.type, {
|
||||
code: passcode.code,
|
||||
|
|
|
@ -119,7 +119,7 @@ export const assignRedirectUrlForSocial = async (
|
|||
state: string
|
||||
) => {
|
||||
const connector = await getSocialConnectorInstanceById(connectorId);
|
||||
assertThat(connector.connector?.enabled, 'connector.not_enabled');
|
||||
assertThat(connector.connector.enabled, 'connector.not_enabled');
|
||||
const redirectTo = await connector.getAuthorizationUri(connectorRedirectUrl, state);
|
||||
ctx.body = { redirectTo };
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue