Skip to content

Commit

Permalink
feat/workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Dec 28, 2024
1 parent f2c5b02 commit 3637c2d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.23.3",
"version": "0.24.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {

import { setContext } from '@apollo/client/link/context';
import Settings from './modules/settings';
import { Workflow } from 'modules/workflow';

export * from './types';
export * from './queries';
Expand Down Expand Up @@ -130,6 +131,7 @@ export default class KanvasCore {
public contact: Contact;
public filesystemMapper: FilesystemMapper;
public event: Event;
public workflow: Workflow;

constructor(protected options: Options) {
this.client = new ApolloClient({
Expand Down Expand Up @@ -169,6 +171,7 @@ export default class KanvasCore {
this.contact = new Contact(this.client);
this.filesystemMapper = new FilesystemMapper(this.client);
this.event = new Event(this.client);
this.workflow = new Workflow(this.client, this.options);
}
protected generateURL() {
return new HttpLink({ uri: this.options.url });
Expand Down
41 changes: 41 additions & 0 deletions src/modules/workflow/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import axios from 'axios';

import { RunWorkflowFromEntity, WorkflowFromEntity } from 'types/workflow';
import { ClientType, Options } from '../../index';

import { RUN_WORKFLOW_FROM_ENTITY } from 'mutations/workflow';

export class Workflow {
protected axiosClient: any;
constructor(protected client: ClientType, protected options?: Options) {
if (this.options) {
this.axiosClient = axios.create({
baseURL: this.options.url,
headers: {
'X-Kanvas-App': this.options.key,
...(this.options.adminKey && {
'X-Kanvas-Key': this.options.adminKey,
}),
},
});

this.axiosClient.interceptors.request.use(
this.options.authAxiosMiddleware,
function (error: any) {
return Promise.reject(error);
}
);
}
}

public async runWorkflowFromEntity(
input: WorkflowFromEntity
): Promise<RunWorkflowFromEntity> {
const response = await this.client.mutate({
mutation: RUN_WORKFLOW_FROM_ENTITY,
variables: { input: input }
});

return response.data;
}
}
7 changes: 7 additions & 0 deletions src/mutations/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { gql } from '@apollo/client/core';

export const RUN_WORKFLOW_FROM_ENTITY = gql`
mutation runWorkflowEntityInput($input: runWorkflowEntityInput!) {
runWorkflowFromEntity(input: $input)
}
`;
13 changes: 13 additions & 0 deletions src/types/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

export interface WorkflowFromEntity {
entity_namespace: string;
entity_id: string;
action: string;
params?: any;
}

export interface RunWorkflowFromEntity {
runWorkflowFromEntity: {
[key: string]: any; // Allow any structure within the response
};
}
31 changes: 31 additions & 0 deletions test/workflow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { initializeClient, getClient } from './setupClient';
import dotenv from 'dotenv';

dotenv.config();

beforeAll(async () => {
await initializeClient(process.env.KANVAS_APP_SECRET!);
});

/**
* @todo add profile photo upload test
*/
describe('Test the Kanvas Workflow', () => {

it('run workflow', async () => {
const client = getClient();

const workflow = await client.workflow.runWorkflowFromEntity({
entity_namespace: 'user',
entity_id: '2',
action: 'updated',
params: {
name: 'John Doe',
}
});

expect(workflow).toBeDefined();
expect(workflow.runWorkflowFromEntity).toBeDefined();
expect(workflow.runWorkflowFromEntity.success).toBeTruthy();
});
});

0 comments on commit 3637c2d

Please sign in to comment.