diff --git a/packages/connector-types/jest.config.ts b/packages/connector-types/jest.config.ts deleted file mode 100644 index 70b68f837..000000000 --- a/packages/connector-types/jest.config.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { Config } from '@jest/types'; - -const config: Config.InitialOptions = { - preset: 'ts-jest', - testEnvironment: 'node', - moduleNameMapper: { - '^@/(.*)$': '/src/$1', - '^jose/(.*)$': '/node_modules/jose/dist/node/cjs/$1', - }, - setupFilesAfterEnv: ['jest-matcher-specific-error'], - coveragePathIgnorePatterns: ['/node_modules/', '/build/'], - coverageReporters: ['text-summary', 'lcov'], - globals: { - 'ts-jest': { - tsconfig: 'tsconfig.test.json', - }, - }, -}; - -export default config; diff --git a/packages/connector-types/package.json b/packages/connector-types/package.json index a50ca4678..20e9c38fb 100644 --- a/packages/connector-types/package.json +++ b/packages/connector-types/package.json @@ -14,9 +14,7 @@ "build": "rm -rf lib/ && tsc --p tsconfig.build.json", "lint": "eslint --ext .ts src", "lint:report": "pnpm lint -- --format json --output-file report.json", - "prepack": "pnpm build", - "test": "jest", - "test:coverage": "jest --coverage --silent" + "prepack": "pnpm build" }, "engines": { "node": "^16.0.0" @@ -24,8 +22,7 @@ "dependencies": { "@logto/phrases": "^0.1.0", "@logto/schemas": "^0.1.0", - "got": "^11.8.2", - "jose": "^3.14.3" + "got": "^11.8.2" }, "devDependencies": { "@jest/types": "^27.5.1", diff --git a/packages/connector-types/src/aliyun.test.ts b/packages/connector-types/src/aliyun.test.ts deleted file mode 100644 index e5a2ecac3..000000000 --- a/packages/connector-types/src/aliyun.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -import got from 'got'; - -import { getSignature, request } from './aliyun'; - -jest.mock('got'); -describe('getSignature', () => { - it('should get valid signature', () => { - const parameters = { - AccessKeyId: 'testid', - AccountName: "", - Action: 'SingleSendMail', - AddressType: '1', - Format: 'XML', - HtmlBody: '4', - RegionId: 'cn-hangzhou', - ReplyToAddress: 'true', - SignatureMethod: 'HMAC-SHA1', - SignatureNonce: 'c1b2c332-4cfb-4a0f-b8cc-ebe622aa0a5c', - SignatureVersion: '1.0', - Subject: '3', - TagName: '2', - Timestamp: '2016-10-20T06:27:56Z', - ToAddress: '1@test.com', - Version: '2015-11-23', - }; - const signature = getSignature(parameters, 'testsecret', 'POST'); - expect(signature).toEqual('llJfXJjBW3OacrVgxxsITgYaYm0='); - }); -}); -describe('request', () => { - it('should call axios.post with extended params', async () => { - const parameters = { - AccessKeyId: 'testid', - AccountName: "", - Action: 'SingleSendMail', - AddressType: '1', - Format: 'XML', - HtmlBody: '4', - RegionId: 'cn-hangzhou', - ReplyToAddress: 'true', - Subject: '3', - TagName: '2', - ToAddress: '1@test.com', - Version: '2015-11-23', - SignatureMethod: 'HMAC-SHA1', - SignatureVersion: '1.0', - }; - await request('http://test.endpoint.com', parameters, 'testsecret'); - const calledData = (got.post as jest.MockedFunction).mock.calls[0]; - expect(calledData).not.toBeUndefined(); - const payload = calledData?.[0].form as URLSearchParams; - - expect(payload.get('AccessKeyId')).toEqual('testid'); - expect(payload.get('Timestamp')).not.toBeNull(); - expect(payload.get('SignatureNonce')).not.toBeNull(); - expect(payload.get('Signature')).not.toBeNull(); - }); -}); diff --git a/packages/connector-types/src/aliyun.ts b/packages/connector-types/src/aliyun.ts deleted file mode 100644 index 0936bfb7e..000000000 --- a/packages/connector-types/src/aliyun.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { createHmac } from 'crypto'; - -import { has } from '@silverhand/essentials'; -import got from 'got'; - -export type { Response } from 'got'; - -// Aliyun has special escape rules. -// https://help.aliyun.com/document_detail/29442.html -const escaper = (string_: string) => - encodeURIComponent(string_) - .replace(/\*/g, '%2A') - .replace(/'/g, '%27') - .replace(/!/g, '%21') - .replace(/"/g, '%22') - .replace(/\(/g, '%28') - .replace(/\)/g, '%29') - .replace(/\+/, '%2B'); - -export const getSignature = ( - parameters: Record, - secret: string, - method: string -) => { - const canonicalizedQuery = Object.keys(parameters) - .slice() - .sort() - .map((key) => { - const value = parameters[key]; - - return value === undefined ? '' : `${escaper(key)}=${escaper(value)}`; - }) - .filter(Boolean) - .join('&'); - const stringToSign = `${method.toUpperCase()}&${escaper('/')}&${escaper(canonicalizedQuery)}`; - - return createHmac('sha1', `${secret}&`).update(stringToSign).digest('base64'); -}; - -export interface PublicParameters { - AccessKeyId: string; - Format?: string; // 'json' or 'xml', default: 'json' - RegionId?: string; // 'cn-hangzhou' | 'ap-southeast-1' | 'ap-southeast-2' - Signature?: string; - SignatureMethod?: string; - SignatureNonce?: string; - SignatureVersion?: string; - Timestamp?: string; - Version?: string; -} - -export const request = async ( - url: string, - parameters: PublicParameters & Record, - accessKeySecret: string -) => { - const finalParameters: Record = { - ...parameters, - SignatureNonce: String(Math.random()), - Timestamp: new Date().toISOString(), - }; - const signature = getSignature(finalParameters, accessKeySecret, 'POST'); - const payload = new URLSearchParams(); - - for (const key in finalParameters) { - if (has(finalParameters, key)) { - const value = finalParameters[key]; - - if (value !== undefined) { - payload.append(key, value); - } - } - } - payload.append('Signature', signature); - - return got.post({ - url, - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - form: payload, - }); -}; diff --git a/packages/connector-types/src/index.ts b/packages/connector-types/src/index.ts index badc0550c..c87cb8ede 100644 --- a/packages/connector-types/src/index.ts +++ b/packages/connector-types/src/index.ts @@ -1,9 +1,6 @@ import { Languages } from '@logto/phrases'; -import { ArbitraryObject, Connector, ConnectorType } from '@logto/schemas'; - -import { Response } from './aliyun'; - -export * from './aliyun'; +import { ArbitraryObject, ConnectorType } from '@logto/schemas'; +import { Response } from 'got'; export interface ConnectorMetadata { id: string; @@ -15,10 +12,6 @@ export interface ConnectorMetadata { configTemplate: string; } -export interface ConnectorDTO extends Connector { - metadata: ConnectorMetadata; -} - export enum ConnectorErrorCodes { General, InsufficientRequestParameters, @@ -58,22 +51,22 @@ export type SendEmailResponse = { EnvId: string; RequestId: string }; export type SendSmsResponse = { BizId: string; Code: string; Message: string; RequestId: string }; export type EmailSendMessageFunction< - T1 extends ArbitraryObject = ArbitraryObject, - T2 = Response + ResponseBody extends ArbitraryObject = ArbitraryObject, + ResponseType = Response > = ( address: string, type: keyof EmailMessageTypes, payload: EmailMessageTypes[typeof type] -) => Promise; +) => Promise; export type SmsSendMessageFunction< - T1 extends ArbitraryObject = ArbitraryObject, - T2 = Response + ResponseBody extends ArbitraryObject = ArbitraryObject, + ResponseType = Response > = ( phone: string, type: keyof SmsMessageTypes, payload: SmsMessageTypes[typeof type] -) => Promise; +) => Promise; export interface BaseConnector { metadata: ConnectorMetadata; diff --git a/packages/connector-types/tsconfig.json b/packages/connector-types/tsconfig.json index 0d050dd14..a07f6b0e4 100644 --- a/packages/connector-types/tsconfig.json +++ b/packages/connector-types/tsconfig.json @@ -1,7 +1,4 @@ { "extends": "./tsconfig.base", - "compilerOptions": { - "types": ["jest", "jest-matcher-specific-error"] - }, - "include": ["src", "jest.config.ts"] + "include": ["src"] } diff --git a/packages/connector-types/tsconfig.test.json b/packages/connector-types/tsconfig.test.json deleted file mode 100644 index 98c16f367..000000000 --- a/packages/connector-types/tsconfig.test.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": "./tsconfig", - "compilerOptions": { - "isolatedModules": false - } -} diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 8f2ed5d60..5e8fe45e3 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -1,6 +1,6 @@ import { existsSync, readFileSync } from 'fs'; -export const getMarkdownContents = (filePath: string, fallbackContent: string): string => { +export const getFileContents = (filePath: string, fallbackContent: string): string => { if (existsSync(filePath)) { return readFileSync(filePath, 'utf8'); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 889e1934d..5963f7875 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -71,7 +71,6 @@ importers: got: ^11.8.2 jest: ^27.5.1 jest-matcher-specific-error: ^1.0.0 - jose: ^3.14.3 lint-staged: ^11.1.1 prettier: ^2.3.2 ts-jest: ^27.1.1 @@ -80,7 +79,6 @@ importers: '@logto/phrases': link:../phrases '@logto/schemas': link:../schemas got: 11.8.3 - jose: 3.20.3 devDependencies: '@jest/types': 27.5.1 '@shopify/jest-koa-mocks': 3.0.8