Fumadocs
Integrations/OpenAPI

Media Adapters

Support other media types

Overview

A media adapter in Fumadocs supports:

  • Converting value into fetch() body compatible with corresponding media type.
  • Generate code example based on different programming language/tool.

Put your media adapters in a separate file.

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")`;
    }
  },
};

Pass the adapter.

lib/source.ts
import { createOpenAPI } from 'fumadocs-openapi/server';
import * as Adapters from './media-adapters';
import * as ClientAdapters from './media-adapters.client';

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

How is this guide?

Last updated on

On this page