Skip to content

Commit 5018b0e

Browse files
authored
feat: Add support for resource samples (#575)
* Update blueprint * Load resourceSampleDefinitions * Add resource sample * Add resourceSamples to resource layout context
1 parent 32eb66d commit 5018b0e

9 files changed

Lines changed: 56 additions & 12 deletions

File tree

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
* @seamapi/sdk
22
/docs/ @seamapi/docs
3-
/src/data/code-sample-definitions @seamapi/docs
3+
/src/data @seamapi/docs

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@metalsmith/metadata": "^0.3.0",
3434
"@prettier/plugin-php": "^0.22.2",
3535
"@prettier/plugin-ruby": "^4.0.4",
36-
"@seamapi/blueprint": "^0.40.2",
36+
"@seamapi/blueprint": "^0.41.0",
3737
"@seamapi/types": "1.392.1",
3838
"change-case": "^5.4.4",
3939
"command-exists": "^1.2.9",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- title: ACS System
3+
description: A basic system.
4+
resource_type: acs_system
5+
properties:
6+
acs_system_id: 'bbcea306-7201-4d85-b527-3abc55277203'

src/lib/blueprint.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ export const blueprint =
1212
const codeSampleDefinitions =
1313
'codeSampleDefinitions' in metadata ? metadata.codeSampleDefinitions : []
1414

15+
const resourceSampleDefinitions =
16+
'resourceSampleDefinitions' in metadata
17+
? metadata.resourceSampleDefinitions
18+
: []
19+
1520
const typesModule = TypesModuleSchema.parse({
1621
...types,
1722
codeSampleDefinitions,
23+
resourceSampleDefinitions,
1824
})
1925

2026
const blueprint = await createBlueprint(typesModule, {

src/lib/format-code.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import * as prettierPluginPhp from '@prettier/plugin-php/standalone'
22
import * as prettierPluginRuby from '@prettier/plugin-ruby'
3-
import type { CodeSampleSyntax } from '@seamapi/blueprint'
3+
import type { SyntaxName } from '@seamapi/blueprint'
44
import commandExists from 'command-exists'
55
import { execa } from 'execa'
66
import { format as prettier } from 'prettier'
77

88
export const formatCode = async (
99
content: string,
10-
syntax: CodeSampleSyntax,
10+
syntax: SyntaxName,
1111
): Promise<string> => {
1212
const output = await formatCodeForSyntax(content, syntax)
1313
return output.trim()
1414
}
1515

1616
const formatCodeForSyntax = async (
1717
content: string,
18-
syntax: CodeSampleSyntax,
18+
syntax: SyntaxName,
1919
): Promise<string> => {
2020
switch (syntax) {
2121
case 'javascript':

src/lib/layout/api-endpoint.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type {
22
ActionAttempt,
3-
CodeSampleSdk,
43
Endpoint,
54
Parameter,
5+
SdkName,
66
SeamAuthMethod,
77
SeamWorkspaceScope,
88
} from '@seamapi/blueprint'
@@ -14,7 +14,7 @@ import {
1414
normalizePropertyFormatForDocs,
1515
} from './api-route.js'
1616

17-
const supportedSdks: CodeSampleSdk[] = [
17+
const supportedSdks: SdkName[] = [
1818
'javascript',
1919
'python',
2020
'php',
@@ -195,7 +195,7 @@ const mapBlueprintParamToEndpointParam = (
195195

196196
const mapCodeSample = (sample: CodeSample): CodeSampleContext => {
197197
const codeEntries = Object.entries(sample.code).filter(([k]) =>
198-
supportedSdks.includes(k as CodeSampleSdk),
198+
supportedSdks.includes(k as SdkName),
199199
)
200200
return {
201201
title: sample.title,

src/lib/layout/api-route.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import type {
77
Route,
88
} from '@seamapi/blueprint'
99
import { pascalCase } from 'change-case'
10+
import type {
11+
SdkName,
12+
SyntaxName,
13+
} from 'node_modules/@seamapi/blueprint/dist/index.cjs'
14+
import type { ResourceSample } from 'node_modules/@seamapi/blueprint/lib/samples/resource-sample.js'
1015

1116
import type { PathMetadata } from 'lib/path-metadata.js'
1217

@@ -18,6 +23,7 @@ export interface ApiRouteLayoutContext {
1823
ApiRouteResource & {
1924
warnings: ApiWarning[]
2025
errors: ApiError[]
26+
resourceSamples: ResourceSampleContext[]
2127
}
2228
>
2329
endpoints: ApiRouteEndpoint[]
@@ -30,6 +36,12 @@ interface ApiRouteEvent {
3036
properties: ApiRouteProperty[]
3137
}
3238

39+
interface ResourceSampleContext {
40+
title: string
41+
resourceData: string
42+
resourceDataSyntax: SyntaxName
43+
}
44+
3345
type ApiRouteProperty = Pick<
3446
Property,
3547
'name' | 'description' | 'isDeprecated' | 'deprecationMessage'
@@ -144,6 +156,7 @@ export function setApiRouteLayoutContext(
144156
errors: resourceErrors,
145157
warnings: resourceWarnings,
146158
events: resourceEvents,
159+
resourceSamples: resource.resourceSamples.map(mapResourceSample),
147160
})
148161
}
149162
}
@@ -337,3 +350,21 @@ function addLinkTargetsToProperties(
337350
}
338351
}
339352
}
353+
354+
const mapResourceSample = (sample: ResourceSample): ResourceSampleContext => {
355+
const jsonSample = Object.entries(sample.resource).find(
356+
([k]) => (k as SdkName) === 'seam_cli',
357+
)?.[1]
358+
359+
if (jsonSample == null) {
360+
throw new Error(
361+
`Missing seam_cli for ${sample.resource_type} resource sample: ${sample.title}`,
362+
)
363+
}
364+
365+
return {
366+
title: sample.title,
367+
resourceData: jsonSample.resource_data,
368+
resourceDataSyntax: jsonSample.resource_data_syntax,
369+
}
370+
}

src/metalsmith.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Metalsmith(rootDir)
2929
.use(
3030
metadata({
3131
codeSampleDefinitions: './data/code-sample-definitions',
32+
resourceSampleDefinitions: './data/resource-sample-definitions',
3233
pathMetadata: './data/paths.yaml',
3334
}),
3435
)

0 commit comments

Comments
 (0)