Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Prepare to release 2.1.3 (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelidlessness authored Apr 21, 2022
1 parent b3c8d07 commit 04019b3
Show file tree
Hide file tree
Showing 13 changed files with 1,072 additions and 902 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [2.1.3] - 2022-04-21

##### Changed

- Code style/formatting: Prettier + Airbnb ESLint preset (#146)

## [2.1.2] - 2022-02-01

##### Fixed
Expand Down
174 changes: 96 additions & 78 deletions docs/api.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,81 +58,93 @@ <h1 class="page-title">api.js</h1>
* PRs are very welcome!
*/

const request = require( 'request' );
const express = require( 'express' );
const request = require('request');
const express = require('express');

const router = express.Router();
const transformer = require( './transformer' );
const transformer = require('./transformer');

router
.all( '/', ( req, res, next ) => {
if ( req.app.get( 'secure' ) ) {
res.status( 405 ).send( 'Not Allowed' );
} else if ( !req.query.xform ) {
res.status( 400 ).send( 'Bad Request.' );
.all('/', (req, res, next) => {
if (req.app.get('secure')) {
res.status(405).send('Not Allowed');
} else if (!req.query.xform) {
res.status(400).send('Bad Request.');
} else {
// allow requests from anywhere
res.set( 'Access-Control-Allow-Origin', '*' );
res.set('Access-Control-Allow-Origin', '*');
next();
}
} )
.get( '/', ( req, res ) => {
_request( {
})
.get('/', (req, res) => {
_request({
method: 'get',
url: req.query.xform
} )
.then( xform => transformer.transform( {
xform,
theme: req.query.theme,
openclinica: req.query.openclinica === 'true',
markdown: req.query.markdown !== 'false'
} ) )
.then( result => {
res.json( result );
} )
.catch( error => {
url: req.query.xform,
})
.then((xform) =>
transformer.transform({
xform,
theme: req.query.theme,
openclinica: req.query.openclinica === 'true',
markdown: req.query.markdown !== 'false',
})
)
.then((result) => {
res.json(result);
})
.catch((error) => {
error.status = error.status || 500;
error.message = error.message || 'Unknown error.';
res.status( error.status ).send( `${error.message} (stack: ${error.stack})` );
} );
} )
.post( '/', ( req, res ) => {
transformer.transform( {
xform: req.body.xform,
theme: req.body.theme,
media: req.body.media,
openclinica: req.body.openclinica === 'true',
markdown: req.body.markdown !== 'false'
} )
.then( result => {
res.json( result );
} )
.catch( error => {
res.status(error.status).send(
`${error.message} (stack: ${error.stack})`
);
});
})
.post('/', (req, res) => {
transformer
.transform({
xform: req.body.xform,
theme: req.body.theme,
media: req.body.media,
openclinica: req.body.openclinica === 'true',
markdown: req.body.markdown !== 'false',
})
.then((result) => {
res.json(result);
})
.catch((error) => {
error.status = error.status || 500;
error.message = error.message || 'Unknown error.';
res.status( error.status ).send( `${error.message} (stack: ${error.stack})` );
} );
} )
res.status(error.status).send(
`${error.message} (stack: ${error.stack})`
);
});
})
// for development purposes, to return HTML that can be easily inspected in the developer console
.get( '/htmlform', ( req, res ) => {
_request( {
.get('/htmlform', (req, res) => {
_request({
method: 'get',
url: req.query.xform
} )
.then( xform => transformer.transform( {
xform,
theme: req.query.theme,
openclinica: req.query.openclinica === 'true',
markdown: req.query.markdown !== 'false'
} ) )
.then( result => {
res.send( result.form );
} )
.catch( error => {
url: req.query.xform,
})
.then((xform) =>
transformer.transform({
xform,
theme: req.query.theme,
openclinica: req.query.openclinica === 'true',
markdown: req.query.markdown !== 'false',
})
)
.then((result) => {
res.send(result.form);
})
.catch((error) => {
error.status = error.status || 500;
error.message = error.message || 'Unknown error.';
res.status( error.status ).send( `${error.message} (stack: ${error.stack})` );
} );
} );
res.status(error.status).send(
`${error.message} (stack: ${error.stack})`
);
});
});

/**
* Sends a request to an OpenRosa server. Only for basic retrieval of
Expand All @@ -141,35 +153,41 @@ <h1 class="page-title">api.js</h1>
* @param { {url: string, method: string} } options - Request options object.
* @return { Promise&lt;Error|object> } a promise that resolves in request body.
*/
function _request( options ) {
function _request(options) {
options.headers = options.headers || {};
options.headers[ 'X-OpenRosa-Version' ] = '1.0';
options.headers['X-OpenRosa-Version'] = '1.0';

const method = options.method || 'get';

return new Promise( ( resolve, reject ) => {
request[ method ]( options, ( error, response, body ) => {
if ( error ) {
console.error( `Error occurred when requesting ${options.url}`, error );
reject( error );
} else if ( response.statusCode === 401 ) {
const error = new Error( 'Forbidden. Authorization Required.' );
return new Promise((resolve, reject) => {
request[method](options, (error, response, body) => {
if (error) {
console.error(
`Error occurred when requesting ${options.url}`,
error
);
reject(error);
} else if (response.statusCode === 401) {
const error = new Error('Forbidden. Authorization Required.');
error.status = response.statusCode;
reject( error );
} else if ( response.statusCode &lt; 200 || response.statusCode >= 300 ) {
const error = new Error( `Request to ${options.url} failed.` );
reject(error);
} else if (
response.statusCode &lt; 200 ||
response.statusCode >= 300
) {
const error = new Error(`Request to ${options.url} failed.`);
error.status = response.statusCode;
reject( error );
reject(error);
} else {
//console.log( `response of request to ${options.url} has status code: `, response.statusCode );
resolve( body );
// console.log( `response of request to ${options.url} has status code: `, response.statusCode );
resolve(body);
}
} );
} );
});
});
}

module.exports = app => {
app.use( '/transform', router );
module.exports = (app) => {
app.use('/transform', router);
};
</code></pre>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ <h4 class="name" id="getMediaPath"><span class="type-signature"></span>getMediaP

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="url.js.html">url.js</a>, <a href="url.js.html#line24">line 24</a>
<a href="url.js.html">url.js</a>, <a href="url.js.html#line26">line 26</a>
</li></ul></dd>


Expand Down
45 changes: 23 additions & 22 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,28 @@ <h3 id="install-as-module">Install as module</h3>
</code></pre>
<h3 id="use-as-module">Use as module</h3>
<pre class="prettyprint source lang-js"><code>const transformer = require('enketo-transformer');
const xform = fs.readFileSync( 'path/to/xform.xml' );

transformer.transform( {
// required string of XForm
xform: xform,
// optional string, to add theme if no theme is defined in the XForm
theme: 'sometheme',
// optional map, to replace jr://..../myfile.png URLs
media: {
'myfile.png' : '/path/to/somefile.png',
'myfile.mp3' : '/another/path/to/2.mp3'
},
// optional ability to disable markdown rendering (default is true)
markdown: false,
// optional preprocess function that transforms the XForm (as libXMLJs object) to
// e.g. correct incompatible XForm syntax before Enketo's transformation takes place
preprocess: doc => doc,
} ).then(function( result ){
// do something with result
});
const xform = fs.readFileSync('path/to/xform.xml');

transformer
.transform({
// required string of XForm
xform: xform,
// optional string, to add theme if no theme is defined in the XForm
theme: 'sometheme',
// optional map, to replace jr://..../myfile.png URLs
media: {
'myfile.png': '/path/to/somefile.png',
'myfile.mp3': '/another/path/to/2.mp3',
},
// optional ability to disable markdown rendering (default is true)
markdown: false,
// optional preprocess function that transforms the XForm (as libXMLJs object) to
// e.g. correct incompatible XForm syntax before Enketo's transformation takes place
preprocess: (doc) => doc,
})
.then(function (result) {
// do something with result
});
</code></pre>
<h3 id="install-as-app-(web-api)">Install as app (web API)</h3>
<ol>
Expand All @@ -117,12 +119,11 @@ <h3 id="use-as-app-(web-api)">Use as app (web API)</h3>
</code></pre>
<h3 id="response-format">Response format</h3>
<pre class="prettyprint source lang-json"><code>{
&quot;form&quot; : &quot;&lt;form>.....&lt;/form>&quot;,
&quot;form&quot;: &quot;&lt;form>.....&lt;/form>&quot;,
&quot;model&quot;: &quot;&lt;model>...&lt;/model>&quot;,
&quot;transformerVersion&quot;: &quot;1.13.0&quot;,
&quot;languageMap&quot;: { &quot;Français&quot;: &quot;fr&quot;, &quot;English&quot;: &quot;en&quot; }
}

</code></pre>
<h3 id="test">Test</h3>
<ul>
Expand Down
Loading

0 comments on commit 04019b3

Please sign in to comment.