SDK Init
SDK Initialization
Overview
$gm.init() asks GMSSH to check the backend service status of the current micro-app. The SDK calls /api/center/check_status and passes the name, version and communication type of the current app.
Scope
A micro-app that includes a backend service must wait for $gm.init() to succeed before its first business request. Frontend-only apps that use host-injected capabilities such as message and dialog do not need to call this method.
Method signature
interface GMWebSdk {
init(): Promise<unknown>;
}init() takes no configuration parameters. The Promise resolves with the response of the check_status request and is rejected when the request fails.
Request payload
The SDK sends a POST request to /api/center/check_status. The values come from:
| Request field | SDK property | Description |
|---|---|---|
app_name | $gm.name | Unique identifier of the current app |
version | $gm.version | Version of the current app |
communication_type | $gm.communicationType | Communication type of the current app |
init() does not create a new IPC connection in the frontend and does not register extra window message listeners. The capabilities injected by the host app are already merged into window.$gm when the SDK loads.
Basic usage
async function initializeApp() {
try {
await window.$gm.init();
// Send app service requests only after initialization succeeds
startApplication();
} catch (error) {
console.error("应用服务初始化失败", error);
showInitializationError(error);
}
}
initializeApp();App types
Apps with a backend service
Call init() once at app startup, and send /api/call/{developer-name}/{app-name}/{method} requests only after it succeeds.
async function startApplication(name) {
await window.$gm.init();
return window.$gm.request("/api/call/official/example/hello", {
method: "post",
data: {
version: window.$gm.version,
transport: window.$gm.communicationType,
params: {
lang: window.$gm.lang,
name,
},
},
});
}Frontend-only apps
If your app has no backend service and never calls app service routes, you can skip init(). UI capabilities injected by the host app are ready to use:
window.$gm.message.success("应用已就绪");
window.$gm.chooseFile((path) => {
console.log("已选择服务器文件", path);
}, "/");Repeated calls and retries
The SDK does not cache the initialization Promise and does not deduplicate repeated calls. Every call to init() sends a new request to /api/center/check_status.
Manage the initialization state in one place at the app entry:
- Call it once at first startup.
- Do not send business requests while initialization is in progress.
- Show a clear error state with a retry action when it fails.
- Call
init()again when the user retries.
Error handling
init() passes through the failure from $gm.request. Do not rely on undocumented, fixed error codes; show a retryable error state and log the error information that is actually returned.
async function initializeWithRetry() {
try {
await window.$gm.init();
return true;
} catch (error) {
const message = error instanceof Error ? error.message : "初始化失败";
window.$gm.message.error(message);
return false;
}
}