Repository navigation
Expand file tree
/
Copy pathrestsharp.js
More file actions
61 lines (50 loc) · 1.79 KB
/
Copy pathrestsharp.js
File metadata and controls
61 lines (50 loc) · 1.79 KB
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
61
'use strict'
const CodeBuilder = require('../../helpers/code-builder')
const { escape } = require('../../helpers/format')
const helpers = require('../../helpers/headers')
module.exports = function (source, options) {
const code = new CodeBuilder()
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
function toPascalCase (str) {
return str.replace(
/\w+/g,
word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
}
if (methods.indexOf(source.method.toUpperCase()) === -1) {
return 'Method not supported'
} else {
code.push('var client = new RestClient("%s//%s");', source.uriObj.protocol, source.uriObj.host)
code.push('var request = new RestRequest("%s", Method.%s);', escape(source.uriObj.path), toPascalCase(source.method))
}
// Add headers, including the cookies
const headers = Object.keys(source.headersObj)
// construct headers
if (headers.length) {
headers.forEach(function (key) {
code.push('request.AddHeader("%s", "%qd");', key, source.headersObj[key])
})
}
// construct cookies
if (source.cookies.length) {
source.cookies.forEach(function (cookie) {
code.push('request.AddCookie("%s", "%s");', cookie.name, cookie.value)
})
}
if (source.postData.text) {
const contentTypeHeader = helpers.getHeader(source.allHeaders, 'content-type')
code.push(
'request.AddParameter(%s, %s, ParameterType.RequestBody);',
contentTypeHeader ? `"${escape(contentTypeHeader)}"` : 'null',
JSON.stringify(source.postData.text)
)
}
code.push('var response = client.Execute(request);')
return code.join()
}
module.exports.info = {
key: 'restsharp',
title: 'RestSharp',
link: 'http://restsharp.org/',
description: 'Simple REST and HTTP API Client for .NET'
}