Skip to content

Commit 64891c5

Browse files
added jwt
1 parent 71f795c commit 64891c5

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# A key value store using Github.
1+
# A simpe key value store using Github.
22

3+
## Install
4+
`npm install github-db`
35
## Usage
46

57
```
68
const GithubDb=require("github-db");
79
8-
const db= GithubDb({db:"test-db",token:"test"});
10+
const db = GithubDb({ db: process.env.DB_NAME, token: process.env.DB_TOKEN });
911
(async function(){
1012
try {
1113
const {identifier}=await db.add({document:"user"},{name:"John"});
@@ -29,3 +31,5 @@ const db= GithubDb({db:"test-db",token:"test"});
2931
})()
3032
```
3133

34+
### Warning
35+
Meant to be used for side-projects, it doesn't scale or fulfil a fully-fledged database needs.

index.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ const url = `https://github-db.glitch.me/db/api`;
44

55
function Client({ db, token }) {
66
const options = { db, token };
7-
let validated = false;
8-
async function validate() {
7+
let auth_token;
8+
async function auth() {
99
try {
10-
if (!validated) {
11-
const { data } = await axios.get(`${url}/validate`, {
12-
params: options
13-
});
14-
validated = data.valid;
10+
if (!auth_token) {
11+
const {data} = await axios.post(`${url}/token`, options);
12+
auth_token = data.token;
13+
axios.defaults.headers.common['Authorization'] = `Bearer ${auth_token}`;
1514
}
1615
} catch (error) {
1716
hanlderError(error);
1817
}
1918
}
2019
async function add({document,identifier}, payload) {
2120
try {
22-
await validate();
21+
await auth();
2322
const resp = await axios.post(`${url}/add`, {
2423
db: options.db,identifier,
2524
token,
@@ -33,7 +32,7 @@ function Client({ db, token }) {
3332
}
3433
async function update({document,identifier} , payload) {
3534
try {
36-
await validate();
35+
await auth();
3736
const resp = await axios.post(`${url}/update`, {
3837
db: options.db,
3938
token,
@@ -48,7 +47,7 @@ function Client({ db, token }) {
4847
}
4948
async function _delete({document,identifier}) {
5049
try {
51-
await validate();
50+
await auth();
5251
const resp = await axios.post(`${url}/delete`, {
5352
db: options.db,
5453
token,
@@ -62,21 +61,17 @@ function Client({ db, token }) {
6261
}
6362
async function fetchOne({document,identifier} ) {
6463
try {
65-
await validate();
66-
const resp = await axios.get(`${url}/fetchOne`, {
67-
params: { db: options.db, token, document, identifier }
68-
});
64+
await auth();
65+
const resp = await axios.post(`${url}/fetchOne`, { db: options.db, token, document, identifier });
6966
return resp.data;
7067
} catch (error) {
7168
hanlderError(error);
7269
}
7370
}
7471
async function fetchAll({document,identifier}) {
7572
try {
76-
await validate();
77-
const resp = await axios.get(`${url}/fetchAll`, {
78-
params: { db: options.db, token,identifier, document }
79-
});
73+
await auth();
74+
const resp = await axios.post(`${url}/fetchAll`, { db: options.db, token,identifier, document });
8075
return resp.data;
8176
} catch (error) {
8277
hanlderError(error);

0 commit comments

Comments
 (0)