import {FetchAdapterResponse} from "../fetchAdapter";

/**
 * ## ResponseHelpers **Response工具类**
 * **/
export class ResponseHelpers {

    /** 将json重新封装成FetchAdapterResponse对象 **/
    public static async repackResponseByJson(json: Object, fetchAdapterResponse: FetchAdapterResponse) {
        const readableStream = new ReadableStream({
            async start(controller) {
                const reader = async () => {
                    const uint8Array: Uint8Array = new TextEncoder().encode(JSON.stringify(json))

                    controller.enqueue(uint8Array)
                    controller.close()

                    return
                }

                return reader()
            }
        })

        return new FetchAdapterResponse(readableStream, fetchAdapterResponse)
    }
}