/* * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include namespace Web::Fetch { // https://fetch.spec.whatwg.org/#responseinit struct ResponseInit { u16 status; DeprecatedString status_text; Optional headers; }; // https://fetch.spec.whatwg.org/#response class Response final : public Bindings::PlatformObject , public BodyMixin { WEB_PLATFORM_OBJECT(Response, Bindings::PlatformObject); public: static JS::NonnullGCPtr create(JS::Realm&, JS::NonnullGCPtr, Headers::Guard); static WebIDL::ExceptionOr> construct_impl(JS::Realm&, Optional const& body = {}, ResponseInit const& init = {}); virtual ~Response() override; // ^BodyMixin virtual Optional mime_type_impl() const override; virtual Optional body_impl() override; virtual Optional body_impl() const override; [[nodiscard]] JS::NonnullGCPtr response() const { return m_response; } // JS API functions [[nodiscard]] static JS::NonnullGCPtr error(JS::VM&); [[nodiscard]] static WebIDL::ExceptionOr> redirect(JS::VM&, DeprecatedString const& url, u16 status); [[nodiscard]] static WebIDL::ExceptionOr> json(JS::VM&, JS::Value data, ResponseInit const& init = {}); [[nodiscard]] Bindings::ResponseType type() const; [[nodiscard]] DeprecatedString url() const; [[nodiscard]] bool redirected() const; [[nodiscard]] u16 status() const; [[nodiscard]] bool ok() const; [[nodiscard]] DeprecatedString status_text() const; [[nodiscard]] JS::NonnullGCPtr headers() const; [[nodiscard]] WebIDL::ExceptionOr> clone() const; // Pull in json() from the BodyMixin, which gets lost due to the static json() above using BodyMixin::json; private: Response(JS::Realm&, JS::NonnullGCPtr); virtual JS::ThrowCompletionOr initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; WebIDL::ExceptionOr initialize_response(ResponseInit const&, Optional const&); // https://fetch.spec.whatwg.org/#concept-response-response // A Response object has an associated response (a response). JS::NonnullGCPtr m_response; // https://fetch.spec.whatwg.org/#response-headers // A Response object also has an associated headers (null or a Headers object), initially null. JS::GCPtr m_headers; }; }