todite
Version:
Connect your application to Todite, a free to-do list app
140 lines • 7.05 kB
JavaScript
/**
* Can't import from `../index.d.ts` because TypeScript will put this line at the top of the file:
* ```js
* Object.defineProperty(exports, "__esModule", { value: true });
* ```
* However `exports` is not defined in the browser
*/
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());
});
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var _apiKey;
// Since this is to be used in the browser, we aren't to export anything because global variables (and classes) will be available anyway
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
class Todite {
constructor(apiKey) {
_apiKey.set(this, void 0);
this.apiKeyRegex = /^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/;
__classPrivateFieldSet(this, _apiKey, apiKey);
if (!this.apiKeyRegex.test(apiKey)) {
throw new Error('Invalid API Key');
}
fetch(`https://todite.vercel.app/api/v1/user?api_key=${__classPrivateFieldGet(this, _apiKey)}`)
.then(res => res.json())
.then((user) => {
if (user.error) {
throw new Error(user.error.message);
}
});
}
create({ name, completed, date }) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield fetch(`https://todite.vercel.app/api/v1/todos?api_key=${__classPrivateFieldGet(this, _apiKey)}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, completed, date })
}).then(res => res.json());
if (data.error) {
throw new Error(data.error.message);
}
if (data.date)
data.date = new Date(data.date);
// Return the object like this so that `__v` isn't included (if you know a cleaner way to do this, feel free to submit a PR :D)
return { _id: data._id, name: data.name, completed: data.completed, user: data.user, date: data.date };
});
}
getAll() {
return __awaiter(this, void 0, void 0, function* () {
const data = yield fetch(`https://todite.vercel.app/api/v1/todos?api_key=${__classPrivateFieldGet(this, _apiKey)}`).then(res => res.json());
if (data.error) {
throw new Error(data.error.message);
}
data.forEach(todo => {
if (todo.date)
todo.date = new Date(todo.date);
});
// Return the array like this so that `__v` isn't included (if you know a cleaner way to do this, feel free to submit a PR :D)
return data.map(todo => ({ _id: todo._id, name: todo.name, completed: todo.completed, user: todo.user, date: todo.date }));
});
}
get(id) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield fetch(`https://todite.vercel.app/api/v1/todo/${id}?api_key=${__classPrivateFieldGet(this, _apiKey)}`).then(res => res.json());
if (data.error) {
if (data.error.status === 404)
return null;
throw new Error(data.error.message);
}
if (data.date)
data.date = new Date(data.date);
// Return the object like this so that `__v` isn't included (if you know a cleaner way to do this, feel free to submit a PR :D)
return { _id: data._id, name: data.name, completed: data.completed, user: data.user, date: data.date };
});
}
update(newTodoDataOrId, name, completed, date) {
return __awaiter(this, void 0, void 0, function* () {
let id;
if (typeof newTodoDataOrId === 'string') {
id = newTodoDataOrId;
}
else {
if (!newTodoDataOrId.id && !newTodoDataOrId._id)
throw new Error('id must be passed in as an argument');
// At least one of these will always be defined
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
id = newTodoDataOrId.id || newTodoDataOrId._id;
if (!name)
name = newTodoDataOrId.name;
if (!completed)
completed = newTodoDataOrId.completed;
if (!date)
date = newTodoDataOrId.date;
}
const data = yield fetch(`https://todite.vercel.app/api/v1/todo/${id}?api_key=${__classPrivateFieldGet(this, _apiKey)}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, completed, date })
}).then(res => res.json());
if (data.error) {
throw new Error(data.error.message);
}
if (data.date)
data.date = new Date(data.date);
// Return the object like this so that `__v` isn't included (if you know a cleaner way to do this, feel free to submit a PR :D)
return { _id: data._id, name: data.name, completed: data.completed, user: data.user, date: data.date };
});
}
delete(id) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const data = yield fetch(`https://todite.vercel.app/api/v1/todo/${id}?api_key=${__classPrivateFieldGet(this, _apiKey)}`, {
method: 'DELETE'
}).then(res => res.json());
if (!data.success) {
throw new Error((_a = data.error) === null || _a === void 0 ? void 0 : _a.message);
}
});
}
}
_apiKey = new WeakMap();
//# sourceMappingURL=browser.js.map
;