backend-integration-fetch
Version:
The Backend-Api-Integration package simplifies interaction with the backend API by providing a set of CRUD methods for common endpoints. It abstracts away the complexity of making HTTP requests and handling responses, allowing developers to focus on appli
134 lines (108 loc) • 4.46 kB
JavaScript
import { UploadFile, fetchHelper } from "./httpFetchHelper.js";
const POST_METHOD = "POST";
const GET_METHOD = "GET";
const PUT_METHOD = "PUT";
const PATCH_METHOD = "PATCH";
const DELETE_METHOD = "DELETE";
const KHATAVANI_ENDPOINT = "khatavani";
const USER_ENDPOINT = "Users";
const RESOURCE_ENDPOINT = "resources";
const ACCOUNT_ENDPOINT = "accounts";
const VOUCHER_ENDPOINT = "vouchers";
const TRANSACTION_ENDPOINT = "transactions";
const RESOURCEGROUPS_ENDPOINT = "resourceGroups";
const USERROLE_ENDPOINT = "userlevels";
const BALANCE_ENDPOINT = "balances";
const FILE_ENDPOINT = "files";
const BATCH_ENDPOINT = "batches";
const FETCHING_ITEMS_LIMIT = 50;
const optionsCallBack = (options, pageParam) => {
return {
limit: FETCHING_ITEMS_LIMIT,
cursur: pageParam * FETCHING_ITEMS_LIMIT,
...options,
}
}
export const UploadData = async (fileStore, authToken, khID, id = null, Public = false, serverUrl) => {
var AllFiles = fileStore.map(async (element) => {
var UploadImageObject = { ...element.ImageMetaData };
Public ? UploadImageObject.public = Public : UploadImageObject.accountID = id;
let fileEndpoint = CreateFileEndpoint(serverUrl)
var data = await fileEndpoint.create(
authToken,
khID,
UploadImageObject
)
var file = element.ImageData
if (element.ImageData.data) {
file = element.ImageData.data;
}
return UploadFile(PUT_METHOD, file,
data.headers, data.url);
})
await Promise.all(AllFiles, authToken, khID);
return AllFiles;
}
class CRUDMethods {
constructor(endpoint, serverUrl) {
this.endpoint = endpoint,
this.serverUrl = serverUrl
}
async create(accessToken, khID = null, body = null) {
return fetchHelper(this.endpoint, POST_METHOD, accessToken, khID, body, null, this.serverUrl);
}
async get(accessToken, khID = null, body = null, options = null, pageParam = 0) {
return fetchHelper(this.endpoint, GET_METHOD, accessToken, khID, body, optionsCallBack(options, pageParam), this.serverUrl);
}
// Use this method when you want to get all the entries at once.
// This should not be used in the UI centric component as the UI
// may freeze.
async getAll(accessToken, khID = null, body = null, options = null) {
return fetchHelper(this.endpoint, GET_METHOD, accessToken, khID, body, options, this.serverUrl);
}
async getOne(accessToken, khID = null, id) {
return fetchHelper(this.endpoint + "/" + id, GET_METHOD, accessToken, khID, null, null, this.serverUrl);
}
async put(accessToken, khID = null, body = null, id) {
return fetchHelper(this.endpoint + "/" + id, PUT_METHOD, accessToken, khID, body, null, this.serverUrl);
}
async patch(accessToken, khID = null, body = null, id) {
return fetchHelper(this.endpoint + "/" + id, PATCH_METHOD, accessToken, khID, body, null, this.serverUrl);
}
async delete(accessToken, khID = null, id) {
return fetchHelper(this.endpoint + "/" + id, DELETE_METHOD, accessToken, khID, null, null, this.serverUrl);
}
}
export const Khatavani = (serverUrl) => {
return new CRUDMethods(KHATAVANI_ENDPOINT, serverUrl);
}
export const Users = (serverUrl) => {
return new CRUDMethods(USER_ENDPOINT, serverUrl);
}
export const Resources = (serverUrl) => {
return new CRUDMethods(RESOURCE_ENDPOINT, serverUrl);
}
export const Accounts = (serverUrl) => {
return new CRUDMethods(ACCOUNT_ENDPOINT, serverUrl);
}
export const Vouchers = (serverUrl) => {
return new CRUDMethods(VOUCHER_ENDPOINT, serverUrl);
}
export const Transactions = (serverUrl) => {
return new CRUDMethods(TRANSACTION_ENDPOINT, serverUrl);
}
export const ResourceGroups = (serverUrl) => {
return new CRUDMethods(RESOURCEGROUPS_ENDPOINT, serverUrl);
}
export const UserRoles = (serverUrl) => {
return new CRUDMethods(USERROLE_ENDPOINT, serverUrl);
}
export const Balance = (serverUrl) => {
return new CRUDMethods(BALANCE_ENDPOINT, serverUrl);
}
export const File = (serverUrl) => {
return new CRUDMethods(FILE_ENDPOINT, serverUrl);
}
export const Batch = (serverUrl) => {
return new CRUDMethods(BATCH_ENDPOINT, serverUrl);
}