@@ -26,20 +26,14 @@ export const http2: Client<Http2Options> = {
2626 link : 'http://devel-m6w6.rhcloud.com/mdref/http' ,
2727 description : 'PHP with pecl/http v2' ,
2828 } ,
29- convert : ( { postData, headersObj, method, queryObj, cookiesObj, url } , options ) => {
30- const opts = {
31- closingTag : false ,
32- indent : ' ' ,
33- noTags : false ,
34- shortTags : false ,
35- ...options ,
36- } ;
37-
38- const { push, blank, join } = new CodeBuilder ( { indent : opts . indent } ) ;
29+ convert : ( { postData, headersObj, method, queryObj, cookiesObj, url } , options = { } ) => {
30+ const { closingTag = false , indent = ' ' , noTags = false , shortTags = false } = options ;
31+
32+ const { push, blank, join } = new CodeBuilder ( { indent } ) ;
3933 let hasBody = false ;
4034
41- if ( ! opts . noTags ) {
42- push ( opts . shortTags ? '<?' : '<?php' ) ;
35+ if ( ! noTags ) {
36+ push ( shortTags ? '<?' : '<?php' ) ;
4337 blank ( ) ;
4438 }
4539
@@ -50,9 +44,7 @@ export const http2: Client<Http2Options> = {
5044 switch ( postData . mimeType ) {
5145 case 'application/x-www-form-urlencoded' :
5246 push ( '$body = new http\\Message\\Body;' ) ;
53- push (
54- `$body->append(new http\\QueryString(${ convertType ( postData . paramsObj , opts . indent ) } ));` ,
55- ) ;
47+ push ( `$body->append(new http\\QueryString(${ convertType ( postData . paramsObj , indent ) } ));` ) ;
5648 blank ( ) ;
5749 hasBody = true ;
5850 break ;
@@ -85,8 +77,8 @@ export const http2: Client<Http2Options> = {
8577 }
8678 } ) ;
8779
88- const field = Object . keys ( fields ) . length ? convertType ( fields , opts . indent ) : 'null' ;
89- const formValue = files . length ? convertType ( files , opts . indent ) : 'null' ;
80+ const field = Object . keys ( fields ) . length ? convertType ( fields , indent ) : 'null' ;
81+ const formValue = files . length ? convertType ( files , indent ) : 'null' ;
9082
9183 push ( '$body = new http\\Message\\Body;' ) ;
9284 push ( `$body->addForm(${ field } , ${ formValue } );` ) ;
@@ -106,6 +98,11 @@ export const http2: Client<Http2Options> = {
10698 hasBody = true ;
10799 break ;
108100 }
101+ case 'application/json' :
102+ push ( '$body = new http\\Message\\Body;' ) ;
103+ push ( `$body->append(json_encode(${ convertType ( postData . jsonObj , indent ) } ));` ) ;
104+ hasBody = true ;
105+ break ;
109106
110107 default :
111108 if ( postData . text ) {
@@ -125,18 +122,18 @@ export const http2: Client<Http2Options> = {
125122 }
126123
127124 if ( Object . keys ( queryObj ) . length ) {
128- push ( `$request->setQuery(new http\\QueryString(${ convertType ( queryObj , opts . indent ) } ));` ) ;
125+ push ( `$request->setQuery(new http\\QueryString(${ convertType ( queryObj , indent ) } ));` ) ;
129126 blank ( ) ;
130127 }
131128
132129 if ( Object . keys ( headersObj ) . length ) {
133- push ( `$request->setHeaders(${ convertType ( headersObj , opts . indent ) } );` ) ;
130+ push ( `$request->setHeaders(${ convertType ( headersObj , indent ) } );` ) ;
134131 blank ( ) ;
135132 }
136133
137134 if ( Object . keys ( cookiesObj ) . length ) {
138135 blank ( ) ;
139- push ( `$client->setCookies(${ convertType ( cookiesObj , opts . indent ) } );` ) ;
136+ push ( `$client->setCookies(${ convertType ( cookiesObj , indent ) } );` ) ;
140137 blank ( ) ;
141138 }
142139
@@ -145,7 +142,7 @@ export const http2: Client<Http2Options> = {
145142 blank ( ) ;
146143 push ( 'echo $response->getBody();' ) ;
147144
148- if ( ! opts . noTags && opts . closingTag ) {
145+ if ( ! noTags && closingTag ) {
149146 blank ( ) ;
150147 push ( '?>' ) ;
151148 }
0 commit comments