integrationsApi.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // integrationsApi.ts - wire types and calls for the external router pushers
  2. // ("integrations") admin API. Mirrors rackApi.ts (req<T> + json helpers) but
  3. // the error envelope here carries a machine code in `error` plus an optional
  4. // human `message`, so ApiError keeps both for the i18n code -> key mapping.
  5. export interface Integration {
  6. slug: string
  7. vendor: string
  8. mode: 'local' | 'ssh'
  9. host: string
  10. username: string
  11. version: string
  12. arch: string
  13. intervalS: number
  14. enabled: boolean
  15. state: string
  16. lastError: string
  17. /** Código estable del motivo offline (#1718): vacío si empuja o está pausada. */
  18. offlineReason?: string
  19. /** Último fallo de poll del pusher, en crudo (#2039). */
  20. pollError?: string
  21. lastStartMs: number
  22. lastPushMs: number
  23. fresh: boolean
  24. }
  25. /** A pusher that feeds the fleet without an integration record (#1743). */
  26. export interface UnmanagedIntegration {
  27. slug: string
  28. kind: string
  29. version?: string
  30. intervalS?: number
  31. lastPushMs: number
  32. fresh: boolean
  33. }
  34. export interface IntegrationListResponse {
  35. integrations: Integration[]
  36. /** Pushers installed by hand: they push, but the app cannot manage them. */
  37. unmanaged?: UnmanagedIntegration[]
  38. }
  39. export interface Vendor {
  40. id: string
  41. mode: 'local' | 'ssh'
  42. arches: string[]
  43. tagPrefix: string
  44. description: string
  45. }
  46. /** Catalog served by GET /api/integrations/vendors (#1697). */
  47. export interface VendorsResponse {
  48. vendors: Vendor[]
  49. /** Unix ms of the last successful fetch, 0 when only the seed was served. */
  50. lastRefreshedAt?: number
  51. }
  52. export interface CreateIntegrationInput {
  53. slug: string
  54. vendor: string
  55. host: string
  56. username: string
  57. password: string
  58. intervalS: number
  59. arch?: string
  60. }
  61. /** Editable fields of an existing integration. Omitted fields are left as they are. */
  62. export interface EditIntegrationInput {
  63. host?: string
  64. username?: string
  65. password?: string
  66. intervalS?: number
  67. }
  68. /** Error carrying the backend machine code plus a human-readable message. */
  69. export class ApiError extends Error {
  70. code?: string
  71. constructor(message: string, code?: string) {
  72. super(message)
  73. this.name = 'ApiError'
  74. this.code = code
  75. }
  76. }
  77. async function req<T>(input: string, init?: RequestInit): Promise<T> {
  78. const res = await fetch(input, init)
  79. if (!res.ok) {
  80. let code: string | undefined
  81. let msg = `HTTP ${res.status}`
  82. try {
  83. const body = (await res.json()) as { error?: string; message?: string }
  84. if (body?.error) code = body.error
  85. if (body?.message) msg = body.message
  86. else if (body?.error) msg = body.error
  87. } catch {
  88. /* default message */
  89. }
  90. throw new ApiError(msg, code)
  91. }
  92. if (res.status === 204) return undefined as T
  93. return res.json() as Promise<T>
  94. }
  95. const json = (method: string, body: unknown): RequestInit => ({
  96. method,
  97. headers: { 'Content-Type': 'application/json' },
  98. body: JSON.stringify(body),
  99. })
  100. export function fetchVendors(): Promise<VendorsResponse> {
  101. return req<VendorsResponse>('/api/integrations/vendors')
  102. }
  103. /** Forces the vendor catalog discovery (admin). Returns the refreshed catalog. */
  104. export function refreshVendors(): Promise<VendorsResponse> {
  105. return req<VendorsResponse>('/api/integrations/vendors/refresh', { method: 'POST' })
  106. }
  107. export function fetchIntegrations(): Promise<IntegrationListResponse> {
  108. return req<IntegrationListResponse>('/api/integrations')
  109. }
  110. export function createIntegration(
  111. input: CreateIntegrationInput,
  112. ): Promise<{ integration: Integration; token: string }> {
  113. return req<{ integration: Integration; token: string }>('/api/integrations', json('POST', input))
  114. }
  115. export function startIntegration(slug: string): Promise<{ integration: Integration }> {
  116. return req<{ integration: Integration }>(`/api/integrations/${encodeURIComponent(slug)}/start`, {
  117. method: 'POST',
  118. })
  119. }
  120. export function stopIntegration(slug: string): Promise<{ integration: Integration }> {
  121. return req<{ integration: Integration }>(`/api/integrations/${encodeURIComponent(slug)}/stop`, {
  122. method: 'POST',
  123. })
  124. }
  125. export function updateIntegration(slug: string): Promise<{ integration: Integration }> {
  126. return req<{ integration: Integration }>(`/api/integrations/${encodeURIComponent(slug)}/update`, {
  127. method: 'POST',
  128. })
  129. }
  130. export function editIntegration(
  131. slug: string,
  132. input: EditIntegrationInput,
  133. ): Promise<{ integration: Integration }> {
  134. return req<{ integration: Integration }>(
  135. `/api/integrations/${encodeURIComponent(slug)}`,
  136. json('PUT', input),
  137. )
  138. }
  139. export function checkIntegration(slug: string): Promise<{ integration: Integration; running: boolean }> {
  140. return req<{ integration: Integration; running: boolean }>(
  141. `/api/integrations/${encodeURIComponent(slug)}/check`,
  142. { method: 'POST' },
  143. )
  144. }
  145. export function deleteIntegration(slug: string): Promise<void> {
  146. return req<void>(`/api/integrations/${encodeURIComponent(slug)}`, { method: 'DELETE' })
  147. }