Skip to content

Instantly share code, notes, and snippets.

@passandscore
Last active May 5, 2021 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passandscore/3f7b0ec7874eb27e846259d36274a472 to your computer and use it in GitHub Desktop.
Save passandscore/3f7b0ec7874eb27e846259d36274a472 to your computer and use it in GitHub Desktop.
Kinvey DB request library
//How to use it in a JS file
document.addEventListener('DOMContentLoaded', main)
const kinvey = new Kinvey('kid_BkYBymjvd', 'ed0eed3599fa4d5d9d062fae8755f43c');
const data = {
username: 'guest',
password: 'guest'
}
const newPost = {
title: "Post edited by Alfred",
body: "This post was edited"
}
async function main() {
const authToken = await kinvey.login(data);
console.log(authToken)
const res = await kinvey.logout(authToken);
console.log(res)
}
//Library File
class Kinvey {
constructor(app_id, app_secret) {
this.app_id = app_id;
this.app_secret = app_secret
}
test(data) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/appdata/${this.app_id}`
let { username, password } = data;
let headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ":" + password)
}
fetch(url, { method: 'GET', headers: headers })
.then(res => {
if (res.ok) {
return res.json();
}
throw res;
}).then(data => resolve(data))
.catch(err => reject(err))
})
}
get(endpoint, authToken, loginData) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/appdata/${this.app_id}/${endpoint}`;
let headers;
if (loginData) {
let { username, password } = data;
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ":" + password)
}
}
if (authToken) {
headers = {
'Content-Type': 'application/json',
'Authorization': 'Kinvey ' + authToken
}
}
fetch(url, { method: 'GET', headers: headers })
.then(res => {
if (res.ok) {
return res.json();
}
throw res;
}).then(data => resolve(data))
.catch(err => reject(err))
})
}
post(endpoint, data, authToken, loginData) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/appdata/${this.app_id}/${endpoint}`;
let headers;
if (loginData) {
let { username, password } = loginData;
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ":" + password)
}
}
if (authToken) {
headers = {
'Content-Type': 'application/json',
'Authorization': 'Kinvey ' + authToken
}
}
fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(data) })
.then(res => {
if (res.status === 201) {
return res.json();
}
throw res;
}).then(data => resolve(data))
.catch(err => reject(err))
})
}
delete(endpoint, id, authToken, loginData) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/appdata/${this.app_id}/${endpoint}/${id}`;
let headers;
if (loginData) {
let { username, password } = loginData;
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ":" + password)
}
}
if (authToken) {
headers = {
'Content-Type': 'application/json',
'Authorization': 'Kinvey ' + authToken
}
}
fetch(url, { method: 'DELETE', headers: headers })
.then(res => {
if (res.ok) {
return res.json();
}
throw res;
}).then(data => resolve(data))
.catch(err => reject(err))
})
}
edit(endpoint, id, data, authToken, loginData) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/appdata/${this.app_id}/${endpoint}/${id}`;
let headers;
if (loginData) {
let { username, password } = loginData;
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ":" + password)
}
}
if (authToken) {
headers = {
'Content-Type': 'application/json',
'Authorization': 'Kinvey ' + authToken
}
}
fetch(url, { method: 'PUT', headers: headers, body: JSON.stringify(data) })
.then(res => {
if (res.ok) {
return res.json();
}
throw res;
}).then(data => resolve(data))
.catch(err => reject(err))
})
}
signup(data) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/user/${this.app_id}`;
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(this.app_id + ":" + this.app_secret)
}
fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(data) })
.then(res => {
if (res.status === 201) {
return res.json();
}
throw res;
}).then(data => resolve(data._kmd.authtoken))
.catch(err => reject(err))
})
}
login(data) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/user/${this.app_id}/login`;
let { username, password } = data;
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ":" + password)
}
fetch(url, { method: 'POST', headers: headers, body: JSON.stringify(data) })
.then(res => {
if (res.ok) {
return res.json();
}
throw res;
}).then(data => resolve(data._kmd.authtoken))
.catch(err => reject(err))
})
}
logout(authToken) {
return new Promise((resolve, reject) => {
let url = `https://baas.kinvey.com/user/${this.app_id}/_logout`;
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Kinvey ' + authToken
}
fetch(url, { method: 'POST', headers: headers })
.then(res => {
if (res.status === 204) {
resolve({ msg: 'user logged out' })
}
}).catch(err => reject(err))
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment