UNPKG

@bitzonegaming/roleplay-engine-sdk

Version:
127 lines (126 loc) 5.14 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.EngineClient = void 0; const axios_1 = __importStar(require("axios")); const engine_error_1 = require("./engine-error"); const uuid_1 = require("uuid"); class EngineClient { constructor(configs, authorization) { this.configs = configs; this.authorization = authorization; this.axiosInstance = axios_1.default.create({ baseURL: this.configs.apiUrl, timeout: this.configs.timeout ?? 10000, }); this.axiosInstance.interceptors.response.use((response) => response, (error) => { if (error.response) { const status = error.response.status; const data = error.response.data; return Promise.reject(new engine_error_1.EngineError(data.key, data.message, data.params, status, { url: error.request.res.responseUrl, })); } return Promise.reject(error); }); } setAuthorization(authorization) { this.authorization = authorization; } changeLocale(locale) { this.configs = { ...this.configs, locale }; } request(config) { config.headers = this.getHeaders(config); return this.axiosInstance.request(config); } get({ url, query, options, }) { const fullUrl = this.appendQuery(url, query); return this.request({ ...options, method: 'GET', url: fullUrl }).then((p) => p.data); } post({ url, data, query, options, }) { const fullUrl = this.appendQuery(url, query); return this.request({ ...options, method: 'POST', url: fullUrl, data }).then((p) => p.data); } put({ url, data, query, options, }) { const fullUrl = this.appendQuery(url, query); return this.request({ ...options, method: 'PUT', url: fullUrl, data }).then((p) => p.data); } patch({ url, data, query, options, }) { const fullUrl = this.appendQuery(url, query); return this.request({ ...options, method: 'PATCH', url: fullUrl, data }).then((p) => p.data); } delete({ url, query, options, }) { const fullUrl = this.appendQuery(url, query); return this.request({ ...options, method: 'DELETE', url: fullUrl }).then((p) => p.data); } getHeaders(cfg) { const headers = new axios_1.AxiosHeaders(); if (cfg.headers) { const existing = cfg.headers; Object.entries(existing).forEach(([key, value]) => { if (value != null) { headers.set(key, String(value)); } }); } headers.set('Accept-Language', this.configs.locale); headers.set('x-agent-name', this.configs.applicationName); headers.set('x-server-id', this.configs.serverId); headers.set('x-correlationid', cfg.correlationId ?? (0, uuid_1.v4)()); if (this.authorization) { headers.set('Authorization', this.authorization.getAuthorizationToken()); } if (cfg.characterId) { headers.set('x-character-id', cfg.characterId); } if (cfg.executorUser) { headers.set('x-executor-user', cfg.executorUser); } return headers; } appendQuery(url, query) { if (!query) return url; const parts = Object.entries(query) .filter(([, v]) => v != null) .map(([k, v]) => { const str = Array.isArray(v) ? v.map((x) => String(x)).join(',') : String(v); return `${encodeURIComponent(k)}=${encodeURIComponent(str)}`; }); return parts.length ? `${url}?${parts.join('&')}` : url; } } exports.EngineClient = EngineClient;