Skip to content

Add TypeScript types #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
264 changes: 264 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
import { AWS } from '@serverless/typescript';

declare module '@serverless/typescript' {
interface AWS {
stepFunctions?: {
stateMachines: StateMachines;
validate?: boolean;
noOutput?: boolean;
};
}
}

type StateMachines = {
[stateMachine: string]: StateMachine
};

export type StateMachine = {
type?: 'STANDARD' | 'EXPRESS';
id?: string;
name?: string;
definition: Definition;
tracingConfig?: TracingConfig;
loggingConfig?: LoggingConfig;
events?: any[];
dependsOn?: string | string[];
};

type LoggingConfig = {
level: "ERROR" | "ALL" | "FATAL" | "OFF";
includeExecutionData: boolean;
destinations: Resource | Resource[]
};

type Selector = string | Record<string, unknown>;

type Input = {
'AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$'?: string;
} & Record<string, any>;

type ResultSelector = Record<string, any>;

type Parameter = {
'Payload.$'?: string;
Input?: Input;
FunctionName?: string;
StateMachineArn?: string;
} & {
[selector: string]: Selector;
};

type TracingConfig = {
enabled: boolean;
};

type Definition = {
Comment?: string;
StartAt: string;
States: States;
ProcessorConfig?: {
Mode: "INLINE" | "DISTRIBUTED";
};
};

type States = {
[state: string]: Choice | Fail | Map | Task | Parallel | Pass | Wait | Succeed;
};

type StateBase = {
Catch?: Catcher[];
Retry?: Retrier[];
End?: boolean;
InputPath?: string;
Next?: string;
OutputPath?: string;
ResultPath?: string | null;
ResultSelector?: { [key: string]: string | { [key: string]: string } };
Type: string;
Comment?: string;
};

type ChoiceRuleComparison = {
Variable: string;
BooleanEquals?: boolean;
BooleanEqualsPath?: string;
IsBoolean?: boolean;
IsNull?: boolean;
IsNumeric?: boolean;
IsPresent?: boolean;
IsString?: boolean;
IsTimestamp?: boolean;
NumericEquals?: number;
NumericEqualsPath?: string;
NumericGreaterThan?: number;
NumericGreaterThanPath?: string;
NumericGreaterThanEquals?: number;
NumericGreaterThanEqualsPath?: string;
NumericLessThan?: number;
NumericLessThanPath?: string;
NumericLessThanEquals?: number;
NumericLessThanEqualsPath?: string;
StringEquals?: string;
StringEqualsPath?: string;
StringGreaterThan?: string;
StringGreaterThanPath?: string;
StringGreaterThanEquals?: string;
StringGreaterThanEqualsPath?: string;
StringLessThan?: string;
StringLessThanPath?: string;
StringLessThanEquals?: string;
StringLessThanEqualsPath?: string;
StringMatches?: string;
TimestampEquals?: string;
TimestampEqualsPath?: string;
TimestampGreaterThan?: string;
TimestampGreaterThanPath?: string;
TimestampGreaterThanEquals?: string;
TimestampGreaterThanEqualsPath?: string;
TimestampLessThan?: string;
TimestampLessThanPath?: string;
TimestampLessThanEquals?: string;
TimestampLessThanEqualsPath?: string;
};

type ChoiceRuleNot = {
Not: ChoiceRuleComparison;
Next: string;
Comment?: string;
};

type ChoiceRuleAnd = {
And: ChoiceRuleComparison[];
Next: string;
Comment?: string;
};

type ChoiceRuleOr = {
Or: ChoiceRuleComparison[];
Next: string;
Comment?: string;
};

type ChoiceRuleSimple = ChoiceRuleComparison & {
Next: string;
Comment?: string:
};

type ChoiceRule = ChoiceRuleSimple | ChoiceRuleNot | ChoiceRuleAnd | ChoiceRuleOr;

interface Choice extends StateBase {
Type: 'Choice';
Choices: ChoiceRule[];
Default?: string;
}

interface Fail extends StateBase {
Type: 'Fail';
Cause?: string;
Error?: string;
}

interface Map extends StateBase {
Type: 'Map';
ItemsPath?: string;
Iterator: Definition;
ItemSelector?: {
[key: string]: string;
}
MaxConcurrency?: number;
Parameters?: {
[key: string]: string;
};
}

type Resource = string | { 'Fn::GetAtt': [string, 'Arn'] } | { 'Fn::Join': [string, Resource[]] };

interface TaskParametersForLambda {
FunctionName?: Resource;
Payload?: {
'token.$': string;
[key: string]: string;
};
[key: string]: unknown;
}

interface TaskParametersForStepFunction {
StateMachineArn: Resource;
Input?: {
'AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$': '$$.Execution.Id';
[key: string]: string;
};
Retry?: [{ ErrorEquals?: string[] }];
End?: boolean;
}

// https://docs.aws.amazon.com/step-functions/latest/dg/connect-sns.html
interface TaskParametersForSNS {
TopicArn: Ref | string;
Message?: string;
Subject?: string;
// 'Message.$': '$.input.message';
MessageAttributes?: Record<string, any>;
}

interface Task extends StateBase {
Type: 'Task';
Resource: Resource;
Parameters?:
| TaskParametersForLambda
| TaskParametersForStepFunction
| TaskParametersForSNS
| { [key: string]: string | { [key: string]: string } };
}

interface Pass extends StateBase {
Type: 'Pass';
Parameters?: {
[key: string]: string | Array<unknown> | { [key: string]: string };
};
Result?: {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Result?: {
Result?: string | {
Suggested change
Result?: {
Result?: string | [] | {

I'm using Result: "${env:something}" to add an array. Not sure what's the best approach here. The string value resolves into an array. The tip from the Ui console says: Must be valid JSON. Array and object are the only ones valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try something like this: microsoft/TypeScript#1897 (comment)

export type JSONPrimitive = string | number | boolean | null;
export type JSONValue = JSONPrimitive | JSONObject | JSONArray;
export type JSONObject = { [member: string]: JSONValue };
export interface JSONArray extends Array<JSONValue> {}
Suggested change
Result?: {
Result?: JSONValue;

[key: string]: string | Array<unknown> | { [key: string]: string };
};
ResultPath?: string;
}

interface Parallel extends StateBase {
Type: 'Parallel';
Branches: Definition[];
}

interface Wait extends StateBase {
Type: 'Wait';
Next?: string;
Seconds: number;
}

interface Succeed extends StateBase {
Type: 'Succeed';
}

type Catcher = {
ErrorEquals: ErrorName[];
Next: string;
ResultPath?: string;
};

type Retrier = {
ErrorEquals: string[];
IntervalSeconds?: number;
MaxAttempts?: number;
BackoffRate?: number;
};

type ErrorName =
| 'States.ALL'
| 'States.DataLimitExceeded'
| 'States.Runtime'
| 'States.Timeout'
| 'States.TaskFailed'
| 'States.Permissions'
| string;

type Ref = {
Ref: string;
};
Loading