@1campus/gadget-sdk
v0.1.0
Published
1Campus Gadget SDK — TypeScript type definitions and utilities
Maintainers
Readme
@1campus/gadget-sdk
TypeScript type definitions for the 1Campus Gadget API.
提供全域 gadget 變數的完整型別定義,讓 gadget 開發者在 IDE 中享有 intellisense、型別檢查與 JSDoc 文件提示。
安裝
# pnpm
pnpm add -D @1campus/gadget-sdk
# npm
npm install -D @1campus/gadget-sdk
# yarn
yarn add -D @1campus/gadget-sdk設定
在 tsconfig.json 的 types 加入 @1campus/gadget-sdk:
{
"compilerOptions": {
"types": ["@1campus/gadget-sdk"]
}
}或在程式碼中加入 triple-slash directive:
/// <reference types="@1campus/gadget-sdk" />使用範例
取得當前角色
const role = gadget.getRole();
console.log(`Current role: ${role}`); // teacher / student / parent / admin呼叫 DSA Service
const contract = gadget.getContract('1campus.mobile.v2.teacher');
contract.send({
service: 'classroom.GetStudents',
body: '<Request><Type>Class</Type><UID>1446</UID></Request>',
result: (response, error) => {
if (error) {
console.error('呼叫失敗', error);
return;
}
console.log('學生資料', response);
}
});取得 Application 資訊
const app = gadget.getApplication();
console.log(`accessPoint: ${app.accessPoint}`); // 例如 dev.sh_d讀取自訂 params
// gadget 設定中的 params JSON
const myConfig = gadget.params['myConfigKey'];已支援 API
| API | 說明 |
|-----|------|
| getContract(name) | 取得指定 contract 的 DSA connection |
| connect(ap, acc, pwd) | 直接連線到指定 accesspoint |
| getApplication() | 取得 { accessPoint } |
| getRole() | 取得當前角色 |
| params | 開發者自訂參數 |
其他 API(如 getUserInfo、getPreference 等)在型別定義中標註為 Not supported in current version,呼叫時會顯示 console.warn。
發佈到 npm
# 1. 更新 package.json 的 version
# 2. 預覽發佈內容
cd gadget-sdk
npm pack --dry-run
# 3. 產生 Granular Access Token(因為 @1campus org 強制要求 2FA)
# 去 https://www.npmjs.com/settings/<username>/tokens
# → Generate New Token → Granular Access Token
# → Packages: Read and write
# → 勾選 "Allow publishing with this token without 2FA"
# 4. 發佈
npm publish --access public --registry https://registry.npmjs.org/ --//registry.npmjs.org/:_authToken=<你的token>
# 5. 發佈完成後去 npmjs 網頁刪掉該 token(一次性使用)License
MIT
