1212
1313const CodeBuilder = require ( '../../helpers/code-builder' )
1414
15+ // Based off org.springframework.http.HttpMethod
1516const standardMethods = [ 'GET' , 'HEAD' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' , 'OPTIONS' , 'TRACE' ]
1617
18+ // Based off org.springframework.http.MediaType
19+ const standardMediaTypes = {
20+ 'application/atom+xml' : 'APPLICATION_ATOM_XML' ,
21+ 'application/cbor' : 'APPLICATION_CBOR' ,
22+ 'application/x-www-form-urlencoded' : 'APPLICATION_FORM_URLENCODED' ,
23+ 'application/graphql-response+json' : 'APPLICATION_GRAPHQL_RESPONSE' ,
24+ 'application/json' : 'APPLICATION_JSON' ,
25+ 'application/x-ndjson' : 'APPLICATION_NDJSON' ,
26+ 'application/octet-stream' : 'APPLICATION_OCTET_STREAM' ,
27+ 'application/pdf' : 'APPLICATION_PDF' ,
28+ 'application/problem+json' : 'APPLICATION_PROBLEM_JSON' ,
29+ 'application/problem+xml' : 'APPLICATION_PROBLEM_XML' ,
30+ 'application/x-protobuf' : 'APPLICATION_PROTOBUF' ,
31+ 'application/rss+xml' : 'APPLICATION_RSS_XML' ,
32+ 'application/xhtml+xml' : 'APPLICATION_XHTML_XML' ,
33+ 'application/xml' : 'APPLICATION_XML' ,
34+ 'application/yaml' : 'APPLICATION_YAML' ,
35+ 'image/gif' : 'IMAGE_GIF' ,
36+ 'image/jpeg' : 'IMAGE_JPEG' ,
37+ 'image/png' : 'IMAGE_PNG' ,
38+ 'multipart/form-data' : 'MULTIPART_FORM_DATA' ,
39+ 'multipart/mixed' : 'MULTIPART_MIXED' ,
40+ 'multipart/related' : 'MULTIPART_RELATED' ,
41+ 'text/event-stream' : 'TEXT_EVENT_STREAM' ,
42+ 'text/html' : 'TEXT_HTML' ,
43+ 'text/markdown' : 'TEXT_MARKDOWN' ,
44+ 'text/plain' : 'TEXT_PLAIN' ,
45+ 'text/xml' : 'TEXT_XML'
46+ }
47+
1748module . exports = function ( source , options ) {
1849 const opts = Object . assign ( {
1950 indent : ' '
@@ -32,39 +63,48 @@ module.exports = function (source, options) {
3263 code . push ( 1 , '.method(HttpMethod.valueOf("%s"))' , source . method . toUpperCase ( ) )
3364 }
3465
35- code . push ( 1 , '.uri("%s")' , source . fullUrl )
66+ if ( Object . keys ( source . queryObj ) . length ) {
67+ code . push ( 1 , '.uri("%s", uriBuilder -> {' , source . url )
68+ Object . keys ( source . queryObj ) . forEach ( function ( key ) {
69+ const value = source . queryObj [ key ]
70+ if ( Array . isArray ( value ) ) {
71+ value . forEach ( function ( val ) {
72+ code . push ( 2 , 'uriBuilder.queryParam("%qd", "%qd");' , key , val )
73+ } )
74+ } else {
75+ code . push ( 2 , 'uriBuilder.queryParam("%qd", "%qd");' , key , value )
76+ }
77+ } )
78+ code . push ( 2 , 'return uriBuilder.build();' )
79+ code . push ( 1 , '})' )
80+ } else {
81+ code . push ( 1 , '.uri("%s")' , source . url )
82+ }
3683
3784 if ( source . cookies && source . cookies . length ) {
3885 source . cookies . forEach ( function ( cookie ) {
39- code . push ( 1 , '.cookie("%s ", "%s ")' , cookie . name , cookie . value )
86+ code . push ( 1 , '.cookie("%qd ", "%qd ")' , cookie . name , cookie . value )
4087 } )
4188 }
4289
43- const headers = Object . keys ( source . allHeaders ) . filter ( function ( key ) {
44- return key . toLowerCase ( ) !== 'cookie'
45- } )
90+ const headers = Object . keys ( source . headersObj )
4691 if ( headers . length ) {
4792 headers . forEach ( function ( key ) {
48- code . push ( 1 , '.header("%s", "%qd")' , key , source . allHeaders [ key ] )
93+ code . push ( 1 , '.header("%s", "%qd")' , key , source . headersObj [ key ] )
4994 } )
5095 }
5196
5297 if ( source . postData && source . postData . text ) {
53- if ( source . postData . mimeType === 'application/json' ) {
54- code . push ( 1 , '.contentType(MediaType.APPLICATION_JSON)' )
55- code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
56- } else if ( source . postData . mimeType === 'application/x-www-form-urlencoded' ) {
57- code . push ( 1 , '.contentType(MediaType.APPLICATION_FORM_URLENCODED)' )
58- code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
59- } else if ( source . postData . mimeType && source . postData . mimeType . startsWith ( 'multipart/form-data' ) ) {
60- code . push ( 1 , '.contentType(MediaType.parseMediaType("multipart/form-data"))' )
61- code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
62- } else {
63- if ( source . postData . mimeType ) {
98+ if ( source . postData . mimeType ) {
99+ const mappedEnumConst = standardMediaTypes [ source . postData . mimeType ]
100+ if ( mappedEnumConst ) {
101+ code . push ( 1 , '.contentType(MediaType.%s)' , mappedEnumConst )
102+ } else {
64103 code . push ( 1 , '.contentType(MediaType.parseMediaType("%s"))' , source . postData . mimeType )
65104 }
66- code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
67105 }
106+
107+ code . push ( 1 , '.body(%s)' , JSON . stringify ( source . postData . text ) )
68108 }
69109
70110 code . push ( 1 , '.retrieve()' )
0 commit comments