JsBaoClientService
Singleton service for managing the JsBaoClient instance.
This service provides a centralized way to initialize, access, and manage the JsBaoClient throughout your application. It ensures only one client instance exists and handles lazy initialization.
Usage
Initializing at App Startup
Call initializeJsBao once at application startup (typically in main.ts) before mounting your app:
import { createApp } from "vue";
import { initializeJsBao, setPrimitiveAppLogLevel } from "primitive-app";
import App from "./App.vue";
import { allModels } from "@/models";
async function bootstrap() {
// Optional: Set log level for debugging
setPrimitiveAppLogLevel("debug");
// Initialize js-bao with your configuration
initializeJsBao({
appId: import.meta.env.VITE_APP_ID,
apiUrl: import.meta.env.VITE_API_URL,
wsUrl: import.meta.env.VITE_WS_URL,
oauthRedirectUri: import.meta.env.VITE_OAUTH_REDIRECT_URI,
models: allModels,
auth: {
persistJwtInStorage: true,
},
});
const app = createApp(App);
app.mount("#app");
}
bootstrap();Configuration Options
Required fields:
appId- Your application ID from the Primitive Admin consoleapiUrl- The API endpoint URL (e.g.,https://api.primitive.dev)wsUrl- The WebSocket endpoint URL (e.g.,wss://ws.primitive.dev)oauthRedirectUri- The OAuth callback URL for your appmodels- Array of all js-bao model classes your app uses
Optional fields:
auth.persistJwtInStorage- Whether to persist auth tokens in localStorage (default:false)auth.refreshProxy- Configuration for token refresh proxy (for enhanced security)logLevel- Client logging levelblobUploadConcurrency- Max concurrent blob uploads
Accessing the Client
After initialization, use jsBaoClientService to access the client:
import { jsBaoClientService } from "primitive-app";
// Get the client instance (creates it if needed)
const client = await jsBaoClientService.getClientAsync();
// Use client methods
await client.auth.signIn();
const docs = await client.me.ownedDocuments();Where the configuration comes from
VITE_APP_ID, VITE_API_URL, VITE_WS_URL and VITE_APP_NAME are NOT authored by hand. They are filled in at build time by the primitiveEnv() plugin (primitive-app/vite) from the Primitive environment selected in primitive/config.json — the one place a backend URL and app ID are typed. Choose the environment with primitive env use <name>, or --primitive-env on a deploy; VITE_WS_URL is derived from the API URL by scheme swap.
Your .env files carry app behavior only:
VITE_OAUTH_REDIRECT_URI=/oauth/callback
VITE_ENABLE_AUTH_PROXY=false
VITE_LOG_LEVEL=warn