Skip to content

Commit

Permalink
Merge branch 'main' of github.com:seamapi/blueprint into fix-go-reque…
Browse files Browse the repository at this point in the history
…st-samples
  • Loading branch information
andrii-balitskyi committed Oct 3, 2024
2 parents 9024f46 + 2868027 commit 7c94131
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@seamapi/blueprint",
"version": "0.22.0",
"version": "0.23.0",
"description": "Build tools for the Seam API using this blueprint.",
"type": "module",
"main": "index.js",
Expand Down
77 changes: 77 additions & 0 deletions src/lib/code-sample/java.ts
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
11 changes: 10 additions & 1 deletion src/lib/code-sample/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { JsonSchema } from 'lib/json.js'

import { formatCodeRecords } from './format.js'
import { createGoRequest, createGoResponse } from './go.js'
import { createJavaRequest, createJavaResponse } from './java.js'
import {
createJavascriptRequest,
createJavascriptResponse,
Expand Down Expand Up @@ -49,12 +50,13 @@ const CodeSampleSyntaxSchema = z.enum([
'ruby',
'bash',
'go',
'java',
])

export type CodeSampleSyntax = z.infer<typeof CodeSampleSyntaxSchema>

const CodeSchema = z.record(
z.enum(['javascript', 'python', 'php', 'ruby', 'seam_cli', 'go']),
z.enum(['javascript', 'python', 'php', 'ruby', 'seam_cli', 'go', 'java']),
z.object({
title: z.string().min(1),
request: z.string(),
Expand Down Expand Up @@ -127,6 +129,13 @@ export const createCodeSample = async (
request_syntax: 'go',
response_syntax: 'json',
},
java: {
title: 'Java',
request: createJavaRequest(codeSampleDefinition, context),
response: createJavaResponse(codeSampleDefinition, context),
request_syntax: 'java',
response_syntax: 'json',
},
}

return {
Expand Down
Loading

0 comments on commit 7c94131

Please sign in to comment.