|
1 | 1 | # restfulclient-typescript
|
2 | 2 | Windward's TypeScript Client for the RESTful Engine.
|
| 3 | + |
| 4 | +## How to use this module |
| 5 | +#### Pre-requisites |
| 6 | +First, let's get some pre-requisites out of the way. This module was designed |
| 7 | +to run in Node, but can also be used in the browser. |
| 8 | + |
| 9 | +It utilizes the new [fetch API](https://jakearchibald.com/2015/thats-so-fetch/) |
| 10 | +and as such, you'll need a fetch polyfill. We suggest |
| 11 | +[isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) |
| 12 | +but you can also use [node-fetch](https://github.com/bitinn/node-fetch) or |
| 13 | +[GitHub's fetch](https://github.com/github/fetch) depending on your needs. |
| 14 | + |
| 15 | +These fetch polyfills also require a Promise polyfill depending on your environment |
| 16 | +requirements. For this, [es6-polyfill](https://github.com/stefanpenner/es6-promise). |
| 17 | + |
| 18 | +Finally, this package uses node's buffer to read and write file data. When running |
| 19 | +from the browser, this isn't available natively unless you pick up a [buffer |
| 20 | +for browser](https://github.com/feross/buffer) -- although if you are making calls |
| 21 | +from a browser, you are most likely going to be using the URI APIs. |
| 22 | + |
| 23 | +#### Tests |
| 24 | +A great way to get up and running is to see how the tests work. If this is your style, |
| 25 | +head over to [test/test.ts](test/test.ts) |
| 26 | + |
| 27 | +If you downloaded this repository, you can run these with `npm test` (after |
| 28 | +running `npm install` of course) |
| 29 | + |
| 30 | +#### Get running |
| 31 | +To get the library, you can either download the dist directory from here or install with |
| 32 | + |
| 33 | + npm install restfulclieint |
| 34 | + |
| 35 | +If you installed with npm, you are set to import directly from node_modules. |
| 36 | +You can follow one of 2 import styles: |
| 37 | + |
| 38 | + import { |
| 39 | + getVersion, createReport, OutputFormat, XmlDataSource, AdoDataSource, |
| 40 | + JsonDataSource, ODataDataSource, TemplateVariable, DataSet, Status, Version |
| 41 | + } from 'restfulclient'; |
| 42 | + |
| 43 | +OR |
| 44 | + |
| 45 | + import * as restfulclient from 'restfulclient'; |
| 46 | + |
| 47 | +#### Reading files from file system |
| 48 | +When running reports, either the templates are already on the server in which case |
| 49 | +you just have to tell restfulclient where they are. Otherwise you need to load up a |
| 50 | +file to send to the server. If you need to load a file, here's how to do it: |
| 51 | + |
| 52 | + import * as fs from 'fs'; |
| 53 | + var template = fs.readFileSync("path/to/template"); |
| 54 | + |
| 55 | +This puts a [`Buffer`](https://nodejs.org/api/buffer.html) in template using Node's |
| 56 | +file system library. You'll have to devise other means of getting a buffer if you are |
| 57 | +running in the browser. |
| 58 | + |
| 59 | +#### Creating a report |
| 60 | +Now, we'll run a report, assuming you have imported everything using option 1 above. |
| 61 | +Use the `createReport()` method which takes a template and an OutputFormat as well as |
| 62 | +the address to your RESTful Engine server. Other options can be specified optionally |
| 63 | +on the returned `Report` object. |
| 64 | + |
| 65 | + var report = createReport("http://localhost:49731/", OutputFormat.PDF, template); |
| 66 | + report.hyphenate = Hyphenation.Off; |
| 67 | + |
| 68 | +#### Adding DataSources |
| 69 | +To add a DataSource, simply push it to the report's datasources array: |
| 70 | + |
| 71 | + report.dataSources.push( |
| 72 | + new AdoDataSource("MSSQL", "System.Data.SqlClient", |
| 73 | + "Data Source=mssql.windward.net;Initial Catalog=Northwind;User=demo;Password=demo" |
| 74 | + ) |
| 75 | + ); |
| 76 | + |
| 77 | +DataSets are done in the same way: |
| 78 | + |
| 79 | + var dataset; // contains an rdlx file in a Buffer previously read by the file system |
| 80 | + report.dataSets.push(new DataSet(dataset)); |
| 81 | + |
| 82 | +#### Adding a variable |
| 83 | +Adding a variable is just as easy -- you push variables onto DataSources rather |
| 84 | +than Templates though: |
| 85 | + |
| 86 | + var dataSource = new AdoDataSource("MSSQL", "System.Data.SqlClient", |
| 87 | + "Data Source=mssql.windward.net;Initial Catalog=Northwind;User=demo;Password=demo" |
| 88 | + ); |
| 89 | + datasource.variables.push(new TemplateVariable("Var1", "hi there", "text")); |
| 90 | + report.dataSources.push(datasource); |
| 91 | + |
| 92 | +#### Running |
| 93 | +You want to run? Call `process()` which returns a `Promise`. It only returns a |
| 94 | +Promise because of how the fetch API works (although it's a really good idea to |
| 95 | +use promises rather than blocking the main thread for a server call). |
| 96 | + |
| 97 | + report.process(); |
| 98 | + |
| 99 | +You can optionally pass dataSources and dataSets to process() rather than adding |
| 100 | +them yourself. These get pushed to report. |
| 101 | + |
| 102 | + report.process(dataSources, dataSets); |
| 103 | + |
| 104 | +#### Async |
| 105 | +The RESTful Engine provides an async API you can take advantage of if you want. |
| 106 | +In JavaScript, this isn't as useful given the async features of the language, |
| 107 | +but it will help if you'd rather not hold a connection open while waiting for |
| 108 | +a potentially large report. |
| 109 | + |
| 110 | +Just call `processAsync()` instead of `process()` -- this simply sends the |
| 111 | +request to the server. |
| 112 | + |
| 113 | +You may use a combination of `getStatus()` and `getReport()` to retrieve the report |
| 114 | +(both of these return a `Promise`) or you may call `getReportWhenReady()` which |
| 115 | +will poll the server at a period specified in its arguments. It returns a `Promise` |
| 116 | +that eventually resolves to a `Buffer` contianing your output (it'll reject if any |
| 117 | +server errors occur) |
0 commit comments