Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bridge

Package Overview
Dependencies
Maintainers
2
Versions
99
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bridge

Bridge is a Typescript Node.js framework that provides an easy and scalable way to create REST APIs while generating the client code.

  • 2.0.21
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
58
decreased by-52.07%
Maintainers
2
Weekly downloads
 
Created
Source
Twitter Discord MIT License

What is Bridge?

Bridge is a Typescript Node.js framework that provides an easy and scalable way to create REST APIs while generating the client code.

Our goal is to make Bridge a great framework for both frontend and backend teams, so if you're familiar with Node.js and Typescript, you'll feel right at home.

👉 See more informations on bridge.codes. 👈

Quickstart

There are a few examples that you can use for playing out with Bridge or bootstrapping your new project.

Quick start by creating a project with create-bridge-app:

npx create-bridge-app@latest
# or
yarn create bridge-app
# or
pnpm create bridge-app

Or by creating a project by yourself with http:

npm init
npm i bridge
npm i typescript --save-dev 
import { handler, initBridge } from 'bridge';

const port = 8080;
const routes = { hello: handler({ method: 'GET', resolve: () => 'hello' }) };

initBridge({ routes })
  .HTTPServer()
  .listen(port, () => {
    console.log(`Listening on port ${port}`);
  });

Or with express:

npm init
npm i bridge express
npm i typescript @types/express --save-dev 
import { handler, initBridge } from 'bridge';
import express from 'express';

const port = 8080;
const routes = { hello: handler({ method: 'GET', resolve: () => 'hello' }) };

const app = express();

app.use('', initBridge({ routes }).expressMiddleware());

app.listen(port, () => {
  console.log(`Listening on port ${port}`);
});

Documentation

Create a handler that validates data

// You can use either zod, yup or superstruct
import z from 'zod'

const hello: handler({
  query: z.object({ name: z.string().optional() }),
  body: z.object({ age: z.number() }),
  headers: z.object({ token: z.string().min(6) }),
  resolve: ({ query, body, headers }) => `Hello ${query.name}`,
}),

Send error to client

import { httpError, StatusCode } from "bridge";

const getMe: handler({
  headers: z.object({ token: z.string().min(6) }),
  resolve: ({ headers }) => {
    if (headers.token !== "private_token") return httpError(StatusCode.UNAUTHORIZED, 'Wrong token');
    else return {
      firstName: 'John',
      lastName: 'Doe',
    }
  },
}),

Creating and using a middleware

import z from 'zod'
import { apply } from 'bridge'

const authMiddleware = handler({
  headers: z.object({ token: z.string().min(5) }),
  resolve: ({ headers }) => {
    if (headers.token !== 'private_token') return httpError(StatusCode.UNAUTHORIZED, 'Wrong token');
    else return { firstName: 'John', name: 'Doe', age: 21 };
  },
});

const updateUser = handler({
  middlewares: apply(authMiddleware),
  body: z.object({ age: z.number() }),
  resolve: ({ mid, body }) => {
    const user = mid;
    user.age = body.age;
    return user;
  },
});

Handle errors

import { initBridge, onError } from 'bridge'

const errorHandler = onError(({ error, path }) => {
  if (error.name === 'Internal server error') console.log(path, error); // Send to bug reporting
  else console.log(path, error.status, error.name);
});

const bridge = initBridge({ routes, errorHandler })

👉 See full documentation on bridge.codes. 👈

Keywords

FAQs

Package last updated on 17 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc