Skip to content

Commit

Permalink
add sdkReturnValue to route_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
itelo committed Aug 17, 2023
1 parent 4958620 commit 08f9be4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 71 deletions.
22 changes: 0 additions & 22 deletions packages/nextlove/src/generators/generate-openapi/ask-question.ts

This file was deleted.

64 changes: 16 additions & 48 deletions packages/nextlove/src/generators/generate-openapi/fern-sdk-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { SchemaObject } from "openapi3-ts/oas31"
import { RouteSpec } from "../../types"
import { askQuestion } from "./ask-question"
import chalk from "chalk"

function transformPathToFernSdkMethodName(path: string) {
const parts = path.split("/").filter((part) => part !== "")
Expand All @@ -21,7 +18,10 @@ function transformPathToFernSdkGroupName(path: string) {
return parts.slice(0, parts.length - 1)
}

function getFernSdkMetadata(path: string, keyToUseAsReturnValue: string):
function getFernSdkMetadata(
path: string,
sdkReturnValue?: string
):
| {
"x-fern-ignore": true
}
Expand All @@ -38,56 +38,24 @@ function getFernSdkMetadata(path: string, keyToUseAsReturnValue: string):
return {
"x-fern-sdk-group-name": transformPathToFernSdkGroupName(path),
"x-fern-sdk-method-name": transformPathToFernSdkMethodName(path),
...(keyToUseAsReturnValue ? {
"x-fern-sdk-return-value": keyToUseAsReturnValue,
}: {})
}
}


async function askForWhichKeyToUseAsReturnValue(
keys: string[],
path: string
) {
if (keys.length === 1) {
return keys[0]
...(sdkReturnValue
? {
"x-fern-sdk-return-value": sdkReturnValue,
}
: {}),
}

const choices = [...keys, "none"]
let answer: string = await askQuestion(
`Which key should be used as the return value for ${chalk.green(path)}?\n`,
choices
)

return answer
}

export async function mapMethodsToFernSdkMetadata({
methods,
path,
responseSchema,
}:{
methods: RouteSpec["methods"],
path: string,
responseSchema: SchemaObject | undefined
}
) {
let keyToUseAsReturnValue;
if (responseSchema && responseSchema.type ==='object') {
const p = responseSchema.properties ?? {}
const ignore_keys = ['ok']
const keys = Object.keys(p).filter((key) => !ignore_keys.includes(key))

keyToUseAsReturnValue = keys[0]
if (keys.length > 1) {
keyToUseAsReturnValue = (await askForWhichKeyToUseAsReturnValue(
keys,
path
))
}
}

const fernSdkMetadata = getFernSdkMetadata(path, keyToUseAsReturnValue)
sdkReturnValue,
}: {
methods: RouteSpec["methods"]
path: string
sdkReturnValue?: string
}) {
const fernSdkMetadata = getFernSdkMetadata(path, sdkReturnValue)
if (methods.length === 1) {
return {
[methods[0]]: fernSdkMetadata,
Expand Down
2 changes: 1 addition & 1 deletion packages/nextlove/src/generators/generate-openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export async function generateOpenAPI(opts: GenerateOpenAPIOpts) {
{
methods,
path: routePath,
responseSchema,
sdkReturnValue: routeSpec.sdkReturnValue,
}
)

Expand Down
1 change: 1 addition & 0 deletions packages/nextlove/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface RouteSpec<
middlewares?: Middlewares
jsonResponse?: JsonResponse
formData?: FormData
sdkReturnValue?: string
}

export type MiddlewareChainOutput<
Expand Down

0 comments on commit 08f9be4

Please sign in to comment.