修改后台权限
This commit is contained in:
21
node_modules/until-async/LICENSE
generated
vendored
Normal file
21
node_modules/until-async/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Artem Zakharchenko
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
114
node_modules/until-async/README.md
generated
vendored
Normal file
114
node_modules/until-async/README.md
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
# `until-async`
|
||||
|
||||
Gracefully handle a Promise using `async`/`await`.
|
||||
|
||||
## Why?
|
||||
|
||||
With the addition of `async`/`await` keywords in ECMAScript 2017 the handling of Promises became much easier. However, one must keep in mind that the `await` keyword provides no standard error handling API. Consider this usage:
|
||||
|
||||
```js
|
||||
async function getUser(id) {
|
||||
const data = await fetchUser(id)
|
||||
// Work with "data"...
|
||||
}
|
||||
```
|
||||
|
||||
In case `fetchUser()` throws an error, the entire `getUser()` function's scope will terminate. Because of this, it's recommended to implement error handling using `try`/`catch` block wrapping `await` expressions:
|
||||
|
||||
```js
|
||||
async function getUser(id) {
|
||||
let data = null
|
||||
|
||||
try {
|
||||
data = await asyncAction()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
|
||||
// Work with "data"...
|
||||
}
|
||||
```
|
||||
|
||||
While this is a semantically valid approach, constructing `try`/`catch` around each awaited operation may be tedious and get overlooked at times. Such error handling also introduces separate closures for execution and error scenarios of an asynchronous operation.
|
||||
|
||||
This library encapsulates the `try`/`catch` error handling in a utility function that does not create a separate closure and exposes a NodeJS-friendly API to work with errors and resolved data.
|
||||
|
||||
## Getting started
|
||||
|
||||
### Install
|
||||
|
||||
```bash
|
||||
npm install until-async
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```js
|
||||
import { until } from 'until-async'
|
||||
|
||||
async function getUserById(id) {
|
||||
const [error, data] = await until(() => fetchUser(id))
|
||||
|
||||
if (error) {
|
||||
return handleError(error)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
```
|
||||
|
||||
### Usage with TypeScript
|
||||
|
||||
```ts
|
||||
import { until } from 'until-async'
|
||||
|
||||
interface User {
|
||||
firstName: string
|
||||
age: number
|
||||
}
|
||||
|
||||
interface UserFetchError {
|
||||
type: 'FORBIDDEN' | 'NOT_FOUND'
|
||||
message?: string
|
||||
}
|
||||
|
||||
async function getUserById(id: string) {
|
||||
const [error, data] = await until<UserFetchError, User>(() => fetchUser(id))
|
||||
|
||||
if (error) {
|
||||
return handleError(error.type, error.message)
|
||||
}
|
||||
|
||||
return data.firstName
|
||||
}
|
||||
```
|
||||
|
||||
## Frequently asked questions
|
||||
|
||||
### Why does `until` accept a function and not a `Promise` directly?
|
||||
|
||||
This has been intentionally introduced to await a single logical unit as opposed to a single `Promise`.
|
||||
|
||||
```js
|
||||
// Notice how a single "until" invocation can handle
|
||||
// a rather complex piece of logic. This way any rejections
|
||||
// or exceptions happening within the given function
|
||||
// can be handled via the same "error".
|
||||
const [error, data] = until(async () => {
|
||||
const user = await fetchUser()
|
||||
const nextUser = normalizeUser(user)
|
||||
const transaction = await saveModel('user', user)
|
||||
|
||||
invariant(transaction.status === 'OK', 'Saving user failed')
|
||||
|
||||
return transaction.result
|
||||
})
|
||||
|
||||
if (error) {
|
||||
// Handle any exceptions happened within the function.
|
||||
}
|
||||
```
|
||||
|
||||
## Special thanks
|
||||
|
||||
- [giuseppegurgone](https://twitter.com/giuseppegurgone) for the discussion about the original `until` API.
|
||||
16
node_modules/until-async/lib/index.d.ts
generated
vendored
Normal file
16
node_modules/until-async/lib/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
//#region src/index.d.ts
|
||||
type UntilResult<RejectionReason, ResolveData> = [reason: RejectionReason, data: null] | [reason: null, data: ResolveData];
|
||||
/**
|
||||
* Gracefully handles a callback that returns a promise.
|
||||
*
|
||||
* @example
|
||||
* await until(() => Promise.resolve(123))
|
||||
* // [null, 123]
|
||||
*
|
||||
* await until(() => Promise.reject(new Error('Oops!')))
|
||||
* // [new Error('Oops!'), null]
|
||||
*/
|
||||
declare function until<RejectionReason = Error, ResolveData = unknown>(callback: () => Promise<ResolveData>): Promise<UntilResult<RejectionReason, ResolveData>>;
|
||||
//#endregion
|
||||
export { UntilResult, until };
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
24
node_modules/until-async/lib/index.js
generated
vendored
Normal file
24
node_modules/until-async/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
//#region src/index.ts
|
||||
/**
|
||||
* Gracefully handles a callback that returns a promise.
|
||||
*
|
||||
* @example
|
||||
* await until(() => Promise.resolve(123))
|
||||
* // [null, 123]
|
||||
*
|
||||
* await until(() => Promise.reject(new Error('Oops!')))
|
||||
* // [new Error('Oops!'), null]
|
||||
*/
|
||||
async function until(callback) {
|
||||
try {
|
||||
return [null, await callback().catch((error) => {
|
||||
throw error;
|
||||
})];
|
||||
} catch (error) {
|
||||
return [error, null];
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
export { until };
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
node_modules/until-async/lib/index.js.map
generated
vendored
Normal file
1
node_modules/until-async/lib/index.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","names":["error: any"],"sources":["../src/index.ts"],"sourcesContent":["export type UntilResult<RejectionReason, ResolveData> =\n | [reason: RejectionReason, data: null]\n | [reason: null, data: ResolveData]\n\n/**\n * Gracefully handles a callback that returns a promise.\n *\n * @example\n * await until(() => Promise.resolve(123))\n * // [null, 123]\n *\n * await until(() => Promise.reject(new Error('Oops!')))\n * // [new Error('Oops!'), null]\n */\nexport async function until<RejectionReason = Error, ResolveData = unknown>(\n callback: () => Promise<ResolveData>,\n): Promise<UntilResult<RejectionReason, ResolveData>> {\n try {\n const data = await callback().catch((error) => {\n throw error\n })\n return [null, data]\n } catch (error: any) {\n return [error, null]\n }\n}\n"],"mappings":";;;;;;;;;;;AAcA,eAAsB,MACpB,UACoD;AACpD,KAAI;AAIF,SAAO,CAAC,MAHK,MAAM,UAAU,CAAC,OAAO,UAAU;AAC7C,SAAM;IACN,CACiB;UACZA,OAAY;AACnB,SAAO,CAAC,OAAO,KAAK"}
|
||||
52
node_modules/until-async/package.json
generated
vendored
Normal file
52
node_modules/until-async/package.json
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"type": "module",
|
||||
"name": "until-async",
|
||||
"version": "3.0.2",
|
||||
"description": "Gracefully handle a Promise using async/await.",
|
||||
"main": "./lib/index.js",
|
||||
"types": "./lib/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./lib/index.d.ts",
|
||||
"default": "./lib/index.js"
|
||||
}
|
||||
},
|
||||
"homepage": "https://github.com/kettanaito/until-async",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/kettanaito/until-async.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "Artem Zakharchenko",
|
||||
"url": "https://github.com/kettanaito"
|
||||
},
|
||||
"license": "MIT",
|
||||
"funding": "https://github.com/sponsors/kettanaito",
|
||||
"files": [
|
||||
"lib",
|
||||
"src"
|
||||
],
|
||||
"keywords": [
|
||||
"async",
|
||||
"promise",
|
||||
"handle",
|
||||
"gracefully",
|
||||
"tuple",
|
||||
"util"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@ossjs/release": "^0.8.1",
|
||||
"@rolldown/binding-darwin-arm64": "1.0.0-beta.38",
|
||||
"publint": "^0.3.13",
|
||||
"tsdown": "^0.15.3",
|
||||
"typescript": "^5.9.2",
|
||||
"vitest": "^3.2.4"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "tsdown -w",
|
||||
"test": "vitest",
|
||||
"lint": "publint",
|
||||
"build": "tsdown",
|
||||
"release": "release publish"
|
||||
}
|
||||
}
|
||||
26
node_modules/until-async/src/index.ts
generated
vendored
Normal file
26
node_modules/until-async/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
export type UntilResult<RejectionReason, ResolveData> =
|
||||
| [reason: RejectionReason, data: null]
|
||||
| [reason: null, data: ResolveData]
|
||||
|
||||
/**
|
||||
* Gracefully handles a callback that returns a promise.
|
||||
*
|
||||
* @example
|
||||
* await until(() => Promise.resolve(123))
|
||||
* // [null, 123]
|
||||
*
|
||||
* await until(() => Promise.reject(new Error('Oops!')))
|
||||
* // [new Error('Oops!'), null]
|
||||
*/
|
||||
export async function until<RejectionReason = Error, ResolveData = unknown>(
|
||||
callback: () => Promise<ResolveData>,
|
||||
): Promise<UntilResult<RejectionReason, ResolveData>> {
|
||||
try {
|
||||
const data = await callback().catch((error) => {
|
||||
throw error
|
||||
})
|
||||
return [null, data]
|
||||
} catch (error: any) {
|
||||
return [error, null]
|
||||
}
|
||||
}
|
||||
25
node_modules/until-async/src/until.test-d.ts
generated
vendored
Normal file
25
node_modules/until-async/src/until.test-d.ts
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
import { until } from './index.js'
|
||||
|
||||
it('infers the data type if no type arguments were provided', async () => {
|
||||
const [error, data] = await until(() => Promise.resolve(123))
|
||||
|
||||
if (error) {
|
||||
expectTypeOf(error).toEqualTypeOf<Error>()
|
||||
expectTypeOf(data).toEqualTypeOf<null>()
|
||||
} else {
|
||||
expectTypeOf(error).toEqualTypeOf<null>()
|
||||
expectTypeOf(data).toEqualTypeOf<number>()
|
||||
}
|
||||
})
|
||||
|
||||
it('treats error/data as a discriminated union type', async () => {
|
||||
const [error, data] = await until<Error, number>(() => Promise.resolve(123))
|
||||
|
||||
if (error) {
|
||||
expectTypeOf(error).toEqualTypeOf<Error>()
|
||||
expectTypeOf(data).toEqualTypeOf<null>()
|
||||
} else {
|
||||
expectTypeOf(error).toEqualTypeOf<null>()
|
||||
expectTypeOf(data).toEqualTypeOf<number>()
|
||||
}
|
||||
})
|
||||
20
node_modules/until-async/src/until.test.ts
generated
vendored
Normal file
20
node_modules/until-async/src/until.test.ts
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
import { until } from './index.js'
|
||||
|
||||
it('resolves with the value returned from the callback', async () => {
|
||||
await expect(until(async () => 'value')).resolves.toEqual([null, 'value'])
|
||||
await expect(until(() => Promise.resolve('value'))).resolves.toEqual([
|
||||
null,
|
||||
'value',
|
||||
])
|
||||
})
|
||||
|
||||
it('resolves with the error thrown in the callback', async () => {
|
||||
await expect(
|
||||
until(() => Promise.reject(new Error('error'))),
|
||||
).resolves.toEqual([new Error('error'), null])
|
||||
|
||||
await expect(until(() => Promise.reject('custom reason'))).resolves.toEqual([
|
||||
'custom reason',
|
||||
null,
|
||||
])
|
||||
})
|
||||
Reference in New Issue
Block a user