Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/onify/flow-extensions

Onify Flow Extensions
https://github.com/onify/flow-extensions

bpmn extensions onify

Last synced: about 5 hours ago
JSON representation

Onify Flow Extensions

Awesome Lists containing this project

README

        

# Onify Flow Extensions

[![Built latest](https://github.com/onify/flow-extensions/actions/workflows/build-latest.yaml/badge.svg)](https://github.com/onify/flow-extensions/actions/workflows/build-latest.yaml)[![Coverage Status](https://coveralls.io/repos/github/onify/flow-extensions/badge.svg?branch=default)](https://coveralls.io/github/onify/flow-extensions?branch=default)

# Api

- `extensions`: Flow extensions
- `extendFn`: extend function to pass to [serializer](https://github.com/paed01/moddle-context-serializer/blob/master/API.md)

# Examples

## Bpmn engine example

```javascript
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { Engine } from 'bpmn-engine';
import { extensions } from '@onify/flow-extensions';
import { FlowScripts } from '@onify/flow-extensions/FlowScripts';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `






next(null, myContextFn());



`;

const name = 'onify flow';
const engine = new Engine({
name,
source,
moddleOptions: {
camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
},
services: {
serviceFn(scope, callback) {
callback(null, { data: 1 });
},
},
extensions: {
onify: extensions,
},
scripts: new FlowScripts(name, './script-resources', {
myContextFn() {
return 2;
},
}),
});

engine.execute((err, instance) => {
if (err) throw err;
console.log(instance.name, instance.environment.output);
});
```

## Extract scripts with extend function

```javascript
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import BpmnModdle from 'bpmn-moddle';
import * as Elements from 'bpmn-elements';
import { Serializer, TypeResolver } from 'moddle-context-serializer';
import { extendFn } from '@onify/flow-extensions';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `





onifyApiRequest

GET
/my/items/workspace-1


next(null, {
id: content.id,
statuscode,
});





\${content.output.result.statuscode}






next(null, 2);



`;

getScripts(source).then(console.log).catch(console.error);

async function getScripts(bpmnSource) {
const moddle = await getModdleContext(bpmnSource, {
camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
});

const serialized = Serializer(moddle, TypeResolver(Elements), extendFn);
return serialized.elements.scripts;
}

function getModdleContext(source, options) {
const bpmnModdle = new BpmnModdle(options);
return bpmnModdle.fromXML(Buffer.isBuffer(source) ? source.toString() : source.trim());
}
```

## Extract timers

```javascript
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import BpmnModdle from 'bpmn-moddle';
import * as Elements from 'bpmn-elements';
import { Serializer, TypeResolver } from 'moddle-context-serializer';
import { extendFn, OnifyTimerEventDefinition } from '@onify/flow-extensions';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `




0 1 * * *






R3/PT1M





\${environment.settings.postpone}





`;

getTimers(source).then(console.log).catch(console.error);

const dummyEventActivity = { broker: {}, environment: { Logger() {} } };

async function getTimers(bpmnSource) {
const moddle = await getModdleContext(bpmnSource, {
camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
});

const serialized = Serializer(moddle, TypeResolver(Elements), extendFn);

for (const t of serialized.elements.timers) {
const ed = new OnifyTimerEventDefinition(dummyEventActivity, t.timer);

try {
t.parsed = ed.parse(t.timer.timerType, t.timer.value);
} catch {}
}

return serialized.elements.timers;
}

function getModdleContext(source, options) {
const bpmnModdle = new BpmnModdle(options);
return bpmnModdle.fromXML(Buffer.isBuffer(source) ? source.toString() : source.trim());
}
```

## Extend sequence flow with properties and take listeners

```javascript
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { Engine } from 'bpmn-engine';
import * as Elements from 'bpmn-elements';
import { OnifySequenceFlow, extensions } from '@onify/flow-extensions';
import { FlowScripts } from '@onify/flow-extensions/FlowScripts';

const nodeRequire = createRequire(fileURLToPath(import.meta.url));

const source = `









environment.output.fields = listener.fields; next();

\${true}


baz












next(null, { foo: environment.variables.required.input });








environment.output.failedBy = content.inbound[0].properties.error;
if (next) next();













`;

const engine = new Engine({
name: 'sequence flow extension',
source,
moddleOptions: {
camunda: nodeRequire('camunda-bpmn-moddle/resources/camunda.json'),
},
extensions: {
onify: extensions,
},
scripts: new FlowScripts('sequence flow extension'),
elements: {
...Elements,
SequenceFlow: OnifySequenceFlow,
},
variables: {
required: {
input: true,
},
},
});

engine.execute((err, instance) => {
if (err) throw err;
console.log(instance.name, instance.environment.output);
});
```