2020-11-16 03:15:49 +00:00
|
|
|
import { MediumPublication } from './medium.classes.publication';
|
|
|
|
import * as plugins from './medium.plugins';
|
|
|
|
|
2020-11-16 23:04:54 +00:00
|
|
|
export interface IMediumAccountData {
|
|
|
|
id: string;
|
|
|
|
username: string;
|
|
|
|
url: string;
|
|
|
|
imageUrl: string;
|
|
|
|
}
|
|
|
|
|
2020-11-16 03:15:49 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-11-16 23:04:54 +00:00
|
|
|
export class MediumAccount implements IMediumAccountData {
|
|
|
|
// STATIC
|
|
|
|
|
|
|
|
// INSTANCE
|
2020-11-16 03:15:49 +00:00
|
|
|
private accessToken: string;
|
2020-11-16 23:04:54 +00:00
|
|
|
public readyDeferred = plugins.smartpromise.defer();
|
|
|
|
public baseApiDomain = 'https://api.medium.com/v1';
|
|
|
|
|
|
|
|
id: string;
|
|
|
|
username: string;
|
|
|
|
url: string;
|
|
|
|
imageUrl: string;
|
2020-11-16 03:15:49 +00:00
|
|
|
|
|
|
|
constructor(accessTokenArg: string) {
|
|
|
|
this.accessToken = accessTokenArg;
|
2020-11-16 23:04:54 +00:00
|
|
|
this.getAccountInfo().then((dataArg) => {
|
|
|
|
Object.assign(this, dataArg);
|
|
|
|
this.readyDeferred.resolve();
|
|
|
|
});
|
2020-11-16 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 23:04:54 +00:00
|
|
|
public async getAccountInfo(): Promise<IMediumAccountData> {
|
2020-11-16 03:15:49 +00:00
|
|
|
const result = await this.request('/me', 'GET');
|
2020-11-16 23:04:54 +00:00
|
|
|
const accountData = result.body.data;
|
|
|
|
return accountData;
|
2020-11-16 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getPublications(): Promise<MediumPublication[]> {
|
2020-11-16 23:04:54 +00:00
|
|
|
return MediumPublication.getAllPublications(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getOwnPublications(): Promise<MediumPublication[]> {
|
|
|
|
return MediumPublication.getOwnPublications(this);
|
2020-11-16 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 23:04:54 +00:00
|
|
|
public async request(routeArg: string, methodArg: 'POST' | 'GET', payloadArg?: any) {
|
2020-11-16 03:15:49 +00:00
|
|
|
const response = await plugins.smartrequest.request(`${this.baseApiDomain}${routeArg}`, {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${this.accessToken}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Accept-Charset': 'utf-8',
|
|
|
|
},
|
|
|
|
method: methodArg,
|
|
|
|
requestBody: payloadArg ? JSON.stringify(payloadArg) : null
|
|
|
|
});
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|