UNPKG

fly-machines-sdk

Version:

API that provides ability to manage applications on Fly.io

88 lines (87 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlyMachinesSDK = void 0; const requests_1 = require("./requests"); class FlyMachinesSDK { constructor(apiKey, orgSlug) { this.apiKey = apiKey; this.orgSlug = orgSlug; if (this.apiKey === '') { console.debug('API Key is not provided. Trying to read from ENV ($FLY_API_TOKEN)'); this.apiKey = process.env.FLY_API_TOKEN; } if (this.apiKey === '') { throw new Error('api_key_cant_be_empty'); } if (this.orgSlug === '') { throw new Error('org_slug_cant_be_empty'); } this.client = new requests_1.Requests(this.apiKey); } async getMachinesForApp(appName) { const QUERY = ` query getMachinesForTheApp ($appId: String!) { machines (state: "started", appId: $appId){ totalCount, nodes { state, name, app { id, name } } } } `; return this.client.query(QUERY, { appId: appName }); } async getApplicationDetails(appName) { const QUERY = ` query getAppDetails ($appId: String!) { app(name: $appId) { hostname, deployed, state, machines { nodes { instanceId, id } }, config { services { internalPort } } } }`; return this.client.query(QUERY, { appId: appName }); } async deleteApplication(name) { const result = await this.client.plainDeleteQuery(`/v1/apps/${name}`, null, true); return result.status === 202; } async stopApplicationInstance(appName, instanceId) { return this.client.plainQuery(`/v1/apps/${appName}/machines/${instanceId}/stop`, null, false); } async startApplicationInstance(appName, instanceId) { return this.client.plainQuery(`/v1/apps/${appName}/machines/${instanceId}/start`, null, false); } async waitUntilState(params) { return this.client.plainQuery(`/v1/apps/${params.appName}/machines/${params.machineId}/wait?instance_id=${params.machineInstanceId}&state=${params.desiredState}`, null, false); } async createApplicationOnMachine(dto) { const appDto = { app_name: dto.name, org_slug: this.orgSlug, }; const application = await this.client.plainQuery('/v1/apps', appDto, true); if (application.status !== 201) { console.error(application.result); throw new Error('failed_to_create_instance'); } return this.client.plainQuery(`/v1/apps/${dto.name}/machines`, dto, false); } } exports.FlyMachinesSDK = FlyMachinesSDK;