Quickstart
Automatic Setup​
We recommend creating a new Bridge app using create-bridge-app
, which sets up everything automatically for you. (You don't need to create an empty directory, create-bridge-app
will make one for you.) To create a project, run:
terminalbash
npx create-bridge-app@latest# orpnpm create bridge-app# oryarn create bridge-app
terminalbash
npx create-bridge-app@latest# orpnpm create bridge-app# oryarn create bridge-app
After the installation is complete:
- Run
cd ./your_project_name
- Run
npm i
orpnpm i
oryarn install
- Run
npm run dev
orpnpm dev
oryarn dev
to start the development server onhttp://localhost:8080
- Edit
index.ts
to start developing your server
For more information on how to use create-bridge-app, you can review the create-bridge-app documentation.
Manual Setup​
Installations​
Install bridge
and zod
in your project:
terminalbash
npm install bridge zod# oryarn add bridge zod# orpnpm add bridge zod
terminalbash
npm install bridge zod# oryarn add bridge zod# orpnpm add bridge zod
Create an index.ts file​
Complete Bridge App with HTTP
server.tsts
import {handler ,initBridge } from 'bridge';Â// A handler can set an endpoint and validate user data such as the body, files,// request parameters or headers sent.consthelloEndpoint =handler ({resolve : () => 'Hello World',});Â// To define the routes for our project, we can create a routes object and place// our handlers inside. The keys of the object correspond to the path.constroutes = {hello :helloEndpoint ,};Âconstport = 8080;Âconstbridge =initBridge ({routes });consthttpServer =bridge .HTTPServer ();ÂhttpServer .listen (port , () => {console .log (`Listening on port ${port }`);});
server.tsts
import {handler ,initBridge } from 'bridge';Â// A handler can set an endpoint and validate user data such as the body, files,// request parameters or headers sent.consthelloEndpoint =handler ({resolve : () => 'Hello World',});Â// To define the routes for our project, we can create a routes object and place// our handlers inside. The keys of the object correspond to the path.constroutes = {hello :helloEndpoint ,};Âconstport = 8080;Âconstbridge =initBridge ({routes });consthttpServer =bridge .HTTPServer ();ÂhttpServer .listen (port , () => {console .log (`Listening on port ${port }`);});
Complete Bridge App with Express
server.tsts
import {handler ,initBridge } from 'bridge';importexpress from 'express';Âconstroutes = {hello :handler ({resolve : () => 'Hello World',}),};Âconstport = 8080;constapp =express ();constbridge =initBridge ({routes });Âapp .use ('',bridge .expressMiddleware ());Âapp .listen (port , () => {console .log (`Listening on port ${port }`);});
server.tsts
import {handler ,initBridge } from 'bridge';importexpress from 'express';Âconstroutes = {hello :handler ({resolve : () => 'Hello World',}),};Âconstport = 8080;constapp =express ();constbridge =initBridge ({routes });Âapp .use ('',bridge .expressMiddleware ());Âapp .listen (port , () => {console .log (`Listening on port ${port }`);});
You can test your endpoint by making an http call to POST http://localhost:8080/hello
. Refer to the route documentation for instructions on customizing the HTTP method.
Congratulations, you just launched your first Bridge server! 🥳
Client code generation and OpenAPI Specification​
terminalbash
npx bridge-compile@latest# orpnpx bridge-compile@latest
terminalbash
npx bridge-compile@latest# orpnpx bridge-compile@latest
This command line generates 3 things:
- JSONType.json file
- openapi.json file
- sdk folder
The JSONType file contains all the types of your project compiled by Bridge. This file was the input to create the openapi specification and to generate the typescript sdk folder. You can develop your own compiler using this file as an input to generate sdk in other languages.
Browse OpenAPI documentation​
Start by installing swagger-ui-express
terminalbash
npm i swagger-ui-express# orpnpm i swagger-ui-express
terminalbash
npm i swagger-ui-express# orpnpm i swagger-ui-express
Use it with Express:
server.tsts
import { handler, initBridge } from 'bridge';import express from 'express';import swaggerUi from 'swagger-ui-express';const openApiDocumentation = require('./openapi.json');const routes = {hello: handler({resolve: () => 'Hello World',}),};const port = 8080;const app = express();const bridge = initBridge({ routes });app.use('', bridge.expressMiddleware());app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiDocumentation));app.listen(port, () => {console.log(`Listening on port ${port}`);});
server.tsts
import { handler, initBridge } from 'bridge';import express from 'express';import swaggerUi from 'swagger-ui-express';const openApiDocumentation = require('./openapi.json');const routes = {hello: handler({resolve: () => 'Hello World',}),};const port = 8080;const app = express();const bridge = initBridge({ routes });app.use('', bridge.expressMiddleware());app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiDocumentation));app.listen(port, () => {console.log(`Listening on port ${port}`);});
Your documentation is now available on http://localhost:8080/docs.
Fetch the typescript SDK anywhere​
You can simply download the typescript client code with the following command line:
terminalbash
npx fetch-bridge-sdk $serverUrl
terminalbash
npx fetch-bridge-sdk $serverUrl
You need to replace serverUrl with your serverUrl. For example "http://localhost:8080".
If you do not have axios and form-data installed in your project, the command line will automatically install them for you.
The upcoming version of the command line will allow you to select your preferred HTTP client library, either axios or fetch, and the required packages will be automatically installed if they are not already present in your project.