Skip to content
Open
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
30 changes: 28 additions & 2 deletions codegen/lib/layouts/resources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { Blueprint, Resource } from '@seamapi/blueprint'
import type {
ActionAttempt,
Blueprint,
Property,
Resource,
} from '@seamapi/blueprint'
import { kebabCase, pascalCase } from 'change-case'

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

// The blueprint merges the per-status variants and drops the nullability.
const nullWhilePendingDoc =
'Null while the action attempt is pending or when this value does not apply.'

const normalizeActionAttempt = (resource: ActionAttempt): ActionAttempt => ({
...resource,
properties: resource.properties.map((property) =>
property.name === 'error' || property.name === 'result'
? toStatusDependentProperty(property)
: property,
),
})

const toStatusDependentProperty = (property: Property): Property => ({
...property,
isNullable: true,
description: [property.description, nullWhilePendingDoc]
.filter((part) => part !== '')
.join(' '),
})

const getBatchResourceLayoutContexts = (
resources: Resource[],
): BatchResourceLayoutContext[] => {
Expand Down
9 changes: 7 additions & 2 deletions src/lib/resolve-action-attempt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ export class SeamActionAttemptFailedError<
code: string

constructor(actionAttempt: FailedActionAttempt<T>) {
super(actionAttempt.error.message, actionAttempt)
const { error } = actionAttempt as T
super(error?.message ?? 'Action attempt failed', actionAttempt)
this.name = this.constructor.name
this.code = actionAttempt.error.type
this.code = error?.type ?? 'unknown'
}
}

Expand Down Expand Up @@ -167,11 +168,15 @@ const isFailedActionAttempt = <T extends ActionAttempt>(
*/
export type SucceededActionAttempt<T extends ActionAttempt> = T & {
status: 'success'
result: NonNullable<T['result']>
error: null
}

/**
* An action attempt that has failed.
*/
export type FailedActionAttempt<T extends ActionAttempt> = T & {
status: 'error'
error: NonNullable<T['error']>
result: null
}
Loading
Loading