Skip to content

Commit

Permalink
Add prefixKeysWithX helper, format custom props added to the openapi …
Browse files Browse the repository at this point in the history
…spec, adjust test
  • Loading branch information
andrii-balitskyi committed Jul 12, 2024
1 parent 96ebcd4 commit cc872cc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test("generateOpenAPI correctly parses description with front matter", async (t)
routeSpec.description.trim(),
"This endpoint allows you to add a new todo item to the list. Deprecated."
)
t.is(routeSpec.deprecated, "Use foobar instead.")
t.is(routeSpec['x-deprecated'], "Use foobar instead.")
t.is(routeSpec["x-fern-sdk-return-value"], "foobar")
t.is(routeSpec.response_key, "foobar")
t.is(routeSpec["x-response-key"], "foobar")
})
9 changes: 8 additions & 1 deletion packages/nextlove/src/generators/generate-openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { embedSchemaReferences } from "./embed-schema-references"
import { mapMethodsToFernSdkMetadata } from "./fern-sdk-utils"
import { parseFrontMatter, testFrontMatter } from "../lib/front-matter"
import dedent from "dedent"
import { prefixKeysWithX } from "../utils/prefix-keys-with-x"

function replaceFirstCharToLowercase(str: string) {
if (str.length === 0) {
Expand Down Expand Up @@ -210,9 +211,15 @@ export async function generateOpenAPI(opts: GenerateOpenAPIOpts) {
}
}

const formattedDescriptionMetadata = Object.fromEntries(
Object.entries(prefixKeysWithX(descriptionMetadata)).map(
([key, value]) => [key.replace(/_/g, "-"), value]
)
)

const route: OperationObject = {
...routeSpec.openApiMetadata,
...descriptionMetadata,
...formattedDescriptionMetadata,
summary: routePath,
...(description && { description }),
responses: {
Expand Down
9 changes: 5 additions & 4 deletions packages/nextlove/src/generators/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import merge from "ts-deepmerge"
import { AnyZodObject, z, ZodTypeAny } from "zod"
import { parseFrontMatter, testFrontMatter } from "./front-matter"
import dedent from "dedent"
import { prefixKeysWithX } from "../utils/prefix-keys-with-x"

type AnatineSchemaObject = SchemaObject & { hideDefinitions?: string[] }

Expand Down Expand Up @@ -81,16 +82,16 @@ function parseDescription(zodRef: OpenApiZodAny): SchemaObject {
if (!testFrontMatter(trimmedDescription))
return { description: zodRef.description }
const { attributes, body } = parseFrontMatter(trimmedDescription)
const output: SchemaObject = {}
let output: SchemaObject = {}
if (body.trim()) output.description = body.trim()
if (typeof attributes === "object" && attributes !== null) {
if ("deprecated" in attributes && attributes.deprecated) {
output.deprecated = true
}
for (const [key, value] of Object.entries(attributes)) {
output[`x-${key}`] = value
}

output = prefixKeysWithX(output)
}

return output
}

Expand Down
9 changes: 9 additions & 0 deletions packages/nextlove/src/generators/utils/prefix-keys-with-x.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type PrefixedObject<T> = {
[K in keyof T as `x-${string & K}`]: T[K]
}

export function prefixKeysWithX<T extends object>(obj: T): PrefixedObject<T> {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [`x-${key}`, value])
) as PrefixedObject<T>
}

0 comments on commit cc872cc

Please sign in to comment.