Example
server.tsts
import {initBridge ,handler ,onError ,StatusCode ,httpError ,apply } from 'bridge';importexpress from 'express';// You can also use Yup or Superstruct for data validationimportz from 'zod';Âconstport = 8080;Â// A middleware is also a handlerconstauthMiddleware =handler ({headers :z .object ({token :z .string ().min (5) }),resolve : ({headers }) => {if (headers .token !== 'private_token') returnhttpError (StatusCode .UNAUTHORIZED , 'Wrong token');else return {firstName : 'John',name : 'Doe',age : 21 };},});Â// A handler can be used as an endpointconstupdateUser =handler ({middlewares :apply (authMiddleware ),body :z .object ({age :z .number () }),resolve : (data ) => {constuser =data .middlewares ;user .age =data .body .age ;returnuser ;},});Â// You can have multiple endpoints for the same route with different methods with the method functionconstroutes = {hey :handler ({resolve : () => 'hey' }), // POST /heyhello :handler ({query :z .object ({name :z .string ().optional () }),resolve : ({query }) => `Hello ${query .name }`,}), // POST /hellouser : {update :updateUser , // POST /user/update},};ÂconsterrorHandler =onError (({error ,path }) => {// The error object can be modified here before it is sent to the clientif (error .name === 'Internal server error')console .log (path ,error ); // Send to bug reportingelseconsole .log (path ,error .status ,error .name );});Â// It is also possible to use HTTP Serverconstapp =express ();Âapp .use ('',initBridge ({routes ,errorHandler }).expressMiddleware ());Âapp .listen (port , () => {console .log (`Listening on port ${port }`);});
server.tsts
import {initBridge ,handler ,onError ,StatusCode ,httpError ,apply } from 'bridge';importexpress from 'express';// You can also use Yup or Superstruct for data validationimportz from 'zod';Âconstport = 8080;Â// A middleware is also a handlerconstauthMiddleware =handler ({headers :z .object ({token :z .string ().min (5) }),resolve : ({headers }) => {if (headers .token !== 'private_token') returnhttpError (StatusCode .UNAUTHORIZED , 'Wrong token');else return {firstName : 'John',name : 'Doe',age : 21 };},});Â// A handler can be used as an endpointconstupdateUser =handler ({middlewares :apply (authMiddleware ),body :z .object ({age :z .number () }),resolve : (data ) => {constuser =data .middlewares ;user .age =data .body .age ;returnuser ;},});Â// You can have multiple endpoints for the same route with different methods with the method functionconstroutes = {hey :handler ({resolve : () => 'hey' }), // POST /heyhello :handler ({query :z .object ({name :z .string ().optional () }),resolve : ({query }) => `Hello ${query .name }`,}), // POST /hellouser : {update :updateUser , // POST /user/update},};ÂconsterrorHandler =onError (({error ,path }) => {// The error object can be modified here before it is sent to the clientif (error .name === 'Internal server error')console .log (path ,error ); // Send to bug reportingelseconsole .log (path ,error .status ,error .name );});Â// It is also possible to use HTTP Serverconstapp =express ();Âapp .use ('',initBridge ({routes ,errorHandler }).expressMiddleware ());Âapp .listen (port , () => {console .log (`Listening on port ${port }`);});