@@ -154,33 +154,33 @@ javascript with just a simple function call to `jsForAPI` from
154`Servant.JS`.
154`Servant.JS`.
156``` haskell
156``` haskell
157−apiJS :: Text
157+apiJS1 :: Text
158−apiJS = jsForAPI api jquery
158+apiJS1 = 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)
168− , success: onSuccess
165+var getPoint = function(onSuccess, onError)
169+ , success: onSuccess
175−var getBooks = function (q, onSuccess, onError)
175+var getBooks = function(q, onSuccess, onError)
178− { url: '/books' + '?q=' + encodeURIComponent(q)
178+ { url: '/books' + '?q=' + encodeURIComponent(q)
179− , success: onSuccess
179+ , success: onSuccess
180− , error: onError
180+ , error: onError
181− , method: 'GET'
181+ , type: 'GET'
186We created a directory `static` that contains two static files: `index.html`,
186We created a directory `static` that contains two static files: `index.html`,
@@ -192,7 +192,7 @@ write the generated javascript into a file:
192``` haskell
192``` haskell
193writeJSFiles :: IO ()
193writeJSFiles :: IO ()
194writeJSFiles = do
194writeJSFiles = do
195− T.writeFile "static/api.js" apiJS
195+ T.writeFile "static/api.js" apiJS1
196 jq <- T.readFile =<< Language.Javascript.JQuery.file
196 jq <- T.readFile =<< Language.Javascript.JQuery.file
197 T.writeFile "static/jq.js" jq
197 T.writeFile "static/jq.js" jq
@@ -220,7 +220,7 @@ The `CommonGeneratorOptions` will let you define different behaviors to
220change how functions are generated. Here is the definition of currently
220change how functions are generated. Here is the definition of currently
221available options:
221available options:
223−```haskell
223+```haskell ignore
224data CommonGeneratorOptions = CommonGeneratorOptions
224data CommonGeneratorOptions = CommonGeneratorOptions
226 -- | function generating function names
226 -- | function generating function names
@@ -249,8 +249,8 @@ Use the same code as before but simply replace the previous `apiJS` with
249the following one:
249the following one:
251``` haskell
251``` haskell
252−apiJS :: String
252+apiJS2 :: Text
253−apiJS = jsForAPI api vanillaJS
253+apiJS2 = jsForAPI api vanillaJS
256The rest is *completely* unchanged.
256The rest is *completely* unchanged.
@@ -259,41 +259,50 @@ The output file is a bit different, but it has the same parameters,
260``` javascript
260``` javascript
262−var getPoint = function (onSuccess, onError)
264− var xhr = new XMLHttpRequest();
265− xhr.open('GET', '/point', true);
263+var getPoint = function(onSuccess, onError)
265+ var xhr = new XMLHttpRequest();
266+ xhr.open('GET', '/point', true);
267+ xhr.setRequestHeader(\"Accept\",\"application/json\");
267− xhr.onreadystatechange = function (e) {
268+ xhr.onreadystatechange = function (e) {
268 if (xhr.readyState == 4) {
269 if (xhr.readyState == 4) {
270+ if (xhr.status == 204 || xhr.status == 205) {
272+ } else if (xhr.status >= 200 && xhr.status < 300) {
273+ var value = JSON.parse(xhr.responseText);
269 var value = JSON.parse(xhr.responseText);
276 var value = JSON.parse(xhr.responseText);
270− if (xhr.status == 200 || xhr.status == 201) {
273− onError(value);
277+ onError(value);
277− xhr.send(null);
281+ xhr.send(null);
280−var getBooks = function (q, onSuccess, onError)
284+var getBooks = function(q, onSuccess, onError)
282− var xhr = new XMLHttpRequest();
286+ var xhr = new XMLHttpRequest();
283− xhr.open('GET', '/books' + '?q=' + encodeURIComponent(q), true);
287+ xhr.open('GET', '/books' + '?q=' + encodeURIComponent(q), true);
284−
288+ xhr.setRequestHeader(\"Accept\",\"application/json\");
285− xhr.onreadystatechange = function (e) {
289+ xhr.onreadystatechange = function (e) {
286 if (xhr.readyState == 4) {
290 if (xhr.readyState == 4) {
291+ if (xhr.status == 204 || xhr.status == 205) {
293+ } else if (xhr.status >= 200 && xhr.status < 300) {
287 var value = JSON.parse(xhr.responseText);
294 var value = JSON.parse(xhr.responseText);
288− if (xhr.status == 200 || xhr.status == 201) {
289− onSuccess(value);
295+ onSuccess(value);
290− } else {
296+ } else {
297+ var value = JSON.parse(xhr.responseText);
291− onError(value);
298+ onError(value);
295− xhr.send(null);
302+ xhr.send(null);
299And that's all, your web service can of course be accessible from those
308And that's all, your web service can of course be accessible from those
@@ -309,8 +318,8 @@ Use the same code as before but simply replace the previous `apiJS` with
309the following one:
318the following one:
311``` haskell
320``` haskell
312−apiJS :: String
321+apiJS3 :: Text
313−apiJS = jsForAPI api $ axios defAxiosOptions
322+apiJS3 = jsForAPI api $ axios defAxiosOptions
316The rest is *completely* unchanged.
325The rest is *completely* unchanged.
@@ -319,19 +328,23 @@ The output file is a bit different,
320``` javascript
329``` javascript
322−var getPoint = function ()
332+var getPoint = function()
324− return axios({ url: '/point'
334+ return axios({ url: '/point'
325− , method: 'get'
335+ , method: 'get'
329−var getBooks = function (q)
341+var getBooks = function(q)
331− return axios({ url: '/books' + '?q=' + encodeURIComponent(q)
343+ return axios({ url: '/books' + '?q=' + encodeURIComponent(q)
332− , method: 'get'
344+ , method: 'get'
337**Caution:** In order to support the promise style of the API, there are no onSuccess
350**Caution:** In order to support the promise style of the API, there are no onSuccess
@@ -366,8 +379,8 @@ generate top level functions.
366The difference is that `angular` Generator always takes an argument.
379The difference is that `angular` Generator always takes an argument.
368``` haskell
381``` haskell
369−apiJS :: String
382+apiJS4 :: Text
370−apiJS = jsForAPI api $ angular defAngularOptions
383+apiJS4 = jsForAPI api $ angular defAngularOptions
373The generated code will be a bit different than previous generators. An extra
386The generated code will be a bit different than previous generators. An extra
@@ -379,21 +392,25 @@ nor onError callback functions.
380``` javascript
393``` javascript
382var getPoint = function($http)
396var getPoint = function($http)
384 return $http(
398 return $http(
385− { url: '/counter'
399+ { url: '/point'
386− , method: 'GET'
400+ , method: 'GET'
390var getBooks = function($http, q)
406var getBooks = function($http, q)
392 return $http(
408 return $http(
393− { url: '/books' + '?q=' + encodeURIComponent(q), true);
409+ { url: '/books' + '?q=' + encodeURIComponent(q)
394 , method: 'GET'
410 , method: 'GET'
399You can then build your controllers easily
416You can then build your controllers easily
@@ -440,8 +457,8 @@ app.service('MyService', function($http) {
440To do so, you just have to use an alternate generator.
457To do so, you just have to use an alternate generator.
442``` haskell
459``` haskell
443−apiJS :: String
460+apiJS5 :: Text
444−apiJS = jsForAPI api $ angularService defAngularOptions
461+apiJS5 = jsForAPI api $ angularService defAngularOptions
447Again, it is possible to customize some portions with the options.
464Again, it is possible to customize some portions with the options.
@@ -468,32 +485,34 @@ Servant comes with three name builders included:
468Keeping the JQuery as an example, let's see the impact:
485Keeping the JQuery as an example, let's see the impact:
470``` haskell
487``` haskell
471−apiJS :: String
488+apiJS6 :: Text
472−apiJS = jsForAPI api $ jqueryWith defCommonGeneratorOptions { functionNameBuilder= snakeCase }
489+apiJS6 = jsForAPI api $ jqueryWith defCommonGeneratorOptions { functionNameBuilder= snakeCase }
475−This `String` contains 2 Javascript functions:
492+This `Text` contains 2 Javascript functions:
477``` javascript
494``` javascript
479−var getPoint = function (onSuccess, onError)
497+var get_point = function(onSuccess, onError)
482 { url: '/point'
500 { url: '/point'
483 , success: onSuccess
501 , success: onSuccess
484 , error: onError
502 , error: onError
485− , method: 'GET'
503+ , type: 'GET'
489−var getBooks = function (q, onSuccess, onError)
507+var get_books = function(q, onSuccess, onError)
492 { url: '/books' + '?q=' + encodeURIComponent(q)
510 { url: '/books' + '?q=' + encodeURIComponent(q)
493 , success: onSuccess
511 , success: onSuccess
494 , error: onError
512 , error: onError
495− , method: 'GET'
513+ , type: 'GET'