UNPKG

@aciiverse/fetcii

Version:

Simple fetch module primary designed for the aciifx backend

384 lines 14.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FilterCollection = exports.fetcii = exports.users = exports.CompareOperator = void 0; const users_1 = require("./users"); var CompareOperator; (function (CompareOperator) { CompareOperator["Equal"] = "eq"; CompareOperator["NotEqual"] = "neq"; CompareOperator["GreaterThan"] = "gt"; CompareOperator["GreaterEqual"] = "ge"; CompareOperator["LessThan"] = "lt"; CompareOperator["LessEqual"] = "le"; CompareOperator["Inside"] = "in"; CompareOperator["NotInside"] = "ni"; })(CompareOperator || (exports.CompareOperator = CompareOperator = {})); // export fetcii's users module exports.users = users_1.users; var fetcii; (function (fetcii) { // export fetcii's users module fetcii.users = users_1.users; class FilterCollection { constructor() { this.filters = {}; } /** * @method gets all filters * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ getAllFilters() { return this.filters; } /** * @method gets a filter by property * @param {string} property key * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ getFilter(property) { return this.filters[property]; } /** * @method adds a filter to the filter (NO filters replace -> only the and property will be replaced) * @param {string} property to filter * @param {Filter[]} filters array * @param {boolean} and? optional -> standard is `true` * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ add(property, filters, and = true) { const content = this.filters[property]; if (!content) { // -> filter NOT existing this.replace(property, filters, and); return; } const existFilters = content.filters; content.filters = existFilters.concat(filters); content.and = and; } /** * @method replaces a filter to the filter | old filter will be **DELETED** * @param {string} property to filter * @param {Filter[]} filters array * @param {boolean} and? optional -> standard is `true` * @author Flowtastisch * @memberof Aciiverse * @date 21.11.2024 */ replace(property, filters, and = true) { this.filters[property] = { filters: filters, and: and, }; } /** * @method removes a filter by property * @param {string} property key * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ remove(property) { if (!this.filters[property]) { // -> property doesn't exist return; } delete this.filters[property]; } } fetcii.FilterCollection = FilterCollection; /** * @method gets the data by using the ```GET``` request * @param {string} url the url where you want to fetch from * @param {GetOptions?} options optional filter, sorting, skip and top * @param {string?} token optional the auth token * @returns {Promise<Result>} ```err``` is undefined if the function **OR** request failed; ```response.ok``` and ```response.status``` shows if the request succeeded * @author Flowtastisch * @memberof Aciiverse * @date 24.08.24 */ function getcii(url, options, token) { return __awaiter(this, void 0, void 0, function* () { try { const queryParams = new URLSearchParams(); // Manage Options if (options) { // -> options defined if (options.filters) { // -> filters exists queryParams.append("$filters", JSON.stringify(options.filters)); } if (options.top) { // -> top exists queryParams.append("$top", String(options.top)); } if (options.skip) { // -> skip exists queryParams.append("$skip", String(options.skip)); } if (options.orderBy) { // -> orderBy exists queryParams.append("$orderBy", JSON.stringify(options.orderBy)); } if (options.select) { // -> select exists queryParams.append("$select", JSON.stringify(options.select)); } if (queryParams.size > 0) { // -> queryParams exists -> add params to url url = `${url}/?${String(queryParams)}`; } } // set header const headers = { "Content-Type": "application/json", }; if (token && !fetcii.users.checkTokenExpired()) { // -> token is set headers["authorization"] = token; } // fire Request const response = yield fetch(url, { method: "GET", headers: headers, }), json = yield response.json(); if (!response.ok) { // -> Response not okay -> fill err field let errMsg = response.statusText; if (json.message) { // -> message is in data errMsg = json.message; } return { response: response, data: json, err: new Error(errMsg), }; } return { response: response, data: json, }; } catch (err) { if (err instanceof Error) { // -> is error console.error(err.message); return { err: err, }; } else { // -> is unknown console.error(err); return { err: new Error(`Unknown Error while fetching: ${err}`), }; } } }); } fetcii.getcii = getcii; /** * @method creates an entry by using the ```POST``` request * @param {string} url the url where you want to fetch from * @param {Record<string, any>} data you want to create * @param {string?} token optional the auth token * @returns {Promise<Result>} ```err``` is undefined if the function **OR** request failed; ```response.ok``` and ```response.status``` shows if the request succeeded * @author Flowtastisch * @memberof Aciiverse * @date 24.08.24 */ function createcii(url, data, token) { return __awaiter(this, void 0, void 0, function* () { try { const headers = { "Content-Type": "application/json", }; if (token && !fetcii.users.checkTokenExpired()) { // -> token is set headers["authorization"] = token; } const response = yield fetch(url, { method: "POST", headers: headers, body: JSON.stringify(data), }), json = yield response.json(); if (!response.ok) { // -> Response not okay -> fill err field let errMsg = response.statusText; if (json.message) { // -> message is in data errMsg = json.message; } return { response: response, data: json, err: new Error(errMsg), }; } return { response: response, data: json, }; } catch (err) { if (err instanceof Error) { // -> is error console.error(err.message); return { err: err, }; } else { // -> is unknown console.error(err); return { err: new Error(`Unknown Error while fetching: ${err}`), }; } } }); } fetcii.createcii = createcii; /** * @method updates an entry by using the ```PUT``` request * @param {string} url the url where you want to fetch from * @param {Record<string, any>} data you want to update * @param {string?} token optional the auth token * @returns {Promise<Result>} ```err``` is undefined if the function **OR** request failed; ```response.ok``` and ```response.status``` shows if the request succeeded * @author Flowtastisch * @memberof Aciiverse * @date 24.08.24 */ function updatecii(url, data, token) { return __awaiter(this, void 0, void 0, function* () { try { const headers = { "Content-Type": "application/json", }; if (token && !fetcii.users.checkTokenExpired()) { // -> token is set headers["authorization"] = token; } const response = yield fetch(url, { method: "PUT", headers: headers, body: JSON.stringify(data), }), json = yield response.json(); if (!response.ok) { // -> Response not okay -> fill err field let errMsg = response.statusText; if (json.message) { // -> message is in data errMsg = json.message; } return { response: response, data: json, err: new Error(errMsg), }; } return { response: response, data: json, }; } catch (err) { if (err instanceof Error) { // -> is error console.error(err.message); return { err: err, }; } else { // -> is unknown console.error(err); return { err: new Error(`Unknown Error while fetching: ${err}`), }; } } }); } fetcii.updatecii = updatecii; /** * @method removes an entry by using the ```DELETE``` request * @param {string} url the url where you want to fetch from * @param {string?} token optional the auth token * @returns {Promise<Result>} ```err``` is undefined if the function **OR** request failed; ```response.ok``` and ```response.status``` shows if the request succeeded * @author Flowtastisch * @memberof Aciiverse * @date 24.08.24 */ function removecii(url, token) { return __awaiter(this, void 0, void 0, function* () { try { const headers = { "Content-Type": "application/json", }; if (token && !fetcii.users.checkTokenExpired()) { // -> token is set headers["authorization"] = token; } const response = yield fetch(url, { method: "DELETE", headers: headers, }), json = yield response.json(); if (!response.ok) { // -> Response not okay -> fill err field let errMsg = response.statusText; if (json.message) { // -> message is in data errMsg = json.message; } return { response: response, data: json, err: new Error(errMsg), }; } return { response: response, data: json, }; } catch (err) { if (err instanceof Error) { // -> is error console.error(err.message); return { err: err, }; } else { // -> is unknown console.error(err); return { err: new Error(`Unknown Error while fetching: ${err}`), }; } } }); } fetcii.removecii = removecii; })(fetcii || (exports.fetcii = fetcii = {})); // export FilterCollection exports.FilterCollection = fetcii.FilterCollection; //# sourceMappingURL=index.js.map