feat(utils): Introduce promiseAll util (#5543)
This commit is contained in:
committed by
GitHub
parent
e4ce2f4e07
commit
f90ba02087
58
packages/utils/src/common/__tests__/promise-all.spec.ts
Normal file
58
packages/utils/src/common/__tests__/promise-all.spec.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { promiseAll } from "../promise-all"
|
||||
import { EOL } from "os"
|
||||
|
||||
describe("promiseAll", function () {
|
||||
it("should throw an error if any of the promises throw", async function () {
|
||||
const res = await promiseAll([
|
||||
Promise.resolve(1),
|
||||
(async () => {
|
||||
throw new Error("error")
|
||||
})(),
|
||||
Promise.resolve(3),
|
||||
]).catch((e) => e)
|
||||
|
||||
expect(res.message).toBe("error")
|
||||
})
|
||||
|
||||
it("should throw errors if any of the promises throw and aggregate them", async function () {
|
||||
const res = await promiseAll(
|
||||
[
|
||||
Promise.resolve(1),
|
||||
(async () => {
|
||||
throw new Error("error")
|
||||
})(),
|
||||
(async () => {
|
||||
throw new Error("error2")
|
||||
})(),
|
||||
Promise.resolve(3),
|
||||
],
|
||||
{
|
||||
aggregateErrors: true,
|
||||
}
|
||||
).catch((e) => e)
|
||||
|
||||
expect(res.message).toBe(["error", "error2"].join(EOL))
|
||||
})
|
||||
|
||||
it("should return all values if all promises are fulfilled", async function () {
|
||||
const res = await promiseAll([
|
||||
Promise.resolve(1),
|
||||
Promise.resolve(2),
|
||||
Promise.resolve(3),
|
||||
])
|
||||
|
||||
expect(res).toEqual([1, 2, 3])
|
||||
})
|
||||
|
||||
it("should return all values if all promises are fulfilled including waiting for nested promises", async function () {
|
||||
const res = await promiseAll([
|
||||
Promise.resolve(1),
|
||||
(async () => {
|
||||
await promiseAll([Promise.resolve(1), Promise.resolve(2)])
|
||||
})(),
|
||||
Promise.resolve(3),
|
||||
])
|
||||
|
||||
expect(res).toEqual([1, undefined, 3])
|
||||
})
|
||||
})
|
||||
@@ -16,9 +16,10 @@ export * from "./map-object-to"
|
||||
export * from "./medusa-container"
|
||||
export * from "./object-from-string-path"
|
||||
export * from "./object-to-string-path"
|
||||
export * from "./promise-all"
|
||||
export * from "./remote-query-object-from-string"
|
||||
export * from "./remote-query-object-to-string"
|
||||
export * from './remove-nullisih'
|
||||
export * from "./remove-nullisih"
|
||||
export * from "./set-metadata"
|
||||
export * from "./simple-hash"
|
||||
export * from "./string-to-select-relation-object"
|
||||
|
||||
40
packages/utils/src/common/promise-all.ts
Normal file
40
packages/utils/src/common/promise-all.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { EOL } from "os"
|
||||
|
||||
const getMessageError = (state: PromiseRejectedResult) =>
|
||||
state.reason.message ?? state.reason
|
||||
|
||||
const isRejected = (
|
||||
state: PromiseSettledResult<unknown>
|
||||
): state is PromiseRejectedResult => {
|
||||
return state.status === "rejected"
|
||||
}
|
||||
|
||||
const getValue = (state: PromiseFulfilledResult<unknown>) => state.value
|
||||
|
||||
/**
|
||||
* Promise.allSettled with error handling, safe alternative to Promise.all
|
||||
* @param promises
|
||||
* @param aggregateErrors
|
||||
*/
|
||||
export async function promiseAll<T extends readonly unknown[] | []>(
|
||||
promises: T,
|
||||
{ aggregateErrors } = { aggregateErrors: false }
|
||||
): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }> {
|
||||
const states = await Promise.allSettled(promises)
|
||||
|
||||
const rejected = (states as PromiseSettledResult<unknown>[]).filter(
|
||||
isRejected
|
||||
)
|
||||
|
||||
if (rejected.length) {
|
||||
if (aggregateErrors) {
|
||||
throw new Error(rejected.map(getMessageError).join(EOL))
|
||||
}
|
||||
|
||||
throw rejected[0].reason // Re throw the error itself
|
||||
}
|
||||
|
||||
return (states as PromiseFulfilledResult<unknown>[]).map(
|
||||
getValue
|
||||
) as unknown as Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>
|
||||
}
|
||||
@@ -3,14 +3,11 @@ import { NextFunction, Request, RequestHandler, Response } from "express"
|
||||
type handler = (req: Request, res: Response) => Promise<void>
|
||||
|
||||
export const wrapHandler = (fn: handler): RequestHandler => {
|
||||
return (
|
||||
req: Request & { errors?: Error[] },
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
if (req?.errors?.length) {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
const req_ = req as Request & { errors?: Error[] }
|
||||
if (req_?.errors?.length) {
|
||||
return res.status(400).json({
|
||||
errors: req.errors,
|
||||
errors: req_.errors,
|
||||
message:
|
||||
"Provided request body contains errors. Please check the data and retry the request",
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user