client.ts 3.6 KB

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