fix(medium): Fix various bugs and improve async handling.
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@mojoio/medium',
|
||||
version: '1.0.5',
|
||||
version: '1.0.6',
|
||||
description: 'an unofficial medium.com API package'
|
||||
}
|
||||
|
@ -9,11 +9,9 @@ export interface IMediumAccountData {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Represents a Medium account with various functionalities to interact with Medium's API.
|
||||
*/
|
||||
export class MediumAccount implements IMediumAccountData {
|
||||
// STATIC
|
||||
|
||||
// INSTANCE
|
||||
private accessToken: string;
|
||||
public readyDeferred = plugins.smartpromise.defer();
|
||||
@ -24,43 +22,104 @@ export class MediumAccount implements IMediumAccountData {
|
||||
url: string;
|
||||
imageUrl: string;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the MediumAccount class.
|
||||
* @param accessTokenArg - The access token for the Medium API.
|
||||
*/
|
||||
constructor(accessTokenArg: string) {
|
||||
this.accessToken = accessTokenArg;
|
||||
this.getAccountInfo().then((dataArg) => {
|
||||
Object.assign(this, dataArg);
|
||||
this.readyDeferred.resolve();
|
||||
if (dataArg) {
|
||||
Object.assign(this, dataArg);
|
||||
this.readyDeferred.resolve();
|
||||
} else {
|
||||
this.readyDeferred.reject('Failed to fetch account info.');
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error('Error fetching account info:', error);
|
||||
this.readyDeferred.reject(error);
|
||||
});
|
||||
}
|
||||
|
||||
public async getAccountInfo(): Promise<IMediumAccountData> {
|
||||
const result = await this.request('/me', 'GET');
|
||||
const accountData = result.body.data;
|
||||
return accountData;
|
||||
/**
|
||||
* Fetches the account information from Medium.
|
||||
* @returns A promise that resolves to the account data.
|
||||
*/
|
||||
public async getAccountInfo(): Promise<IMediumAccountData | undefined> {
|
||||
try {
|
||||
const result = await this.request('/me', 'GET');
|
||||
console.log(result.statusCode);
|
||||
const accountData: IMediumAccountData = result.body.data;
|
||||
return accountData;
|
||||
} catch (error) {
|
||||
console.error('Error in getAccountInfo:', error);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all publications associated with this account.
|
||||
* @returns A promise that resolves to an array of MediumPublication objects.
|
||||
*/
|
||||
public async getAllPublications(): Promise<MediumPublication[]> {
|
||||
return MediumPublication.getAllPublications(this);
|
||||
const result = await this.request(`/users/${this.id}/publications`, 'GET');
|
||||
return result.data.map((pub: any) => new MediumPublication(this, pub));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all publications authored by this account.
|
||||
* @returns A promise that resolves to an array of MediumPublication objects.
|
||||
*/
|
||||
public async getOwnPublications(): Promise<MediumPublication[]> {
|
||||
return MediumPublication.getOwnPublications(this);
|
||||
const allPublications = await this.getAllPublications();
|
||||
const ownPublications: MediumPublication[] = [];
|
||||
|
||||
for (const publication of allPublications) {
|
||||
const response = await this.request(`/publications/${publication.id}/contributors`, 'GET');
|
||||
const contributors: { publicationId: string; userId: string; role: string; }[] = response.data;
|
||||
|
||||
if (contributors.some(contributor => contributor.userId === this.id)) {
|
||||
ownPublications.push(publication);
|
||||
}
|
||||
}
|
||||
|
||||
return ownPublications;
|
||||
}
|
||||
|
||||
public async getPublicationByName(nameArg: string): Promise<MediumPublication> {
|
||||
return MediumPublication.getPublicationByName(this, nameArg);
|
||||
/**
|
||||
* Fetches a publication by its name.
|
||||
* @param nameArg - The name of the publication.
|
||||
* @returns A promise that resolves to the MediumPublication object.
|
||||
*/
|
||||
public async getPublicationByName(nameArg: string): Promise<MediumPublication | undefined> {
|
||||
const publications = await this.getAllPublications();
|
||||
return publications.find(publication => publication.name === nameArg);
|
||||
}
|
||||
|
||||
public async request(routeArg: string, methodArg: 'POST' | 'GET', payloadArg?: any) {
|
||||
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;
|
||||
/**
|
||||
* Makes an authenticated request to the Medium API.
|
||||
* @param routeArg - The API route to request.
|
||||
* @param methodArg - The HTTP method to use for the request.
|
||||
* @param payloadArg - Optional payload for POST requests.
|
||||
* @returns A promise that resolves to the API response.
|
||||
*/
|
||||
public async request(routeArg: string, methodArg: 'POST' | 'GET', payloadArg?: any): Promise<any> {
|
||||
try {
|
||||
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,
|
||||
keepAlive: false,
|
||||
requestBody: payloadArg ? JSON.stringify(payloadArg) : null
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Error in request:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -12,9 +12,9 @@ export interface IPostData {
|
||||
|
||||
export class MediumPost implements IPostData {
|
||||
// STATIC
|
||||
public static async createPost(mediumPublicationArg: MediumPublication, dataArg: IPostData) {
|
||||
const response = await mediumPublicationArg.mediumAccountRef.request(`/publications/${mediumPublicationArg.id}/posts`, 'POST', dataArg);
|
||||
const post = new MediumPost(mediumPublicationArg, response.body.data);
|
||||
public static async createPost(mediumPublication: MediumPublication, data: IPostData): Promise<MediumPost> {
|
||||
const response = await mediumPublication.mediumAccountRef.request(`/publications/${mediumPublication.id}/posts`, 'POST', data);
|
||||
const post = new MediumPost(mediumPublication, response.data);
|
||||
return post;
|
||||
}
|
||||
|
||||
@ -28,7 +28,8 @@ export class MediumPost implements IPostData {
|
||||
tags: string[];
|
||||
publishStatus: 'public' | 'draft' | 'unlisted';
|
||||
|
||||
constructor(mediumPublication: MediumPublication, dataArg: IPostData) {
|
||||
|
||||
constructor(mediumPublication: MediumPublication, data: IPostData) {
|
||||
this.mediumPublicationRef = mediumPublication;
|
||||
Object.assign(this, data);
|
||||
}
|
||||
}
|
||||
|
@ -12,45 +12,33 @@ export interface IMediumPublication {
|
||||
|
||||
export class MediumPublication implements IMediumPublication {
|
||||
// STATIC
|
||||
public static async getAllPublications(mediumAccount: MediumAccount) {
|
||||
public static async getAllPublications(mediumAccount: MediumAccount): Promise<MediumPublication[]> {
|
||||
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;
|
||||
const publicationsDataArray: IMediumPublication[] = response.data;
|
||||
return publicationsDataArray.map(publicationData => new MediumPublication(mediumAccount, publicationData));
|
||||
}
|
||||
|
||||
public static async getOwnPublications(mediumAccount: MediumAccount) {
|
||||
public static async getOwnPublications(mediumAccount: MediumAccount): Promise<MediumPublication[]> {
|
||||
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;
|
||||
}
|
||||
|
||||
for (const publication of allPublications) {
|
||||
const response = await mediumAccount.request(`/publications/${publication.id}/contributors`, 'GET');
|
||||
const contributors: { publicationId: string; userId: string; role: string; }[] = response.data;
|
||||
|
||||
if (contributors.some(contributor => contributor.userId === mediumAccount.id)) {
|
||||
ownPublications.push(publication);
|
||||
}
|
||||
}
|
||||
|
||||
return ownPublications;
|
||||
}
|
||||
|
||||
public static async getPublicationByName(mediumAccountArg: MediumAccount, publicationNameArg: string) {
|
||||
const publications = await this.getAllPublications(mediumAccountArg);
|
||||
return publications.find(publicationArg => publicationArg.name === publicationNameArg);
|
||||
public static async getPublicationByName(mediumAccount: MediumAccount, publicationName: string): Promise<MediumPublication | undefined> {
|
||||
const publications = await this.getAllPublications(mediumAccount);
|
||||
return publications.find(publication => publication.name === publicationName);
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
@ -62,13 +50,13 @@ export class MediumPublication implements IMediumPublication {
|
||||
url: string;
|
||||
imageUrl: string;
|
||||
|
||||
constructor(mediumAccount: MediumAccount, dataArg: IMediumPublication) {
|
||||
constructor(mediumAccount: MediumAccount, data: IMediumPublication) {
|
||||
this.mediumAccountRef = mediumAccount;
|
||||
Object.assign(this, dataArg);
|
||||
Object.assign(this, data);
|
||||
}
|
||||
|
||||
public async createPost(dataArg: IPostData): Promise<MediumPost> {
|
||||
const result = await MediumPost.createPost(this, dataArg);
|
||||
public async createPost(data: IPostData): Promise<MediumPost> {
|
||||
const result = await MediumPost.createPost(this, data);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user