From b3bfd0c877fd09da8c315b43cf1dd858f3ac1612 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 18 May 2022 09:45:48 +0200 Subject: [PATCH] Add Response.json() static method A method that can be used to create well-formed JSON responses with very little effort. The JSON response is not pretty printed. Tests: https://github.com/web-platform-tests/wpt/pull/32825. Closes #1389. --- fetch.bs | 93 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/fetch.bs b/fetch.bs index 20f888059..28b1a99e6 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7445,6 +7445,7 @@ interface Response { [NewObject] static Response error(); [NewObject] static Response redirect(USVString url, optional unsigned short status = 302); + [NewObject] static Response json(any data, optional ResponseInit init = {}); readonly attribute ResponseType type; @@ -7496,6 +7497,10 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

Creates a redirect {{Response}} that redirects to url with status status. +

response = Response . json(data [, init]) +

Creates a {{Response}} whose body is the JSON-encoded data, and status, status + message, and headers are provided by init. +

response . type

Returns response's type, e.g., "cors". @@ -7541,11 +7546,9 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

  • Return responseObject. -


    - -

    The -new Response(body, init) -constructor steps are: +

    To initialize a response, given a {{Response}} object response, +{{ResponseInit}} init, and an optional body with type body, run +these steps:

    1. If init["{{ResponseInit/status}}"] is not in the range 200 to 599, inclusive, @@ -7554,50 +7557,61 @@ constructor steps are:

    2. If init["{{ResponseInit/statusText}}"] does not match the reason-phrase token production, then throw a {{TypeError}}. -

    3. Set this's response to a new response. - -

    4. Set this's headers to a new {{Headers}} object with - this's relevant Realm, whose header list is this's - response's header list and guard is - "response". - -

    5. Set this's response's status to +

    6. Set response's response's status to init["{{ResponseInit/status}}"]. -

    7. Set this's response's status message to - init["{{ResponseInit/statusText}}"]. +

    8. Set response's response's status message + to init["{{ResponseInit/statusText}}"].

    9. If init["{{ResponseInit/headers}}"] exists, then - fill this's headers with + fill response's headers with init["{{ResponseInit/headers}}"].

    10. -

      If body is non-null, then: +

      If body was given, then:

      1. -

        If init["{{ResponseInit/status}}"] is a null body status, then throw - a {{TypeError}}. +

        If response's status is a null body status, then + throw a {{TypeError}}.

        101 is included in null body status due to its use elsewhere. It does not affect this step. -

      2. Let bodyWithType be the result of extracting - body. - -

      3. Set this's response's body to - bodyWithType's body. +

      4. Set response's body to body's + body. -

      5. Let type be bodyWithType's type. - -

      6. If type is non-null and this's response's - header list does not contain +

      7. If body's type is non-null and + response's header list does not contain `Content-Type`, then append (`Content-Type`, - type) to this's response's + body's type) to response's header list.

    +
    + +

    The +new Response(body, init) +constructor steps are: + +

      +
    1. Set this's response to a new response. + +

    2. Set this's headers to a new {{Headers}} object with + this's relevant Realm, whose header list is this's + response's header list and guard is + "response". + +

    3. Let bodyWithType be null. + +

    4. If body is non-null, then set bodyWithType to the result of + extracting body. + +

    5. Perform initialize a response given this, init, and + bodyWithType. +

    +

    The static error() method steps are to return the result of creating a {{Response}} object, given a new network error, "immutable", and this's relevant Realm. @@ -7630,6 +7644,27 @@ are:

  • Return responseObject. + +

    The static +json(data, init) method steps +are: + +

      +
    1. Let bytes the result of running serialize a JavaScript value to JSON bytes + on data. + +

    2. Let body be the result of extracting bytes. + +

    3. Let responseObject be the result of creating a {{Response}} + object, given a new response, "response", and this's + relevant Realm. + +

    4. Perform initialize a response given responseObject, init, and + (body, "application/json"). + +

    5. Return responseObject. +

    +

    The type getter steps are to return this's response's type.