mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor: use essentials tryThat and trySafe
This commit is contained in:
parent
fa3c13a68d
commit
169c4450eb
27 changed files with 35 additions and 179 deletions
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@logto/language-kit": "workspace:*",
|
||||
"@silverhand/essentials": "2.5.0",
|
||||
"@silverhand/essentials": "^2.5.0",
|
||||
"zod": "^3.20.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@logto/language-kit": "workspace:*",
|
||||
"@silverhand/essentials": "2.5.0",
|
||||
"@silverhand/essentials": "^2.5.0",
|
||||
"zod": "^3.20.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
3
packages/shared/src/env/GlobalValues.ts
vendored
3
packages/shared/src/env/GlobalValues.ts
vendored
|
@ -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';
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -1,54 +0,0 @@
|
|||
export const isPromise = (value: unknown): value is Promise<unknown> =>
|
||||
value !== null &&
|
||||
(typeof value === 'object' || typeof value === 'function') &&
|
||||
'then' in value &&
|
||||
typeof value.then === 'function';
|
||||
|
||||
export type TryThat = {
|
||||
<T>(exec: () => T, onError: Error | ((error: unknown) => never)): T;
|
||||
<T>(
|
||||
exec: Promise<T> | (() => Promise<T>),
|
||||
onError: Error | ((error: unknown) => never)
|
||||
): Promise<T>;
|
||||
};
|
||||
|
||||
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 = {
|
||||
<T>(exec: () => T): T | undefined;
|
||||
<T>(exec: Promise<T> | (() => Promise<T>)): Promise<T | undefined>;
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
|
@ -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';
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@logto/language-kit": "workspace:*",
|
||||
"@silverhand/essentials": "2.5.0"
|
||||
"@silverhand/essentials": "^2.5.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"zod": "^3.20.2"
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue