mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
feat(github): validateConfig (#189)
This commit is contained in:
parent
aaa6f4dcc2
commit
872b1d6450
3 changed files with 35 additions and 2 deletions
|
@ -1,6 +1,6 @@
|
|||
import nock from 'nock';
|
||||
|
||||
import { getAccessToken, getAuthorizationUri } from '.';
|
||||
import { getAccessToken, getAuthorizationUri, validateConfig } from '.';
|
||||
import { getConnectorConfig } from '../utilities';
|
||||
import { accessTokenEndpoint, authorizationEndpoint } from './constant';
|
||||
|
||||
|
@ -36,3 +36,18 @@ describe('getAccessToken', () => {
|
|||
expect(accessToken).toEqual('access_token');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateConfig', () => {
|
||||
it('should pass on valid config', async () => {
|
||||
await expect(
|
||||
validateConfig({ clientId: 'clientId', clientSecret: 'clientSecret' })
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
it('should throw on empty config', async () => {
|
||||
// @ts-expect-error
|
||||
await expect(validateConfig()).rejects.toThrowError();
|
||||
});
|
||||
it('should throw when missing clientSecret', async () => {
|
||||
await expect(validateConfig({ clientId: 'clientId' })).rejects.toThrowError();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -3,7 +3,13 @@ import got from 'got';
|
|||
import { stringify } from 'query-string';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { ConnectorMetadata, GetAccessToken, GetAuthorizationUri } from '../types';
|
||||
import {
|
||||
ConnectorConfigError,
|
||||
ConnectorMetadata,
|
||||
GetAccessToken,
|
||||
GetAuthorizationUri,
|
||||
ValidateConfig,
|
||||
} from '../types';
|
||||
import { getConnectorConfig } from '../utilities';
|
||||
import { authorizationEndpoint, accessTokenEndpoint, scope } from './constant';
|
||||
|
||||
|
@ -28,6 +34,17 @@ const githubConfigGuard = z.object({
|
|||
|
||||
type GithubConfig = z.infer<typeof githubConfigGuard>;
|
||||
|
||||
export const validateConfig: ValidateConfig = async (config: unknown) => {
|
||||
if (!config) {
|
||||
throw new ConnectorConfigError('Missing config');
|
||||
}
|
||||
|
||||
const result = githubConfigGuard.safeParse(config);
|
||||
if (!result.success) {
|
||||
throw new ConnectorConfigError(result.error.message);
|
||||
}
|
||||
};
|
||||
|
||||
export const getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => {
|
||||
const config = await getConnectorConfig<GithubConfig>(metadata.id, metadata.type);
|
||||
return `${authorizationEndpoint}?${stringify({
|
||||
|
|
|
@ -14,6 +14,7 @@ export type ConnectorInstance = EmailConector | SocialConector;
|
|||
|
||||
export interface BaseConnector {
|
||||
metadata: ConnectorMetadata;
|
||||
validateConfig: ValidateConfig;
|
||||
}
|
||||
|
||||
export interface EmailConector extends BaseConnector {
|
||||
|
|
Loading…
Add table
Reference in a new issue