UNPKG

sdk-lord-of-the-rings

Version:

```bash npm i sdk-lord-of-the-rings ```

225 lines (181 loc) 5.95 kB
'use strict'; import { API_URL, VERSION } from './constants/api.js'; import Network from './network.js'; class Endpoint { constructor(options) { const { path, token, version = VERSION } = options; this._baseUrl = this._createUrl(API_URL, version, path); this._token = token; } async get(options) { const url = this._createQueryUrl(options); return this._request(url); } getOne(id) { const urlTemplate = this._createIdUrl(id); return this._request(urlTemplate); } _createIdUrl(id) { return this._addUrlSubpath([id]); } _addUrlSubpath(path = []) { const formattedPath = path.join('/'); return `${this._baseUrl}/${formattedPath}`; } async _request(url) { const params = this._token ? { headers: { Authorization: this._token } } : {}; return await Network.get(url, params); } _createQueryUrl(options = {}) { const { sort, pagination, filter } = options; const urlArgs = []; if (sort) { const result = this._sortConstructor(sort); urlArgs.push(result); } if (pagination) { const result = this._paginationConstructor(pagination); urlArgs.push([result]); } if (filter) { const result = this._filteringConstructor(filter); urlArgs.push(result); } const url = new URL(this._baseUrl); let existsValue; let isOneArgs = false; urlArgs.forEach((items) => { items.forEach((arg) => { if (typeof arg === 'string') { existsValue = existsValue ? `${existsValue}?${arg}` : arg; return } Object.keys(arg).forEach((key) => { const value = arg[key]; if (value === '') { existsValue = existsValue ? `${existsValue}?${key}` : key; return; } isOneArgs = true; url.searchParams.append(key, value); }); }); }); const urlHref = url.href; if (existsValue) { const value = isOneArgs ? `&${existsValue}` : `?${existsValue}`; return urlHref + value; } return urlHref; } _createUrl(url, version, path) { const finalURl = `${url}/${version}${path}`; const createdUrl = new URL(finalURl); return createdUrl.href; } _sortConstructor(sort) { return Object.entries(sort).map(([key, value]) => { return { sort: `${key}:${value}` }; }); } _paginationConstructor(pagination) { const { page, offset, limit = 10 } = pagination; if (offset && page) { throw new Error('Please, use page or offset'); } return { ...(offset ? { offset } : {}), ...(page ? { page } : {}), limit, }; } _filteringConstructor(filter) { const filtered = []; const { match = [], notMatch = [], include, exclude, exist, notExist, regex, notRegex, numberComparison, } = filter; [notMatch, exclude, notExist, notRegex].forEach((item) => { if (item) { this._addNegativePostfix(item); } }); filtered.push(...match); filtered.push(...notMatch); filtered.push(...this._handleIncludeCases(include)); filtered.push(...this._handleIncludeCases(exclude)); filtered.push(...this._handleExistCases(exist)); filtered.push(...this._handleExistCases(notExist)); filtered.push(...this._handleRegexCases(regex)); filtered.push(...this._handleRegexCases(notRegex)); filtered.push(...this._handleNumberComparison(numberComparison)); return filtered; } _addNegativePostfix(query) { query.forEach((item, index) => { if (typeof item === 'string') { query[index] = `!${item}`; } else { Object.entries(item).forEach(([key, value]) => { delete item[key]; const negativeKey = `${key}!`; item[negativeKey] = value; }); } }); }; _handleIncludeCases(queries) { if (!queries) { return []; } return queries.flatMap((query) => { return Object.entries(query).map(([key, value]) => { return { [key]: value.join(',') }; }); }); } _handleRegexCases(queries) { if (!queries) { return []; } return queries.flatMap((query) => { return Object.entries(query).map(([key, value]) => { return { [key]: `/${value}/i` }; }); }); } _handleExistCases(query) { if (!query) { return []; } return query.map((item) => { return { [item]: '' }; }); } _handleNumberComparison(numberComparison) { const results = []; if (!numberComparison) { return []; } const operators = { gt: '>', gte: '>=', lt: '<', lte: '<=', }; Object.keys(numberComparison).forEach((numberKey) => { const value = numberComparison[numberKey]; const operator = operators[numberKey]; value.forEach((item) => { Object.entries(item).forEach(([key, value]) => { results.push(`${key}${operator}${value}`); }); }); }); return results; } } export default Endpoint;