From cde61b77b128f38f6570b86ef1d3c05c03f9672a Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Mon, 14 Mar 2022 11:17:24 +0800 Subject: [PATCH] feat(core): add README.md for connectors (#362) * feat(core): add README.md for connectors * feat(core): add README.md files' existence check * feat(core): fix path of README.md --- packages/core/src/connectors/aliyun-dm/README.md | 2 ++ packages/core/src/connectors/aliyun-dm/index.ts | 9 +++++++++ packages/core/src/connectors/aliyun-sms/README.md | 2 ++ packages/core/src/connectors/aliyun-sms/index.ts | 9 +++++++++ packages/core/src/connectors/facebook/README.md | 2 ++ packages/core/src/connectors/facebook/index.ts | 9 +++++++++ packages/core/src/connectors/github/README.md | 2 ++ packages/core/src/connectors/github/index.ts | 9 +++++++++ packages/core/src/connectors/google/README.md | 2 ++ packages/core/src/connectors/google/index.ts | 8 ++++++++ packages/core/src/lib/passcode.test.ts | 1 + packages/core/src/routes/connector.test.ts | 10 ++++++++++ packages/core/src/utils/mock.ts | 7 +++++++ packages/schemas/src/types/connector.ts | 1 + 14 files changed, 73 insertions(+) create mode 100644 packages/core/src/connectors/aliyun-dm/README.md create mode 100644 packages/core/src/connectors/aliyun-sms/README.md create mode 100644 packages/core/src/connectors/facebook/README.md create mode 100644 packages/core/src/connectors/github/README.md create mode 100644 packages/core/src/connectors/google/README.md diff --git a/packages/core/src/connectors/aliyun-dm/README.md b/packages/core/src/connectors/aliyun-dm/README.md new file mode 100644 index 000000000..339f0b4de --- /dev/null +++ b/packages/core/src/connectors/aliyun-dm/README.md @@ -0,0 +1,2 @@ +### Aliyun DM README +placeholder diff --git a/packages/core/src/connectors/aliyun-dm/index.ts b/packages/core/src/connectors/aliyun-dm/index.ts index 41e8eb8b3..ac593ea37 100644 --- a/packages/core/src/connectors/aliyun-dm/index.ts +++ b/packages/core/src/connectors/aliyun-dm/index.ts @@ -1,3 +1,6 @@ +import { existsSync, readFileSync } from 'fs'; +import path from 'path'; + import { z } from 'zod'; import assertThat from '@/utils/assert-that'; @@ -13,6 +16,9 @@ import { import { getConnectorConfig } from '../utilities'; import { singleSendMail } from './single-send-mail'; +// eslint-disable-next-line unicorn/prefer-module +const pathToReadmeFile = path.join(__dirname, 'README.md'); +const readmeContentFallback = 'Please check README.md file directory.'; export const metadata: ConnectorMetadata = { id: 'aliyun-dm', type: ConnectorType.Email, @@ -27,6 +33,9 @@ export const metadata: ConnectorMetadata = { 'zh-CN': '邮件推送(DirectMail)是款简单高效的电子邮件群发服务,构建在阿里云基础之上,帮您快速、精准地实现事务邮件、通知邮件和批量邮件的发送。', }, + readme: existsSync(pathToReadmeFile) + ? readFileSync(pathToReadmeFile, 'utf8') + : readmeContentFallback, }; /** diff --git a/packages/core/src/connectors/aliyun-sms/README.md b/packages/core/src/connectors/aliyun-sms/README.md new file mode 100644 index 000000000..c67a67b8a --- /dev/null +++ b/packages/core/src/connectors/aliyun-sms/README.md @@ -0,0 +1,2 @@ +### Aliyun SMS README +placeholder diff --git a/packages/core/src/connectors/aliyun-sms/index.ts b/packages/core/src/connectors/aliyun-sms/index.ts index 01ca8e02b..dbb747c9e 100644 --- a/packages/core/src/connectors/aliyun-sms/index.ts +++ b/packages/core/src/connectors/aliyun-sms/index.ts @@ -1,3 +1,6 @@ +import { existsSync, readFileSync } from 'fs'; +import path from 'path'; + import { z } from 'zod'; import assertThat from '@/utils/assert-that'; @@ -13,6 +16,9 @@ import { import { getConnectorConfig } from '../utilities'; import { sendSms } from './single-send-text'; +// eslint-disable-next-line unicorn/prefer-module +const pathToReadmeFile = path.join(__dirname, 'README.md'); +const readmeContentFallback = 'Please check README.md file directory.'; export const metadata: ConnectorMetadata = { id: 'aliyun-sms', type: ConnectorType.SMS, @@ -27,6 +33,9 @@ export const metadata: ConnectorMetadata = { 'zh-CN': '短信服务(Short Message Service)是指通过调用短信发送API,将指定短信内容发送给指定手机用户。', }, + readme: existsSync(pathToReadmeFile) + ? readFileSync(pathToReadmeFile, 'utf8') + : readmeContentFallback, }; /** diff --git a/packages/core/src/connectors/facebook/README.md b/packages/core/src/connectors/facebook/README.md new file mode 100644 index 000000000..1cd436100 --- /dev/null +++ b/packages/core/src/connectors/facebook/README.md @@ -0,0 +1,2 @@ +### FB Social Connector README +placeholder diff --git a/packages/core/src/connectors/facebook/index.ts b/packages/core/src/connectors/facebook/index.ts index d29a62f67..9b0fa9c0a 100644 --- a/packages/core/src/connectors/facebook/index.ts +++ b/packages/core/src/connectors/facebook/index.ts @@ -2,6 +2,9 @@ * Reference: Manually Build a Login Flow * https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow */ +import { existsSync, readFileSync } from 'fs'; +import path from 'path'; + import got, { RequestError as GotRequestError } from 'got'; import { stringify } from 'query-string'; import { z } from 'zod'; @@ -25,6 +28,9 @@ import { import { getConnectorConfig, getConnectorRequestTimeout } from '@/connectors/utilities'; import assertThat from '@/utils/assert-that'; +// eslint-disable-next-line unicorn/prefer-module +const pathToReadmeFile = path.join(__dirname, 'README.md'); +const readmeContentFallback = 'Please check README.md file directory.'; export const metadata: ConnectorMetadata = { id: 'facebook', type: ConnectorType.Social, @@ -38,6 +44,9 @@ export const metadata: ConnectorMetadata = { en: 'Sign In with Facebook', 'zh-CN': 'Facebook 登录', }, + readme: existsSync(pathToReadmeFile) + ? readFileSync(pathToReadmeFile, 'utf8') + : readmeContentFallback, }; const facebookConfigGuard = z.object({ diff --git a/packages/core/src/connectors/github/README.md b/packages/core/src/connectors/github/README.md new file mode 100644 index 000000000..fdb834c0a --- /dev/null +++ b/packages/core/src/connectors/github/README.md @@ -0,0 +1,2 @@ +### Github Social Connector README +placeholder diff --git a/packages/core/src/connectors/github/index.ts b/packages/core/src/connectors/github/index.ts index d23ed7e78..17e94b845 100644 --- a/packages/core/src/connectors/github/index.ts +++ b/packages/core/src/connectors/github/index.ts @@ -1,3 +1,6 @@ +import { existsSync, readFileSync } from 'fs'; +import path from 'path'; + import got, { RequestError as GotRequestError } from 'got'; import { stringify } from 'query-string'; import { z } from 'zod'; @@ -17,6 +20,9 @@ import { import { getConnectorConfig, getConnectorRequestTimeout } from '../utilities'; import { authorizationEndpoint, accessTokenEndpoint, scope, userInfoEndpoint } from './constant'; +// eslint-disable-next-line unicorn/prefer-module +const pathToReadmeFile = path.join(__dirname, 'README.md'); +const readmeContentFallback = 'Please check README.md file directory.'; export const metadata: ConnectorMetadata = { id: 'github', type: ConnectorType.Social, @@ -29,6 +35,9 @@ export const metadata: ConnectorMetadata = { en: 'Sign In with GitHub', 'zh-CN': 'GitHub登录', }, + readme: existsSync(pathToReadmeFile) + ? readFileSync(pathToReadmeFile, 'utf8') + : readmeContentFallback, }; const githubConfigGuard = z.object({ diff --git a/packages/core/src/connectors/google/README.md b/packages/core/src/connectors/google/README.md new file mode 100644 index 000000000..dcb92c2c7 --- /dev/null +++ b/packages/core/src/connectors/google/README.md @@ -0,0 +1,2 @@ +### Google Social Connector README +placeholder diff --git a/packages/core/src/connectors/google/index.ts b/packages/core/src/connectors/google/index.ts index 20a1b8d59..f279b4933 100644 --- a/packages/core/src/connectors/google/index.ts +++ b/packages/core/src/connectors/google/index.ts @@ -2,6 +2,8 @@ * The Implementation of OpenID Connect of Google Identity Platform. * https://developers.google.com/identity/protocols/oauth2/openid-connect */ +import { existsSync, readFileSync } from 'fs'; +import path from 'path'; import { conditional } from '@silverhand/essentials'; import got, { RequestError as GotRequestError } from 'got'; @@ -23,6 +25,9 @@ import { import { getConnectorConfig, getConnectorRequestTimeout } from '../utilities'; import { accessTokenEndpoint, authorizationEndpoint, scope, userInfoEndpoint } from './constant'; +// eslint-disable-next-line unicorn/prefer-module +const pathToReadmeFile = path.join(__dirname, 'README.md'); +const readmeContentFallback = 'Please check README.md file directory.'; export const metadata: ConnectorMetadata = { id: 'google', type: ConnectorType.Social, @@ -36,6 +41,9 @@ export const metadata: ConnectorMetadata = { en: 'Sign In with Google', 'zh-CN': 'Google登录', }, + readme: existsSync(pathToReadmeFile) + ? readFileSync(pathToReadmeFile, 'utf8') + : readmeContentFallback, }; const googleConfigGuard = z.object({ diff --git a/packages/core/src/lib/passcode.test.ts b/packages/core/src/lib/passcode.test.ts index 6ea12a549..f851edf2d 100644 --- a/packages/core/src/lib/passcode.test.ts +++ b/packages/core/src/lib/passcode.test.ts @@ -134,6 +134,7 @@ describe('sendPasscode', () => { name: {}, logo: '', description: {}, + readme: '', }, sendMessage, validateConfig: jest.fn(), diff --git a/packages/core/src/routes/connector.test.ts b/packages/core/src/routes/connector.test.ts index 51d0aafee..a1bde26e3 100644 --- a/packages/core/src/routes/connector.test.ts +++ b/packages/core/src/routes/connector.test.ts @@ -196,6 +196,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }; }); @@ -235,6 +236,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }; }); @@ -254,6 +256,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }); expect(response).toHaveProperty('statusCode', 200); @@ -275,6 +278,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }; }); @@ -309,6 +313,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }); expect(response).toHaveProperty('statusCode', 200); @@ -329,6 +334,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }; }); @@ -348,6 +354,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }); expect(response).toHaveProperty('statusCode', 200); @@ -406,6 +413,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, validateConfig: async (_config) => { throw new ConnectorError(ConnectorErrorCodes.InvalidConfig); @@ -433,6 +441,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, // eslint-disable-next-line @typescript-eslint/no-empty-function validateConfig: async (_config) => {}, @@ -454,6 +463,7 @@ describe('connector route', () => { name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }); expect(response).toHaveProperty('statusCode', 200); diff --git a/packages/core/src/utils/mock.ts b/packages/core/src/utils/mock.ts index 1967dd49f..71b4a4837 100644 --- a/packages/core/src/utils/mock.ts +++ b/packages/core/src/utils/mock.ts @@ -252,6 +252,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, { @@ -267,6 +268,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, { @@ -282,6 +284,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, { @@ -297,6 +300,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, { @@ -312,6 +316,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, { @@ -327,6 +332,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, { @@ -342,6 +348,7 @@ export const mockConnectorInstanceList: Array<{ name: {}, logo: './logo.png', description: {}, + readme: 'README.md', }, }, ]; diff --git a/packages/schemas/src/types/connector.ts b/packages/schemas/src/types/connector.ts index fe5484676..045c98c88 100644 --- a/packages/schemas/src/types/connector.ts +++ b/packages/schemas/src/types/connector.ts @@ -13,6 +13,7 @@ export interface ConnectorMetadata { name: Record; logo: string; description: Record; + readme: string; } export interface ConnectorDTO extends Connector { metadata: ConnectorMetadata;