jiniu-byteda
v0.1.3
Published
Auth JS SDK for published H5 apps.
Readme
jiniu-byteda
Auth JS SDK for published H5 apps.
This package assumes a browser runtime and reads location, navigator, history, fetch, and localStorage from globalThis.
The package is built as a standard ESM library for Vite projects and publishes dist/index.js as its runtime entry.
Goals
- Load
appIdandreleaseIdfrom the app link profile API - Handle WeChat silent login without manual configuration
- Configure WeChat share-to-chat and share-to-timeline from the system service account
- Read the API
baseURLfrom the caller configuration - Use an app-scoped
x-customer-tokenheader for authenticated requests - Keep the public API small and framework-agnostic
Public API
import { ByteDa } from "jiniu-byteda"
const sdk = new ByteDa({
baseURL: "https://api-test4.jiniutech.com/byte-da",
})
await sdk.init()
const token = sdk.getToken()
sdk.setToken("token-value")Current public methods:
sdk.init()sdk.getToken()sdk.setToken(token)
Before init() finishes:
sdk.getToken()returnsnullsdk.setToken(token)throws because the token storage is not ready yet
Runtime Behavior
ByteDa.init() currently works as an initialization side effect and should be called once before the app renders.
Current flow:
- Read API
baseURLfromnew ByteDa({ baseURL }) - Send the current page link to
/api/apps/link-profile - Read
appId,releaseId,name,logo, anddescriptionfrom the link profile response - Fetch
/api/apps/{appId}/config - If the current browser is not WeChat, update the page title and favicon
- If the current browser is WeChat, run the default silent-login flow:
- use callback
codewhen present - otherwise validate the stored token with
/customer-api/v1/auth/profile - if no valid token exists, redirect to the WeChat authorize URL
- use callback
- After the WeChat login state is ready, reuse the cached profile for page branding and WeChat share configuration
Notes:
ByteDa.init()does not currently return a structured{ appConfig, profile, share }object- the
login_requiredbranch is reserved for later work and is not yet implemented - WeChat share configuration only runs inside the WeChat browser
- the SDK currently does not expose
context
Current Scope
First implementation batch:
- Runtime profile loading
- Token session storage
- WeChat authorize URL fetch
- WeChat callback login
- WeChat JS-SDK share config
- App link profile lookup
- App branding sync for title and favicon
agent-h5 Integration
Install the package:
pnpm add jiniu-bytedaInitialize it before rendering the app:
import { ByteDa } from "jiniu-byteda"
import { createRoot } from "react-dom/client"
import App from "./App.jsx"
const sdk = new ByteDa({
baseURL: "https://api-test4.jiniutech.com/byte-da",
})
await sdk.init()
createRoot(document.getElementById("root")).render(<App />)Configuration
baseURL is a required parameter of new ByteDa():
// Test environment
const sdk = new ByteDa({
baseURL: "https://api-test4.jiniutech.com/byte-da",
})
// Production environment
const sdk = new ByteDa({
baseURL: "https://api.jiniutech.com/byte-da",
})You can use environment variables or build-time config to switch between environments:
const sdk = new ByteDa({
baseURL: import.meta.env.VITE_BYTE_DA_BASE_URL,
})Package Build
Build the published package output with:
npm run buildThis emits the Vite-consumable ESM bundle at dist/index.js.
