Bundles
バンドルは、Capgo のコア更新パッケージです。各バンドルには、アプリのコンテンツを構成する Web アセット (HTML、CSS、JS) が含まれています。バンドル API を使用すると、これらの更新パッケージの一覧表示や削除などを管理できます。
バンドルを理解する
Section titled “バンドルを理解する”バンドルは、アプリの Web コンテンツの特定のバンドル (バージョン) を表し、次のものが含まれます。
- バンドル (バージョン): バンドルのセマンティック バージョン番号
- チェックサム: バンドルの整合性を検証するための固有のハッシュ
- ストレージ情報: バンドルが保存される場所と方法に関する詳細
- ネイティブ要件: ネイティブ アプリの最小バージョン要件
- メタデータ: 作成時間、所有権、その他の追跡情報
手動バンドル作成 (CLI なし)
Section titled “手動バンドル作成 (CLI なし)”Capgo CLI を使用せずにバンドルを手動で作成してアップロードする方法は次のとおりです。
ステップ 1: アプリを構築する
Section titled “ステップ 1: アプリを構築する”まず、アプリの Web アセットを構築します。
npm run buildステップ 2: Capgo CLI と同じパッケージを使用してバンドル ZIP を作成する
Section titled “ステップ 2: Capgo CLI と同じパッケージを使用してバンドル ZIP を作成する”重要: 互換性を確保するために、Capgo CLI が内部で使用するものとまったく同じ JavaScript パッケージを使用してください。
必要なパッケージをインストールする
Section titled “必要なパッケージをインストールする”npm install adm-zip @tomasklaen/checksumJavaScript で Zip バンドルを作成 (Capgo CLI と同じ)
Section titled “JavaScript で Zip バンドルを作成 (Capgo CLI と同じ)”注: 以下の例では、version は、API で使用されるバンドル (バージョン) 名を指します。
const fs = require('node:fs');const path = require('node:path');const os = require('node:os');const AdmZip = require('adm-zip');const { checksum: getChecksum } = require('@tomasklaen/checksum');
// Exact same implementation as Capgo CLIfunction zipFileUnix(filePath) { const zip = new AdmZip(); zip.addLocalFolder(filePath); return zip.toBuffer();}
async function zipFileWindows(filePath) { console.log('Zipping file windows mode'); const zip = new AdmZip();
const addToZip = (folderPath, zipPath) => { const items = fs.readdirSync(folderPath);
for (const item of items) { const itemPath = path.join(folderPath, item); const stats = fs.statSync(itemPath);
if (stats.isFile()) { const fileContent = fs.readFileSync(itemPath); zip.addFile(path.join(zipPath, item).split(path.sep).join('/'), fileContent); } else if (stats.isDirectory()) { addToZip(itemPath, path.join(zipPath, item)); } } };
addToZip(filePath, ''); return zip.toBuffer();}
// Main zipFile function (exact same logic as CLI)async function zipFile(filePath) { if (os.platform() === 'win32') { return zipFileWindows(filePath); } else { return zipFileUnix(filePath); }}
async function createBundle(inputPath, outputPath, version) { // Create zip using exact same method as Capgo CLI const zipped = await zipFile(inputPath);
// Write to file fs.writeFileSync(outputPath, zipped);
// Calculate checksum using exact same package as CLI const checksum = await getChecksum(zipped, 'sha256');
return { filename: path.basename(outputPath), version: version, size: zipped.length, checksum: checksum };}
// Usageasync function main() { try { const result = await createBundle('./dist', './my-app-1.2.3.zip', '1.2.3'); console.log('Bundle info:', JSON.stringify(result, null, 2)); } catch (error) { console.error('Error creating bundle:', error); }}
main();ステップ 3: CLI と同じパッケージを使用して SHA256 チェックサムを計算する
Section titled “ステップ 3: CLI と同じパッケージを使用して SHA256 チェックサムを計算する”const fs = require('node:fs');const { checksum: getChecksum } = require('@tomasklaen/checksum');
async function calculateChecksum(filePath) { const fileBuffer = fs.readFileSync(filePath); // Use exact same package and method as Capgo CLI const checksum = await getChecksum(fileBuffer, 'sha256'); return checksum;}
// Usageasync function main() { const checksum = await calculateChecksum('./my-app-1.2.3.zip'); console.log('Checksum:', checksum);}
main();ステップ 4: バンドルをストレージにアップロードする
Section titled “ステップ 4: バンドルをストレージにアップロードする”zip ファイルを Web アクセス可能なストレージにアップロードします。
# Example: Upload to your server via scpscp my-app-1.2.3.zip user@your-server.com:/var/www/bundles/
# Example: Upload to S3 using AWS CLIaws s3 cp my-app-1.2.3.zip s3://your-bucket/bundles/
# Example: Upload via curl to a custom endpointcurl -X POST https://your-storage-api.com/upload \ -H "Authorization: Bearer YOUR_TOKEN" \ -F "file=@my-app-1.2.3.zip"重要: バンドルは HTTPS URL 経由で パブリックにアクセス可能である必要があります (認証は必要ありません)。 Capgo のサーバーは、この URL からバンドルをダウンロードする必要があります。
有効なパブリック URL の例:
https://your-storage.com/bundles/my-app-1.2.3.ziphttps://github.com/username/repo/releases/download/v1.2.3/bundle.ziphttps://cdn.jsdelivr.net/gh/username/repo@v1.2.3/dist.zip
ステップ 5: Capgo API でバンドルを登録する
Section titled “ステップ 5: Capgo API でバンドルを登録する”直接 API 呼び出しを使用して、外部バンドルを Capgo に登録します。
async function registerWithCapgo(appId, version, bundleUrl, checksum, apiKey) { const fetch = require('node-fetch');
// Create bundle (version) const response = await fetch('https://api.capgo.app/bundle/', { method: 'POST', headers: { 'Content-Type': 'application/json', 'authorization': apiKey }, body: JSON.stringify({ app_id: appId, version: version, external_url: bundleUrl, checksum: checksum }) });
if (!response.ok) { throw new Error(`Failed to create bundle: ${response.statusText}`); }
const data = await response.json(); console.log('Bundle created:', data);
return data;}API パラメータ
Section titled “API パラメータ”| パラメータ | 説明 | 必須 |
|---|---|---|
app_id | アプリの識別子 | はい |
version | バンドル (バージョン) セマンティック バージョン (例: “1.2.3”) | はい |
external_url | パブリックにアクセス可能 バンドルをダウンロードできる HTTPS URL (認証は必要ありません) | はい |
checksum | zip ファイルの SHA256 チェックサム | はい |
バンドル構造の要件
Section titled “バンドル構造の要件”バンドル zip は次の要件に従う必要があります。
- ルート インデックス ファイル: ルート レベルに
index.htmlが必要です - Capacitor 統合: アプリ コードで
notifyAppReady()を呼び出す必要があります - アセット パス: すべてのアセットに相対パスを使用します
有効なバンドル構造
Section titled “有効なバンドル構造”bundle.zip├── index.html├── assets/│ ├── app.js│ └── styles.css└── images/完全な手動ワークフローの例
Section titled “完全な手動ワークフローの例”zip、チェックサム、Capgo にアップロードする単純な Node.js スクリプト:
const fs = require('node:fs');const os = require('node:os');const AdmZip = require('adm-zip');const { checksum: getChecksum } = require('@tomasklaen/checksum');const fetch = require('node-fetch');
async function deployToCapgo() { const APP_ID = 'com.example.app'; const VERSION = '1.2.3'; const BUNDLE_URL = 'https://your-storage.com/bundles/app-1.2.3.zip'; const API_KEY = process.env.CAPGO_API_KEY;
// 1. Create zip (same as Capgo CLI) const zip = new AdmZip(); zip.addLocalFolder('./dist'); const zipped = zip.toBuffer();
// 2. Calculate checksum (same as Capgo CLI) const checksum = await getChecksum(zipped, 'sha256'); console.log('Checksum:', checksum);
// 3. Upload to your storage (replace with your upload logic) // fs.writeFileSync('./bundle.zip', zipped); // ... upload bundle.zip to your storage ...
// 4. Register with Capgo API const response = await fetch('https://api.capgo.app/bundle/', { method: 'POST', headers: { 'Content-Type': 'application/json', 'authorization': API_KEY }, body: JSON.stringify({ app_id: APP_ID, version: VERSION, external_url: BUNDLE_URL, checksum: checksum }) });
if (!response.ok) { throw new Error(`Failed: ${response.statusText}`); }
console.log('Bundle registered with Capgo!');}
deployToCapgo().catch(console.error);依存関係をインストールします。
npm install adm-zip @tomasklaen/checksum node-fetchチェックサムの検証
Section titled “チェックサムの検証”JavaScript チェックサム計算 (Capgo CLI と同じ)
Section titled “JavaScript チェックサム計算 (Capgo CLI と同じ)”Capgo CLI が内部で使用するものとまったく同じパッケージとメソッドを使用します。
const fs = require('node:fs');const { checksum: getChecksum } = require('@tomasklaen/checksum');
async function calculateChecksum(filePath) { const fileBuffer = fs.readFileSync(filePath); // Use exact same package and method as Capgo CLI const checksum = await getChecksum(fileBuffer, 'sha256'); return checksum;}
// Verify checksum matchesasync function verifyChecksum(filePath, expectedChecksum) { const actualChecksum = await calculateChecksum(filePath); const isValid = actualChecksum === expectedChecksum;
console.log(`File: ${filePath}`); console.log(`Expected: ${expectedChecksum}`); console.log(`Actual: ${actualChecksum}`); console.log(`Valid: ${isValid}`);
return isValid;}
// Usageasync function main() { const bundleChecksum = await calculateChecksum('./my-app-1.2.3.zip'); console.log('SHA256 Checksum:', bundleChecksum);}
main();```### チェックサムの重要性
- **バンドルの整合性**: 転送中にバンドルが破損していないことを確認します- **API 検証**: Capgo はバンドルを受け入れる前にチェックサムを検証します- **プラグインの検証**: モバイル プラグインは、アップデートを適用する前にチェックサムを検証します。
## ベストプラクティス
1. **バンドル (バージョン) 管理**: セマンティック バージョニングを一貫して使用する2. **ストレージの最適化**: 未使用のバンドルを定期的に削除します3. **バンドル (バージョン) の互換性**: 適切なネイティブ バージョンの最小要件を設定します。4. **バックアップ戦略**: 重要なバンドル (バージョン) のバックアップを維持します。
## エンドポイント
### 入手
`https://api.capgo.app/bundle/`
バンドル情報を取得します。ページごとに 50 バンドルを返します。
#### クエリパラメータ- `app_id`: 必須。アプリのID- `page`: オプション。ページネーションのページ番号
#### 応答タイプ
```typescriptinterface Bundle { app_id: string bucket_id: string | null checksum: string | null created_at: string | null deleted: boolean external_url: string | null id: number minUpdateVersion: string | null name: string native_packages: Json[] | null owner_org: string r2_path: string | null session_key: string | null storage_provider: string updated_at: string | null user_id: string | null}リクエストの例
Section titled “リクエストの例”# Get all bundlescurl -H "authorization: your-api-key" \ "https://api.capgo.app/bundle/?app_id=app_123"
# Get next pagecurl -H "authorization: your-api-key" \ "https://api.capgo.app/bundle/?app_id=app_123&page=1"{ "data": [ { "id": 1, "app_id": "app_123", "name": "1.0.0", "checksum": "abc123...", "minUpdateVersion": "1.0.0", "storage_provider": "r2", "created_at": "2024-01-01T00:00:00Z", "updated_at": "2024-01-01T00:00:00Z", "deleted": false, "owner_org": "org_123", "user_id": "user_123" } ]}https://api.capgo.app/bundle/
アプリの 1 つまたはすべてのバンドルを削除します。この操作は元に戻せないため、注意して使用してください。
クエリパラメータ
Section titled “クエリパラメータ”特定のバンドルを削除するには:
interface BundleDelete { app_id: string version: string}すべてのバンドルを削除するには:
interface BundleDeleteAll { app_id: string}リクエストの例
Section titled “リクエストの例”# Delete specific bundlecurl -X DELETE \ -H "authorization: your-api-key" \ -H "Content-Type: application/json" \ -d '{ "app_id": "app_123", "version": "1.0.0" }' \ https://api.capgo.app/bundle/
# Delete all bundlescurl -X DELETE \ -H "authorization: your-api-key" \ -H "Content-Type: application/json" \ -d '{ "app_id": "app_123" }' \ https://api.capgo.app/bundle/{ "status": "ok"}https://api.capgo.app/bundle/
外部 URL を使用して新しいバンドルを作成します。
リクエスト本文
Section titled “リクエスト本文”interface CreateBundleBody { app_id: string version: string external_url: string // Must be publicly accessible HTTPS URL checksum: string}リクエストの例
Section titled “リクエストの例”curl -X POST \ -H "authorization: your-api-key" \ -H "Content-Type: application/json" \ -d '{ "app_id": "com.example.app", "version": "1.2.3", "external_url": "https://your-storage.com/bundles/app-1.2.3.zip", "checksum": "a1b2c3d4e5f6789abcdef123456789abcdef123456789abcdef123456789abcd" }' \ https://api.capgo.app/bundle/{ "status": "ok"}POST (メタデータ)
Section titled “POST (メタデータ)”https://api.capgo.app/bundle/metadata
リンクやコメント情報などのバンドルのメタデータを更新します。
リクエスト本文
Section titled “リクエスト本文”interface UpdateMetadataBody { app_id: string version_id: number // bundle (version) id link?: string comment?: string}リクエストの例
Section titled “リクエストの例”curl -X POST \ -H "authorization: your-api-key" \ -H "Content-Type: application/json" \ -d '{ "app_id": "app_123", "version_id": 456, "link": "https://github.com/myorg/myapp/releases/tag/v1.0.0", "comment": "Fixed critical bug in authentication" }' \ https://api.capgo.app/bundle/metadata{ "status": "success"}https://api.capgo.app/bundle/
バンドルを特定のチャンネルに設定します。これにより、バンドル (バージョン) が配布用のチャネルにリンクされます。
リクエスト本文
Section titled “リクエスト本文”interface SetChannelBody { app_id: string version_id: number // bundle (version) id channel_id: number}リクエストの例
Section titled “リクエストの例”curl -X PUT \ -H "authorization: your-api-key" \ -H "Content-Type: application/json" \ -d '{ "app_id": "app_123", "version_id": 456, "channel_id": 789 }' \ https://api.capgo.app/bundle/{ "status": "success", "message": "Bundle 1.0.0 set to channel production"}一般的なエラーのシナリオとその対応:
// Bundle not found{ "error": "Bundle not found", "status": "KO"}
// Invalid bundle (version) format{ "error": "Invalid version format", "status": "KO"}
// Storage error{ "error": "Failed to delete bundle from storage", "status": "KO"}
// Permission denied{ "error": "Insufficient permissions to manage bundles", "status": "KO"}一般的な使用例
Section titled “一般的な使用例”- 古いバンドル (バージョン) をクリーンアップ
// Delete outdated beta bundles (versions){ "app_id": "app_123", "version": "1.0.0-beta.1"}- アプリのリセット
// Remove all bundles to start fresh{ "app_id": "app_123"}ストレージに関する考慮事項
Section titled “ストレージに関する考慮事項”- 保存ポリシー: 古いバンドルを保存する期間を定義します。
- サイズ管理: バンドルのサイズとストレージの使用状況を監視します。
- バックアップ戦略: 重要なバンドル (バージョン) のバックアップを検討する
- コストの最適化: 不要なバンドルを削除してストレージ コストを最適化します。