Skip to content

Commit

Permalink
Add dashifyObjectKeys helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Jul 12, 2024
1 parent 36ac0dd commit 98926e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 3 additions & 4 deletions packages/nextlove/src/generators/generate-openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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"
import { dashifyObjectKeys } from "../utils/dashify-object-keys"

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

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

const route: OperationObject = {
Expand Down
9 changes: 6 additions & 3 deletions packages/nextlove/src/generators/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { AnyZodObject, z, ZodTypeAny } from "zod"
import { parseFrontMatter, testFrontMatter } from "./front-matter"
import dedent from "dedent"
import { prefixKeysWithX } from "../utils/prefix-keys-with-x"
import { dashifyObjectKeys } from "../utils/dashify-object-keys"

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

Expand Down Expand Up @@ -89,9 +90,11 @@ function parseDescription(zodRef: OpenApiZodAny): SchemaObject {
output.deprecated = true
}

Object.entries(prefixKeysWithX(attributes)).forEach(([key, value]) => {
output[key] = value
})
Object.entries(prefixKeysWithX(dashifyObjectKeys(attributes))).forEach(
([key, value]) => {
output[key] = value
}
)
}

return output
Expand Down
13 changes: 13 additions & 0 deletions packages/nextlove/src/generators/utils/dashify-object-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type SnakeToDash<T> = {
[K in keyof T as K extends string ? SnakeToDashString<K> : K]: T[K]
}

type SnakeToDashString<S extends string> = S extends `${infer T}_${infer U}`
? `${T}-${SnakeToDashString<U>}`
: S

export function dashifyObjectKeys<T extends object>(obj: T): SnakeToDash<T> {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key.replace(/_/g, "-"), value])
) as SnakeToDash<T>
}

0 comments on commit 98926e5

Please sign in to comment.