0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor: validate config generic (#4286)

This commit is contained in:
Gao Sun 2023-08-03 13:28:23 +08:00 committed by GitHub
parent 5b34338484
commit 3752b86797
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 62 additions and 85 deletions

View file

@ -52,7 +52,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<AlipayNativeConfig>(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<AlipayNativeConfig>(config, alipayNativeConfigGuard);
validateConfig(config, alipayNativeConfigGuard);
const { accessToken } = await getAccessToken(auth_code, config);

View file

@ -47,7 +47,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<AlipayConfig>(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<AlipayConfig>(config, alipayConfigGuard);
validateConfig(config, alipayConfigGuard);
const { accessToken } = await getAccessToken(auth_code, config);

View file

@ -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<AliyunDmConfig>(config, aliyunDmConfigGuard);
validateConfig(config, aliyunDmConfigGuard);
const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config;
const template = templates.find((template) => template.usageType === type);

View file

@ -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<AliyunSmsConfig>(config, aliyunSmsConfigGuard);
validateConfig(config, aliyunSmsConfigGuard);
const { accessKeyId, accessKeySecret, signName, templates } = config;
const template = templates.find(({ usageType }) => usageType === type);

View file

@ -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<AppleConfig>(config, appleConfigGuard);
validateConfig(config, appleConfigGuard);
const nonce = generateNonce();
@ -63,7 +62,7 @@ const getUserInfo =
}
const config = await getConfig(defaultMetadata.id);
validateConfig<AppleConfig>(config, appleConfigGuard);
validateConfig(config, appleConfigGuard);
const { clientId } = config;

View file

@ -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<AwsSesConfig>(config, awsSesConfigGuard);
validateConfig(config, awsSesConfigGuard);
const { accessKeyId, accessKeySecret, region, templates } = config;
const template = templates.find((template) => template.usageType === type);

View file

@ -40,7 +40,7 @@ const getAuthorizationUri =
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<AzureADConfig>(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<AzureADConfig>(config, azureADConfigGuard);
validateConfig(config, azureADConfigGuard);
const { accessToken } = await getAccessToken(config, code, redirectUri);

View file

@ -42,7 +42,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<DiscordConfig>(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<DiscordConfig>(config, discordConfigGuard);
validateConfig(config, discordConfigGuard);
const { accessToken } = await getAccessToken(config, { code, redirectUri });
try {

View file

@ -42,7 +42,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<FacebookConfig>(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<FacebookConfig>(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<FacebookConfig>(config, facebookConfigGuard);
validateConfig(config, facebookConfigGuard);
const { accessToken } = await getAccessToken(config, { code, redirectUri });
try {

View file

@ -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<FeishuConfig>(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<FeishuConfig>(config, feishuConfigGuard);
validateConfig(config, feishuConfigGuard);
const { accessToken } = await getAccessToken(code, config.appId, config.appSecret, redirectUri);

View file

@ -38,7 +38,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<GithubConfig>(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<GithubConfig>(config, githubConfigGuard);
validateConfig(config, githubConfigGuard);
const { accessToken } = await getAccessToken(config, { code });
try {

View file

@ -40,7 +40,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<GoogleConfig>(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<GoogleConfig>(config, googleConfigGuard);
validateConfig(config, googleConfigGuard);
const { accessToken } = await getAccessToken(config, { code, redirectUri });
try {

View file

@ -39,7 +39,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<KakaoConfig>(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<KakaoConfig>(config, kakaoConfigGuard);
validateConfig(config, kakaoConfigGuard);
const { accessToken } = await getAccessToken(config, { code, redirectUri });
try {

View file

@ -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<LogtoEmailConfig>(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<LogtoEmailConfig>(config, logtoEmailConfigGuard);
validateConfig(config, logtoEmailConfigGuard);
const { endpoint, tokenEndpoint, appId, appSecret, resource } = config;

View file

@ -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<LogtoSmsConfig>(config, logtoSmsConfigGuard);
validateConfig(config, logtoSmsConfigGuard);
const { endpoint, tokenEndpoint, appId, appSecret, resource } = config;

View file

@ -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<SocialDemoConfig>(config, socialDemoConfigGuard);
validateConfig(config, socialDemoConfigGuard);
const { provider, clientId, redirectUri } = config;

View file

@ -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<string, unknown>) =>
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<MailgunConfig>(config, mailgunConfigGuard);
validateConfig(config, mailgunConfigGuard);
const { domain, apiKey, from, deliveries } = config;
const type = supportTemplateGuard.safeParse(typeInput);

View file

@ -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<MockMailConfig>(config, mockMailConfigGuard);
validateConfig(config, mockMailConfigGuard);
const { templates } = config;
const template = templates?.find((template) => template.usageType === type);

View file

@ -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<MockMailConfig>(config, mockMailConfigGuard);
validateConfig(config, mockMailConfigGuard);
const { templates } = config;
const template = templates.find((template) => template.usageType === type);

View file

@ -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<MockSmsConfig>(config, mockSmsConfigGuard);
validateConfig(config, mockSmsConfigGuard);
const { templates } = config;
const template = templates.find((template) => template.usageType === type);

View file

@ -39,7 +39,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<NaverConfig>(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<NaverConfig>(config, naverConfigGuard);
validateConfig(config, naverConfigGuard);
const { accessToken } = await getAccessToken(config, { code, redirectUri });
try {

View file

@ -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<OauthConfig>(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<OauthConfig>(config, oauthConfigGuard);
validateConfig(config, oauthConfigGuard);
const parsedConfig = oauthConfigGuard.parse(config);
const { redirectUri } = await getSession();

View file

@ -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<OidcConfig>(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<OidcConfig>(config, oidcConfigGuard);
validateConfig(config, oidcConfigGuard);
const parsedConfig = oidcConfigGuard.parse(config);
assert(

View file

@ -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<SamlConfig>(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<SamlConfig>(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<SamlConfig>(config, samlConfigGuard);
validateConfig(config, samlConfigGuard);
const parsedConfig = samlConfigGuard.parse(config);
const { profileMap } = parsedConfig;

View file

@ -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<SendGridMailConfig>(config, sendGridMailConfigGuard);
validateConfig(config, sendGridMailConfigGuard);
const { apiKey, fromEmail, fromName, templates } = config;
const template = templates.find((template) => template.usageType === type);

View file

@ -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<SmsAeroConfig>(config, smsAeroConfigGuard);
validateConfig(config, smsAeroConfigGuard);
const { email, apiKey, senderName, templates } = config;

View file

@ -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<SmtpConfig>(config, smtpConfigGuard);
validateConfig(config, smtpConfigGuard);
const template = config.templates.find((template) => template.usageType === type);
assert(

View file

@ -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<T>(value: Array<T | undefined>, 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<TencentSmsConfig>(config, SmsConfigGuard);
validateConfig(config, SmsConfigGuard);
const { accessKeyId, accessKeySecret, signName, templates, sdkAppId, region } = config;
const template = templates.find(({ usageType }) => usageType === type);

View file

@ -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<TwilioSmsConfig>(config, twilioSmsConfigGuard);
validateConfig(config, twilioSmsConfigGuard);
const { accountSID, authToken, fromMessagingServiceSID, templates } = config;
const template = templates.find((template) => template.usageType === type);

View file

@ -47,7 +47,7 @@ const getAuthorizationUri =
async ({ state }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<WechatNativeConfig>(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<WechatNativeConfig>(config, wechatNativeConfigGuard);
validateConfig(config, wechatNativeConfigGuard);
const { accessToken, openid } = await getAccessToken(code, config);
try {

View file

@ -47,7 +47,7 @@ const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
async ({ state, redirectUri }) => {
const config = await getConfig(defaultMetadata.id);
validateConfig<WechatConfig>(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<WechatConfig>(config, wechatConfigGuard);
validateConfig(config, wechatConfigGuard);
const { accessToken, openid } = await getAccessToken(code, config);
try {

View file

@ -9,7 +9,7 @@ describe('connector-kit', () => {
type TestingType = z.infer<typeof testingTypeGuard>;
const testingConfig = { foo: 'foo', bar: 1, baz: true };
expect(() => {
validateConfig<TestingType>(testingConfig, testingTypeGuard);
validateConfig(testingConfig, testingTypeGuard);
}).not.toThrow();
});
@ -18,7 +18,7 @@ describe('connector-kit', () => {
type TestingType = z.infer<typeof testingTypeGuard>;
const testingConfig = { foo: 'foo', bar: 1 };
expect(() => {
validateConfig<TestingType>(testingConfig, testingTypeGuard);
validateConfig(testingConfig, testingTypeGuard);
}).toThrow();
});
});

View file

@ -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<T>(config: unknown, guard: ZodType): asserts config is T {
export function validateConfig<Output, Input = Output>(
config: unknown,
guard: ZodType<Output, ZodTypeDef, Input>
): asserts config is Output {
const result = guard.safeParse(config);
if (!result.success) {