Server Side Calls
You can directly access the resolve function of any handler. Its type will be inferred automatically.
Example​
ts
import {handler ,httpError } from 'bridge';importz from 'zod';Âconstusers :Record <string, {firstName : string;lastName : string }> = {'1': {firstName : 'John',lastName : 'Doe',},};ÂconstgetMyIdWithToken =handler ({headers :z .object ({token :z .string () }),resolve : ({headers }) => {if (headers .token === 'token') return {id : '1' };else returnhttpError (400, 'Wrong token');},});ÂconstgetProfileWithToken =handler ({headers :z .object ({token :z .string () }),resolve : ({headers }) => {constres =getMyIdWithToken .resolve ({headers });if ('error' inres ) returnres ;returnusers [res .id ];},});
ts
import {handler ,httpError } from 'bridge';importz from 'zod';Âconstusers :Record <string, {firstName : string;lastName : string }> = {'1': {firstName : 'John',lastName : 'Doe',},};ÂconstgetMyIdWithToken =handler ({headers :z .object ({token :z .string () }),resolve : ({headers }) => {if (headers .token === 'token') return {id : '1' };else returnhttpError (400, 'Wrong token');},});ÂconstgetProfileWithToken =handler ({headers :z .object ({token :z .string () }),resolve : ({headers }) => {constres =getMyIdWithToken .resolve ({headers });if ('error' inres ) returnres ;returnusers [res .id ];},});
This example shows two handlers that can be used as endpoints and called separately. getProfileWithToken
uses the resolve function of getMyIdWithToken
.
caution
The above example doesn't make much sense, it would be more logical to use getMyIdWithToken as a middleware for getProfileWithToken. This would make the code shorter and clearer. Here is the revised code:
ts
import {handler ,httpError ,apply } from 'bridge';importz from 'zod';Âconstusers :Record <string, {firstName : string;lastName : string }> = {'1': {firstName : 'John',lastName : 'Doe',},};ÂconstgetMyIdWithToken =handler ({headers :z .object ({token :z .string () }),resolve : ({headers }) => {if (headers .token === 'token') return {id : '1' };else returnhttpError (400, 'Wrong token');},});ÂconstgetProfileWithToken =handler ({middlewares :apply (getMyIdWithToken ),resolve : ({middlewares }) =>users [middlewares .id ],});
ts
import {handler ,httpError ,apply } from 'bridge';importz from 'zod';Âconstusers :Record <string, {firstName : string;lastName : string }> = {'1': {firstName : 'John',lastName : 'Doe',},};ÂconstgetMyIdWithToken =handler ({headers :z .object ({token :z .string () }),resolve : ({headers }) => {if (headers .token === 'token') return {id : '1' };else returnhttpError (400, 'Wrong token');},});ÂconstgetProfileWithToken =handler ({middlewares :apply (getMyIdWithToken ),resolve : ({middlewares }) =>users [middlewares .id ],});