Skip to content

Commit

Permalink
feat: add seam cli code samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewuu committed Aug 12, 2024
1 parent 14dc668 commit b874ba4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/lib/code-sample/bash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { kebabCase } from 'change-case'

import { createJsonResponse } from './create-json-response.js'
import type { CodeSampleDefinition, Context } from './schema.js'

export const createBashRequest = (
{ request }: CodeSampleDefinition,
_context: Context,
): string => {
const parts = request.path.split('/')
const requestParams = Object.entries(request.parameters)
.map(([key, value]) => `--${key} "${JSON.stringify(value)}"`)
.join(' ')

return `seam${parts.map((p) => kebabCase(p)).join(' ')} ${requestParams}`
}

export const createBashResponse = createJsonResponse
8 changes: 7 additions & 1 deletion src/lib/code-sample/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod'

import type { Endpoint } from 'lib/blueprint.js'
import { createBashRequest, createBashResponse } from 'lib/code-sample/bash.js'
import { JsonSchema } from 'lib/json.js'

import {
Expand Down Expand Up @@ -37,7 +38,7 @@ export type CodeSampleDefinition = z.output<typeof CodeSampleDefinitionSchema>

const CodeSampleSchema = CodeSampleDefinitionSchema.extend({
code: z.record(
z.enum(['javascript', 'python', 'php', 'ruby']),
z.enum(['javascript', 'python', 'php', 'ruby', 'bash']),
z.object({
title: z.string().min(1),
request: z.string(),
Expand Down Expand Up @@ -79,6 +80,11 @@ export const createCodeSample = (
request: createPhpRequest(codeSampleDefinition, context),
response: createPhpResponse(codeSampleDefinition, context),
},
bash: {
title: 'Bash',
request: createBashRequest(codeSampleDefinition, context),
response: createBashResponse(codeSampleDefinition, context),
},
},
}
}
20 changes: 20 additions & 0 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Generated by [AVA](https://avajs.dev).
codeSamples: [
{
code: {
bash: {
request: 'seam foos get --foo_id ""8d7e0b3a-b889-49a7-9164-4b71a0506a33""',
response: '{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}',
title: 'Bash',
},
javascript: {
request: 'await seam.foos.get({"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33"})',
response: '{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}',
Expand Down Expand Up @@ -137,6 +142,11 @@ Generated by [AVA](https://avajs.dev).
codeSamples: [
{
code: {
bash: {
request: 'seam foos get --foo_id ""8d7e0b3a-b889-49a7-9164-4b71a0506a33""',
response: '{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}',
title: 'Bash',
},
javascript: {
request: 'await seam.foos.get({"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33"})',
response: '{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}',
Expand Down Expand Up @@ -206,6 +216,11 @@ Generated by [AVA](https://avajs.dev).
codeSamples: [
{
code: {
bash: {
request: 'seam foos list ',
response: '[{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}]',
title: 'Bash',
},
javascript: {
request: 'await seam.foos.list()',
response: '[{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}]',
Expand Down Expand Up @@ -275,6 +290,11 @@ Generated by [AVA](https://avajs.dev).
codeSamples: [
{
code: {
bash: {
request: 'seam foos list ',
response: '[{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}]',
title: 'Bash',
},
javascript: {
request: 'await seam.foos.list()',
response: '[{"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo","nullable_property":null}]',
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.

0 comments on commit b874ba4

Please sign in to comment.