@@ -4,22 +4,21 @@ const url = `https://github-db.glitch.me/db/api`;
44
55function 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