Code coverage report for src/targets/ocaml/cohttp.js

Statements: 100% (22 / 22)      Branches: 100% (8 / 8)      Functions: 100% (2 / 2)      Lines: 100% (22 / 22)      Ignored: none     

All files » src/targets/ocaml/ » cohttp.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60    1   1 10       10   10 10 10     10     10   10 8   8 11     8       10   6       10   10             10 10   10     1            
'use strict'
 
var util = require('util')
 
module.exports = function (source, options) {
  var opts = util._extend({
    indent: '  '
  }, options)
 
  var code = []
 
  code.push('open Cohttp_lwt_unix')
  code.push('open Lwt')
  code.push('')
 
  // Create URI
  code.push(util.format('let uri = Uri.of_string "%s" in', source.fullUrl))
 
  // Add headers, including the cookies
  var headers = Object.keys(source.allHeaders)
 
  if (headers.length) {
    code.push('let headers = Header.init ()')
 
    headers.map(function (key) {
      code.push(util.format(opts.indent + '|> fun h -> Header.add h "%s" "%s"', key, source.allHeaders[key]))
    })
 
    code.push('in')
  }
 
  // Add body
  if (source.postData.text) {
    // Just text
    code.push(util.format('let body = %s in', JSON.stringify(source.postData.text)))
  }
 
  // Do the request
  code.push('')
 
  code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri',
    headers.length ? '~headers ' : '',
    source.postData.text ? '~body ' : '',
    source.method
  ))
 
  // Catch result
  code.push('>>= fun (res, body_stream) ->')
  code.push(opts.indent + '(* Do stuff with the result *)')
 
  return code.join('\n')
}
 
module.exports.info = {
  key: 'cohttp',
  title: 'CoHTTP',
  link: 'https://github.com/mirage/ocaml-cohttp',
  description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml'
}