Auto-Generated SDK
Why write frontend fetch wrappers when the backend already knows what your endpoints are?
The Magic Command
Run the following command in your terminal:
bro sdkbro.js will instantly parse your routes/ folder and generate a bro-sdk.js file right in your directory.
Using the Client
The generated client is a pure vanilla JavaScript fetch wrapper. It perfectly maps to your API structure.
import { api, setBaseURL, setTokenKey, setLocale } from './bro-sdk.js';
// Setup (optional, defaults to localhost:5000 and 'bro_token')
setBaseURL('https://api.myproduction.com');
setTokenKey('my_custom_token_key');
setLocale('fr'); // Sends Accept-Language: fr
// The client automatically checks localStorage for your JWT
// and attaches it as a Bearer token!
async function fetchUsers() {
// Standard dot-notation calls. Maps to `GET /users?page=1`
const users = await api.users.get({ page: 1 });
console.log(users);
}
async function fetchUserPosts() {
// Parameterized segment invocations. Maps to `GET /users/:userId/posts`
const posts = await api.users.userId('123').posts.get();
console.log(posts);
}
async function login(email, password) {
// Maps to `POST /auth/login`
// Automatically stringifies the body and sets Content-Type!
const response = await api.auth.login.post({ email, password });
// Save the token, the client will use it automatically on the next request
localStorage.setItem('my_custom_token_key', response.token);
}