Fumadocs
Integrations/OpenAPI

Media Adapters

Support other media types

Overview

A media adapter in Fumadocs handle:

  • Encode: convert form data into fetch() body with corresponding media type.
  • Example: generate code example based on different programming language/tool.

Put your media adapters in a separate file with use client directive.

lib/media-adapters.ts
'use client';

import type { MediaAdapter } from 'fumadocs-openapi';

export const : MediaAdapter = {
  () {
    return .(.);
  },
  // returns code that inits a `body` variable, used for request body
  (, ) {
    if (. === 'js') {
      return `const body = "hello world"`;
    }

    if (. === 'python') {
      return `body = "hello world"`;
    }

    if (. === 'go' && 'addImport' in ) {
      .('strings');

      return `body := strings.NewReader("hello world")`;
    }
  },
};
lib/source.ts
import { createOpenAPI } from 'fumadocs-openapi/server';
import { myAdapter } from './media-adapters';

export const openapi = createOpenAPI({
  proxyUrl: '/api/proxy',
  mediaAdapters: {
    // override the default adapter of `application/json`
    'application/json': myAdapter,
  },
});

How is this guide?

Last updated on

On this page