diff --git a/package.json b/package.json index 30afa7d..8c63071 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "dependencies": { "@pushrocks/qenv": "^4.0.10", + "@pushrocks/smartpromise": "^3.1.3", "@pushrocks/smartrequest": "^1.1.51" }, "browserslist": [ diff --git a/test/test.ts b/test/test.ts index 8c05e1e..c81b498 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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); }); diff --git a/ts/medium.classes.mediumaccount.ts b/ts/medium.classes.mediumaccount.ts index 9890db9..7fd71d5 100644 --- a/ts/medium.classes.mediumaccount.ts +++ b/ts/medium.classes.mediumaccount.ts @@ -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 { const result = await this.request('/me', 'GET'); - return result.body.data; + const accountData = result.body.data; + return accountData; } public async getPublications(): Promise { - return MediumPublication.getPublications(this); + return MediumPublication.getAllPublications(this); } - public async request(routeArg: string, methodArg: string, payloadArg?: any) { + public async getOwnPublications(): Promise { + 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}`, diff --git a/ts/medium.classes.publication.ts b/ts/medium.classes.publication.ts index 9831213..04937a0 100644 --- a/ts/medium.classes.publication.ts +++ b/ts/medium.classes.publication.ts @@ -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; } -} \ No newline at end of file + + 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); + } +} diff --git a/ts/medium.plugins.ts b/ts/medium.plugins.ts index 3dc2e1f..2ff59ec 100644 --- a/ts/medium.plugins.ts +++ b/ts/medium.plugins.ts @@ -1,5 +1,7 @@ +import * as smartpromise from '@pushrocks/smartpromise'; import * as smartrequest from '@pushrocks/smartrequest'; export { + smartpromise, smartrequest };