50 lines
4.7 KiB
TypeScript
50 lines
4.7 KiB
TypeScript
import axios from 'axios'
|
|
|
|
export class ApiError extends Error {
|
|
readonly code?:string
|
|
readonly status?:number
|
|
|
|
constructor(message:string, code?:unknown, status?:unknown) {
|
|
super(message)
|
|
this.name = 'ApiError'
|
|
// Retain bounded machine-readable metadata, never the response/config/request.
|
|
if (typeof code === 'string' && /^[a-z][a-z0-9_]{0,63}$/.test(code)) this.code = code
|
|
if (typeof status === 'number' && Number.isInteger(status) && status >= 100 && status <= 599) this.status = status
|
|
}
|
|
}
|
|
|
|
export const api = axios.create({ baseURL: '/api/v1', timeout: 8000, withCredentials: true })
|
|
api.interceptors.response.use((response) => response, (error) => {
|
|
const detail = error.response?.data?.error
|
|
const message = typeof detail?.message === 'string' && detail.message ? detail.message : '请求失败,请稍后重试'
|
|
return Promise.reject(new ApiError(message, detail?.code, error.response?.status))
|
|
})
|
|
|
|
export type Customer = { id:number; name:string; contact_email:string; status:string; created_at:string; updated_at:string }
|
|
export type Product = { id:number; code:string; name:string; description:string; active:boolean; created_at:string; updated_at:string }
|
|
export async function login(username:string,password:string) { return api.post('/auth/login',{username,password}) }
|
|
export async function logout() { return api.post('/auth/logout') }
|
|
export async function me() { return api.get('/me') }
|
|
export async function listCustomers(params?:Record<string,string|number>) { return api.get<{data:Customer[];meta:{page:number;page_size:number;total:number}}>('/customers',{params}) }
|
|
export async function createCustomer(value:Pick<Customer,'name'|'contact_email'>) { return api.post('/customers',value) }
|
|
export async function updateCustomer(id:number,value:Partial<Customer>) { return api.put(`/customers/${id}`,value) }
|
|
export async function deleteCustomer(id:number) { return api.delete(`/customers/${id}`) }
|
|
export async function listProducts() { return api.get<{data:Product[]}>('/products') }
|
|
export async function createProduct(value:Pick<Product,'code'|'name'|'description'>) { return api.post('/products',value) }
|
|
export async function updateProduct(id:number,value:Partial<Product>) { return api.put(`/products/${id}`,value) }
|
|
export async function deleteProduct(id:number) { return api.delete(`/products/${id}`) }
|
|
export type License = { id:number; license_key:string; customer_id:number; customer_name:string; product_id:number; product_name:string; product_code:string; status:'active'|'scheduled'|'expired'|'revoked'|string; starts_at:string; expires_at:string; created_at:string }
|
|
export async function listLicenses(params?:Record<string,string|number>) { return api.get<{data:License[];meta:{page:number;page_size:number;total:number}}>('/licenses',{params}) }
|
|
export async function issueLicense(value:{customer_id:number;product_id:number;starts_at:string;expires_at:string}, idempotencyKey:string) { return api.post('/licenses',value,{headers:{'Idempotency-Key':idempotencyKey}}) }
|
|
export type LicenseInstance = { online_state?:'online'|'offline'|'never_seen'|'unknown'; id:number; instance_id:string; license_id:number; customer_id:number; product_id:number; status:string; activated_at?:string; certificate_version?:string; created_at:string; last_seen_at?:string; lease_expires_at?:string; freeze_state:string; observed_state:string; entitlement_state:string }
|
|
export async function setInstanceFrozen(instanceID:string,frozen:boolean,key:string) { return api.post(`/instances/${instanceID}/freeze`,{frozen},{headers:{'Idempotency-Key':key}}) }
|
|
export async function listLicenseInstances(id:number) { return api.get<{data:LicenseInstance[]}>(`/licenses/${id}/instances`) }
|
|
export async function getLicense(id:number) { return api.get<{data:License}>(`/licenses/${id}`) }
|
|
export async function deleteLicense(id:number) { return api.delete(`/licenses/${id}`) }
|
|
export async function restoreLicense(id:number,idempotencyKey:string) { return api.post(`/licenses/${id}/restore`,{}, {headers:{'Idempotency-Key':idempotencyKey}}) }
|
|
export async function revokeLicense(id:number,idempotencyKey:string) { return api.post(`/licenses/${id}/revoke`,{}, {headers:{'Idempotency-Key':idempotencyKey}}) }
|
|
export async function renewLicense(id:number,expires_at:string,idempotencyKey:string) { return api.post(`/licenses/${id}/renew`,{expires_at},{headers:{'Idempotency-Key':idempotencyKey}}) }
|
|
|
|
export async function getInstanceCredentials(id:number) { return api.get<{data:{instance_id:string;license_key:string;secret:string}}>(`/licenses/${id}/instance-credentials`) }
|
|
export async function provisionLicenseInstance(id:number) { return api.post<{data:{id:number;instance_id:string;license_id:number;status:string;secret:string};credentials_retained:boolean}>(`/licenses/${id}/instances`,{}) }
|