client.ts 3.3 KB

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