|
| 1 | +import { joinPaths } from '../../util/path'; |
| 2 | +import type { PaginatedResourceResponse } from '../resources/Deserializer'; |
| 3 | +import type { Machine } from '../resources/Machine'; |
| 4 | +import { AbstractAPI } from './AbstractApi'; |
| 5 | + |
| 6 | +const basePath = '/machines'; |
| 7 | + |
| 8 | +type CreateMachineParams = { |
| 9 | + name: string; |
| 10 | +}; |
| 11 | + |
| 12 | +type UpdateMachineParams = { |
| 13 | + machineId: string; |
| 14 | + name: string; |
| 15 | +}; |
| 16 | + |
| 17 | +type GetMachineListParams = { |
| 18 | + limit?: number; |
| 19 | + offset?: number; |
| 20 | + query?: string; |
| 21 | +}; |
| 22 | + |
| 23 | +export class MachineApi extends AbstractAPI { |
| 24 | + async get(machineId: string) { |
| 25 | + this.requireId(machineId); |
| 26 | + return this.request<Machine>({ |
| 27 | + method: 'GET', |
| 28 | + path: joinPaths(basePath, machineId), |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + async list(queryParams: GetMachineListParams = {}) { |
| 33 | + return this.request<PaginatedResourceResponse<Machine[]>>({ |
| 34 | + method: 'GET', |
| 35 | + path: basePath, |
| 36 | + queryParams, |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + async create(bodyParams: CreateMachineParams) { |
| 41 | + return this.request<Machine>({ |
| 42 | + method: 'POST', |
| 43 | + path: basePath, |
| 44 | + bodyParams, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + async update(params: UpdateMachineParams) { |
| 49 | + const { machineId, ...bodyParams } = params; |
| 50 | + this.requireId(machineId); |
| 51 | + return this.request<Machine>({ |
| 52 | + method: 'PATCH', |
| 53 | + path: joinPaths(basePath, machineId), |
| 54 | + bodyParams, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + async delete(machineId: string) { |
| 59 | + this.requireId(machineId); |
| 60 | + return this.request<Machine>({ |
| 61 | + method: 'DELETE', |
| 62 | + path: joinPaths(basePath, machineId), |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
0 commit comments