@@ -151,57 +151,36 @@ so we need a `Proxy` for our API type `API'` without its `Raw` endpoint.
152Very similarly to how one can derive haskell functions, we can derive the
152Very similarly to how one can derive haskell functions, we can derive the
153javascript with just a simple function call to `jsForAPI` from
153javascript with just a simple function call to `jsForAPI` from
154−`Servant.JQuery`.
154+`Servant.JS`.
156``` haskell
156``` haskell
157apiJS :: Text
157apiJS :: Text
158−apiJS = jsForAPI api vanillaJS
158+apiJS = jsForAPI api jquery
161This `Text` contains 2 Javascript functions, 'getPoint' and 'getBooks':
161This `Text` contains 2 Javascript functions, 'getPoint' and 'getBooks':
163``` javascript
163``` javascript
164−var getPoint = function(onSuccess, onError)
164+var getPoint = function (onSuccess, onError)
166− var xhr = new XMLHttpRequest();
166+ $.ajax(
167− xhr.open('GET', '/point', true);
167+ { url: '/point'
168− xhr.setRequestHeader("Accept","application/json");
169− xhr.onreadystatechange = function (e) {
170− if (xhr.readyState == 4) {
171− if (xhr.status == 204 || xhr.status == 205) {
172− onSuccess();
168+ , success: onSuccess
173− } else if (xhr.status >= 200 && xhr.status < 300) {
169+ , error: onError
174− var value = JSON.parse(xhr.responseText);
170+ , method: 'GET'
175− onSuccess(value);
171+ });
177− var value = JSON.parse(xhr.responseText);
185−var getBooks = function(q, onSuccess, onError)
175+var getBooks = function (q, onSuccess, onError)
187− var xhr = new XMLHttpRequest();
177+ $.ajax(
188− xhr.open('GET', '/books' + '?q=' + encodeURIComponent(q), true);
178+ { url: '/books' + '?q=' + encodeURIComponent(q)
189− xhr.setRequestHeader("Accept","application/json");
190− xhr.onreadystatechange = function (e) {
191− if (xhr.readyState == 4) {
192− if (xhr.status == 204 || xhr.status == 205) {
193− onSuccess();
179+ , success: onSuccess
194− } else if (xhr.status >= 200 && xhr.status < 300) {
180+ , error: onError
195− var value = JSON.parse(xhr.responseText);
181+ , method: 'GET'
196− onSuccess(value);
182+ });
198− var value = JSON.parse(xhr.responseText);
207We created a directory `static` that contains two static files: `index.html`,
186We created a directory `static` that contains two static files: `index.html`,
@@ -226,3 +205,295 @@ And we're good to go. You can start the `main` function of this file and go to
226`http://localhost:8000/`. Start typing in the name of one of the authors in our
205`http://localhost:8000/`. Start typing in the name of one of the authors in our
227database or part of a book title, and check out how long it takes to
206database or part of a book title, and check out how long it takes to
228approximate pi using the method mentioned above.
207approximate pi using the method mentioned above.
211+Instead of calling `jquery`, you can call its variant `jqueryWith`.
212+Here are the type definitions
215+jquery :: JavaScriptGenerator
216+jqueryWith :: CommonGeneratorOptions -> JavaScriptGenerator
219+The `CommonGeneratorOptions` will let you define different behaviors to
220+change how functions are generated. Here is the definition of currently
224+data CommonGeneratorOptions = CommonGeneratorOptions
226+ -- | function generating function names
227+ functionNameBuilder :: FunctionName -> String
228+ -- | name used when a user want to send the request body (to let you redefine it)
229+ , requestBody :: String
230+ -- | name of the callback parameter when the request was successful
231+ , successCallback :: String
232+ -- | name of the callback parameter when the request reported an error
233+ , errorCallback :: String
234+ -- | namespace on which we define the js function (empty mean local var)
235+ , moduleName :: String
236+ -- | a prefix that should be prepended to the URL in the generated JS
237+ , urlPrefix :: String
241+This pattern is available with all supported backends, and default values are provided.
245+If you don't use JQuery for your application, you can reduce your
246+dependencies to simply use the `XMLHttpRequest` object from the standard API.
248+Use the same code as before but simply replace the previous `apiJS` with
253+apiJS = jsForAPI api vanillaJS
256+The rest is *completely* unchanged.
258+The output file is a bit different, but it has the same parameters,
262+var getPoint = function (onSuccess, onError)
264+ var xhr = new XMLHttpRequest();
265+ xhr.open('GET', '/point', true);
267+ xhr.onreadystatechange = function (e) {
268+ if (xhr.readyState == 4) {
269+ var value = JSON.parse(xhr.responseText);
270+ if (xhr.status == 200 || xhr.status == 201) {
280+var getBooks = function (q, onSuccess, onError)
282+ var xhr = new XMLHttpRequest();
283+ xhr.open('GET', '/books' + '?q=' + encodeURIComponent(q), true);
285+ xhr.onreadystatechange = function (e) {
286+ if (xhr.readyState == 4) {
287+ var value = JSON.parse(xhr.responseText);
288+ if (xhr.status == 200 || xhr.status == 201) {
299+And that's all, your web service can of course be accessible from those
300+two clients at the same time!
306+If you use Axios library for your application, we support that too!
308+Use the same code as before but simply replace the previous `apiJS` with
313+apiJS = jsForAPI api $ axios defAxiosOptions
316+The rest is *completely* unchanged.
318+The output file is a bit different,
322+var getPoint = function ()
324+ return axios({ url: '/point'
329+var getBooks = function (q)
331+ return axios({ url: '/books' + '?q=' + encodeURIComponent(q)
337+**Caution:** In order to support the promise style of the API, there are no onSuccess
338+nor onError callback functions.
340+### Defining Axios configuration
342+Axios lets you define a 'configuration' to determine the behavior of the
343+program when the AJAX request is sent.
345+We mapped this into a configuration
348+data AxiosOptions = AxiosOptions
349+ { -- | indicates whether or not cross-site Access-Control requests
350+ -- should be made using credentials
351+ withCredentials :: !Bool
352+ -- | the name of the cookie to use as a value for xsrf token
353+ , xsrfCookieName :: !(Maybe String)
354+ -- | the name of the header to use as a value for xsrf token
355+ , xsrfHeaderName :: !(Maybe String)
363+You can apply the same procedure as with `vanillaJS` and `jquery`, and
364+generate top level functions.
366+The difference is that `angular` Generator always takes an argument.
370+apiJS = jsForAPI api $ angular defAngularOptions
373+The generated code will be a bit different than previous generators. An extra
374+argument `$http` will be added to let Angular magical Dependency Injector
377+**Caution:** In order to support the promise style of the API, there are no onSuccess
378+nor onError callback functions.
382+var getPoint = function($http)
390+var getBooks = function($http, q)
393+ { url: '/books' + '?q=' + encodeURIComponent(q), true);
399+You can then build your controllers easily
403+app.controller("MyController", function($http) {
404+ this.getPoint = getPoint($http)
405+ .success(/* Do something */)
406+ .error(/* Report error */);
408+ this.getPoint = getBooks($http, q)
409+ .success(/* Do something */)
410+ .error(/* Report error */);
414+### Service generator
416+You can also generate automatically a service to wrap the whole API as
417+a single Angular service:
420+app.service('MyService', function($http) {
422+ postCounter: function()
429+ getCounter: function()
432+ { url: '/books' + '?q=' + encodeURIComponent(q), true);
440+To do so, you just have to use an alternate generator.
444+apiJS = jsForAPI api $ angularService defAngularOptions
447+Again, it is possible to customize some portions with the options.
450+data AngularOptions = AngularOptions
451+ { -- | When generating code with wrapInService, name of the service to generate, default is 'app'
452+ serviceName :: String
453+ , -- | beginning of the service definition
454+ prologue :: String -> String -> String
455+ , -- | end of the service definition
460+# Custom function name builder
462+Servant comes with three name builders included:
464+- camelCase (the default)
468+Keeping the JQuery as an example, let's see the impact:
472+apiJS = jsForAPI api $ jqueryWith defCommonGeneratorOptions { functionNameBuilder: snakeCase }
475+This `String` contains 2 Javascript functions:
479+var getPoint = function (onSuccess, onError)
483+ , success: onSuccess
489+var getBooks = function (q, onSuccess, onError)
492+ { url: '/books' + '?q=' + encodeURIComponent(q)
493+ , success: onSuccess