Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/fly

src/index.ts

138 lines · 3.4 KB · TypeScript
export {
	API_TOKEN_SETUP_URL,
	DEFAULT_API_TOKEN_SECRET,
	FLY_API_BASE_URL,
	FLY_API_HOST,
	FLY_GRAPHQL_URL,
	FlyApiError,
	MACHINES_API_BASE_URL,
	MACHINES_HOST,
	MACHINES_OPENAPI_SPEC,
	assertNever,
	flyApiAuthHeaders,
	graphqlAuthHeaders,
	machinesAuthHeaders,
	machinesCurrentToken,
	machinesRequest,
	mutationPreview,
	parseAction,
	requestFlyApi,
	requestGraphql,
	requireConfirm,
	resolveApiTokenSecretName,
	resolveOrganizationSlug,
	secretSetupUrl,
} from './fly-core.ts'
export { listOrganizations, getOrganization } from './organizations.ts'
export { listApps, getApp } from './apps.ts'
export {
	listMachines,
	getMachine,
	startMachine,
	stopMachine,
	destroyMachine,
} from './machines.ts'
export { default as machineCount } from './machine-count.ts'
export { default as appMachines } from './app-machines.ts'
export {
	analyzeLogEntries,
	fetchAppLogs,
	logsToJsonl,
	normalizeLogEntry,
	default as appLogs,
} from './logs.ts'
export {
	appMetricQueries,
	prometheusQuery,
	prometheusQueryRange,
	runMetricQueries,
	default as metrics,
} from './metrics.ts'
export { smokeTest } from './smoke-test.ts'
export { runMachineStatus } from './scheduled-status.ts'
export { default as docs } from './docs.ts'

import { getApp, listApps } from './apps.ts'
import appMachines from './app-machines.ts'
import docs from './docs.ts'
import { assertNever, parseAction, requestGraphql } from './fly-core.ts'
import graphql from './graphql.ts'
import appLogs from './logs.ts'
import machineCount from './machine-count.ts'
import {
	destroyMachine,
	getMachine,
	listMachines,
	startMachine,
	stopMachine,
} from './machines.ts'
import metrics from './metrics.ts'
import { getOrganization, listOrganizations } from './organizations.ts'
import { runMachineStatus } from './scheduled-status.ts'
import { smokeTest } from './smoke-test.ts'

const actions = {
	'smoke-test': smokeTest,
	docs,
	organizations: listOrganizations,
	'list-organizations': listOrganizations,
	'get-organization': getOrganization,
	apps: listApps,
	'list-apps': listApps,
	'get-app': getApp,
	machines: listMachines,
	'list-machines': listMachines,
	'get-machine': getMachine,
	'start-machine': startMachine,
	'stop-machine': stopMachine,
	'destroy-machine': destroyMachine,
	'machine-count': machineCount,
	'app-machines': appMachines,
	logs: appLogs,
	metrics,
	graphql,
	'scheduled-status': runMachineStatus,
	request: requestGraphql,
} as const

export type FlyAction = keyof typeof actions

const actionNames = Object.keys(actions) as FlyAction[]

/**
 * Root dispatcher for Fly.io helpers. Defaults to the read-only smoke test.
 * @example
 * import fly from 'kody:@kody/fly'
 * const result = await fly({ action: 'list-apps', organizationSlug: 'my-org' })
 */
export default async function fly(input: Record<string, unknown> = {}) {
	const action = parseAction(input.action, actionNames, 'smoke-test', 'Fly')
	const handler = actions[action]
	switch (action) {
		case 'smoke-test':
		case 'docs':
		case 'organizations':
		case 'list-organizations':
		case 'get-organization':
		case 'apps':
		case 'list-apps':
		case 'get-app':
		case 'machines':
		case 'list-machines':
		case 'get-machine':
		case 'start-machine':
		case 'stop-machine':
		case 'destroy-machine':
		case 'machine-count':
		case 'app-machines':
		case 'logs':
		case 'metrics':
		case 'graphql':
		case 'scheduled-status':
		case 'request':
			return await handler(input as never)
		default: {
			assertNever(action, 'Unhandled Fly action: ')
		}
	}
}