jeet-kiit-crud
Version:
A simple CRUD client npm package using API key + Postgres
46 lines (45 loc) • 1.3 kB
JavaScript
// src/index.ts
import axios from "axios";
import dotenv from "dotenv";
dotenv.config();
function CrudLibrary({ apiKey, apiUrl, API_TODO_URL }) {
if (!apiKey || !apiUrl || !API_TODO_URL) {
throw new Error("Missing apiKey or apiUrl or API_TODO_URL when creating the CRUD client.");
}
const headers = {
"x-api-key": apiKey
};
return {
async create(data) {
const res = await axios.post(`${API_TODO_URL}/create`, data, { headers });
return res.data;
},
async get(id) {
const res = await axios.get(`${API_TODO_URL}/${id}`, { headers });
return res.data;
},
async getAll() {
const res = await axios.get(`${API_TODO_URL}/all`, { headers });
return res.data;
},
async getCredits() {
const res = await axios.get(`${API_TODO_URL}/credits`, { headers });
return res.data;
},
async getCreditLimit() {
const res = await axios.get(`${API_TODO_URL}/credit-limit`, { headers });
return res.data;
},
async update(id, data) {
const res = await axios.put(`${API_TODO_URL}/${id}`, data, { headers });
return res.data;
},
async remove(id) {
const res = await axios.delete(`${API_TODO_URL}/${id}`, { headers });
return res.data;
}
};
}
export {
CrudLibrary
};