-
Notifications
You must be signed in to change notification settings - Fork 7
Typescript SDK Integration #55
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
Open
lornakelly
wants to merge
5
commits into
serverlessworkflow:main
Choose a base branch
from
lornakelly:43/sdk-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45a3b85
Add typescript sdk package
lornakelly 188005f
Set up sdk wrapper and add tests
lornakelly 8255eb2
update lock file
lornakelly e52a590
Add js-yaml packages
lornakelly 8584e3f
Refactor SDK wrapper to use Classes.Workflow directly instead of dese…
lornakelly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,5 @@ pnpm-lock.yaml | |
| pnpm-workspace.yaml | ||
| repo/graph.dot | ||
| repo/repo.iml | ||
| **/__snapshots__/** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/serverless-workflow-diagram-editor/src/core/workflowSdk.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import yaml from "js-yaml"; | ||
| import { Classes, Specification, validate } from "@serverlessworkflow/sdk"; | ||
|
|
||
| export type WorkflowParseResult = { | ||
| model: Specification.Workflow | null; | ||
| errors: Error[]; | ||
| }; | ||
|
|
||
| export function validateWorkflow(model: Specification.Workflow): Error[] { | ||
| try { | ||
| validate("Workflow", model); | ||
| return []; | ||
| } catch (err) { | ||
| // TODO: Parse individual validation errors from the SDK into separate Error objects when we are ready to render them. | ||
| return [err instanceof Error ? err : new Error(String(err))]; | ||
| } | ||
| } | ||
|
|
||
| export function parseWorkflow(text: string): WorkflowParseResult { | ||
| let raw: Partial<Specification.Workflow>; | ||
|
|
||
| try { | ||
| raw = yaml.load(text, { schema: yaml.DEFAULT_SCHEMA }) as Partial<Specification.Workflow>; | ||
| } catch (err) { | ||
| return { | ||
| model: null, | ||
| errors: [err instanceof Error ? err : new Error(String(err))], | ||
| }; | ||
| } | ||
|
|
||
| if (raw == null || typeof raw !== "object") { | ||
| return { model: null, errors: [new Error("Not a valid workflow object")] }; | ||
| } | ||
|
|
||
| const model = new Classes.Workflow(raw) as Specification.Workflow; | ||
| const errors = validateWorkflow(model); | ||
|
|
||
| return { model, errors }; | ||
| } | ||
91 changes: 91 additions & 0 deletions
91
...ess-workflow-diagram-editor/tests/core/__snapshots__/workflowSdk.integration.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`parseWorkflow > parses valid 'JSON' and returns model with no errors 1`] = ` | ||
| { | ||
| "errors": [], | ||
| "model": t { | ||
| "do": t [ | ||
| t { | ||
| "step1": t { | ||
| "set": t { | ||
| "variable": "my first workflow", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| "document": t { | ||
| "dsl": "1.0.0", | ||
| "name": "valid-workflow-json", | ||
| "namespace": "default", | ||
| "version": "1.0.0", | ||
| }, | ||
| }, | ||
| } | ||
| `; | ||
|
|
||
| exports[`parseWorkflow > parses valid 'YAML' and returns model with no errors 1`] = ` | ||
| { | ||
| "errors": [], | ||
| "model": t { | ||
| "do": t [ | ||
| t { | ||
| "step1": t { | ||
| "set": t { | ||
| "variable": "my first workflow", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| "document": t { | ||
| "dsl": "1.0.0", | ||
| "name": "valid-workflow-yaml", | ||
| "namespace": "default", | ||
| "version": "1.0.0", | ||
| }, | ||
| }, | ||
| } | ||
| `; | ||
|
|
||
| exports[`parseWorkflow > returns model and errors for invalid but parseable 'JSON' 1`] = ` | ||
| { | ||
| "errors": [ | ||
| [Error: 'Workflow' is invalid - The DSL version of the workflow 'undefined' doesn't match the supported version of the SDK '1.0.0'.], | ||
| ], | ||
| "model": t { | ||
| "do": t [ | ||
| t { | ||
| "step1": t { | ||
| "set": t { | ||
| "variable": "my first invalid json workflow", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
| `; | ||
|
|
||
| exports[`parseWorkflow > returns model and errors for invalid but parseable 'YAML' 1`] = ` | ||
| { | ||
| "errors": [ | ||
| [Error: 'Workflow' is invalid - The DSL version of the workflow 'undefined' doesn't match the supported version of the SDK '1.0.0'.], | ||
| ], | ||
| "model": t { | ||
| "do": t [ | ||
| t { | ||
| "step1": t { | ||
| "set": t { | ||
| "variable": "my first invalid yaml workflow", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
| `; | ||
|
|
||
| exports[`validateWorkflow > returns errors for an invalid workflow 1`] = ` | ||
| [ | ||
| [Error: 'Workflow' is invalid - The DSL version of the workflow 'undefined' doesn't match the supported version of the SDK '1.0.0'.], | ||
| ] | ||
| `; |
83 changes: 83 additions & 0 deletions
83
packages/serverless-workflow-diagram-editor/tests/core/workflowSdk.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { describe, it, expect } from "vitest"; | ||
| import { parseWorkflow, validateWorkflow } from "../../src/core"; | ||
| import { | ||
| BASIC_VALID_WORKFLOW_YAML, | ||
| BASIC_VALID_WORKFLOW_JSON, | ||
| BASIC_INVALID_WORKFLOW_YAML, | ||
| BASIC_INVALID_WORKFLOW_JSON, | ||
| } from "../fixtures/workflows"; | ||
| import { Classes, Specification } from "@serverlessworkflow/sdk"; | ||
|
|
||
| describe("parseWorkflow", () => { | ||
| it.each([ | ||
| { format: "YAML", input: BASIC_VALID_WORKFLOW_YAML, expectedName: "valid-workflow-yaml" }, | ||
| { format: "JSON", input: BASIC_VALID_WORKFLOW_JSON, expectedName: "valid-workflow-json" }, | ||
| ])("parses valid $format and returns model with no errors", ({ input, expectedName }) => { | ||
| const result = parseWorkflow(input); | ||
| expect(result.errors).toHaveLength(0); | ||
| expect(result.model?.document?.name).toBe(expectedName); | ||
| expect(result).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { format: "YAML", input: BASIC_INVALID_WORKFLOW_YAML }, | ||
| { format: "JSON", input: BASIC_INVALID_WORKFLOW_JSON }, | ||
| ])("returns model and errors for invalid but parseable $format", ({ input }) => { | ||
| const result = parseWorkflow(input); | ||
| expect(result.model).not.toBeNull(); | ||
| expect(result.errors).toHaveLength(1); | ||
| expect(result).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { description: "empty string", input: "" }, | ||
| { description: "non-object YAML", input: "just a string" }, | ||
| { description: "numeric YAML", input: "42" }, | ||
| ])("returns null model with error for $description", ({ input }) => { | ||
| const result = parseWorkflow(input); | ||
| expect(result.model).toBeNull(); | ||
| expect(result.errors[0].message).toBe("Not a valid workflow object"); | ||
| }); | ||
|
|
||
| it("returns null model with errors for unparseable text", () => { | ||
| const result = parseWorkflow("}{not yaml or json}{"); | ||
| expect(result.model).toBeNull(); | ||
| expect(result.errors).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("validateWorkflow", () => { | ||
| it("returns empty array for a valid workflow", () => { | ||
| const valid = new Classes.Workflow({ | ||
| document: { dsl: "1.0.0", name: "valid-workflow", version: "1.0.0", namespace: "default" }, | ||
| do: [{ step1: { set: { variable: "value" } } }], | ||
| }) as Specification.Workflow; | ||
| const errors = validateWorkflow(valid); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("returns errors for an invalid workflow", () => { | ||
| const invalid = new Classes.Workflow({ | ||
| do: [{ step1: { set: { variable: "value" } } }], | ||
| }) as Specification.Workflow; | ||
| const errors = validateWorkflow(invalid); | ||
| expect(errors).toHaveLength(1); | ||
| expect(errors).toMatchSnapshot(); | ||
| }); | ||
| }); |
71 changes: 71 additions & 0 deletions
71
packages/serverless-workflow-diagram-editor/tests/fixtures/workflows.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| * Workflow test fixtures for parsing, validation, and error handling tests. | ||
| * Includes valid and invalid workflow definitions in YAML and JSON formats. | ||
| */ | ||
|
|
||
| export const BASIC_VALID_WORKFLOW_YAML = ` | ||
| document: | ||
| dsl: 1.0.0 | ||
| name: valid-workflow-yaml | ||
| version: 1.0.0 | ||
| namespace: default | ||
| do: | ||
| - step1: | ||
| set: | ||
| variable: 'my first workflow' | ||
| `; | ||
|
|
||
| export const BASIC_VALID_WORKFLOW_JSON = JSON.stringify({ | ||
| document: { | ||
| dsl: "1.0.0", | ||
| name: "valid-workflow-json", | ||
| version: "1.0.0", | ||
| namespace: "default", | ||
| }, | ||
| do: [ | ||
| { | ||
| step1: { | ||
| set: { | ||
| variable: "my first workflow", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // Missing required 'document' field | ||
| export const BASIC_INVALID_WORKFLOW_YAML = ` | ||
| do: | ||
| - step1: | ||
| set: | ||
| variable: 'my first invalid yaml workflow' | ||
| `; | ||
|
|
||
| // Missing required 'document' field | ||
| export const BASIC_INVALID_WORKFLOW_JSON = JSON.stringify({ | ||
| do: [ | ||
| { | ||
| step1: { | ||
| set: { | ||
| variable: "my first invalid json workflow", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.