Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
refactor!: use ESM (#54)
Browse files Browse the repository at this point in the history
* refactor: use ESM

* refactor: use ESM
  • Loading branch information
gao-sun authored Jan 2, 2023
1 parent d935eba commit 742fe26
Show file tree
Hide file tree
Showing 86 changed files with 1,260 additions and 791 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ node_modules

# generated files
/packages/*/package.json
/packages/*/types
/packages/*/tsconfig.*
/packages/*/jest.config.*
/packages/*/rollup.config.*

# logs
logs
Expand Down
3 changes: 2 additions & 1 deletion packages/connector-alipay-native/package.extend.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"description": "Alipay Native implementation.",
"author": "Silverhand Inc. <[email protected]>",
"dependencies": {
"dayjs": "^1.10.5"
"dayjs": "^1.10.5",
"iconv-lite": "^0.6.3"
},
"devDependencies": {
"@shopify/jest-koa-mocks": "^5.0.0"
Expand Down
8 changes: 5 additions & 3 deletions packages/connector-alipay-native/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ConnectorError, ConnectorErrorCodes } from '@logto/connector-kit';
import nock from 'nock';

import createConnector, { getAccessToken } from '.';
import { alipayEndpoint } from './constant';
import { mockedAlipayNativeConfigWithValidPrivateKey } from './mock';
import { alipayEndpoint } from './constant.js';
import createConnector, { getAccessToken } from './index.js';
import { mockedAlipayNativeConfigWithValidPrivateKey } from './mock.js';

const { jest } = import.meta;

const getConfig = jest.fn().mockResolvedValue(mockedAlipayNativeConfigWithValidPrivateKey);

Expand Down
20 changes: 12 additions & 8 deletions packages/connector-alipay-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@logto/connector-kit';
import { assert } from '@silverhand/essentials';
import dayjs from 'dayjs';
import got from 'got';
import { got } from 'got';
import { z } from 'zod';

import {
Expand All @@ -36,12 +36,16 @@ import {
timestampFormat,
invalidAccessTokenCode,
invalidAccessTokenSubCode,
} from './constant';
import type { AlipayNativeConfig, ErrorHandler } from './types';
import { alipayNativeConfigGuard, accessTokenResponseGuard, userInfoResponseGuard } from './types';
import { signingParameters } from './utils';
} from './constant.js';
import type { AlipayNativeConfig, ErrorHandler } from './types.js';
import {
alipayNativeConfigGuard,
accessTokenResponseGuard,
userInfoResponseGuard,
} from './types.js';
import { signingParameters } from './utils.js';

export type { AlipayNativeConfig } from './types';
export type { AlipayNativeConfig } from './types.js';

const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
Expand Down Expand Up @@ -71,7 +75,7 @@ export const getAccessToken = async (code: string, config: AlipayNativeConfig) =

const httpResponse = await got.post(alipayEndpoint, {
searchParams: signedSearchParameters,
timeout: defaultTimeout,
timeout: { request: defaultTimeout },
});

const result = accessTokenResponseGuard.safeParse(parseJson(httpResponse.body));
Expand Down Expand Up @@ -124,7 +128,7 @@ const getUserInfo =

const httpResponse = await got.post(alipayEndpoint, {
searchParams: signedSearchParameters,
timeout: defaultTimeout,
timeout: { request: defaultTimeout },
});

const result = userInfoResponseGuard.safeParse(parseJson(httpResponse.body));
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-alipay-native/src/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AlipayNativeConfig } from './types';
import type { AlipayNativeConfig } from './types.js';

export const mockedTimestamp = '2022-02-22 22:22:22';

Expand Down
2 changes: 1 addition & 1 deletion packages/connector-alipay-native/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';

import { alipaySigningAlgorithms } from './constant';
import { alipaySigningAlgorithms } from './constant.js';

export const alipayNativeConfigGuard = z.object({
appId: z.string().max(16),
Expand Down
8 changes: 5 additions & 3 deletions packages/connector-alipay-native/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { methodForAccessToken } from './constant';
import { methodForAccessToken } from './constant.js';
import {
mockedAlipayNativeConfigWithValidPrivateKey,
mockedAlipayNativePublicParameters,
} from './mock';
import { signingParameters } from './utils';
} from './mock.js';
import { signingParameters } from './utils.js';

const { jest } = import.meta;

const listenJSONParse = jest.spyOn(JSON, 'parse');
const listenJSONStringify = jest.spyOn(JSON, 'stringify');
Expand Down
8 changes: 4 additions & 4 deletions packages/connector-alipay-native/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as crypto from 'crypto';
import crypto from 'crypto';

import { parseJson } from '@logto/connector-kit';
import * as iconv from 'iconv-lite';
import iconv from 'iconv-lite';
import snakeCaseKeys from 'snakecase-keys';

import { alipaySigningAlgorithmMapping } from './constant';
import type { AlipayNativeConfig } from './types';
import { alipaySigningAlgorithmMapping } from './constant.js';
import type { AlipayNativeConfig } from './types.js';

export type SigningParameters = (
parameters: AlipayNativeConfig & Record<string, string | undefined>
Expand Down
3 changes: 2 additions & 1 deletion packages/connector-alipay-web/package.extend.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"description": "Alipay implementation.",
"dependencies": {
"@logto/core-kit": "1.0.0-beta.29",
"dayjs": "^1.10.5"
"dayjs": "^1.10.5",
"iconv-lite": "^0.6.3"
},
"devDependencies": {
"@shopify/jest-koa-mocks": "^5.0.0"
Expand Down
8 changes: 5 additions & 3 deletions packages/connector-alipay-web/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ConnectorError, ConnectorErrorCodes } from '@logto/connector-kit';
import nock from 'nock';

import createConnector, { getAccessToken } from '.';
import { alipayEndpoint, authorizationEndpoint } from './constant';
import { mockedAlipayConfigWithValidPrivateKey } from './mock';
import { alipayEndpoint, authorizationEndpoint } from './constant.js';
import createConnector, { getAccessToken } from './index.js';
import { mockedAlipayConfigWithValidPrivateKey } from './mock.js';

const { jest } = import.meta;

const getConfig = jest.fn().mockResolvedValue(mockedAlipayConfigWithValidPrivateKey);

Expand Down
16 changes: 8 additions & 8 deletions packages/connector-alipay-web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@logto/connector-kit';
import { assert } from '@silverhand/essentials';
import dayjs from 'dayjs';
import got from 'got';
import { got } from 'got';
import { z } from 'zod';

import {
Expand All @@ -35,12 +35,12 @@ import {
timestampFormat,
invalidAccessTokenCode,
invalidAccessTokenSubCode,
} from './constant';
import type { AlipayConfig, ErrorHandler } from './types';
import { alipayConfigGuard, accessTokenResponseGuard, userInfoResponseGuard } from './types';
import { signingParameters } from './utils';
} from './constant.js';
import type { AlipayConfig, ErrorHandler } from './types.js';
import { alipayConfigGuard, accessTokenResponseGuard, userInfoResponseGuard } from './types.js';
import { signingParameters } from './utils.js';

export type { AlipayConfig } from './types';
export type { AlipayConfig } from './types.js';

const getAuthorizationUri =
(getConfig: GetConnectorConfig): GetAuthorizationUri =>
Expand Down Expand Up @@ -76,7 +76,7 @@ export const getAccessToken = async (code: string, config: AlipayConfig) => {

const httpResponse = await got.post(alipayEndpoint, {
searchParams: signedSearchParameters,
timeout: defaultTimeout,
timeout: { request: defaultTimeout },
});

const result = accessTokenResponseGuard.safeParse(parseJson(httpResponse.body));
Expand Down Expand Up @@ -127,7 +127,7 @@ const getUserInfo =

const httpResponse = await got.post(alipayEndpoint, {
searchParams: signedSearchParameters,
timeout: defaultTimeout,
timeout: { request: defaultTimeout },
});

const { body: rawBody } = httpResponse;
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-alipay-web/src/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AlipayConfig } from './types';
import type { AlipayConfig } from './types.js';

export const mockedTimestamp = '2022-02-22 22:22:22';

Expand Down
2 changes: 1 addition & 1 deletion packages/connector-alipay-web/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fallback } from '@logto/core-kit';
import { z } from 'zod';

import { alipaySigningAlgorithms, charsetEnum, fallbackCharset } from './constant';
import { alipaySigningAlgorithms, charsetEnum, fallbackCharset } from './constant.js';

const charsetGuard = z.enum(charsetEnum);

Expand Down
8 changes: 5 additions & 3 deletions packages/connector-alipay-web/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { methodForAccessToken } from './constant';
import { mockedAlipayConfigWithValidPrivateKey, mockedAlipayPublicParameters } from './mock';
import { signingParameters } from './utils';
import { methodForAccessToken } from './constant.js';
import { mockedAlipayConfigWithValidPrivateKey, mockedAlipayPublicParameters } from './mock.js';
import { signingParameters } from './utils.js';

const { jest } = import.meta;

const listenJSONParse = jest.spyOn(JSON, 'parse');
const listenJSONStringify = jest.spyOn(JSON, 'stringify');
Expand Down
8 changes: 4 additions & 4 deletions packages/connector-alipay-web/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as crypto from 'crypto';
import crypto from 'crypto';

import { parseJson } from '@logto/connector-kit';
import * as iconv from 'iconv-lite';
import iconv from 'iconv-lite';
import snakeCaseKeys from 'snakecase-keys';

import { alipaySigningAlgorithmMapping } from './constant';
import type { AlipayConfig } from './types';
import { alipaySigningAlgorithmMapping } from './constant.js';
import type { AlipayConfig } from './types.js';

export type SigningParameters = (
parameters: AlipayConfig & Record<string, string | undefined>
Expand Down
26 changes: 13 additions & 13 deletions packages/connector-aliyun-dm/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { VerificationCodeType } from '@logto/connector-kit';

import createConnector from '.';
import { mockedConfigWithAllRequiredTemplates } from './mock';
import { singleSendMail } from './single-send-mail';
import { mockedConfigWithAllRequiredTemplates } from './mock.js';

const { jest } = import.meta;

const getConfig = jest.fn().mockResolvedValue(mockedConfigWithAllRequiredTemplates);

jest.mock('./single-send-mail', () => {
return {
singleSendMail: jest.fn(() => {
return {
body: JSON.stringify({ EnvId: 'env-id', RequestId: 'request-id' }),
statusCode: 200,
};
}),
};
});
const singleSendMail = jest.fn(() => ({
body: JSON.stringify({ EnvId: 'env-id', RequestId: 'request-id' }),
statusCode: 200,
}));

jest.unstable_mockModule('./single-send-mail.js', () => ({
singleSendMail,
}));

const { default: createConnector } = await import('./index.js');

describe('sendMessage()', () => {
afterEach(() => {
Expand Down
12 changes: 8 additions & 4 deletions packages/connector-aliyun-dm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import {
import { assert } from '@silverhand/essentials';
import { HTTPError } from 'got';

import { defaultMetadata } from './constant';
import { singleSendMail } from './single-send-mail';
import type { AliyunDmConfig } from './types';
import { aliyunDmConfigGuard, sendEmailResponseGuard, sendMailErrorResponseGuard } from './types';
import { defaultMetadata } from './constant.js';
import { singleSendMail } from './single-send-mail.js';
import type { AliyunDmConfig } from './types.js';
import {
aliyunDmConfigGuard,
sendEmailResponseGuard,
sendMailErrorResponseGuard,
} from './types.js';

const sendMessage =
(getConfig: GetConnectorConfig): SendMessageFunction =>
Expand Down
15 changes: 11 additions & 4 deletions packages/connector-aliyun-dm/src/single-send-mail.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { singleSendMail } from './single-send-mail';
import { request } from './utils';
const { jest } = import.meta;

jest.mock('./utils');
const request = jest.fn();

jest.unstable_mockModule('./utils.js', () => ({
request,
}));

const { singleSendMail } = await import('./single-send-mail.js');

describe('singleSendMail', () => {
it('should call request with action SingleSendMail', async () => {
Expand All @@ -18,8 +23,10 @@ describe('singleSendMail', () => {
},
'<access-key-secret>'
);
const calledData = (request as jest.MockedFunction<typeof request>).mock.calls[0];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const calledData = request.mock.calls[0];
expect(calledData).not.toBeUndefined();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const payload = calledData?.[1];
expect(payload).toHaveProperty('Action', 'SingleSendMail');
});
Expand Down
6 changes: 3 additions & 3 deletions packages/connector-aliyun-dm/src/single-send-mail.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { endpoint, staticConfigs } from './constant';
import type { PublicParameters, SingleSendMail } from './types';
import { request } from './utils';
import { endpoint, staticConfigs } from './constant.js';
import type { PublicParameters, SingleSendMail } from './types.js';
import { request } from './utils.js';

/**
* @doc https://help.aliyun.com/document_detail/29444.html
Expand Down
4 changes: 2 additions & 2 deletions packages/connector-aliyun-dm/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockedConfig, mockedConfigWithAllRequiredTemplates } from './mock';
import { aliyunDmConfigGuard } from './types';
import { mockedConfig, mockedConfigWithAllRequiredTemplates } from './mock.js';
import { aliyunDmConfigGuard } from './types.js';

describe('aliyunDmConfigGuard', () => {
it('throws when required templates not provided', () => {
Expand Down
16 changes: 11 additions & 5 deletions packages/connector-aliyun-dm/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import got from 'got';
import { mockedParameters } from './mock.js';

import { mockedParameters } from './mock';
import { getSignature, request } from './utils';
const { jest } = import.meta;

jest.mock('got');
const post = jest.fn();

jest.unstable_mockModule('got', () => ({
got: { post },
}));

const { getSignature, request } = await import('./utils.js');

describe('getSignature', () => {
it('should get valid signature', () => {
Expand All @@ -21,7 +26,8 @@ describe('request', () => {
it('should call got.post with extended params', async () => {
const parameters = mockedParameters;
await request('http://test.endpoint.com', parameters, 'testsecret');
const calledData = (got.post as jest.MockedFunction<typeof got.post>).mock.calls[0];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const calledData = post.mock.calls[0];
expect(calledData).not.toBeUndefined();
const payload = calledData?.[0].form as URLSearchParams;
expect(payload.get('AccessKeyId')).toEqual('testid');
Expand Down
4 changes: 2 additions & 2 deletions packages/connector-aliyun-dm/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createHmac } from 'crypto';

import got from 'got';
import { got } from 'got';

import type { PublicParameters } from './types';
import type { PublicParameters } from './types.js';

// Aliyun has special escape rules.
// https://help.aliyun.com/document_detail/29442.html
Expand Down
Loading

0 comments on commit 742fe26

Please sign in to comment.