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 myAdapter: MediaAdapter = {
  encode(data) {
    return JSON.stringify(data.body);
  },
  // returns code that inits a `body` variable, used for request body
  generateExample(data, ctx) {
    if (ctx.lang === 'js') {
      return `const body = "hello world"`;
    }
    if (ctx.lang === 'python') {
      return `body = "hello world"`;
    }
    if (ctx.lang === 'go' && 'addImport' in ctx) {
      ctx.addImport('strings');
      return `body := strings.NewReader("hello world")`;
    }
  },
};Pass the adapter.
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
