// Copyright Epic Games, Inc. All Rights Reserved. "use strict"; import { CbObject } from "./compactbinary.js" //////////////////////////////////////////////////////////////////////////////// export class Fetcher { constructor() { this._resource = ""; this._query = {}; } resource(...parts) { var value = parts.join("/"); if (!value.startsWith("/")) value= "/" + value; this._resource = value; return this; } param(name, value) { this._query[name] = value; return this; } async json() { const response = await this._get("application/json"); return response ? (await response.json()) : {}; } async cbo() { const response = await this._get("application/x-ue-cb"); if (!response) return null; const buffer = await response.arrayBuffer(); const data = new Uint8Array(buffer); return new CbObject(data); } async delete() { const resource = this._build_uri(); const response = await fetch(resource, { "method" : "DELETE" }); } _build_uri() { var suffix = ""; for (var key in this._query) { suffix += suffix ? "&" : "?"; suffix += key + "=" + this._query[key]; } return this._resource + suffix; } async _get(accept="*") { const resource = this._build_uri(); const response = await fetch(resource, { "method" : "GET", "headers" : { "Accept": accept }, }); if (response.status >= 200 && response.status <= 299) return response; } }