From 3752b867978525b361fba0d2c391e5dfbb092b9c Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Thu, 3 Aug 2023 13:28:23 +0800 Subject: [PATCH] refactor: validate config generic (#4286) --- .../connectors/connector-alipay-native/src/index.ts | 4 ++-- packages/connectors/connector-alipay-web/src/index.ts | 4 ++-- packages/connectors/connector-aliyun-dm/src/index.ts | 3 +-- packages/connectors/connector-aliyun-sms/src/index.ts | 4 ++-- packages/connectors/connector-apple/src/index.ts | 5 ++--- packages/connectors/connector-aws-ses/src/index.ts | 3 +-- packages/connectors/connector-azuread/src/index.ts | 4 ++-- packages/connectors/connector-discord/src/index.ts | 4 ++-- packages/connectors/connector-facebook/src/index.ts | 6 +++--- packages/connectors/connector-feishu-web/src/index.ts | 5 ++--- packages/connectors/connector-github/src/index.ts | 4 ++-- packages/connectors/connector-google/src/index.ts | 4 ++-- packages/connectors/connector-kakao/src/index.ts | 4 ++-- packages/connectors/connector-logto-email/src/index.ts | 5 ++--- packages/connectors/connector-logto-sms/src/index.ts | 3 +-- .../connector-logto-social-demo/src/index.ts | 3 +-- packages/connectors/connector-mailgun/src/index.ts | 9 ++------- .../connector-mock-email-alternative/src/index.ts | 3 +-- packages/connectors/connector-mock-email/src/index.ts | 3 +-- packages/connectors/connector-mock-sms/src/index.ts | 3 +-- packages/connectors/connector-naver/src/index.ts | 4 ++-- packages/connectors/connector-oauth2/src/index.ts | 5 ++--- packages/connectors/connector-oidc/src/index.ts | 5 ++--- packages/connectors/connector-saml/src/index.ts | 7 +++---- .../connectors/connector-sendgrid-email/src/index.ts | 10 ++-------- packages/connectors/connector-smsaero/src/index.ts | 4 ++-- packages/connectors/connector-smtp/src/index.ts | 3 +-- packages/connectors/connector-tencent-sms/src/index.ts | 3 +-- packages/connectors/connector-twilio-sms/src/index.ts | 4 ++-- .../connectors/connector-wechat-native/src/index.ts | 4 ++-- packages/connectors/connector-wechat-web/src/index.ts | 4 ++-- packages/toolkit/connector-kit/src/index.test.ts | 4 ++-- packages/toolkit/connector-kit/src/index.ts | 7 +++++-- 33 files changed, 62 insertions(+), 85 deletions(-) diff --git a/packages/connectors/connector-alipay-native/src/index.ts b/packages/connectors/connector-alipay-native/src/index.ts index 4c919297b..fa412d68f 100644 --- a/packages/connectors/connector-alipay-native/src/index.ts +++ b/packages/connectors/connector-alipay-native/src/index.ts @@ -52,7 +52,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, alipayNativeConfigGuard); + validateConfig(config, alipayNativeConfigGuard); const { appId } = config; @@ -105,7 +105,7 @@ const getUserInfo = const { auth_code } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, alipayNativeConfigGuard); + validateConfig(config, alipayNativeConfigGuard); const { accessToken } = await getAccessToken(auth_code, config); diff --git a/packages/connectors/connector-alipay-web/src/index.ts b/packages/connectors/connector-alipay-web/src/index.ts index f9c3c1ae9..0329e5a4e 100644 --- a/packages/connectors/connector-alipay-web/src/index.ts +++ b/packages/connectors/connector-alipay-web/src/index.ts @@ -47,7 +47,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, alipayConfigGuard); + validateConfig(config, alipayConfigGuard); const { appId: app_id, scope } = config; @@ -105,7 +105,7 @@ const getUserInfo = async (data) => { const { auth_code } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, alipayConfigGuard); + validateConfig(config, alipayConfigGuard); const { accessToken } = await getAccessToken(auth_code, config); diff --git a/packages/connectors/connector-aliyun-dm/src/index.ts b/packages/connectors/connector-aliyun-dm/src/index.ts index ec6a6d67e..25bb9c513 100644 --- a/packages/connectors/connector-aliyun-dm/src/index.ts +++ b/packages/connectors/connector-aliyun-dm/src/index.ts @@ -17,7 +17,6 @@ import { import { defaultMetadata } from './constant.js'; import { singleSendMail } from './single-send-mail.js'; -import type { AliyunDmConfig } from './types.js'; import { aliyunDmConfigGuard, sendEmailResponseGuard, @@ -29,7 +28,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, aliyunDmConfigGuard); + validateConfig(config, aliyunDmConfigGuard); const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config; const template = templates.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-aliyun-sms/src/index.ts b/packages/connectors/connector-aliyun-sms/src/index.ts index ebc9db7a5..be32e1ae0 100644 --- a/packages/connectors/connector-aliyun-sms/src/index.ts +++ b/packages/connectors/connector-aliyun-sms/src/index.ts @@ -17,7 +17,7 @@ import { import { defaultMetadata } from './constant.js'; import { sendSms } from './single-send-text.js'; -import type { AliyunSmsConfig, Template } from './types.js'; +import type { Template } from './types.js'; import { aliyunSmsConfigGuard, sendSmsResponseGuard } from './types.js'; const isChinaNumber = (to: string) => /^(\+86|0086|86)?\d{11}$/.test(to); @@ -35,7 +35,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, aliyunSmsConfigGuard); + validateConfig(config, aliyunSmsConfigGuard); const { accessKeyId, accessKeySecret, signName, templates } = config; const template = templates.find(({ usageType }) => usageType === type); diff --git a/packages/connectors/connector-apple/src/index.ts b/packages/connectors/connector-apple/src/index.ts index 9b82cb1ee..a65b242f9 100644 --- a/packages/connectors/connector-apple/src/index.ts +++ b/packages/connectors/connector-apple/src/index.ts @@ -17,7 +17,6 @@ import { generateStandardId } from '@logto/shared/universal'; import { createRemoteJWKSet, jwtVerify } from 'jose'; import { scope, defaultMetadata, jwksUri, issuer, authorizationEndpoint } from './constant.js'; -import type { AppleConfig } from './types.js'; import { appleConfigGuard, dataGuard } from './types.js'; const generateNonce = () => generateStandardId(); @@ -27,7 +26,7 @@ const getAuthorizationUri = async ({ state, redirectUri }, setSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, appleConfigGuard); + validateConfig(config, appleConfigGuard); const nonce = generateNonce(); @@ -63,7 +62,7 @@ const getUserInfo = } const config = await getConfig(defaultMetadata.id); - validateConfig(config, appleConfigGuard); + validateConfig(config, appleConfigGuard); const { clientId } = config; diff --git a/packages/connectors/connector-aws-ses/src/index.ts b/packages/connectors/connector-aws-ses/src/index.ts index 63736ef4f..3451e3719 100644 --- a/packages/connectors/connector-aws-ses/src/index.ts +++ b/packages/connectors/connector-aws-ses/src/index.ts @@ -16,7 +16,6 @@ import { } from '@logto/connector-kit'; import { defaultMetadata } from './constant.js'; -import type { AwsSesConfig } from './types.js'; import { awsSesConfigGuard } from './types.js'; import { makeClient, makeCommand, makeEmailContent } from './utils.js'; @@ -25,7 +24,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, awsSesConfigGuard); + validateConfig(config, awsSesConfigGuard); const { accessKeyId, accessKeySecret, region, templates } = config; const template = templates.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-azuread/src/index.ts b/packages/connectors/connector-azuread/src/index.ts index eda0a254a..91c95332d 100644 --- a/packages/connectors/connector-azuread/src/index.ts +++ b/packages/connectors/connector-azuread/src/index.ts @@ -40,7 +40,7 @@ const getAuthorizationUri = async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, azureADConfigGuard); + validateConfig(config, azureADConfigGuard); const { clientId, clientSecret, cloudInstance, tenantId } = config; const defaultAuthCodeUrlParameters: AuthorizationUrlRequest = { @@ -106,7 +106,7 @@ const getUserInfo = // Temporarily keep this as this is a refactor, which should not change the logics. const config = await getConfig(defaultMetadata.id); - validateConfig(config, azureADConfigGuard); + validateConfig(config, azureADConfigGuard); const { accessToken } = await getAccessToken(config, code, redirectUri); diff --git a/packages/connectors/connector-discord/src/index.ts b/packages/connectors/connector-discord/src/index.ts index e2144016a..224543952 100644 --- a/packages/connectors/connector-discord/src/index.ts +++ b/packages/connectors/connector-discord/src/index.ts @@ -42,7 +42,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, discordConfigGuard); + validateConfig(config, discordConfigGuard); const queryParameters = new URLSearchParams({ client_id: config.clientId, @@ -92,7 +92,7 @@ const getUserInfo = async (data) => { const { code, redirectUri } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, discordConfigGuard); + validateConfig(config, discordConfigGuard); const { accessToken } = await getAccessToken(config, { code, redirectUri }); try { diff --git a/packages/connectors/connector-facebook/src/index.ts b/packages/connectors/connector-facebook/src/index.ts index e2f96ae22..7cb622c0f 100644 --- a/packages/connectors/connector-facebook/src/index.ts +++ b/packages/connectors/connector-facebook/src/index.ts @@ -42,7 +42,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, facebookConfigGuard); + validateConfig(config, facebookConfigGuard); const queryParameters = new URLSearchParams({ client_id: config.clientId, @@ -60,7 +60,7 @@ export const getAccessToken = async ( codeObject: { code: string; redirectUri: string } ) => { const { code, redirectUri } = codeObject; - validateConfig(config, facebookConfigGuard); + validateConfig(config, facebookConfigGuard); const { clientId: client_id, clientSecret: client_secret } = config; @@ -92,7 +92,7 @@ const getUserInfo = async (data) => { const { code, redirectUri } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, facebookConfigGuard); + validateConfig(config, facebookConfigGuard); const { accessToken } = await getAccessToken(config, { code, redirectUri }); try { diff --git a/packages/connectors/connector-feishu-web/src/index.ts b/packages/connectors/connector-feishu-web/src/index.ts index f8d31839e..d5c8f36f7 100644 --- a/packages/connectors/connector-feishu-web/src/index.ts +++ b/packages/connectors/connector-feishu-web/src/index.ts @@ -22,7 +22,6 @@ import { defaultMetadata, userInfoEndpoint, } from './constant.js'; -import type { FeishuConfig } from './types.js'; import { feishuAccessTokenResponse, feishuAuthCodeGuard, @@ -49,7 +48,7 @@ export function buildAuthorizationUri( export function getAuthorizationUri(getConfig: GetConnectorConfig): GetAuthorizationUri { return async function ({ state, redirectUri }) { const config = await getConfig(defaultMetadata.id); - validateConfig(config, feishuConfigGuard); + validateConfig(config, feishuConfigGuard); const { appId } = config; @@ -127,7 +126,7 @@ export function getUserInfo(getConfig: GetConnectorConfig): GetUserInfo { return async function (data) { const { code, redirectUri } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, feishuConfigGuard); + validateConfig(config, feishuConfigGuard); const { accessToken } = await getAccessToken(code, config.appId, config.appSecret, redirectUri); diff --git a/packages/connectors/connector-github/src/index.ts b/packages/connectors/connector-github/src/index.ts index 056132f82..547ce3964 100644 --- a/packages/connectors/connector-github/src/index.ts +++ b/packages/connectors/connector-github/src/index.ts @@ -38,7 +38,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, githubConfigGuard); + validateConfig(config, githubConfigGuard); const queryParameters = new URLSearchParams({ client_id: config.clientId, redirect_uri: redirectUri, @@ -107,7 +107,7 @@ const getUserInfo = async (data) => { const { code } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, githubConfigGuard); + validateConfig(config, githubConfigGuard); const { accessToken } = await getAccessToken(config, { code }); try { diff --git a/packages/connectors/connector-google/src/index.ts b/packages/connectors/connector-google/src/index.ts index df2400b38..db6a6ad09 100644 --- a/packages/connectors/connector-google/src/index.ts +++ b/packages/connectors/connector-google/src/index.ts @@ -40,7 +40,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, googleConfigGuard); + validateConfig(config, googleConfigGuard); const queryParameters = new URLSearchParams({ client_id: config.clientId, @@ -91,7 +91,7 @@ const getUserInfo = async (data) => { const { code, redirectUri } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, googleConfigGuard); + validateConfig(config, googleConfigGuard); const { accessToken } = await getAccessToken(config, { code, redirectUri }); try { diff --git a/packages/connectors/connector-kakao/src/index.ts b/packages/connectors/connector-kakao/src/index.ts index 65dc0fa2b..1c8dbf726 100644 --- a/packages/connectors/connector-kakao/src/index.ts +++ b/packages/connectors/connector-kakao/src/index.ts @@ -39,7 +39,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, kakaoConfigGuard); + validateConfig(config, kakaoConfigGuard); const queryParameters = new URLSearchParams({ client_id: config.clientId, @@ -89,7 +89,7 @@ const getUserInfo = async (data) => { const { code, redirectUri } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, kakaoConfigGuard); + validateConfig(config, kakaoConfigGuard); const { accessToken } = await getAccessToken(config, { code, redirectUri }); try { diff --git a/packages/connectors/connector-logto-email/src/index.ts b/packages/connectors/connector-logto-email/src/index.ts index daabec401..31ac686f4 100644 --- a/packages/connectors/connector-logto-email/src/index.ts +++ b/packages/connectors/connector-logto-email/src/index.ts @@ -19,14 +19,13 @@ import { import { defaultMetadata, defaultTimeout, emailEndpoint, usageEndpoint } from './constant.js'; import { grantAccessToken } from './grant-access-token.js'; -import type { LogtoEmailConfig } from './types.js'; import { logtoEmailConfigGuard } from './types.js'; const sendMessage = (getConfig: GetConnectorConfig): SendMessageFunction => async (data, inputConfig) => { const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, logtoEmailConfigGuard); + validateConfig(config, logtoEmailConfigGuard); const { endpoint, @@ -76,7 +75,7 @@ const getUsage = (getConfig: GetConnectorConfig): GetUsageFunction => async (startFrom?: Date) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, logtoEmailConfigGuard); + validateConfig(config, logtoEmailConfigGuard); const { endpoint, tokenEndpoint, appId, appSecret, resource } = config; diff --git a/packages/connectors/connector-logto-sms/src/index.ts b/packages/connectors/connector-logto-sms/src/index.ts index 14913ab70..e8dcb8bd6 100644 --- a/packages/connectors/connector-logto-sms/src/index.ts +++ b/packages/connectors/connector-logto-sms/src/index.ts @@ -10,14 +10,13 @@ import { ConnectorType, validateConfig } from '@logto/connector-kit'; import { defaultMetadata, defaultTimeout, smsEndpoint } from './constant.js'; import { grantAccessToken } from './grant-access-token.js'; -import type { LogtoSmsConfig } from './types.js'; import { logtoSmsConfigGuard } from './types.js'; const sendMessage = (getConfig: GetConnectorConfig): SendMessageFunction => async (data, inputConfig) => { const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, logtoSmsConfigGuard); + validateConfig(config, logtoSmsConfigGuard); const { endpoint, tokenEndpoint, appId, appSecret, resource } = config; diff --git a/packages/connectors/connector-logto-social-demo/src/index.ts b/packages/connectors/connector-logto-social-demo/src/index.ts index 0bf42c412..89b248208 100644 --- a/packages/connectors/connector-logto-social-demo/src/index.ts +++ b/packages/connectors/connector-logto-social-demo/src/index.ts @@ -7,14 +7,13 @@ import type { import { validateConfig, ConnectorType } from '@logto/connector-kit'; import { defaultMetadata, getProviderConfigs } from './constant.js'; -import type { SocialDemoConfig } from './types.js'; import { socialDemoConfigGuard } from './types.js'; const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, socialDemoConfigGuard); + validateConfig(config, socialDemoConfigGuard); const { provider, clientId, redirectUri } = config; diff --git a/packages/connectors/connector-mailgun/src/index.ts b/packages/connectors/connector-mailgun/src/index.ts index 4ceaae82e..55ee7d139 100644 --- a/packages/connectors/connector-mailgun/src/index.ts +++ b/packages/connectors/connector-mailgun/src/index.ts @@ -15,12 +15,7 @@ import { } from '@logto/connector-kit'; import { defaultMetadata } from './constant.js'; -import { - type DeliveryConfig, - mailgunConfigGuard, - supportTemplateGuard, - type MailgunConfig, -} from './types.js'; +import { type DeliveryConfig, mailgunConfigGuard, supportTemplateGuard } from './types.js'; const removeUndefinedKeys = (object: Record) => Object.fromEntries(Object.entries(object).filter(([, value]) => value !== undefined)); @@ -52,7 +47,7 @@ const getDataFromDeliveryConfig = ( const sendMessage = (getConfig: GetConnectorConfig): SendMessageFunction => { return async ({ to, type: typeInput, payload: { code } }, inputConfig) => { const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, mailgunConfigGuard); + validateConfig(config, mailgunConfigGuard); const { domain, apiKey, from, deliveries } = config; const type = supportTemplateGuard.safeParse(typeInput); diff --git a/packages/connectors/connector-mock-email-alternative/src/index.ts b/packages/connectors/connector-mock-email-alternative/src/index.ts index 07f52cd40..e764c84f1 100644 --- a/packages/connectors/connector-mock-email-alternative/src/index.ts +++ b/packages/connectors/connector-mock-email-alternative/src/index.ts @@ -16,7 +16,6 @@ import { } from '@logto/connector-kit'; import { defaultMetadata } from './constant.js'; -import type { MockMailConfig } from './types.js'; import { mockMailConfigGuard } from './types.js'; const sendMessage = @@ -24,7 +23,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, mockMailConfigGuard); + validateConfig(config, mockMailConfigGuard); const { templates } = config; const template = templates?.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-mock-email/src/index.ts b/packages/connectors/connector-mock-email/src/index.ts index d8e340856..794d7a1df 100644 --- a/packages/connectors/connector-mock-email/src/index.ts +++ b/packages/connectors/connector-mock-email/src/index.ts @@ -16,7 +16,6 @@ import { } from '@logto/connector-kit'; import { defaultMetadata } from './constant.js'; -import type { MockMailConfig } from './types.js'; import { mockMailConfigGuard } from './types.js'; const sendMessage = @@ -24,7 +23,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, mockMailConfigGuard); + validateConfig(config, mockMailConfigGuard); const { templates } = config; const template = templates.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-mock-sms/src/index.ts b/packages/connectors/connector-mock-sms/src/index.ts index dc0d2f447..88ec1b865 100644 --- a/packages/connectors/connector-mock-sms/src/index.ts +++ b/packages/connectors/connector-mock-sms/src/index.ts @@ -16,7 +16,6 @@ import { } from '@logto/connector-kit'; import { defaultMetadata } from './constant.js'; -import type { MockSmsConfig } from './types.js'; import { mockSmsConfigGuard } from './types.js'; const sendMessage = @@ -24,7 +23,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, mockSmsConfigGuard); + validateConfig(config, mockSmsConfigGuard); const { templates } = config; const template = templates.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-naver/src/index.ts b/packages/connectors/connector-naver/src/index.ts index f46d958f2..0a85a9c31 100644 --- a/packages/connectors/connector-naver/src/index.ts +++ b/packages/connectors/connector-naver/src/index.ts @@ -39,7 +39,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, naverConfigGuard); + validateConfig(config, naverConfigGuard); const queryParameters = new URLSearchParams({ client_id: config.clientId, @@ -89,7 +89,7 @@ const getUserInfo = async (data) => { const { code, redirectUri } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, naverConfigGuard); + validateConfig(config, naverConfigGuard); const { accessToken } = await getAccessToken(config, { code, redirectUri }); try { diff --git a/packages/connectors/connector-oauth2/src/index.ts b/packages/connectors/connector-oauth2/src/index.ts index 821c53bec..4d952dc58 100644 --- a/packages/connectors/connector-oauth2/src/index.ts +++ b/packages/connectors/connector-oauth2/src/index.ts @@ -16,7 +16,6 @@ import { } from '@logto/connector-kit'; import { defaultMetadata, defaultTimeout } from './constant.js'; -import type { OauthConfig } from './types.js'; import { oauthConfigGuard } from './types.js'; import { userProfileMapping, getAccessToken } from './utils.js'; @@ -27,7 +26,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }, setSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, oauthConfigGuard); + validateConfig(config, oauthConfigGuard); const parsedConfig = oauthConfigGuard.parse(config); const { customConfig, ...rest } = parsedConfig; @@ -52,7 +51,7 @@ const getUserInfo = (getConfig: GetConnectorConfig): GetUserInfo => async (data, getSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, oauthConfigGuard); + validateConfig(config, oauthConfigGuard); const parsedConfig = oauthConfigGuard.parse(config); const { redirectUri } = await getSession(); diff --git a/packages/connectors/connector-oidc/src/index.ts b/packages/connectors/connector-oidc/src/index.ts index 3ccb58bbe..ae8e6d749 100644 --- a/packages/connectors/connector-oidc/src/index.ts +++ b/packages/connectors/connector-oidc/src/index.ts @@ -19,7 +19,6 @@ import { generateStandardId } from '@logto/shared/universal'; import { createRemoteJWKSet, jwtVerify } from 'jose'; import { defaultMetadata } from './constant.js'; -import type { OidcConfig } from './types.js'; import { idTokenProfileStandardClaimsGuard, oidcConfigGuard } from './types.js'; import { getIdToken } from './utils.js'; @@ -29,7 +28,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }, setSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, oidcConfigGuard); + validateConfig(config, oidcConfigGuard); const parsedConfig = oidcConfigGuard.parse(config); const nonce = generateNonce(); @@ -62,7 +61,7 @@ const getUserInfo = (getConfig: GetConnectorConfig): GetUserInfo => async (data, getSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, oidcConfigGuard); + validateConfig(config, oidcConfigGuard); const parsedConfig = oidcConfigGuard.parse(config); assert( diff --git a/packages/connectors/connector-saml/src/index.ts b/packages/connectors/connector-saml/src/index.ts index 24dab3c38..55ed75c89 100644 --- a/packages/connectors/connector-saml/src/index.ts +++ b/packages/connectors/connector-saml/src/index.ts @@ -19,14 +19,13 @@ import * as saml from 'samlify'; import { defaultMetadata } from './constant.js'; import { samlConfigGuard } from './types.js'; -import type { SamlConfig } from './types.js'; import { samlAssertionHandler, getUserInfoFromRawUserProfile } from './utils.js'; const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri, connectorId, connectorFactoryId, jti }, setSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, samlConfigGuard); + validateConfig(config, samlConfigGuard); const { entityID, x509Certificate, @@ -99,7 +98,7 @@ export const validateSamlAssertion = (getConfig: GetConnectorConfig): ValidateSamlAssertion => async (assertion, getSession, setSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, samlConfigGuard); + validateConfig(config, samlConfigGuard); const parsedConfig = samlConfigGuard.parse(config); const connectorSession = await getSession(); @@ -137,7 +136,7 @@ const getUserInfo = (getConfig: GetConnectorConfig): GetUserInfo => async (_data, getSession) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, samlConfigGuard); + validateConfig(config, samlConfigGuard); const parsedConfig = samlConfigGuard.parse(config); const { profileMap } = parsedConfig; diff --git a/packages/connectors/connector-sendgrid-email/src/index.ts b/packages/connectors/connector-sendgrid-email/src/index.ts index a3a19985d..3b1ec7e44 100644 --- a/packages/connectors/connector-sendgrid-email/src/index.ts +++ b/packages/connectors/connector-sendgrid-email/src/index.ts @@ -16,20 +16,14 @@ import { import { defaultMetadata, endpoint } from './constant.js'; import { sendGridMailConfigGuard } from './types.js'; -import type { - EmailData, - Personalization, - Content, - PublicParameters, - SendGridMailConfig, -} from './types.js'; +import type { EmailData, Personalization, Content, PublicParameters } from './types.js'; const sendMessage = (getConfig: GetConnectorConfig): SendMessageFunction => async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, sendGridMailConfigGuard); + validateConfig(config, sendGridMailConfigGuard); const { apiKey, fromEmail, fromName, templates } = config; const template = templates.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-smsaero/src/index.ts b/packages/connectors/connector-smsaero/src/index.ts index aa1006629..66a6bbc9b 100644 --- a/packages/connectors/connector-smsaero/src/index.ts +++ b/packages/connectors/connector-smsaero/src/index.ts @@ -15,7 +15,7 @@ import { } from '@logto/connector-kit'; import { defaultMetadata, endpoint } from './constant.js'; -import type { PublicParameters, SmsAeroConfig } from './types.js'; +import type { PublicParameters } from './types.js'; import { smsAeroConfigGuard } from './types.js'; function sendMessage(getConfig: GetConnectorConfig): SendMessageFunction { @@ -23,7 +23,7 @@ function sendMessage(getConfig: GetConnectorConfig): SendMessageFunction { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, smsAeroConfigGuard); + validateConfig(config, smsAeroConfigGuard); const { email, apiKey, senderName, templates } = config; diff --git a/packages/connectors/connector-smtp/src/index.ts b/packages/connectors/connector-smtp/src/index.ts index efc5fccd2..0cc2b556b 100644 --- a/packages/connectors/connector-smtp/src/index.ts +++ b/packages/connectors/connector-smtp/src/index.ts @@ -16,7 +16,6 @@ import nodemailer from 'nodemailer'; import type SMTPTransport from 'nodemailer/lib/smtp-transport'; import { defaultMetadata } from './constant.js'; -import type { SmtpConfig } from './types.js'; import { ContextType, smtpConfigGuard } from './types.js'; const sendMessage = @@ -24,7 +23,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, smtpConfigGuard); + validateConfig(config, smtpConfigGuard); const template = config.templates.find((template) => template.usageType === type); assert( diff --git a/packages/connectors/connector-tencent-sms/src/index.ts b/packages/connectors/connector-tencent-sms/src/index.ts index 5aeef1b5c..1d6fe34cc 100644 --- a/packages/connectors/connector-tencent-sms/src/index.ts +++ b/packages/connectors/connector-tencent-sms/src/index.ts @@ -16,7 +16,6 @@ import { import { defaultMetadata } from './constant.js'; import { isSmsErrorResponse, sendSmsRequest } from './http.js'; -import type { TencentSmsConfig } from './schema.js'; import { SmsConfigGuard, tencentErrorResponse } from './schema.js'; function safeGetArray(value: Array, index: number): T { @@ -34,7 +33,7 @@ function sendMessage(getConfig: GetConnectorConfig): SendMessageFunction { return async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, SmsConfigGuard); + validateConfig(config, SmsConfigGuard); const { accessKeyId, accessKeySecret, signName, templates, sdkAppId, region } = config; const template = templates.find(({ usageType }) => usageType === type); diff --git a/packages/connectors/connector-twilio-sms/src/index.ts b/packages/connectors/connector-twilio-sms/src/index.ts index 793d477f4..a5e00b192 100644 --- a/packages/connectors/connector-twilio-sms/src/index.ts +++ b/packages/connectors/connector-twilio-sms/src/index.ts @@ -15,7 +15,7 @@ import { } from '@logto/connector-kit'; import { defaultMetadata, endpoint } from './constant.js'; -import type { TwilioSmsConfig, PublicParameters } from './types.js'; +import type { PublicParameters } from './types.js'; import { twilioSmsConfigGuard } from './types.js'; const sendMessage = @@ -23,7 +23,7 @@ const sendMessage = async (data, inputConfig) => { const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); - validateConfig(config, twilioSmsConfigGuard); + validateConfig(config, twilioSmsConfigGuard); const { accountSID, authToken, fromMessagingServiceSID, templates } = config; const template = templates.find((template) => template.usageType === type); diff --git a/packages/connectors/connector-wechat-native/src/index.ts b/packages/connectors/connector-wechat-native/src/index.ts index bb97c9ab2..fc976cc09 100644 --- a/packages/connectors/connector-wechat-native/src/index.ts +++ b/packages/connectors/connector-wechat-native/src/index.ts @@ -47,7 +47,7 @@ const getAuthorizationUri = async ({ state }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, wechatNativeConfigGuard); + validateConfig(config, wechatNativeConfigGuard); const { appId, universalLinks } = config; @@ -92,7 +92,7 @@ const getUserInfo = async (data) => { const { code } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, wechatNativeConfigGuard); + validateConfig(config, wechatNativeConfigGuard); const { accessToken, openid } = await getAccessToken(code, config); try { diff --git a/packages/connectors/connector-wechat-web/src/index.ts b/packages/connectors/connector-wechat-web/src/index.ts index bf1efdafd..7e1d96ff4 100644 --- a/packages/connectors/connector-wechat-web/src/index.ts +++ b/packages/connectors/connector-wechat-web/src/index.ts @@ -47,7 +47,7 @@ const getAuthorizationUri = (getConfig: GetConnectorConfig): GetAuthorizationUri => async ({ state, redirectUri }) => { const config = await getConfig(defaultMetadata.id); - validateConfig(config, wechatConfigGuard); + validateConfig(config, wechatConfigGuard); const { appId, scope } = config; @@ -93,7 +93,7 @@ const getUserInfo = async (data) => { const { code } = await authorizationCallbackHandler(data); const config = await getConfig(defaultMetadata.id); - validateConfig(config, wechatConfigGuard); + validateConfig(config, wechatConfigGuard); const { accessToken, openid } = await getAccessToken(code, config); try { diff --git a/packages/toolkit/connector-kit/src/index.test.ts b/packages/toolkit/connector-kit/src/index.test.ts index f275d5f3f..3a57b6521 100644 --- a/packages/toolkit/connector-kit/src/index.test.ts +++ b/packages/toolkit/connector-kit/src/index.test.ts @@ -9,7 +9,7 @@ describe('connector-kit', () => { type TestingType = z.infer; const testingConfig = { foo: 'foo', bar: 1, baz: true }; expect(() => { - validateConfig(testingConfig, testingTypeGuard); + validateConfig(testingConfig, testingTypeGuard); }).not.toThrow(); }); @@ -18,7 +18,7 @@ describe('connector-kit', () => { type TestingType = z.infer; const testingConfig = { foo: 'foo', bar: 1 }; expect(() => { - validateConfig(testingConfig, testingTypeGuard); + validateConfig(testingConfig, testingTypeGuard); }).toThrow(); }); }); diff --git a/packages/toolkit/connector-kit/src/index.ts b/packages/toolkit/connector-kit/src/index.ts index 4483ad77d..21e578f14 100644 --- a/packages/toolkit/connector-kit/src/index.ts +++ b/packages/toolkit/connector-kit/src/index.ts @@ -1,10 +1,13 @@ -import type { ZodType } from 'zod'; +import type { ZodType, ZodTypeDef } from 'zod'; import { ConnectorError, ConnectorErrorCodes } from './types.js'; export * from './types.js'; -export function validateConfig(config: unknown, guard: ZodType): asserts config is T { +export function validateConfig( + config: unknown, + guard: ZodType +): asserts config is Output { const result = guard.safeParse(config); if (!result.success) {