11import { Code , Pre } from "nextra/components" ;
22import { CodeBlock } from "./code" ;
33
4- import React , { useState } from "react" ;
4+ import React from "react" ;
55import { ApiIntro , Col , Properties , Property , Row } from "./mdx" ;
6- import Tooltip from "./tooltip" ;
7- import Link from "next/link" ;
86
97interface PropertyType {
108 name : string ;
@@ -13,18 +11,6 @@ interface PropertyType {
1311 required ?: boolean ;
1412}
1513
16- // Helper function to replace path parameters in the URL
17- const replacePathParameters = (
18- path : string ,
19- pathParameters : Record < string , string > = { } ,
20- ) => {
21- let updatedPath = path ;
22- for ( const param in pathParameters ) {
23- updatedPath = updatedPath . replace ( `{${ param } }` , pathParameters [ param ] ) ;
24- }
25- return updatedPath ;
26- } ;
27-
2814const PythonCodeTab : React . FC < {
2915 method : "GET" | "POST" | "PUT" | "PATCH" | "DELETE" ;
3016 url : string ;
@@ -334,58 +320,6 @@ export const ApiEndpointRequestResponse: React.FC<{
334320 pathParameters ?: Record < string , string > ;
335321 isBearerTokenRequired ?: boolean ;
336322} > = ( { isBearerTokenRequired = true , ...properties } ) => {
337- const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
338- const [ requestPayload , setRequestPayload ] = useState < string | null > (
339- properties . samplePayload
340- ? JSON . stringify ( properties . samplePayload , null , 2 )
341- : null ,
342- ) ;
343-
344- const [ loading , setLoading ] = useState ( false ) ;
345- const [ error , setError ] = useState ( "" ) ;
346- const [ bearerToken , setBearerToken ] = useState ( "" ) ;
347- const [ actualResponse , setActualResponse ] = useState ( "" ) ;
348- const [ pathParameters , setPathParameters ] = useState (
349- properties . pathParameters || { } ,
350- ) ;
351-
352- const openModal = ( ) => {
353- setIsModalOpen ( true ) ;
354- } ;
355-
356- const closeModal = ( ) => {
357- setIsModalOpen ( false ) ;
358- } ;
359-
360- const hitRequestWithoutLogin = async ( ) => {
361- try {
362- setLoading ( true ) ;
363- setError ( "" ) ;
364- const requestPayloadJSON = JSON . parse ( requestPayload || "{}" ) ;
365- const bodyPayload = properties . samplePayload ?. code
366- ? JSON . stringify ( {
367- code : JSON . stringify ( properties . samplePayload . code ) ,
368- } )
369- : JSON . stringify ( requestPayloadJSON ) ;
370- const apiUrlWithParams = ( properties . apiUrl +
371- replacePathParameters ( properties . path , pathParameters ) ) as string ;
372- const response = await fetch ( apiUrlWithParams , {
373- method : properties . method ,
374- headers : {
375- "Content-Type" : "application/json" ,
376- Authorization : `Bearer ${ bearerToken } ` ,
377- } ,
378- body : properties . method . includes ( "GET" ) ? null : bodyPayload ,
379- } ) ;
380- const data = await response . json ( ) ;
381- const responseText = JSON . stringify ( data , null , 2 ) ;
382- setActualResponse ( responseText ) ;
383- } catch ( error ) {
384- setError ( `Error: ${ error . message } ` ) ;
385- } finally {
386- setLoading ( false ) ;
387- }
388- } ;
389323 return (
390324 < >
391325 < Row >
@@ -399,187 +333,8 @@ export const ApiEndpointRequestResponse: React.FC<{
399333 < span className = "nx-text-purple nx-font-normal dark:nx-text-indigo-250" >
400334 { properties . path }
401335 </ span >
402- { isModalOpen ? (
403- < button
404- className = "nx-bg-white nx-text-fetch-main nx-py-2 nx-px-4 nx-rounded-xxl dark:nx-text-white dark:nx-bg-indigo-500"
405- onClick = { closeModal }
406- >
407- Cancel
408- </ button >
409- ) : (
410- < button
411- className = "nx-bg-white nx-text-fetch-main nx-py-2 nx-px-4 nx-rounded-xxl dark:nx-text-white dark:nx-bg-indigo-500"
412- onClick = { openModal }
413- >
414- Run Code
415- </ button >
416- ) }
417336 </ p >
418337 </ Row >
419-
420- { isModalOpen && (
421- < div className = "nx-bg-grey nx-px-6 nx-py-8 nx-rounded nx-mt-12 dark:nx-bg-dark-mode-white-2" >
422- < div className = "nx-bg-white nextra-content nx-py-2 nx-px-4 nx-rounded dark:nx-bg-dark-mode-white-5" >
423- Parameters
424- </ div >
425-
426- < div className = "nx-flex nx-flex-col nx-text-sm" >
427- < div className = "nx-flex nx-mt-4 nx-ml-4" >
428- < div className = "nx-w-1/4" >
429- < p className = "nextra-content nx-text-sm dark:nx-text-white-60" >
430- Name
431- </ p >
432- </ div >
433- < div className = "nx-w-3/4" >
434- < p className = "nextra-content nx-text-sm dark:nx-text-white-60" >
435- Description
436- </ p >
437- </ div >
438- </ div >
439- < div className = "nx-mt-2 nx-mb-4 nx-border-t nx-border-gray-300" />
440- { isBearerTokenRequired && (
441- < div className = "md:nx-flex nx-block nx-items-center nx-ml-4" >
442- < div className = "nx-flex nx-gap-2 nx-w-1/4" >
443- < p className = "nextra-content nx-text-sm dark:nx-text-white-60" >
444- Bearer Token required
445- </ p >
446- < Tooltip >
447- < p className = "nx-text-sm nx-font-bold nx-text-gray-800 dark:nx-text-white-90 nx-pb-1" >
448- To access your Agentverse API key, please follow these
449- steps:
450- </ p >
451- < ol className = "nx-text-xs nx-leading-4 nx-text-gray-600 dark:nx-text-white-60 nx-pb-3 nx-list-decimal nx-mt-2 nx-p-[10px]" >
452- < li >
453- Log in to your{ " " }
454- < Link
455- target = "_blank"
456- className = "nx-text-blue-500"
457- href = "https://agentverse.ai/"
458- >
459- Agentverse
460- </ Link > { " " }
461- account.
462- </ li >
463- < li >
464- Once logged in, click on your profile icon in the
465- top-right corner.
466- </ li >
467- < li >
468- In the dropdown menu, select < b > API Keys</ b > .
469- </ li >
470- < li >
471- If you haven`t generated an API key yet, click on{ " " }
472- < b > Create New API Key</ b > .
473- </ li >
474- < li > Copy the generated API key and paste it here.</ li >
475- </ ol >
476- </ Tooltip >
477- </ div >
478-
479- < div className = "nx-w-3/4" >
480- < input
481- type = "text"
482- placeholder = "Bearer Token"
483- value = { bearerToken }
484- onChange = { ( e ) => setBearerToken ( e . target . value ) }
485- className = "nx-p-2 nx-rounded nx-border nx-border-gray-300 nx-mt-2 nx-w-full dark:nx-bg-dark-mode-white-10 dark:nx-border-none"
486- />
487- </ div >
488- </ div >
489- ) }
490-
491- { /* Render Path Parameters as input fields */ }
492- { Object . keys ( pathParameters ) . map ( ( paramName ) => (
493- < div className = "nx-flex nx-items-center nx-ml-4" key = { paramName } >
494- < div className = "nx-w-1/4" >
495- < p className = "nextra-content nx-text-sm" >
496- { paramName } required
497- </ p >
498- </ div >
499- < div className = "nx-w-3/4" >
500- < input
501- type = "text"
502- placeholder = { paramName }
503- value = { pathParameters [ paramName ] }
504- onChange = { ( e ) => {
505- // Update the path parameter value
506- const updatedPathParameters = { ...pathParameters } ;
507- updatedPathParameters [ paramName ] = e . target . value ;
508- setPathParameters ( updatedPathParameters ) ;
509- } }
510- className = "nx-p-2 nx-rounded nx-border nx-border-gray-300 nx-mt-2 nx-w-full dark:nx-bg-dark-mode-white-10 dark:nx-border-none"
511- />
512- </ div >
513- </ div >
514- ) ) }
515-
516- { /* Additional Sample Payload */ }
517- { requestPayload && (
518- < div className = "nx-flex nx-items-center nx-ml-4" >
519- < div className = "nx-w-1/4" >
520- < p className = "nextra-content nx-text-sm" >
521- Additional Sample Payload
522- </ p >
523- </ div >
524- < div className = "nx-w-3/4" >
525- < textarea
526- value = { requestPayload }
527- onChange = { ( e ) => setRequestPayload ( e . target . value ) }
528- style = { { resize : "none" } }
529- className = "nx-p-2 nx-rounded nx-border nx-border-gray-300 nextra-content nx-mt-2 nx-h-24 nx-w-full dark:nx-bg-dark-mode-white-10 dark:nx-border-none"
530- />
531- </ div >
532- </ div >
533- ) }
534-
535- { /* Execute Button */ }
536- < div className = "nx-flex nx-ml-4" >
537- < div className = "nx-w-1/4" >
538- < button
539- className = "nx-bg-purple hover:nx-bg-purple-500 nx-text-white nx-py-2 nx-px-4 nx-rounded-xxl nx-text-sm nx-mt-6"
540- onClick = { hitRequestWithoutLogin }
541- >
542- Execute
543- </ button >
544- </ div >
545- </ div >
546-
547- { loading && < div className = "nx-mt-4 nx-ml-4" > Loading...</ div > }
548-
549- { error && (
550- < div className = "nx-mt-4 nx-text-red-500 nx-ml-4" > { error } </ div >
551- ) }
552-
553- { /* Display Actual Response */ }
554- { actualResponse && (
555- < div className = "nx-mt-6" >
556- < div className = "nx-bg-white nextra-content nx-text-base nx-py-2 nx-px-4 nx-rounded dark:nx-bg-dark-mode-white-5" >
557- Actual Response
558- </ div >
559- < div className = "nx-bg-white nx-rounded nx-p-2 nx-mt-4 dark:nx-bg-dark-mode-white-10" >
560- < pre className = "nx-whitespace-pre-wrap dark:nx-bg-dark-mode-white-10" >
561- { actualResponse }
562- </ pre >
563- </ div >
564- </ div >
565- ) }
566-
567- { /* Display Sample Response if Available */ }
568- { ! actualResponse && properties . responses && (
569- < div className = "nx-mt-6" >
570- < div className = "nx-bg-white nextra-content nx-text-base nx-py-2 nx-px-4 nx-rounded dark:nx-bg-dark-mode-white-5" >
571- Sample Response
572- </ div >
573- < div className = "nx-bg-white nx-rounded nx-p-2 nx-mt-4 dark:nx-bg-dark-mode-white-5" >
574- < pre className = "nx-whitespace-pre-wrap dark:nx-bg-dark-mode-white-5" >
575- { JSON . stringify ( properties . responses , null , 2 ) }
576- </ pre >
577- </ div >
578- </ div >
579- ) }
580- </ div >
581- </ div >
582- ) }
583338 < ApiRequest
584339 apiUrl = { properties . apiUrl }
585340 method = { properties . method }
0 commit comments