# jaxify

> A browser/server ajax calls code generator

Latest version **0.0.3** (published 2015-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install jaxify
pnpm add jaxify
yarn add jaxify
bun add jaxify
```

Provides the command `jaxify`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2015-06-05 |
| First published | 2015-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | erandros |
| Maintainers | erandros |
| Keywords | ajax, generator |

## Links

- npm: https://www.npmjs.com/package/jaxify
- Repository: https://github.com/erandros/jaxify
- Issues: https://github.com/erandros/jaxify/issues
- npm.io page: https://npm.io/package/jaxify

## Dependencies (7)

- [yargs](https://npm.io/package/yargs.md) ^3.9.1
- [esprima](https://npm.io/package/esprima.md) ^2.2.0
- [minimist](https://npm.io/package/minimist.md) ^1.1.1
- [mustache](https://npm.io/package/mustache.md) ^2.0.0
- [escodegen](https://npm.io/package/escodegen.md) ^1.6.1
- [dustjs-helpers](https://npm.io/package/dustjs-helpers.md) ^1.7.1
- [dustjs-linkedin](https://npm.io/package/dustjs-linkedin.md) ^2.7.1

## Recent versions

- 0.0.3 (latest) — 2015-06-05
- 0.0.0 — 2015-06-03

## README

[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/erandros/jaxify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

# jaxify

Generate client and server side ajax calls code.

# why

* Code less. In the README.md example, I wrote an input file of 16 lines, and got two necessary files with a sum of around 30.  
* It forces the developer to use the promise returned from `$.ajax` instead of `success`, `error` and `complete` properties. This is better because promises make code more readable.  
* When you change something (ie: the url), you only have to modify one codebase. This is also true because of the previous point, ajax calls are meant to be defined once client side.  
* It automatically maps parameters of `fn` that are in the url (like id in `/find/:id`) to `req.params`. And, the parameters of `fn`that are not in the url are mapped to `req.body`. No confusion between `req.params` and `req.body`. And because of this, client ajax methods can be called with simple code, for example: `ajax.find(1, false)` even if the first parameter is a url parameter, and the other is mapped to `data`.

# how

1) Write a `jaxi.js` file like this:
```javascript
var ajax = {
    find: {
        method: 'get',
        url: '/find/:id',
        fn: function(id, filter) {
            if (filter) reply('James');
            reply(['Jimmy', 'James', 'Found'][id]);
        }
    },
    delete: {
        method: 'delete',
        fn: function() {
            reply('Deleted');
        }
    }
}
```

2) Run `jaxify` in a terminal to generate a `client.jaxi.js` and a `server.jaxi.js`

3) Look at the magic:  

`client.jaxi.js`
```javascript
var ajax = {
    find: function(id, filter) {
        return $.ajax({
            url: '/find/' + id,
            method: 'get',
            data: {filter: filter}
        })
    },
    delete: function() {
        return $.ajax({
            url: '/delete',
            method: 'delete'
        })
    }
}
// $.ajax returns a jqXHR, that implements the promise interface, so you can do 
ajax.find(1, false).done(function(res) { });
```  

`server.jaxi.js`
```javascript

function setExpress(app) {
    for (var name in ajax) {
        var action = ajax[name];
        var url = action.url ? action.url : '/' + name;
        app[action.method](url, function(req, res) {
            action.fn(req, res);
        });
    }
}

var ajax = {
    find: {
        method: 'get',
        url: '/find/:id',
        fn: function(req, res) {
            var id = req.params.id;
            var filter = req.body.filter;
            if (filter) res.send('James')
			res.send(['Jimmy', 'James', 'Found'][id]);
        }
    },
    delete: {
        method: 'delete',
        fn: function(req, res) {
            res.send('Deleted');
        }
    }
}
module.exports = setExpress;
```

4) Add `client.jaxi.js`as a script to HTML.  
5) Require `server.jaxi.js` in server and invoke with Express, for example:
```javascript
var app = require('express')();
require('server.jaxi')(app);
```

# install

With [npm](http://npmjs.org) do:

```
npm install -g jaxify
```

# usage

```
Usage: jaxify {OPTIONS}

Standard Options:

    --clientfile, -c  Write the client output to this file.
                      If unspecified, use 'client.jaxi.js'
		      
    --serverfile, -s  Write the server output to this file.
                      If unspecified, use 'server.jaxi.js'

     --inputfile, -i  Use this file as input.
                      If unspecified, use 'jaxi.js'
    		     
     --webserver, -w  Use the specified webserver.
                      Options are 'express'.

          --ajax, -a  Use the specified client side ajax library.
                      Options are 'jquery'.

          --help, -h  Show this message
```

# license

MIT

---
_Source: https://npm.io/package/jaxify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
