hbi99 / defiant.js

http://defiantjs.com
GNU Affero General Public License v3.0
913 stars 91 forks source link

NodeJS API documentation? #106

Closed gtamas closed 5 years ago

gtamas commented 5 years ago

Hi

The library works fine with node so far, but the API is different. Once I require() defiant, the API looks like this:


  render: [AsyncFunction: render],
  render_xml: [AsyncFunction: render_xml],
  register_template: [AsyncFunction: register_template],
  create_snapshot: [AsyncFunction: create_snapshot],
  search: [AsyncFunction: search]```

My understanding is that documentation is in progress. However, since typescript types are not provided by this library, it's hard to figure out what to do. Could you at least post method signatures here?

When can we expect documentation to be uploaded?
gtamas commented 5 years ago

Update.

After checking the code, I tried this with node 10.8:

const def = require('defiant.js');
def.init()
    .then(() => {
        return def.register_template(
            'books',
            '<script type="<defiant/xsl-template"><xsl:template name="books"><xsl:for-each select="//movie"><xsl:value-of select="title"/><br/></xsl:for-each></xsl:template></script>'
        );
    })
    .then(() => {
        return def.render('books', { movie: [{ title: 'thing' }] });
    })
    .then(res => {
        console.log(res);
    })
    .catch(e => {
        throw new Error(e);
    });

But I'm getting this error:

Unhandled promise rejection (rejection id: 1): Error: Error: Evaluation failed: TypeError: Cannot read property 'setAttribute' of null at Object.render (:6:1323)

What am I doing wrong? This seems to be a bug.

gtamas commented 5 years ago

@hbi99 Please get back to me on this if you can. It would be so awesome to use this lib in my app. :) Searching is good, snapshot is good, so we only have to figure out rendering now.

hbi99 commented 5 years ago

I’ll write as soon as I am at a computer 😊

gtamas commented 5 years ago

OK thx vety much By the way, this is when the error happens. Line 62 of defiant.js:

temp = this.node.selectSingleNode(this.xsl_template, tmpltXpath);

This returns null, but null is not handled in the next line..

hbi99 commented 5 years ago

I know why it happens. So i will post you the code to get it running. Also, there is a middleware i’ve written for express.js and koa - that is doing exactly what you want and the setup is easy complemented with debug tool. I’ll write more with example code 😊

gtamas commented 5 years ago

ok thx! that sounds great! :) mine is a cli app, i need to make this work somehow

ChrisChinchilla commented 5 years ago

Trying something similar here :)

hbi99 commented 5 years ago

@gtamas, First of all, sorry for delayed response. I am in Spain right now and we had a tiresome travel yesterday and I was exhausted in the night. Anyway, regarding your script - when I switched this library from Grunt Gulp - which was a few weeks ago, I messed up with the position of a IE-polyfill code, which in turn messed up the rendering functionality of Defiant. This is fixed and a new version is deployed 2.1.3. But also, you need to adjust your script to this - the only change is the register_template method:

const def = require('defiant.js');
def.init()
    .then(() => {
        return def.register_template(
            '<xsl:template name="books"><xsl:for-each select="//movie"><xsl:value-of select="title"/><br/></xsl:for-each></xsl:template>'
        );
    })
    .then(() => {
        return def.render('books', { movie: [{ title: 'thing' }] });
    })
    .then(res => {
        console.log(res);
    })
    .catch(e => {
        throw new Error(e);
    });
hbi99 commented 5 years ago

Now regarding the documentation; it is coming with some really cool features - please be patient, because the new feature will is a library that allows features like Jupyter Notebook but in markdown-files. I'll get back to you soon :-)

Regarding the middleware for express.js - you can try it out if you follow the instructions below. If not, here are two screenshots with explanation of the debug-feature - though it is not as advanced as I want it to be.

If you've followed the instructions below, you should see something like this: image

But, you can in dev-mode add ?debug=true. When you do this, you will see the debug view which in turn will display your XSL file to the left, your JSON data to the right. But the neat feature is that you can move the cursor in the XSL file and the XPaths, and the tools can and will indicate what the selections match. Similar to this view:

image

If you want to try it out, here are the instructions, to enter in bash / terminal:

$ mkdir tmp
$ cd tmp
$ npm init // press yes all the way
$ npm install rebellious --save
$ cd node_modules/rebellious
$ npm install
$ cd example/
$ nodemon app.js  // in your browser, go to http://localhost:3000/books
gtamas commented 5 years ago

@hbi99 Thank you very much! The code works fine :) cool. Regarding the middleware and the example, I installed them. This is pretty cool, especially the debug feature!

So using this middleware I can use XSL templates and run them in visual debug mode. That's nice! :) Will this debug feature also be able to execute XPATH queries? I mean, there is a bar at the bottom which seems to be designed for queries, but it's read-only in my browser.

Anyway thanks again for all the help. Now I will implement the features I planned. :) Btw, when can we expect the Jupyter Notebook-like stuff? Sounds interesting as well.