UNPKG

sesterce-cli

Version:

A powerful command-line interface tool for managing Sesterce Cloud services. Sesterce CLI provides easy access to GPU cloud instances, AI inference services, container registries, and SSH key management directly from your terminal.

97 lines (80 loc) 2.21 kB
import { getApiKey } from "@/access/get-api-key"; type StandardApiError = { readonly statusCode: number; readonly message: string; }; function isStandardApiError(error: any): error is StandardApiError { return "statusCode" in error && "message" in error; } class _ApiClientProvider { private readonly client = fetch; private readonly baseUrl = process.env.SESTERCE_CLOUD_API_URL; private readonly apiKey: string; constructor() { this.apiKey = getApiKey(); } async request<T = unknown>( path: string, init?: RequestInit ): Promise<T | null> { const response = await this.client(`${this.baseUrl}${path}`, { ...init, headers: { ...init?.headers, "x-api-key": this.apiKey, }, }); if (!response.ok) { const error = await response.json(); if (isStandardApiError(error)) { throw new Error(error.message); } throw new Error("Unexpected error occurred."); } if (response.status === 204) { return null; } return response.json(); } async get<T = unknown>( path: string, init?: Omit<RequestInit, "method"> ): Promise<T | null> { return this.request<T>(path, { ...init, method: "GET", }); } async post<T = unknown>( path: string, init?: Omit<RequestInit, "method"> ): Promise<T | null> { return this.request<T>(path, { ...init, method: "POST", headers: init?.body ? { ...init?.headers, "Content-Type": "application/json" } : init?.headers, }); } async patch<T = unknown>( path: string, init?: Omit<RequestInit, "method"> ): Promise<T | null> { return this.request<T>(path, { ...init, method: "PATCH", headers: init?.body ? { ...init?.headers, "Content-Type": "application/json" } : init?.headers, }); } async delete<T = unknown>( path: string, init?: Omit<RequestInit, "method" | "body"> ): Promise<T | null> { return this.request<T>(path, { ...init, method: "DELETE" }); } } export type ApiClientProvider = InstanceType<typeof _ApiClientProvider>; export const apiClientProvider = new _ApiClientProvider();