UNPKG

@squarecloud/api

Version:
210 lines (204 loc) 6.65 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; 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); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // package.json var require_package = __commonJS({ "package.json"(exports2, module2) { module2.exports = { name: "@squarecloud/api", version: "3.7.9", description: "A NodeJS wrapper for Square Cloud API", exports: { ".": { import: { types: "./lib/index.d.ts", default: "./lib/index.js" }, require: { types: "./lib/index.d.cts", default: "./lib/index.cjs" }, default: "./lib/index.js" } }, packageManager: "pnpm@10.11.1", type: "module", scripts: { release: "pnpm build && changeset publish", build: "tsup ./src", "check-types": "tsc --noEmit", lint: "biome check --write .", "lint:ci": "biome check .", test: "node --test test/*.test.js" }, engines: { node: ">=18.0.0" }, devDependencies: { "@biomejs/biome": "^1.9.4", "@changesets/cli": "^2.29.4", "@squarecloud/api-types": "^0.5.0", "@types/node": "^22.15.29", "ts-node": "^10.9.2", "ts-node-dev": "^2.0.0", "tsc-alias": "^1.8.16", tsup: "^8.5.0", typescript: "^5.8.3" }, keywords: [ "wrapper", "square", "squarecloud", "api", "typescript", "app", "bot", "website", "host" ], author: { name: "joaotonaco", url: "https://github.com/joaotonaco" }, repository: { type: "git", url: "git+https://github.com/squarecloudofc/sdk-api-js.git" }, bugs: { url: "https://github.com/squarecloudofc/sdk-api-js/issues" }, homepage: "https://docs.squarecloud.app/sdks/js/client", license: "MIT" }; } }); // src/services/api.ts var api_exports = {}; __export(api_exports, { APIService: () => APIService }); module.exports = __toCommonJS(api_exports); // src/assertions/common.ts function assert({ validate, value, expect, name }) { if (!validate(value)) { const code = name ? `INVALID_${name}` : "VALIDATION_ERROR"; const message = `Expected ${expect}, got ${typeof value}`; throw new SquareCloudAPIError(code, message); } } function makeAssertion(expect, validate) { return (value, name) => { assert({ validate, value, expect, name }); }; } // src/assertions/literal.ts var assertString = makeAssertion( "string", (value) => typeof value === "string" ); var assertBoolean = makeAssertion( "boolean", (value) => typeof value === "boolean" ); var assertPathLike = makeAssertion( "string or Buffer", (value) => typeof value === "string" || value instanceof Buffer ); // src/structures/error.ts var SquareCloudAPIError = class extends TypeError { constructor(code, message, options) { super(code); this.name = "SquareCloudAPIError"; this.message = (code?.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase()) || "UNKNOWN_CODE") + (message ? `: ${message}` : ""); if (options?.stack) { this.stack = options.stack; } if (options?.cause) { this.cause = options.cause; } } }; // src/services/api.ts var APIService = class { constructor(apiKey) { this.apiKey = apiKey; __publicField(this, "baseUrl", "https://api.squarecloud.app"); __publicField(this, "version", "v2"); __publicField(this, "sdkVersion", require_package().version); __publicField(this, "userId"); this.userId = apiKey.split("-")[0]; } async request(...[path, options]) { const { url, init } = this.parseRequestOptions(path, options); const response = await fetch(url, init).catch((err) => { throw new SquareCloudAPIError(err.code, err.message); }); if (response.status === 413) { throw new SquareCloudAPIError("PAYLOAD_TOO_LARGE"); } if (response.status === 429) { throw new SquareCloudAPIError("RATE_LIMIT_EXCEEDED", "Try again later"); } if (response.status === 502 || response.status === 504) { throw new SquareCloudAPIError("SERVER_UNAVAILABLE", "Try again later"); } const data = await response.json().catch(() => { throw new SquareCloudAPIError( "CANNOT_PARSE_RESPONSE", `Failed with status ${response.status}` ); }); if (!data || data.status === "error" || !response.ok) { throw new SquareCloudAPIError(data?.code || "COMMON_ERROR"); } return data; } parseRequestOptions(path, options) { const init = options || {}; init.method = init.method || "GET"; init.headers = { Accept: "application/json", ...init.headers || {}, Authorization: this.apiKey, "User-Agent": `squarecloud-sdk-js/${this.sdkVersion}` }; const url = new URL(path, `${this.baseUrl}/${this.version}/`); if ("query" in init && init.query) { const query = new URLSearchParams(init.query); url.search = query.toString(); init.query = void 0; } if ("body" in init && init.body && !(init.body instanceof FormData)) { init.body = JSON.stringify(init.body); init.headers = { ...init.headers, "Content-Type": "application/json" }; } return { url, init }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { APIService }); //# sourceMappingURL=api.cjs.map