|
1
|
"use client" |
|
2
|
|
|
3
|
import React from "react" |
|
4
|
import { useEffect, useMemo, useState } from "react" |
|
5
|
import { useRequestRunner } from "../../hooks/use-request-runner" |
|
6
|
import { CodeBlock } from "../CodeBlock" |
|
7
|
import { Button } from "@/components/Button" |
|
8
|
import { ApiMethod, ApiTestingOptions } from "types" |
|
9
|
import { ApiRunnerParamInputs } from "./ParamInputs" |
|
10
|
import clsx from "clsx" |
|
11
|
import { ArrowDownRightMini } from "@medusajs/icons" |
|
12
|
import { ArrowRightDownIcon } from "../Icons/ArrowRightDown" |
|
13
|
import { ApiRunnerFooterBackground } from "./FooterBackground" |
|
14
|
|
|
15
|
type ApiRunnerProps = { |
|
16
|
apiMethod: ApiMethod |
|
17
|
apiUrl: string |
|
18
|
pathData?: Record<string, unknown> |
|
19
|
queryData?: Record<string, unknown> |
|
20
|
bodyData?: Record<string, unknown> |
|
21
|
} |
|
22
|
|
|
23
|
export const ApiRunner = React.forwardRef<HTMLDivElement, ApiRunnerProps>( |
|
24
|
function ApiRunner( |
|
25
|
{ apiMethod, apiUrl, pathData, bodyData, queryData }: ApiRunnerProps, |
|
26
|
ref |
|
27
|
) { |
|
28
|
// assemble api testing options |
|
29
|
const [apiTestingOptions, setApiTestingOptions] = |
|
30
|
useState<ApiTestingOptions>({ |
|
31
|
method: apiMethod, |
|
32
|
url: apiUrl, |
|
33
|
pathData, |
|
34
|
bodyData, |
|
35
|
queryData, |
|
36
|
}) |
|
37
|
const [isRunning, setIsRunning] = useState(false) |
|
38
|
const [ran, setRan] = useState(false) |
|
39
|
const hasData = (data?: Record<string, unknown>): boolean => |
|
40
|
data !== undefined && Object.keys(data).length > 0 |
|
41
|
// TODO change to be based on whether auth/data needed |
|
42
|
const manualTestTrigger = useMemo( |
|
43
|
() => |
|
44
|
hasData(apiTestingOptions.pathData) || |
|
45
|
hasData(apiTestingOptions.queryData) || |
|
46
|
hasData(apiTestingOptions.bodyData), |
|
47
|
[apiTestingOptions] |
|
48
|
) |
|
49
|
const [responseLogs, setResponseLogs] = useState<string[]>([]) |
|
50
|
const [responseCode, setResponseCode] = useState<string | undefined>() |
|
51
|
const pushMessage = (...message: string[]) => |
|
52
|
setResponseLogs((prev) => [...prev, ...message]) |
|
53
|
const { runRequest } = useRequestRunner({ |
|
54
|
pushLog: pushMessage, |
|
55
|
onFinish: (_message, statusCode) => { |
|
56
|
setIsRunning(false) |
|
57
|
setResponseCode(statusCode) |
|
58
|
}, |
|
59
|
replaceLog: (message) => setResponseLogs([message]), |
|
60
|
}) |
|
61
|
|
|
62
|
useEffect(() => { |
|
63
|
if (!isRunning && !manualTestTrigger && !ran) { |
|
64
|
setIsRunning(true) |
|
65
|
} |
|
66
|
}, [apiTestingOptions, manualTestTrigger, isRunning, ran]) |
|
67
|
|
|
68
|
useEffect(() => { |
|
69
|
if (isRunning && !ran) { |
|
70
|
setRan(true) |
|
71
|
setResponseLogs(["Sending request..."]) |
|
72
|
runRequest(apiTestingOptions) |
|
73
|
} |
|
74
|
}, [isRunning, ran]) |
|
75
|
|
|
76
|
return ( |
|
77
|
<div className="mb-docs_1" ref={ref}> |
|
78
|
{manualTestTrigger && ( |
|
79
|
<div |
|
80
|
className={clsx( |
|
81
|
"bg-medusa-bg-component rounded-docs_DEFAULT", |
|
82
|
"shadow-elevation-card-rest dark:shadow-elevation-card-rest-dark", |
|
83
|
"mb-docs_0.75" |
|
84
|
)} |
|
85
|
> |
|
86
|
<div |
|
87
|
className={clsx( |
|
88
|
"flex items-center gap-docs_0.75", |
|
89
|
"px-docs_0.75 pb-docs_0.75 pt-docs_0.5" |
|
90
|
)} |
|
91
|
> |
|
92
|
<ArrowDownRightMini className="text-medusa-fg-muted" /> |
|
93
|
<div className="flex-1 flex items-center gap-docs_0.75"> |
|
94
|
{apiTestingOptions.pathData && ( |
|
95
|
<ApiRunnerParamInputs |
|
96
|
data={apiTestingOptions.pathData} |
|
97
|
title="Path Parameters" |
|
98
|
baseObjPath="pathData" |
|
99
|
setValue={ |
|
100
|
setApiTestingOptions as React.Dispatch< |
|
101
|
React.SetStateAction<unknown> |
|
102
|
> |
|
103
|
} |
|
104
|
/> |
|
105
|
)} |
|
106
|
{apiTestingOptions.bodyData && ( |
|
107
|
<ApiRunnerParamInputs |
|
108
|
data={apiTestingOptions.bodyData} |
|
109
|
title="Request Body Parameters" |
|
110
|
baseObjPath="bodyData" |
|
111
|
setValue={ |
|
112
|
setApiTestingOptions as React.Dispatch< |
|
113
|
React.SetStateAction<unknown> |
|
114
|
> |
|
115
|
} |
|
116
|
/> |
|
117
|
)} |
|
118
|
{apiTestingOptions.queryData && ( |
|
119
|
<ApiRunnerParamInputs |
|
120
|
data={apiTestingOptions.queryData} |
|
121
|
title="Request Query Parameters" |
|
122
|
baseObjPath="queryData" |
|
123
|
setValue={ |
|
124
|
setApiTestingOptions as React.Dispatch< |
|
125
|
React.SetStateAction<unknown> |
|
126
|
> |
|
127
|
} |
|
128
|
/> |
|
129
|
)} |
|
130
|
</div> |
|
131
|
<ArrowRightDownIcon className="text-medusa-fg-muted" /> |
|
132
|
</div> |
|
133
|
<div |
|
134
|
className={clsx( |
|
135
|
"border-t border-medusa-border-base", |
|
136
|
"py-docs_0.5 px-docs_0.75 relative", |
|
137
|
"flex justify-end items-center gap-docs_0.5" |
|
138
|
)} |
|
139
|
> |
|
140
|
<ApiRunnerFooterBackground /> |
|
141
|
<Button |
|
142
|
onClick={() => { |
|
143
|
setIsRunning(true) |
|
144
|
setRan(false) |
|
145
|
}} |
|
146
|
className="relative" |
|
147
|
variant="secondary" |
|
148
|
> |
|
149
|
Send Request |
|
150
|
</Button> |
|
151
|
</div> |
|
152
|
</div> |
|
153
|
)} |
|
154
|
{(isRunning || ran) && ( |
|
155
|
<CodeBlock |
|
156
|
source={responseLogs.join("\n")} |
|
157
|
lang="json" |
|
158
|
title="Testing Result" |
|
159
|
collapsed={true} |
|
160
|
noReport={true} |
|
161
|
badgeLabel={responseCode || "Failed"} |
|
162
|
badgeColor={ |
|
163
|
!responseCode |
|
164
|
? "red" |
|
165
|
: responseCode.startsWith("2") |
|
166
|
? "green" |
|
167
|
: "red" |
|
168
|
} |
|
169
|
/> |
|
170
|
)} |
|
171
|
</div> |
|
172
|
) |
|
173
|
} |
|
174
|
) |