jruizgit / rules

Durable Rules Engine
MIT License
1.14k stars 209 forks source link

Using with TypeScript / Tools that parse Javascript like Jest #320

Open thekevinbrown opened 4 years ago

thekevinbrown commented 4 years ago

I noticed that at some point there was Typescript support (https://github.com/jruizgit/rules/issues/15).

I don't see any type definitions in the NPM package, and @types/durable doesn't seem to work. Is there an example of how to do this somewhere?

I'm also really struggling in general because the examples don't look like normal Javascript to me. (Functions with kind of object bodies in them?) So both typescript and javascript tools like Jest are giving me syntax errors. I noticed there's mention of strict mode, but for example, how could I define something like this in TypeScript?

d.statechart('expense3', function() {
    input: {
        to: 'denied'
        whenAll: m.subject == 'approve' && m.amount > 1000
        run: s.status = 'expense3: Denied amount: ' + m.amount

        to: 'pending'
        whenAll: m.subject == 'approve' && m.amount <= 1000
        run: s.status = 'expense3: sid ' + m.sid + ' requesting approve amount: ' + m.amount
    }

    pending: {
        to: 'approved'
        whenAll: m.subject == 'approved'
        run: s.status = 'expense3: Expense approved for sid ' + m.sid

        to: 'denied'
        whenAll: m.subject == 'denied'
        run: s.status = 'expense3: Expense denied for sid ' + m.sid
    }

    denied: {}
    approved: {}
});

console.log(d.post('expense3', { subject: 'approve', amount: 100 }).status);
console.log(d.post('expense3', { subject: 'approved' }).status);
console.log(d.post('expense3', { sid: 1, subject: 'approve', amount: 100 }).status);
console.log(d.post('expense3', { sid: 1, subject: 'denied' }).status);
console.log(d.post('expense3', { sid: 2, subject: 'approve', amount: 10000 }).status);
jruizgit commented 4 years ago

Hi, thanks for asking the question. TypeScript was supported in the very early versions of the framework. However, after publishing version 2, I haven't added TypeScript support back. I will look into this scenario and report back in a few days.

thekevinbrown commented 4 years ago

Thanks @jruizgit, that'd be awesome. Also we're happy to help with contributing and/or maintaining a set of types for use with TypeScript.

The main question / difficulty at this point is around the syntax errors in Jest and TypeScript (basically any tools that want to parse the input). We intend to write a suite of tests for our rules in Jest so that we can see if rule changes in the future cause any unexpected behaviour regressions in other areas of the system that the rule author wasn't intending.

thekevinbrown commented 4 years ago

Hey @jruizgit,

I think I can figure out most of this if I can get past the syntax errors. Any hints on a syntactically valid way to express the above?

thekevinbrown commented 4 years ago

@jruizgit anything I can do to help here?