OpenAPi: Mark mediaAdapters API stable

This commit is contained in:
Fuma Nama
2025-05-21 22:42:11 +08:00
parent b0e88b6096
commit c945b5fee5
4 changed files with 61 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': minor
---
Mark `mediaAdapters` API stable
@@ -0,0 +1,54 @@
---
title: Media Adapters
description: 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.
```ts title="lib/media-adapters.ts" twoslash
'use client';
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")`;
}
},
};
```
```ts title="lib/source.ts"
import { createOpenAPI } from 'fumadocs-openapi/server';
import { myAdapter } from './media-adapters';
export const openapi = createOpenAPI({
proxyUrl: '/api/proxy',
mediaAdapters: {
// [!code ++] override the default adapter of `application/json`
'application/json': myAdapter,
},
});
```
+1
View File
@@ -1,3 +1,4 @@
export * from './generate';
export * from './generate-file';
export * from './types';
export type { MediaAdapter } from './media/adapter';
+1 -8
View File
@@ -1,14 +1,7 @@
import type { ApiPageProps } from '@/render/api-page';
import type { DocumentInput } from '@/utils/process-document';
import { createProxy } from '@/server/proxy';
export interface OpenAPIOptions
extends Omit<Partial<ApiPageProps>, 'document'> {
/**
* @deprecated Pass document to `APIPage` instead
*/
documentOrPath?: DocumentInput;
}
export type OpenAPIOptions = Omit<Partial<ApiPageProps>, 'document'>;
export interface OpenAPIServer {
getAPIPageProps: (from: ApiPageProps) => ApiPageProps;