@logto/shared
Advanced tools
Comparing version 1.0.0-beta.18 to 1.0.0-rc.0
@@ -8,2 +8,5 @@ import type { SchemaValuePrimitive, SchemaValue } from '@logto/schemas'; | ||
}>; | ||
export declare const conditionalArraySql: <T>(value: T[], buildSql: (value: T[]) => SqlSqlToken) => SqlSqlToken<{ | ||
[x: string]: any; | ||
}>; | ||
export declare const autoSetFields: readonly ["createdAt", "updatedAt"]; | ||
@@ -10,0 +13,0 @@ export type OmitAutoSetFields<T> = Omit<T, typeof autoSetFields[number]>; |
import { notFalsy } from '@silverhand/essentials'; | ||
import { sql } from 'slonik'; | ||
export const conditionalSql = (value, buildSql) => notFalsy(value) ? buildSql(value) : sql ``; | ||
export const conditionalArraySql = (value, buildSql) => (value.length > 0 ? buildSql(value) : sql ``); | ||
export const autoSetFields = Object.freeze(['createdAt', 'updatedAt']); | ||
@@ -5,0 +6,0 @@ export const excludeAutoSetFields = (fields) => Object.freeze(fields.filter((field) => |
@@ -1,7 +0,11 @@ | ||
export declare const tryThat: <T, E extends Error>(exec: Promise<T> | (() => Promise<T>), onError: E | ((error: unknown) => never)) => Promise<T>; | ||
export declare const isPromise: (value: unknown) => value is Promise<unknown>; | ||
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 declare const tryThat: TryThat; | ||
export type TrySafe = { | ||
<T>(exec: () => T): T | undefined; | ||
<T>(exec: Promise<T> | (() => Promise<T>)): Promise<T | undefined>; | ||
<T>(exec: () => T): T | undefined; | ||
}; | ||
export declare const trySafe: TrySafe; |
@@ -1,18 +0,23 @@ | ||
export const tryThat = async (exec, onError) => { | ||
try { | ||
return await (typeof exec === 'function' ? exec() : exec); | ||
} | ||
catch (error) { | ||
export const isPromise = (value) => value !== null && | ||
(typeof value === 'object' || typeof value === 'function') && | ||
'then' in value && | ||
typeof value.then === 'function'; | ||
export const tryThat = (exec, onError) => { | ||
const handleError = (error) => { | ||
if (onError instanceof Error) { | ||
// https://github.com/typescript-eslint/typescript-eslint/issues/3814 | ||
// eslint-disable-next-line @typescript-eslint/no-throw-literal | ||
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) { | ||
return handleError(error); | ||
} | ||
}; | ||
export const isPromise = (value) => value !== null && | ||
(typeof value === 'object' || typeof value === 'function') && | ||
'then' in value && | ||
typeof value.then === 'function'; | ||
export const trySafe = (exec) => { | ||
@@ -19,0 +24,0 @@ try { |
@@ -1,6 +0,33 @@ | ||
import { trySafe } from './function.js'; | ||
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(trySafe(() => { | ||
expect( | ||
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression | ||
trySafe(() => { | ||
throw new Error('Test'); | ||
@@ -7,0 +34,0 @@ }) |
export * from './function.js'; | ||
export * from './object.js'; | ||
export { default as findPackage } from './find-package.js'; |
export * from './function.js'; | ||
export * from './object.js'; | ||
export { default as findPackage } from './find-package.js'; |
{ | ||
"name": "@logto/shared", | ||
"version": "1.0.0-beta.18", | ||
"version": "1.0.0-rc.0", | ||
"main": "lib/index.js", | ||
@@ -26,3 +26,3 @@ "author": "Silverhand Inc. <contact@silverhand.io>", | ||
"@types/jest": "^29.1.2", | ||
"@types/node": "^16.0.0", | ||
"@types/node": "^18.11.18", | ||
"eslint": "^8.21.0", | ||
@@ -35,3 +35,3 @@ "jest": "^29.1.2", | ||
"engines": { | ||
"node": "^16.13.0 || ^18.12.0" | ||
"node": "^18.12.0" | ||
}, | ||
@@ -50,3 +50,3 @@ "eslintConfig": { | ||
"slonik": "^30.0.0", | ||
"@logto/schemas": "1.0.0-beta.18" | ||
"@logto/schemas": "1.0.0-rc.0" | ||
}, | ||
@@ -53,0 +53,0 @@ "scripts": { |
37166
28
430
+ Added@logto/connector-kit@1.0.0-rc.0(transitive)
+ Added@logto/core-kit@1.0.0-rc.0(transitive)
+ Added@logto/language-kit@1.0.0-rc.0(transitive)
+ Added@logto/phrases@1.0.0-rc.0(transitive)
+ Added@logto/phrases-ui@1.0.0-rc.0(transitive)
+ Added@logto/schemas@1.0.0-rc.0(transitive)
+ Added@withtyped/server@0.4.1(transitive)
- Removed@logto/connector-kit@1.0.0-beta.32(transitive)
- Removed@logto/core-kit@1.0.0-beta.30(transitive)
- Removed@logto/language-kit@1.0.0-beta.30(transitive)
- Removed@logto/phrases@1.0.0-beta.17(transitive)
- Removed@logto/phrases-ui@1.0.0-beta.17(transitive)
- Removed@logto/schemas@1.0.0-beta.18(transitive)
- Removed@withtyped/server@0.3.1(transitive)
Updated@logto/schemas@1.0.0-rc.0