diff --git a/packages/cli/package.json b/packages/cli/package.json index 25f900a47..e07c74b86 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -47,7 +47,7 @@ "@logto/core-kit": "workspace:*", "@logto/schemas": "workspace:*", "@logto/shared": "workspace:*", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "chalk": "^5.0.0", "decamelize": "^6.0.0", "dotenv": "^16.0.0", diff --git a/packages/cloud/package.json b/packages/cloud/package.json index e5d2aacb5..c93a5bdf9 100644 --- a/packages/cloud/package.json +++ b/packages/cloud/package.json @@ -28,7 +28,7 @@ "@logto/core-kit": "workspace:*", "@logto/schemas": "workspace:*", "@logto/shared": "workspace:*", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "@withtyped/postgres": "^0.8.1", "@withtyped/server": "^0.8.1", "accepts": "^1.3.8", diff --git a/packages/cloud/src/libraries/services.ts b/packages/cloud/src/libraries/services.ts index 44d5524fc..9ae5a8444 100644 --- a/packages/cloud/src/libraries/services.ts +++ b/packages/cloud/src/libraries/services.ts @@ -10,7 +10,7 @@ import { validateConfig } from '@logto/connector-kit'; import { generateStandardId } from '@logto/core-kit'; import type { ServiceLogType } from '@logto/schemas'; import { adminTenantId } from '@logto/schemas'; -import { trySafe } from '@logto/shared'; +import { trySafe } from '@silverhand/essentials'; import { RequestError } from '@withtyped/server'; import type { Queries } from '#src/queries/index.js'; diff --git a/packages/cloud/src/middleware/with-auth.ts b/packages/cloud/src/middleware/with-auth.ts index 33405cf8a..8ea27b36d 100644 --- a/packages/cloud/src/middleware/with-auth.ts +++ b/packages/cloud/src/middleware/with-auth.ts @@ -1,8 +1,7 @@ import assert from 'node:assert'; import type { IncomingHttpHeaders } from 'node:http'; -import { tryThat } from '@logto/shared'; -import { appendPath } from '@silverhand/essentials'; +import { appendPath, tryThat } from '@silverhand/essentials'; import type { NextFunction, RequestContext } from '@withtyped/server'; import { RequestError } from '@withtyped/server'; import fetchRetry from 'fetch-retry'; diff --git a/packages/console/package.json b/packages/console/package.json index 4f9e5d700..0d23bdcda 100644 --- a/packages/console/package.json +++ b/packages/console/package.json @@ -35,7 +35,7 @@ "@parcel/transformer-svg-react": "2.8.3", "@silverhand/eslint-config": "2.0.1", "@silverhand/eslint-config-react": "2.0.1", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "@silverhand/ts-config": "2.0.3", "@silverhand/ts-config-react": "2.0.3", "@tsconfig/docusaurus": "^1.0.5", diff --git a/packages/core/package.json b/packages/core/package.json index fc5b8bae8..1da55b0b4 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -35,7 +35,7 @@ "@logto/phrases-ui": "workspace:*", "@logto/schemas": "workspace:*", "@logto/shared": "workspace:*", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "aws-sdk": "^2.1329.0", "chalk": "^5.0.0", "clean-deep": "^3.4.0", diff --git a/packages/core/src/libraries/hook.ts b/packages/core/src/libraries/hook.ts index d7cd82c7d..78f8aa321 100644 --- a/packages/core/src/libraries/hook.ts +++ b/packages/core/src/libraries/hook.ts @@ -6,8 +6,7 @@ import { LogResult, userInfoSelectFields, } from '@logto/schemas'; -import { trySafe } from '@logto/shared'; -import { conditional, pick } from '@silverhand/essentials'; +import { conditional, pick, trySafe } from '@silverhand/essentials'; import type { Response } from 'got'; import { got, HTTPError } from 'got'; import type Provider from 'oidc-provider'; diff --git a/packages/core/src/oidc/adapter.ts b/packages/core/src/oidc/adapter.ts index 84d725e20..986a35ceb 100644 --- a/packages/core/src/oidc/adapter.ts +++ b/packages/core/src/oidc/adapter.ts @@ -1,7 +1,6 @@ import type { CreateApplication } from '@logto/schemas'; import { ApplicationType, adminConsoleApplicationId, demoAppApplicationId } from '@logto/schemas'; -import { tryThat } from '@logto/shared'; -import { appendPath } from '@silverhand/essentials'; +import { appendPath, tryThat } from '@silverhand/essentials'; import { addSeconds } from 'date-fns'; import type { AdapterFactory, AllClientMetadata } from 'oidc-provider'; import { errors } from 'oidc-provider'; diff --git a/packages/core/src/routes/admin-user-role.ts b/packages/core/src/routes/admin-user-role.ts index 00e4617f0..1e27ac1ff 100644 --- a/packages/core/src/routes/admin-user-role.ts +++ b/packages/core/src/routes/admin-user-role.ts @@ -1,5 +1,5 @@ import { generateStandardId } from '@logto/core-kit'; -import { tryThat } from '@logto/shared'; +import { tryThat } from '@silverhand/essentials'; import { object, string } from 'zod'; import RequestError from '#src/errors/RequestError/index.js'; diff --git a/packages/core/src/routes/admin-user.ts b/packages/core/src/routes/admin-user.ts index faded7c90..e9e850a98 100644 --- a/packages/core/src/routes/admin-user.ts +++ b/packages/core/src/routes/admin-user.ts @@ -1,7 +1,6 @@ import { emailRegEx, passwordRegEx, phoneRegEx, usernameRegEx } from '@logto/core-kit'; import { arbitraryObjectGuard, userInfoSelectFields } from '@logto/schemas'; -import { tryThat } from '@logto/shared'; -import { conditional, has, pick } from '@silverhand/essentials'; +import { conditional, has, pick, tryThat } from '@silverhand/essentials'; import { boolean, literal, object, string } from 'zod'; import RequestError from '#src/errors/RequestError/index.js'; diff --git a/packages/core/src/routes/interaction/middleware/koa-interaction-hooks.ts b/packages/core/src/routes/interaction/middleware/koa-interaction-hooks.ts index 62aa74d37..2b3482d21 100644 --- a/packages/core/src/routes/interaction/middleware/koa-interaction-hooks.ts +++ b/packages/core/src/routes/interaction/middleware/koa-interaction-hooks.ts @@ -1,4 +1,4 @@ -import { trySafe } from '@logto/shared'; +import { trySafe } from '@silverhand/essentials'; import type { MiddlewareType } from 'koa'; import type { IRouterParamContext } from 'koa-router'; diff --git a/packages/core/src/routes/resource.ts b/packages/core/src/routes/resource.ts index 72ff5358e..30130c3ba 100644 --- a/packages/core/src/routes/resource.ts +++ b/packages/core/src/routes/resource.ts @@ -1,7 +1,6 @@ import { buildIdGenerator } from '@logto/core-kit'; import { Resources, Scopes } from '@logto/schemas'; -import { tryThat } from '@logto/shared'; -import { yes } from '@silverhand/essentials'; +import { tryThat, yes } from '@silverhand/essentials'; import { object, string } from 'zod'; import RequestError from '#src/errors/RequestError/index.js'; diff --git a/packages/core/src/routes/role.scope.ts b/packages/core/src/routes/role.scope.ts index b609c0785..6422f6e81 100644 --- a/packages/core/src/routes/role.scope.ts +++ b/packages/core/src/routes/role.scope.ts @@ -1,6 +1,6 @@ import { generateStandardId } from '@logto/core-kit'; import type { ScopeResponse } from '@logto/schemas'; -import { tryThat } from '@logto/shared'; +import { tryThat } from '@silverhand/essentials'; import { object, string } from 'zod'; import RequestError from '#src/errors/RequestError/index.js'; diff --git a/packages/core/src/routes/role.ts b/packages/core/src/routes/role.ts index c29fba0ac..c704d6fe2 100644 --- a/packages/core/src/routes/role.ts +++ b/packages/core/src/routes/role.ts @@ -1,8 +1,7 @@ import { generateStandardId } from '@logto/core-kit'; import type { RoleResponse } from '@logto/schemas'; import { userInfoSelectFields, Roles } from '@logto/schemas'; -import { tryThat } from '@logto/shared'; -import { pick } from '@silverhand/essentials'; +import { pick, tryThat } from '@silverhand/essentials'; import { object, string, z } from 'zod'; import RequestError from '#src/errors/RequestError/index.js'; diff --git a/packages/integration-tests/package.json b/packages/integration-tests/package.json index 7f520e847..d0343d37d 100644 --- a/packages/integration-tests/package.json +++ b/packages/integration-tests/package.json @@ -28,7 +28,7 @@ "@logto/schemas": "workspace:*", "@peculiar/webcrypto": "^1.3.3", "@silverhand/eslint-config": "2.0.1", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "@silverhand/ts-config": "2.0.3", "@types/expect-puppeteer": "^5.0.3", "@types/jest": "^29.4.0", diff --git a/packages/phrases-ui/package.json b/packages/phrases-ui/package.json index 1644c877a..3a2bbcae1 100644 --- a/packages/phrases-ui/package.json +++ b/packages/phrases-ui/package.json @@ -34,7 +34,7 @@ }, "dependencies": { "@logto/language-kit": "workspace:*", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "zod": "^3.20.2" }, "devDependencies": { diff --git a/packages/phrases/package.json b/packages/phrases/package.json index e5da8bc9b..47a0c26c5 100644 --- a/packages/phrases/package.json +++ b/packages/phrases/package.json @@ -34,7 +34,7 @@ }, "dependencies": { "@logto/language-kit": "workspace:*", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "zod": "^3.20.2" }, "devDependencies": { diff --git a/packages/schemas/package.json b/packages/schemas/package.json index b3a5cb15d..dd3c5172b 100644 --- a/packages/schemas/package.json +++ b/packages/schemas/package.json @@ -41,7 +41,7 @@ }, "devDependencies": { "@silverhand/eslint-config": "2.0.1", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "@silverhand/ts-config": "2.0.3", "@types/inquirer": "^9.0.0", "@types/jest": "^29.4.0", diff --git a/packages/shared/package.json b/packages/shared/package.json index dfce46ded..1dcce883a 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -56,7 +56,7 @@ "dependencies": { "@logto/core-kit": "workspace:*", "@logto/schemas": "workspace:*", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "chalk": "^5.0.0", "find-up": "^6.3.0", "nanoid": "^4.0.0", diff --git a/packages/shared/src/env/GlobalValues.ts b/packages/shared/src/env/GlobalValues.ts index 7abddb1ef..37c54708d 100644 --- a/packages/shared/src/env/GlobalValues.ts +++ b/packages/shared/src/env/GlobalValues.ts @@ -1,6 +1,5 @@ -import { assertEnv, getEnv, getEnvAsStringArray, yes } from '@silverhand/essentials'; +import { assertEnv, getEnv, getEnvAsStringArray, tryThat, yes } from '@silverhand/essentials'; -import { tryThat } from '../utils/index.js'; import UrlSet from './UrlSet.js'; import { throwErrorWithDsnMessage } from './throw-errors.js'; diff --git a/packages/shared/src/utils/function.test.ts b/packages/shared/src/utils/function.test.ts deleted file mode 100644 index 44a5af294..000000000 --- a/packages/shared/src/utils/function.test.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { trySafe, tryThat } from './function.js'; - -describe('tryThat()', () => { - it('should directly execute and return or throw if the function is not a Promise', () => { - expect(tryThat(() => 'foo', new Error('try'))).toStrictEqual('foo'); - expect(() => - tryThat(() => { - throw new Error('Test'); - }, new Error('try')) - ).toThrowError(new Error('try')); - expect(() => - tryThat( - () => { - throw new Error('Test'); - }, - (error) => { - throw new Error(String(error instanceof Error && error.message) + ' try'); - } - ) - ).toThrowError(new Error('Test try')); - }); - - it('should execute or unwrap a Promise and throw the error', async () => { - expect( - await tryThat( - new Promise((resolve) => { - setTimeout(() => { - resolve('bar'); - }, 0); - }), - new Error('try') - ) - ).toStrictEqual('bar'); - - await expect( - tryThat( - async () => - new Promise((resolve, reject) => { - reject(); - }), - () => { - throw new Error('try'); - } - ) - ).rejects.toStrictEqual(new Error('try')); - }); -}); - -describe('trySafe()', () => { - it('should directly execute and return if the function is not a Promise', () => { - expect(trySafe(() => 'foo')).toStrictEqual('foo'); - expect( - // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression - trySafe(() => { - throw new Error('Test'); - }) - // eslint-disable-next-line unicorn/no-useless-undefined - ).toStrictEqual(undefined); - }); - - it('should execute or unwrap a Promise and catch the error', async () => { - expect( - await trySafe( - new Promise((resolve) => { - setTimeout(() => { - resolve('bar'); - }, 0); - }) - ) - ).toStrictEqual('bar'); - - expect( - await trySafe( - async () => - new Promise((resolve, reject) => { - reject(); - }) - ) - // eslint-disable-next-line unicorn/no-useless-undefined - ).toStrictEqual(undefined); - }); -}); diff --git a/packages/shared/src/utils/function.ts b/packages/shared/src/utils/function.ts deleted file mode 100644 index bc505e37e..000000000 --- a/packages/shared/src/utils/function.ts +++ /dev/null @@ -1,54 +0,0 @@ -export const isPromise = (value: unknown): value is Promise => - value !== null && - (typeof value === 'object' || typeof value === 'function') && - 'then' in value && - typeof value.then === 'function'; - -export type TryThat = { - (exec: () => T, onError: Error | ((error: unknown) => never)): T; - ( - exec: Promise | (() => Promise), - onError: Error | ((error: unknown) => never) - ): Promise; -}; - -export const tryThat: TryThat = (exec, onError) => { - const handleError = (error: unknown) => { - if (onError instanceof Error) { - throw onError; - } - - return onError(error); - }; - - try { - const unwrapped = typeof exec === 'function' ? exec() : exec; - - return isPromise(unwrapped) - ? // eslint-disable-next-line promise/prefer-await-to-then - unwrapped.catch(handleError) - : unwrapped; - } catch (error: unknown) { - return handleError(error); - } -}; - -export type TrySafe = { - (exec: () => T): T | undefined; - (exec: Promise | (() => Promise)): Promise; -}; - -export const trySafe: TrySafe = (exec) => { - try { - const unwrapped = typeof exec === 'function' ? exec() : exec; - - return isPromise(unwrapped) - ? // eslint-disable-next-line promise/prefer-await-to-then - unwrapped.catch((error: unknown) => { - console.error('trySafe() caught error', error); - }) - : unwrapped; - } catch (error: unknown) { - console.error('trySafe() caught error', error); - } -}; diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 685466dc2..af9d1d8c9 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -1,4 +1,3 @@ -export * from './function.js'; export * from './object.js'; export { default as findPackage } from './find-package.js'; export * from './ttl-cache.js'; diff --git a/packages/toolkit/connector-kit/package.json b/packages/toolkit/connector-kit/package.json index 2c7856a6c..e3cce1734 100644 --- a/packages/toolkit/connector-kit/package.json +++ b/packages/toolkit/connector-kit/package.json @@ -33,7 +33,7 @@ }, "dependencies": { "@logto/language-kit": "workspace:*", - "@silverhand/essentials": "2.5.0" + "@silverhand/essentials": "^2.5.0" }, "optionalDependencies": { "zod": "^3.20.2" diff --git a/packages/toolkit/core-kit/package.json b/packages/toolkit/core-kit/package.json index 842a401a1..b770905ce 100644 --- a/packages/toolkit/core-kit/package.json +++ b/packages/toolkit/core-kit/package.json @@ -50,7 +50,7 @@ "@jest/types": "^29.0.3", "@silverhand/eslint-config": "2.0.1", "@silverhand/eslint-config-react": "2.0.1", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "@silverhand/ts-config": "2.0.3", "@types/color": "^3.0.3", "@types/jest": "^29.4.0", diff --git a/packages/ui/package.json b/packages/ui/package.json index b2a650790..83753f952 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -33,7 +33,7 @@ "@react-spring/web": "^9.6.1", "@silverhand/eslint-config": "2.0.1", "@silverhand/eslint-config-react": "2.0.1", - "@silverhand/essentials": "2.5.0", + "@silverhand/essentials": "^2.5.0", "@silverhand/jest-config": "1.2.2", "@silverhand/ts-config": "2.0.3", "@silverhand/ts-config-react": "2.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f59b74ed3..24966f0bd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,7 +32,7 @@ importers: '@logto/schemas': workspace:* '@logto/shared': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/inquirer': ^9.0.0 '@types/jest': ^29.4.0 @@ -116,7 +116,7 @@ importers: '@logto/schemas': workspace:* '@logto/shared': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/jest-config': ^2.0.1 '@silverhand/ts-config': 2.0.3 '@types/accepts': ^1.3.5 @@ -196,7 +196,7 @@ importers: '@parcel/transformer-svg-react': 2.8.3 '@silverhand/eslint-config': 2.0.1 '@silverhand/eslint-config-react': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@silverhand/ts-config-react': 2.0.3 '@tsconfig/docusaurus': ^1.0.5 @@ -346,7 +346,7 @@ importers: '@logto/schemas': workspace:* '@logto/shared': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/debug': ^4.1.7 '@types/etag': ^1.8.1 @@ -575,7 +575,7 @@ importers: '@logto/schemas': workspace:* '@peculiar/webcrypto': ^1.3.3 '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/expect-puppeteer': ^5.0.3 '@types/jest': ^29.4.0 @@ -627,7 +627,7 @@ importers: specifiers: '@logto/language-kit': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 eslint: ^8.34.0 lint-staged: ^13.0.0 @@ -650,7 +650,7 @@ importers: specifiers: '@logto/language-kit': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 buffer: ^5.7.1 eslint: ^8.34.0 @@ -679,7 +679,7 @@ importers: '@logto/phrases': workspace:* '@logto/phrases-ui': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/inquirer': ^9.0.0 '@types/jest': ^29.4.0 @@ -732,7 +732,7 @@ importers: '@logto/core-kit': workspace:* '@logto/schemas': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/jest': ^29.4.0 '@types/node': ^18.11.18 @@ -769,7 +769,7 @@ importers: specifiers: '@logto/language-kit': workspace:* '@silverhand/eslint-config': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/node': ^18.11.18 eslint: ^8.34.0 @@ -799,7 +799,7 @@ importers: '@logto/language-kit': workspace:* '@silverhand/eslint-config': 2.0.1 '@silverhand/eslint-config-react': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/ts-config': 2.0.3 '@types/color': ^3.0.3 '@types/jest': ^29.4.0 @@ -888,7 +888,7 @@ importers: '@react-spring/web': ^9.6.1 '@silverhand/eslint-config': 2.0.1 '@silverhand/eslint-config-react': 2.0.1 - '@silverhand/essentials': 2.5.0 + '@silverhand/essentials': ^2.5.0 '@silverhand/jest-config': 1.2.2 '@silverhand/ts-config': 2.0.3 '@silverhand/ts-config-react': 2.0.3