Skip to content
← Public packages

@kentcdodds/fly

Manage Fly.io apps, Machines, logs, and metrics.

src/lib/openapi-client.ts

225 lines · 6.5 KB · TypeScript
/**
 * Scaffolded by Kody from an OpenAPI spec.
 * Spec: Machines API v1.0
 * Provider label: Fly Machines API
 * Secret placeholders (e.g. {{secret:name}}) resolve via Kody's fetch gateway;
 * host approval is enforced there and is never widened by this spec.
 *
 * Regenerated via openapi_client_scaffold:
 *   specUrl: https://docs.machines.dev/spec/openapi3.json
 *   operationIds: apps_list, apps_show, machines_list, machines_show, machines_org_list, currenttoken_show
 *   auth: bearerSecret flyApiToken
 *   apiBaseUrl: https://api.machines.dev/v1
 */

const API_BASE_URL = "https://api.machines.dev/v1";

function buildUrl(pathTemplate, params = {}) {
	return API_BASE_URL + pathTemplate.replace(/\{([^}]+)\}/g, (_match, name) => {
		const value = params[name];
		if (value === undefined || value === null) {
			throw new Error(`Missing required path parameter: ${name}`);
		}
		return encodeURIComponent(String(value));
	});
}

function appendQuery(url, query = {}) {
	const search = new URLSearchParams();
	for (const [key, value] of Object.entries(query)) {
		if (value === undefined || value === null) continue;
		if (Array.isArray(value)) {
			for (const item of value) {
				if (item === undefined || item === null) continue;
				search.append(key, String(item));
			}
			continue;
		}
		search.append(key, String(value));
	}
	const qs = search.toString();
	return qs ? `${url}?${qs}` : url;
}

function mergeHeaders(userHeaders, authHeaders) {
	const merged = { ...(userHeaders ?? {}) };
	for (const [key, value] of Object.entries(authHeaders)) {
		// Auth headers always win; callers cannot override them.
		const lower = key.toLowerCase();
		for (const existing of Object.keys(merged)) {
			if (existing.toLowerCase() === lower) {
				delete merged[existing];
			}
		}
		merged[key] = value;
	}
	return merged;
}

function hasHeader(headers, name) {
	const lower = name.toLowerCase();
	return Object.keys(headers).some((key) => key.toLowerCase() === lower);
}

async function resolveFetch(fetchImpl) {
	return fetchImpl ?? fetch;
}

function authHeaders() {
	return {
	"Authorization": "Bearer {{secret:flyApiToken}}"
};
}

/**
 * GET /apps — List Apps
 * Auth: bearerSecret
 */
export async function appsList(input = {}, options = {}) {
	const params = input.params ?? {};
	const url = appendQuery(buildUrl("/apps", params), input.query);
	let body = undefined;
	const headers = mergeHeaders(input.headers, authHeaders());
	if (input.body !== undefined) {
		body = JSON.stringify(input.body);
		if (!hasHeader(headers, "content-type")) {
			headers["content-type"] = "application/json";
		}
	}
	const fetchImpl = await resolveFetch(options.fetchImpl);
	return fetchImpl(url, {
		method: "GET",
		headers,
		body,
	});
}

/**
 * GET /apps/{app_name} — Get App
 * Auth: bearerSecret
 */
export async function appsShow(input = {}, options = {}) {
	const params = input.params ?? {};
	if (params["app_name"] === undefined || params["app_name"] === null) {
		throw new Error("Missing required path parameter: app_name");
	}
	const url = appendQuery(buildUrl("/apps/{app_name}", params), input.query);
	let body = undefined;
	const headers = mergeHeaders(input.headers, authHeaders());
	if (input.body !== undefined) {
		body = JSON.stringify(input.body);
		if (!hasHeader(headers, "content-type")) {
			headers["content-type"] = "application/json";
		}
	}
	const fetchImpl = await resolveFetch(options.fetchImpl);
	return fetchImpl(url, {
		method: "GET",
		headers,
		body,
	});
}

/**
 * GET /apps/{app_name}/machines — List Machines
 * Auth: bearerSecret
 */
export async function machinesList(input = {}, options = {}) {
	const params = input.params ?? {};
	if (params["app_name"] === undefined || params["app_name"] === null) {
		throw new Error("Missing required path parameter: app_name");
	}
	const url = appendQuery(buildUrl("/apps/{app_name}/machines", params), input.query);
	let body = undefined;
	const headers = mergeHeaders(input.headers, authHeaders());
	if (input.body !== undefined) {
		body = JSON.stringify(input.body);
		if (!hasHeader(headers, "content-type")) {
			headers["content-type"] = "application/json";
		}
	}
	const fetchImpl = await resolveFetch(options.fetchImpl);
	return fetchImpl(url, {
		method: "GET",
		headers,
		body,
	});
}

/**
 * GET /apps/{app_name}/machines/{machine_id} — Get Machine
 * Auth: bearerSecret
 */
export async function machinesShow(input = {}, options = {}) {
	const params = input.params ?? {};
	if (params["app_name"] === undefined || params["app_name"] === null) {
		throw new Error("Missing required path parameter: app_name");
	}
	if (params["machine_id"] === undefined || params["machine_id"] === null) {
		throw new Error("Missing required path parameter: machine_id");
	}
	const url = appendQuery(buildUrl("/apps/{app_name}/machines/{machine_id}", params), input.query);
	let body = undefined;
	const headers = mergeHeaders(input.headers, authHeaders());
	if (input.body !== undefined) {
		body = JSON.stringify(input.body);
		if (!hasHeader(headers, "content-type")) {
			headers["content-type"] = "application/json";
		}
	}
	const fetchImpl = await resolveFetch(options.fetchImpl);
	return fetchImpl(url, {
		method: "GET",
		headers,
		body,
	});
}

/**
 * GET /orgs/{org_slug}/machines — List All Machines
 * Auth: bearerSecret
 */
export async function machinesOrgList(input = {}, options = {}) {
	const params = input.params ?? {};
	if (params["org_slug"] === undefined || params["org_slug"] === null) {
		throw new Error("Missing required path parameter: org_slug");
	}
	const url = appendQuery(buildUrl("/orgs/{org_slug}/machines", params), input.query);
	let body = undefined;
	const headers = mergeHeaders(input.headers, authHeaders());
	if (input.body !== undefined) {
		body = JSON.stringify(input.body);
		if (!hasHeader(headers, "content-type")) {
			headers["content-type"] = "application/json";
		}
	}
	const fetchImpl = await resolveFetch(options.fetchImpl);
	return fetchImpl(url, {
		method: "GET",
		headers,
		body,
	});
}

/**
 * GET /tokens/current — Get Current Token Information
 * Auth: bearerSecret
 */
export async function currenttokenShow(input = {}, options = {}) {
	const params = input.params ?? {};
	const url = appendQuery(buildUrl("/tokens/current", params), input.query);
	let body = undefined;
	const headers = mergeHeaders(input.headers, authHeaders());
	if (input.body !== undefined) {
		body = JSON.stringify(input.body);
		if (!hasHeader(headers, "content-type")) {
			headers["content-type"] = "application/json";
		}
	}
	const fetchImpl = await resolveFetch(options.fetchImpl);
	return fetchImpl(url, {
		method: "GET",
		headers,
		body,
	});
}