Created
September 1, 2022 13:34
-
-
Save joeydebreuk/5cbe4392f551edde0b52917f6926b346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {z} from "zod"; | |
import {extendZodWithOpenApi, OpenAPIGenerator, OpenAPIRegistry} from "@asteasolutions/zod-to-openapi"; | |
// We can now use `.openapi()` to specify OpenAPI metadata, eg z.string().openapi({ description: 'Some string' }); | |
extendZodWithOpenApi(z); | |
const registry = new OpenAPIRegistry(); | |
const HALLink = z.object({ | |
api: z.string().openapi({ example: 'https://egeniq.com/page' }), | |
web: z.string().openapi({ example: 'https://egeniq.com/page' }), | |
}) | |
const InternalTextLink = z.object({ | |
type: z.literal("text-link"), | |
text: z.string(), | |
link: HALLink, | |
}) | |
const LoadMore = z.object({ | |
type: z.literal("load-more"), | |
link: HALLink, | |
}) | |
const generateOpenAPI = () => { | |
registry.register('HALLink', HALLink); | |
registry.register('InternalTextLink', InternalTextLink); | |
registry.register('LoadMore', LoadMore); | |
const generator = new OpenAPIGenerator(registry.definitions) | |
console.dir(generator.generateComponents(), { depth: null }) | |
console.dir(generator.generateDocument({ | |
openapi: '3.0.0', | |
info: { | |
version: '1.0.0', | |
title: 'My API', | |
description: 'This is the API', | |
}, | |
servers: [{url: 'v1'}], | |
}), { depth: null }) | |
} | |
generateOpenAPI() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment