client.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import type {
  2. ContentItemsResponse,
  3. DashboardResponse,
  4. FlowLedgerApiResponse,
  5. FlowLedgerVideoResponse,
  6. FlowLedgerVideosResponse,
  7. FlowLedgerWalkResponse,
  8. QueryListResponse,
  9. RunListResponse,
  10. TimelineResponse
  11. } from "./types";
  12. const DEFAULT_API_BASE_URL = "http://47.245.103.121:8000";
  13. export class ApiError extends Error {
  14. status: number;
  15. detail: unknown;
  16. constructor(message: string, status: number, detail: unknown) {
  17. super(message);
  18. this.name = "ApiError";
  19. this.status = status;
  20. this.detail = detail;
  21. }
  22. }
  23. export function apiBaseUrl() {
  24. return (
  25. process.env.NEXT_PUBLIC_CONTENTFIND_API_BASE_URL ||
  26. process.env.VITE_CONTENTFIND_API_BASE_URL ||
  27. DEFAULT_API_BASE_URL
  28. ).replace(/\/$/, "");
  29. }
  30. async function request<T>(path: string): Promise<T> {
  31. const response = await fetch(`${apiBaseUrl()}${path}`, {
  32. headers: { Accept: "application/json" },
  33. cache: "no-store"
  34. });
  35. if (!response.ok) {
  36. let detail: unknown = null;
  37. try {
  38. detail = await response.json();
  39. } catch {
  40. detail = await response.text();
  41. }
  42. throw new ApiError(`request failed: ${path}`, response.status, detail);
  43. }
  44. return response.json() as Promise<T>;
  45. }
  46. export function listRuns(params = new URLSearchParams()) {
  47. const query = params.toString();
  48. return request<RunListResponse>(`/runs${query ? `?${query}` : ""}`);
  49. }
  50. export function getDashboard(runId: string) {
  51. return request<DashboardResponse>(`/runs/${encodeURIComponent(runId)}/dashboard`);
  52. }
  53. export function getQueries(runId: string) {
  54. return request<QueryListResponse>(`/runs/${encodeURIComponent(runId)}/queries`);
  55. }
  56. export function getTimeline(runId: string) {
  57. return request<TimelineResponse>(`/runs/${encodeURIComponent(runId)}/timeline`);
  58. }
  59. export function getContentItems(runId: string) {
  60. return request<ContentItemsResponse>(`/runs/${encodeURIComponent(runId)}/content-items`);
  61. }
  62. function debugSuffix(debug = false) {
  63. return debug ? "?debug=true" : "";
  64. }
  65. export function getFlowLedger(runId: string, debug = false) {
  66. return request<FlowLedgerApiResponse>(
  67. `/runs/${encodeURIComponent(runId)}/flow-ledger${debugSuffix(debug)}`
  68. );
  69. }
  70. export function getFlowLedgerVideos(runId: string, queryId: string, debug = false) {
  71. return request<FlowLedgerVideosResponse>(
  72. `/runs/${encodeURIComponent(runId)}/flow-ledger/queries/${encodeURIComponent(queryId)}/videos${debugSuffix(debug)}`
  73. );
  74. }
  75. export function getFlowLedgerWalk(runId: string, queryId: string, debug = false) {
  76. return request<FlowLedgerWalkResponse>(
  77. `/runs/${encodeURIComponent(runId)}/flow-ledger/queries/${encodeURIComponent(queryId)}/walk${debugSuffix(debug)}`
  78. );
  79. }
  80. export function getFlowLedgerVideo(runId: string, contentId: string, debug = false) {
  81. return request<FlowLedgerVideoResponse>(
  82. `/runs/${encodeURIComponent(runId)}/flow-ledger/videos/${encodeURIComponent(contentId)}${debugSuffix(debug)}`
  83. );
  84. }