"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/api.ts
var api_exports = {};
__export(api_exports, {
  APIClient: () => APIClient,
  isAPIError: () => isAPIError
});
module.exports = __toCommonJS(api_exports);
var MK_API_ERROR = /* @__PURE__ */ Symbol();
function isAPIError(reason) {
  return reason[MK_API_ERROR] === true;
}
var APIClient = class {
  constructor(opts) {
    this.origin = opts.origin;
    this.credential = opts.credential;
    this.fetch = opts.fetch || ((...args) => fetch(...args));
  }
  request(endpoint, params = {}, credential) {
    const promise = new Promise((resolve, reject) => {
      this.fetch(`${this.origin}/api/${endpoint}`, {
        method: "POST",
        body: JSON.stringify({
          ...params,
          i: credential !== void 0 ? credential : this.credential
        }),
        credentials: "omit",
        cache: "no-cache"
      }).then(async (res) => {
        const body = res.status === 204 ? null : await res.json();
        if (res.status === 200) {
          resolve(body);
        } else if (res.status === 204) {
          resolve(null);
        } else {
          reject({
            [MK_API_ERROR]: true,
            ...body.error
          });
        }
      }).catch(reject);
    });
    return promise;
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  APIClient,
  isAPIError
});
