Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codegen/layouts/partials/resource-object.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
{{#each properties}}
{{> doc}}
{{json name}}{{#if isOptional}}?{{/if}}: {{> resource-property-type }}{{#if isNullable}} | null{{/if}}{{#if isOptional}} | undefined{{/if}}
{{json name}}{{#if isOptional}}?{{/if}}: {{#if renderAsNull}}null{{else}}{{> resource-property-type }}{{#if isNullable}} | null{{/if}}{{/if}}{{#if isOptional}} | undefined{{/if}}
{{/each}}
}
48 changes: 46 additions & 2 deletions codegen/lib/layouts/resources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { Blueprint, Resource } from '@seamapi/blueprint'
import type {
ActionAttemptStatus,
Blueprint,
EnumProperty,
Property,
Resource,
} from '@seamapi/blueprint'
import { kebabCase, pascalCase } from 'change-case'

export interface ResourceLayoutContext {
Expand Down Expand Up @@ -39,7 +45,7 @@ export const getResourceLayoutContexts = (
({ resourceType }) => !discriminatedResourceTypes.has(resourceType),
),
...blueprint.events,
...blueprint.actionAttempts,
...blueprint.actionAttempts.flatMap(expandActionAttemptByStatus),
]
const resourceTypes = [
...new Set(resources.map(({ resourceType }) => resourceType)),
Expand All @@ -57,6 +63,44 @@ export const getResourceLayoutContexts = (
}))
}

// Expand an action attempt into one union member per status from its status
// enum. In each member, the status property is rendered as the status literal,
// and any property annotated with actionAttemptStatuses is rendered as null
// for the statuses it does not list.
const expandActionAttemptByStatus = (resource: Resource): Resource[] => {
const statusProperty = resource.properties.find(
(property): property is EnumProperty =>
property.name === 'status' && property.format === 'enum',
)
if (statusProperty == null) return [resource]

return statusProperty.values.map(({ name }) => {
const status = name as ActionAttemptStatus
return {
...resource,
properties: resource.properties.map((property): Property => {
if (property === statusProperty) {
return {
...statusProperty,
values: statusProperty.values.filter(
(value) => value.name === status,
),
}
}
const { actionAttemptStatuses } = property
if (actionAttemptStatuses == null) return property
if (actionAttemptStatuses.includes(status)) return property
const nullRenderedProperty: Property & { renderAsNull: true } = {
...property,
isNullable: false,
renderAsNull: true,
}
return nullRenderedProperty
}),
}
})
}

const getBatchResourceLayoutContexts = (
resources: Resource[],
): BatchResourceLayoutContext[] => {
Expand Down
8 changes: 4 additions & 4 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
Expand Up @@ -85,7 +85,7 @@
"axios-retry": "^4.4.2"
},
"devDependencies": {
"@seamapi/blueprint": "^1.9.1",
"@seamapi/blueprint": "^1.10.0",
"@seamapi/fake-seam-connect": "^2.0.4",
"@seamapi/smith": "^1.1.0",
"@seamapi/types": "1.1047.0",
Expand Down
14 changes: 8 additions & 6 deletions src/lib/resolve-action-attempt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ const isFailedActionAttempt = <T extends ActionAttempt>(
/**
* An action attempt that has succeeded.
*/
export type SucceededActionAttempt<T extends ActionAttempt> = T & {
status: 'success'
}
export type SucceededActionAttempt<T extends ActionAttempt> = Extract<
T,
{ status: 'success' }
>

/**
* An action attempt that has failed.
*/
export type FailedActionAttempt<T extends ActionAttempt> = T & {
status: 'error'
}
export type FailedActionAttempt<T extends ActionAttempt> = Extract<
T,
{ status: 'error' }
>
Loading
Loading