add api client

This commit is contained in:
b1ek 2023-05-17 04:39:30 +10:00
parent d1cd071f06
commit 63132abc20
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,72 @@
import axios, { Axios } from "axios";
import { User } from "./User";
import { APIToken } from './APIToken';
class ClientCreateOptions {
/** Key used to access APIs, if you don't have user data
* @type {APIToken?}
*/
key;
/** Current user's data
* @type {User?}
*/
user;
}
/**
* An API client which is basically a User but with
* API token and with access to private fields.
*/
class Client extends User {
/** @type {APIToken} */
key;
/** @type {string} */
openid_id;
/** @type {string} */
favorites_apartments;
/** @type {string} */
comparison_apartments;
/**
*
* @param {ClientCreateOptions} options
*/
constructor(options) {
super();
if (options.key === undefined & options.user === undefined) {
throw new Error('Either key or user is required.');
}
this.key = options.key;
if (options.user) {
for (const key in options.user) {
this[key] = options.user[key];
}
}
}
/**
*
* @param {string} uri
* @param {string} method
* @param {import('axios').AxiosRequestConfig<any>} options
*/
fetchData(url, method, options) {
axios.request({
url,
method,
headers: {
...(options.headers ? options.headers : {}),
'X-Pairent-Auth': this.key.key
},
...options
});
}
}