UNPKG

@swell/cli

Version:

Swell's command line interface/utility

40 lines (39 loc) 1.51 kB
import { isObjectId } from './object-id.js'; import { slugFromInstalledApp } from './slug.js'; /** * Resolve an app's ObjectId from a public slug or private slug; pass through * a value that already looks like an ObjectId. * * Priority mirrors schema-api-server installer.js:3596 `getAppBySlugId`: * private match (`_<slug>`) wins over public match. For typical apps the * order is invisible (a single record matches both branches), but it * matters when two apps share a slug across publish state. */ export async function resolveAppId(api, appIdOrSlug) { if (isObjectId(appIdOrSlug)) { return appIdOrSlug; } const installedApps = await api.get({ adminPath: `/client/apps` }); const records = (installedApps?.results ?? []); const privateMatch = records.find((a) => a.app_private_id === `_${appIdOrSlug}`); const app = privateMatch ?? records.find((a) => a.app_public_id === appIdOrSlug); if (!app) { throw new Error(`App '${appIdOrSlug}' not found`); } return app.app_id; } /** * Build a map of installed-app ObjectId -> friendly slug, using the shared * server-canonical slug priority (see `slug.ts`). */ export async function buildAppSlugMap(api) { const installedApps = await api.get({ adminPath: `/client/apps` }); const map = {}; for (const a of (installedApps?.results ?? [])) { const slug = slugFromInstalledApp(a); if (a.app_id && slug) { map[a.app_id] = slug; } } return map; }