fix(core): update

This commit is contained in:
Philipp Kunz 2020-11-16 23:04:54 +00:00
parent 05349ba947
commit fd0dc50d3b
5 changed files with 101 additions and 16 deletions

View File

@ -22,6 +22,7 @@
},
"dependencies": {
"@pushrocks/qenv": "^4.0.10",
"@pushrocks/smartpromise": "^3.1.3",
"@pushrocks/smartrequest": "^1.1.51"
},
"browserslist": [

View File

@ -12,7 +12,17 @@ tap.test('first test', async () => {
});
tap.test('should get me info', async () => {
const result = await testMediumAccount.getUserInfo();
const result = await testMediumAccount.getAccountInfo();
// console.log(result);
});
tap.test('should get publications', async () => {
const result = await testMediumAccount.getPublications();
// console.log(result);
});
tap.test('should get own publications', async () => {
const result = await testMediumAccount.getOwnPublications();
// console.log(result);
});

View File

@ -1,33 +1,52 @@
import { MediumPublication } from './medium.classes.publication';
import * as plugins from './medium.plugins';
export interface IMediumAccountData {
id: string;
username: string;
url: string;
imageUrl: string;
}
/**
*
*/
export class MediumAccount {
public baseApiDomain = 'https://api.medium.com/v1';
export class MediumAccount implements IMediumAccountData {
// STATIC
// INSTANCE
private accessToken: string;
public readyDeferred = plugins.smartpromise.defer();
public baseApiDomain = 'https://api.medium.com/v1';
id: string;
username: string;
url: string;
imageUrl: string;
constructor(accessTokenArg: string) {
this.accessToken = accessTokenArg;
this.getAccountInfo().then((dataArg) => {
Object.assign(this, dataArg);
this.readyDeferred.resolve();
});
}
public async getUserInfo(): Promise<{
id: string,
username: string,
name: string,
url: string,
imageUrl: string
}> {
public async getAccountInfo(): Promise<IMediumAccountData> {
const result = await this.request('/me', 'GET');
return result.body.data;
const accountData = result.body.data;
return accountData;
}
public async getPublications(): Promise<MediumPublication[]> {
return MediumPublication.getPublications(this);
return MediumPublication.getAllPublications(this);
}
public async request(routeArg: string, methodArg: string, payloadArg?: any) {
public async getOwnPublications(): Promise<MediumPublication[]> {
return MediumPublication.getOwnPublications(this);
}
public async request(routeArg: string, methodArg: 'POST' | 'GET', payloadArg?: any) {
const response = await plugins.smartrequest.request(`${this.baseApiDomain}${routeArg}`, {
headers: {
Authorization: `Bearer ${this.accessToken}`,

View File

@ -1,9 +1,62 @@
import { MediumAccount } from './medium.classes.mediumaccount';
import * as plugins from './medium.plugins';
export class MediumPublication {
public static getPublications(mediumAccount: MediumAccount) {
export interface IMediumPublication {
id: string;
name: string;
description: string;
url: string;
imageUrl: string;
}
export class MediumPublication implements IMediumPublication {
// STATIC
public static async getAllPublications(mediumAccount: MediumAccount) {
await mediumAccount.readyDeferred.promise;
const returnArray: MediumPublication[] = [];
const response = await mediumAccount.request(`/users/${mediumAccount.id}/publications`, 'GET');
const publicationsDataArray: IMediumPublication[] = response.body.data;
for (const publicationData of publicationsDataArray) {
const publication = new MediumPublication(mediumAccount, publicationData);
returnArray.push(publication);
}
return returnArray;
}
}
public static async getOwnPublications(mediumAccount: MediumAccount) {
await mediumAccount.readyDeferred.promise;
const allPublications = await this.getAllPublications(mediumAccount);
const ownPublications: MediumPublication[] = [];
for (const publicationArg of allPublications) {
const response = await mediumAccount.request(
`/publications/${publicationArg.id}/contributors`,
'GET'
);
const contributors: {
publicationId: string;
userId: string;
role: string;
}[] = response.body.data;
for (const contributor of contributors) {
if (contributor.userId === mediumAccount.id) {
ownPublications.push(publicationArg);
break;
}
}
}
return ownPublications;
}
// INSTANCE
public mediumAccountRef: MediumAccount;
id: string;
name: string;
description: string;
url: string;
imageUrl: string;
constructor(mediumAccount: MediumAccount, dataArg: IMediumPublication) {
Object.assign(this, dataArg);
}
}

View File

@ -1,5 +1,7 @@
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest';
export {
smartpromise,
smartrequest
};