Skip to content

Commit e2d470e

Browse files
Update, delete, get ETAG, access token auth and more additions.
1 parent 81884cd commit e2d470e

File tree

2 files changed

+128
-11
lines changed

2 files changed

+128
-11
lines changed

src/index.ts

Lines changed: 118 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@ export default class Database {
1010
*/
1111
private readonly URL_SUFFIX: string = '.json';
1212

13+
/**
14+
* @type {string}
15+
* Suffix to be appended to DB URL to access json and do authed requests if you are using ID auth
16+
*/
17+
private readonly ID_SUFFIX = '.json?auth=';
18+
19+
/**
20+
* @type {string}
21+
* Suffix to be appended to DB URL to access json and do authed requests if you are using token auth
22+
*/
23+
private readonly TOKEN_SUFFIX = '.json?access_token=';
24+
1325
/**
1426
* @type {string}
1527
* Suffix to be appended to DB URL to access json and do authed requests
1628
*/
17-
private readonly AUTH_SUFFIX = '.json?auth=';
29+
private authSuffix = '';
1830

1931
/**
2032
* @type {Options}
@@ -28,29 +40,32 @@ export default class Database {
2840
constructor(options: Options) {
2941
this.options = options;
3042

31-
if (!options.databaseUrl || options.databaseUrl.length == 0) {
43+
// Firebase only responds to HTTPS traffic
44+
if (!options.databaseUrl || options.databaseUrl.length == 0 || !options.databaseUrl.search('https')) {
3245
// console.log('Database URL is invalid. SDK not initialized');
3346
}
47+
48+
this.authSuffix = (options.tokenAuthentication ? this.TOKEN_SUFFIX : this.ID_SUFFIX);
3449
}
3550

3651
/**
3752
* Writes the given data to the database
3853
* @param {string} location Where to write the data
3954
* @param {string} data JSON to write, stringified
40-
* @param {HeadersInit} reqHeaders Headers to be sent with the req
55+
* @param {Headers} reqHeaders Headers to be sent with the req
4156
* @param {boolean} authenticated Should the request be authenticated
4257
* @return {boolean} If the write operation suceeded
4358
*/
4459
async write(
4560
location: string,
4661
data: string,
47-
reqHeaders: HeadersInit,
62+
reqHeaders: Headers,
4863
authenticated: boolean,
4964
): Promise<boolean> {
5065
try {
51-
await fetch(
66+
var res: Response = await fetch(
5267
this.options.databaseUrl + location + authenticated ?
53-
this.AUTH_SUFFIX + this.options.authentication :
68+
this.authSuffix + this.options.authentication :
5469
this.URL_SUFFIX,
5570
{
5671
method: 'PUT',
@@ -59,7 +74,7 @@ export default class Database {
5974
},
6075
);
6176

62-
return true;
77+
return res.status == 200;
6378
} catch {
6479
return false;
6580
}
@@ -68,20 +83,112 @@ export default class Database {
6883
/**
6984
* Read data at the given location in DB
7085
* @param {string} location Where to read the data
71-
* @param {HeadersInit} reqHeaders Headers to be sent with the req
86+
* @param {Headers} reqHeaders Headers to be sent with the req
7287
* @param {boolean} authenticated Should the request be authenticated
73-
* @return {string} JSON or boolean false
88+
* @return {string} The read JSON data, or an empty string in the case of a failure
7489
*/
7590
async read(
7691
location: string,
77-
reqHeaders: HeadersInit,
92+
reqHeaders: Headers,
7893
authenticated: boolean,
7994
): Promise<string> {
8095
try {
8196
return await (
8297
await fetch(
8398
this.options.databaseUrl + location + authenticated ?
84-
this.AUTH_SUFFIX + this.options.authentication :
99+
this.authSuffix + this.options.authentication :
100+
this.URL_SUFFIX,
101+
{
102+
method: 'GET',
103+
headers: reqHeaders,
104+
},
105+
)
106+
).json();
107+
} catch {
108+
return ''; // Let's not return null...
109+
}
110+
}
111+
112+
/**
113+
* Delete data at the given location in DB
114+
* @param {string} location Where to delete the data
115+
* @param {Headers} reqHeaders Headers to be sent with the req
116+
* @param {boolean} authenticated Should the request be authenticated
117+
* @return {boolean} If the delete operation suceeded
118+
*/
119+
async delete(
120+
location: string,
121+
reqHeaders: Headers,
122+
authenticated: boolean,
123+
): Promise<boolean> {
124+
try {
125+
var res: Response = await fetch(
126+
this.options.databaseUrl + location + authenticated ?
127+
this.authSuffix + this.options.authentication :
128+
this.URL_SUFFIX,
129+
{
130+
method: 'DELETE',
131+
headers: reqHeaders,
132+
},
133+
);
134+
135+
return res.status == 200;
136+
} catch {
137+
return false;
138+
}
139+
}
140+
141+
/**
142+
* Attempts to update the given data in the database
143+
* @param {string} location Where to update the data
144+
* @param {string} data JSON to update, stringified
145+
* @param {Headers} reqHeaders Headers to be sent with the req
146+
* @param {boolean} authenticated Should the request be authenticated
147+
* @return {boolean} If the update operation suceeded
148+
*/
149+
async update(
150+
location: string,
151+
data: string,
152+
reqHeaders: Headers,
153+
authenticated: boolean,
154+
): Promise<boolean> {
155+
try {
156+
var res: Response = await fetch(
157+
this.options.databaseUrl + location + authenticated ?
158+
this.authSuffix + this.options.authentication :
159+
this.URL_SUFFIX,
160+
{
161+
method: 'PATCH',
162+
headers: reqHeaders,
163+
body: data,
164+
},
165+
);
166+
167+
return res.status == 200;
168+
} catch {
169+
return false;
170+
}
171+
}
172+
173+
/**
174+
* Get ETag of data at location
175+
* @param {string} location Location of data to get the ETag of
176+
* @param {Headers} reqHeaders Headers to be sent with the req
177+
* @param {boolean} authenticated Should the request be authenticated
178+
* @return {string} The ETag of the data, or an empty string in the case of a failure
179+
*/
180+
async getETag(
181+
location: string,
182+
reqHeaders: Headers,
183+
authenticated: boolean,
184+
): Promise<string> {
185+
reqHeaders.append('X-Firebase-ETag', 'true');
186+
187+
try {
188+
return await (
189+
await fetch(
190+
this.options.databaseUrl + location + authenticated ?
191+
this.authSuffix + this.options.authentication :
85192
this.URL_SUFFIX,
86193
{
87194
method: 'GET',

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
export type Options = {
22
databaseUrl?: string;
33
authentication?: string;
4+
// requestQuery?: Query;
5+
tokenAuthentication?: boolean,
46
}
7+
8+
/* export enum Query {
9+
ID_AUTH = '.json?auth=',
10+
TOKEN_AUTH = '.json?access_token=',
11+
SHALLOW_QUERY = '.json?shallow=true'
12+
}
13+
FOR ANOTHER DAY
14+
*/

0 commit comments

Comments
 (0)