-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:seamapi/blueprint into fix-go-reque…
…st-samples
- Loading branch information
Showing
6 changed files
with
378 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { camelCase, pascalCase } from 'change-case' | ||
|
||
import type { Json, NonNullJson } from 'lib/json.js' | ||
|
||
import { createJsonResponse } from './json.js' | ||
import type { CodeSampleDefinition, Context } from './schema.js' | ||
|
||
interface JavaRequestBuilderOptions { | ||
path: string | ||
parameters: NonNullJson | ||
} | ||
|
||
export const createJavaRequest = ( | ||
{ request }: CodeSampleDefinition, | ||
_context: Context, | ||
): string => { | ||
const pathParts = request.path.split('/').slice(1) | ||
const requestBuilderOptions: JavaRequestBuilderOptions = { | ||
path: request.path, | ||
parameters: request.parameters, | ||
} | ||
const clientArgs = createJavaRequestBuilder(requestBuilderOptions) | ||
|
||
return `seam.${pathParts.map((p) => camelCase(p)).join('().')}(${clientArgs});` | ||
} | ||
|
||
const createJavaRequestBuilder = ({ | ||
path, | ||
parameters, | ||
}: JavaRequestBuilderOptions): string => { | ||
const requestBuilderName = getRequestBuilderName(path) | ||
const isReqWithParams = Object.keys(parameters).length !== 0 | ||
|
||
if (!isReqWithParams) return '' | ||
|
||
const formattedParams = formatJavaArgs(parameters) | ||
return `${requestBuilderName}.builder()${formattedParams}.build()` | ||
} | ||
|
||
const getRequestBuilderName = (path: string): string => { | ||
const requestBuilderNameSuffix = 'Request' | ||
const pathParts = path.split('/').slice(1) | ||
|
||
return isPathNested(pathParts) | ||
? `${pascalCase(pathParts.slice(1).join('_'))}${requestBuilderNameSuffix}` | ||
: `${pascalCase(path)}${requestBuilderNameSuffix}` | ||
} | ||
|
||
const isPathNested = (pathParts: string[]): boolean => pathParts.length > 2 | ||
|
||
const formatJavaArgs = (jsonParams: NonNullJson): string => | ||
Object.entries(jsonParams as Record<string, Json>) | ||
.map(([paramKey, paramValue]) => { | ||
const formattedValue = formatJavaValue(paramValue) | ||
return `.${camelCase(paramKey)}(${formattedValue})` | ||
}) | ||
.join('\n') | ||
|
||
const formatJavaValue = (value: Json): string => { | ||
if (value === null) return 'null' | ||
if (typeof value === 'boolean') return value.toString() | ||
if (typeof value === 'number') return value.toString() | ||
if (typeof value === 'string') return `"${value}"` | ||
if (Array.isArray(value)) { | ||
const formattedItems = value.map(formatJavaValue).join(', ') | ||
return `List.of(${formattedItems})` | ||
} | ||
if (typeof value === 'object') { | ||
const formattedEntries = Object.entries(value) | ||
.map(([key, val]) => `"${key}", ${formatJavaValue(val)}`) | ||
.join(', ') | ||
return `Map.of(${formattedEntries})` | ||
} | ||
throw new Error(`Unsupported type: ${typeof value}`) | ||
} | ||
|
||
export const createJavaResponse = createJsonResponse |
This file contains 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
Oops, something went wrong.