microcms_sdk_generator
Version:
A type-safe MicroCMS SDK Generator
23 lines (19 loc) • 1.03 kB
JavaScript
import { match } from "ts-pattern";
import { format } from "prettier";
import { defTypeSchemaName, outputTypeName, outputTypeSchemaName, typeName, typeSchemaName, } from "./helper.js";
export async function printOutputSchema({ endpointName, apiType, }) {
return await format(`
export const ${outputTypeSchemaName(endpointName)} = ${match(apiType)
.with("list", () => `makeListResponseSchema(${defTypeSchemaName(endpointName)})`)
.with("object", () => `${defTypeSchemaName(endpointName)}`)
.exhaustive()}
export type ${outputTypeName(endpointName)} = z.infer<typeof ${outputTypeSchemaName(endpointName)}>
export const ${typeSchemaName(endpointName)} = ${match(apiType)
.with("list", () => `${outputTypeSchemaName(endpointName)}.shape.contents.element`)
.with("object", () => `${outputTypeSchemaName(endpointName)}`)
.exhaustive()}
export type ${typeName(endpointName)} = z.infer<typeof ${typeSchemaName(endpointName)}>
`, {
parser: "typescript",
});
}