CHAN.RUN
function combines<TArgs, TReturn, TFns>(_sources, fn): DeclaredFn<TArgs, TReturn, MergeErrors<TFns>>;Defined in: src/declares.ts
Compose error surfaces from multiple declared functions into one. Use when a function calls several declared functions and can throw any of their errors.
const getUser = declares([NotFoundError, DbError], ...);
const getOrder = declares([OrderError, DbError], ...);
const getUserOrder = combines(
[getUser, getOrder],
async (userId: string, orderId: string) => {
const user = await getUser(userId);
const order = await getOrder(orderId);
return { user, order };
},
);
// getUserOrder can throw NotFoundError | DbError | OrderError| Type Parameter |
|---|
TArgs extends unknown[] |
TReturn |
TFns extends { __faultErrors: NamedFaultErrorClass<string>[]; }[] |
| Parameter | Type |
|---|---|
_sources | [...TFns[]] |
fn | (...args) => TReturn |
DeclaredFn<TArgs, TReturn, MergeErrors<TFns>>
const getUser = declares([UserNotFoundError, DbError], ...);
const getBilling = declares([BillingError, DbError], ...);
const getPermissions = declares([AuthError], ...);
const getDashboard = combines(
[getUser, getBilling, getPermissions],
async (userId: string) => {
const [user, billing, perms] = await Promise.all([
getUser(userId),
getBilling(userId),
getPermissions(userId),
]);
return { user, billing, perms };
},
);
// Error type is UserNotFoundError | DbError | BillingError | AuthError
const result = await tryAsync(getDashboard, userId);const findUser = declares([DbError], ...);
const validateToken = declares([AuthError, TokenExpiredError], ...);
const authenticateUser = combines(
[findUser, validateToken],
async (token: string) => {
const claims = validateToken(token);
return findUser(claims.userId);
},
);
// authenticateUser throws DbError | AuthError | TokenExpiredError