Files
gitbook/packages/react-openapi/src/code-samples.ts
T
Samy Pessé 85ec1884e6 OpenAPI blocks (#74)
* Start methods to resolve refs

* bun

* Start package

* Add code to resolve ref

* Continue

* Deref

* Start monorepo

* Continue

* Fix ref resolver

* Style a bit more

* Start toggling section

* Continue

* Start displaying query/path/headers params

* Hide respo se if empty

* Fix recursive refs

* Simplify interactive and styling

* Display enums

* Format

* Improve type name being displayed

* Improve naming

* Render oneOf/anyOf/allOf

* Handle circular references

* Start variable in server url

* Use client component for the spec part

* Improve CSS sizing

* Display open api blocks in aside

* Make aside an overlay

* Improve stickiness of openapi

* Align document on the left when api page

* Improve general layout

* Better align

* Fix padding in aside

* Use syntax highlighting

* Show sample of response

* Improve code generation

* Improve code generated

* Format

* Format

* Rename to OpenAPI

* Skip deprecated properties and handle additionalProperties

* Use discriminator for naming

* Better name enum

* Make entire header toggeable

* Format and lint

* Add curl example

* Improve curl

* style pass

* Start securities

* Bun

* Fix TS

* Improve label

* Start markdown

* Improve code samples

* Test

* Use custom skeleton for api block

* Format

* Add support for redocly code samples

* Fix api blocks in aside

* Use typography for markdown

* Format

* Fix spacing in markdown

* Render headers

* Format

* Format

---------

Co-authored-by: Sebastian Graz <graz@live.se>
2024-01-30 10:15:58 +01:00

77 lines
2.0 KiB
TypeScript

export interface CodeSampleInput {
method: string;
url: string;
headers?: Record<string, string>;
body?: any;
}
interface CodeSampleGenerator {
id: string;
label: string;
syntax: string;
generate: (operation: CodeSampleInput) => string;
}
export const codeSampleGenerators: CodeSampleGenerator[] = [
{
id: 'javascript',
label: 'JavaScript',
syntax: 'javascript',
generate: ({ method, url, headers, body }) => {
let code = '';
code += `const response = await fetch('${url}', {
method: '${method.toUpperCase()}',\n`;
if (headers) {
code += indent(`headers: ${JSON.stringify(headers, null, 2)},\n`, 4);
}
if (body) {
code += indent(`body: JSON.stringify(${JSON.stringify(body, null, 2)}),\n`, 4);
}
code += `});\n`;
code += `const data = await response.json();`;
return code;
},
},
{
id: 'curl',
label: 'Curl',
syntax: 'bash',
generate: ({ method, url, headers, body }) => {
const separator = ' \\\n';
const lines: string[] = ['curl -L'];
if (method.toUpperCase() !== 'GET') {
lines.push(`-X ${method.toUpperCase()}`);
}
if (headers) {
Object.entries(headers).forEach(([key, value]) => {
lines.push(`-H '${key}: ${value}'`);
});
}
lines.push(`'${url}'`);
if (body) {
lines.push(`-d '${JSON.stringify(body)}'`);
}
return lines.map((line, index) => (index > 0 ? indent(line, 2) : line)).join(separator);
},
},
];
function indent(code: string, spaces: number) {
const indent = ' '.repeat(spaces);
return code
.split('\n')
.map((line) => (line ? indent + line : ''))
.join('\n');
}