Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | 'use client'; |
| 2 | |
| 3 | import { |
| 4 | createContext, createElement, use, useEffect, useEffectEvent, useMemo, |
| 5 | type FC, type ReactNode, |
| 6 | } from 'react'; |
| 7 | import { |
| 8 | createOpenAPIPage, useOperationContext, useServerContext, type APIPlaygroundProps, |
| 9 | } from 'fumadocs-openapi/ui'; |
| 10 | import { hideDeprecatedFields } from '@/lib/openapi-deprecated'; |
| 11 | import { generateSchemaData } from './schema-generator'; |
| 12 | import { CustomSchemaUI } from './custom-schema-ui'; |
| 13 | import { apiCodeUsages } from '@/lib/api-code-usages'; |
| 14 | |
| 15 | export const APIPage = createOpenAPIPage({ |
| 16 | codeUsages: apiCodeUsages, |
| 17 | generateTypeScriptDefinitions: false, |
| 18 | playground: { |
| 19 | enabled: true, |
| 20 | render: (props) => <DeprecatedFieldsPlayground {...props} />, |
| 21 | }, |
| 22 | schemaUI: { |
| 23 | render: (options, ctx) => { |
| 24 | const client = ( |
| 25 | options as typeof options & { |
| 26 | client: { name: string; required?: boolean; as?: 'property' | 'body' }; |
| 27 | } |
| 28 | ).client; |
| 29 | // Skip rendering the shared Error schema on error responses - |
| 30 | // the status code and description are shown by the accordion already |
| 31 | // options.root can be boolean for simple schemas, only check refs for objects |
| 32 | const ref = typeof options.root === 'object' ? getRawRef(options.root) : null; |
| 33 | if (ref === '#/components/schemas/Error') return null; |
| 34 | |
| 35 | const generated = generateSchemaData( |
| 36 | { |
| 37 | // Fumadocs' public type permits non-string `type` values even though |
| 38 | // JSON Schema does not; the renderer's local type keeps that contract strict. |
| 39 | root: options.root as Parameters<typeof generateSchemaData>[0]['root'], |
| 40 | readOnly: options.readOnly, |
| 41 | writeOnly: options.writeOnly, |
| 42 | }, |
| 43 | { |
| 44 | renderMarkdown: resolveRenderMarkdown(ctx), |
| 45 | // Schema nodes can be unresolved Reference Objects. |
| 46 | schema: { getRawRef, resolve: ctx.schema.resolve }, |
| 47 | } |
| 48 | ); |
| 49 | // Parameters, request bodies, and responses all pass through this hook: |
| 50 | // parameter -> client { name, required } readOnly = method === 'get' |
| 51 | // request body -> client { name: 'body', as: 'body', … } readOnly = method === 'get' |
| 52 | // response -> client { name: 'response', as: 'body' } readOnly = true |
| 53 | // `readOnly` cannot distinguish GET inputs from responses; the client name can. |
| 54 | const isResponse = client.name === 'response'; |
| 55 | return ( |
| 56 | <CustomSchemaUI |
| 57 | name={client.name} |
| 58 | required={client.required} |
| 59 | as={client.as} |
| 60 | generated={generated} |
| 61 | isResponse={isResponse} |
| 62 | /> |
| 63 | ); |
| 64 | }, |
| 65 | }, |
| 66 | }); |
| 67 | |
| 68 | // A separate page context keeps filtered schemas and initial request examples |
| 69 | // inside the playground. The surrounding reference uses the complete contract. |
| 70 | const PlaygroundSyncContext = createContext<{ |
| 71 | operation: ReturnType<typeof useOperationContext>; |
| 72 | server: ReturnType<typeof useServerContext>; |
| 73 | } | null>(null); |
| 74 | |
| 75 | function PlaygroundSync({ children }: { children: ReactNode }) { |
| 76 | const parent = use(PlaygroundSyncContext)!; |
| 77 | const { example, setExample, addListener, removeListener } = useOperationContext(); |
| 78 | const { server } = useServerContext(); |
| 79 | useEffect(() => { |
| 80 | if (parent.operation.example && parent.operation.example !== example) |
| 81 | setExample(parent.operation.example); |
| 82 | }, [parent.operation.example, example, setExample]); |
| 83 | useEffect(() => { |
| 84 | const listener = parent.operation.setExampleData; |
| 85 | addListener(listener); |
| 86 | return () => removeListener(listener); |
| 87 | }, [addListener, removeListener, parent.operation.setExampleData]); |
| 88 | const syncServer = useEffectEvent(() => { |
| 89 | if (!server) return; |
| 90 | parent.server.setServer(server.url); |
| 91 | parent.server.setServerVariables(server.variables); |
| 92 | }); |
| 93 | useEffect(() => { |
| 94 | syncServer(); |
| 95 | }, [server]); |
| 96 | return children; |
| 97 | } |
| 98 | |
| 99 | const PlaygroundPage = createOpenAPIPage({ |
| 100 | generateTypeScriptDefinitions: false, |
| 101 | content: { |
| 102 | renderOperationLayout: ({ apiPlayground }) => <PlaygroundSync>{apiPlayground}</PlaygroundSync>, |
| 103 | }, |
| 104 | }); |
| 105 | |
| 106 | function DeprecatedFieldsPlayground({ path, method, ctx }: APIPlaygroundProps) { |
| 107 | const operation = useOperationContext(); |
| 108 | const server = useServerContext(); |
| 109 | const bundled = useMemo( |
| 110 | () => hideDeprecatedFields(ctx.schema.bundled), |
| 111 | [ctx.schema.bundled] |
| 112 | ); |
| 113 | return ( |
| 114 | <PlaygroundSyncContext value={{ operation, server }}> |
| 115 | <PlaygroundPage |
| 116 | payload={{ bundled, proxyUrl: ctx.proxyUrl }} |
| 117 | operations={[{ path, method }]} |
| 118 | /> |
| 119 | </PlaygroundSyncContext> |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | function getRawRef(value: object): string | undefined { |
| 124 | if (!('$ref' in value)) return undefined; |
| 125 | return typeof value.$ref === 'string' ? value.$ref : undefined; |
| 126 | } |
| 127 | |
| 128 | // Structural subset of fumadocs-openapi's `RenderContext` covering only the |
| 129 | // markdown-rendering fields we read below. Kept narrow (instead of importing |
| 130 | // the internal `RenderContext` type) so this stays valid against whatever |
| 131 | // shape `ctx` actually has at the call site. |
| 132 | interface MarkdownRenderSource { |
| 133 | components?: { Markdown?: FC<{ md: string }> }; |
| 134 | /** @deprecated Prefer `components.Markdown`. */ |
| 135 | renderMarkdown?: (md: string) => ReactNode; |
| 136 | /** @private fumadocs-internal fallback; not part of the public API */ |
| 137 | _default_processMarkdown?: (md: string) => ReactNode; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Resolves the markdown renderer used by the schema generator, preferring |
| 142 | * supported APIs and degrading safely if fumadocs removes its private field: |
| 143 | * |
| 144 | * 1. `components.Markdown` - adapted from a React FC to `(text) => ReactNode`. |
| 145 | * 2. `renderMarkdown` - the deprecated user-configurable option, honoured if |
| 146 | * someone sets it. |
| 147 | * 3. `_default_processMarkdown` - fumadocs' private default renderer. |
| 148 | * 4. Plain text - avoids throwing if no renderer is available. |
| 149 | */ |
| 150 | function resolveRenderMarkdown(ctx: MarkdownRenderSource): (text: string) => ReactNode { |
| 151 | const Markdown = ctx.components?.Markdown; |
| 152 | if (Markdown) { |
| 153 | // Not a component definition despite returning an element -- it's the |
| 154 | // `(text) => ReactNode` callback the schema generator invokes directly. |
| 155 | // eslint-disable-next-line react/display-name -- render callback, never mounted as a component |
| 156 | return (text: string) => createElement(Markdown, { md: text }); |
| 157 | } |
| 158 | return ctx.renderMarkdown ?? ctx._default_processMarkdown ?? (text => text); |
| 159 | } |