Skip to content

Commit 153ab74

Browse files
committed
feature: set the default value of the awaited values if there's already a body stream
1 parent 4b487a2 commit 153ab74

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/Response.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ public function arrayBuffer():Promise {
210210

211211
public function awaitArrayBuffer():ArrayBuffer {
212212
$arrayBuffer = null;
213+
if($bodyText = $this->getBody()->getContents()) {
214+
$bytes = strlen($bodyText);
215+
$arrayBuffer = new ArrayBuffer($bytes);
216+
for($i = 0; $i < $bytes; $i++) {
217+
$arrayBuffer->offsetSet($i, ord($bodyText[$i]));
218+
}
219+
}
213220

214221
$this->arrayBuffer()->then(function(ArrayBuffer $resolved) use(&$arrayBuffer) {
215222
$arrayBuffer = $resolved;
@@ -239,6 +246,12 @@ public function blob():Promise {
239246

240247
public function awaitBlob():Blob {
241248
$blob = null;
249+
if($bodyText = $this->getBody()->getContents()) {
250+
$blobOptions = [
251+
"type" => $this->getResponseHeaders()->get("content-type")?->getValues()[0],
252+
];
253+
$blob = new Blob([$bodyText], $blobOptions);
254+
}
242255

243256
$this->blob()->then(function(Blob $resolved) use(&$blob) {
244257
$blob = $resolved;
@@ -277,6 +290,16 @@ public function formData():Promise {
277290

278291
public function awaitFormData():FormData {
279292
$formData = null;
293+
if($bodyText = $this->getBody()->getContents()) {
294+
parse_str($bodyText, $bodyData);
295+
$formData = new FormData();
296+
foreach($bodyData as $key => $value) {
297+
if(is_array($value)) {
298+
$value = implode(",", $value);
299+
}
300+
$formData->set((string)$key, (string)$value);
301+
}
302+
}
280303

281304
$this->blob()->then(function(FormData $resolved) use(&$formData) {
282305
$formData = $resolved;
@@ -308,6 +331,10 @@ public function json(int $depth = 512, int $options = 0):Promise {
308331
/** @param int<1, max> $depth */
309332
public function awaitJson(int $depth = 512, int $options = 0):JsonObject {
310333
$jsonObject = null;
334+
if($bodyText = $this->getBody()->getContents()) {
335+
$builder = new JsonObjectBuilder();
336+
$jsonObject = $builder->fromJsonString($bodyText);
337+
}
311338

312339
$this->json($depth, $options)->then(function(JsonObject $resolved) use(&$jsonObject) {
313340
$jsonObject = $resolved;
@@ -333,7 +360,7 @@ public function text():Promise {
333360
}
334361

335362
public function awaitText():string {
336-
$text = null;
363+
$text = $this->getBody()->getContents();
337364

338365
$this->text()->then(function(string $resolved) use(&$text) {
339366
$text = $resolved;

0 commit comments

Comments
 (0)