add api client
This commit is contained in:
parent
d1cd071f06
commit
63132abc20
|
@ -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
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue