fix(medium): Fix various bugs and improve async handling.
This commit is contained in:
parent
93e8ffcc95
commit
6b557ba71c
@ -1,5 +1,13 @@
|
||||
# Changelog
|
||||
|
||||
## 2024-07-01 - 1.0.6 - fix(medium)
|
||||
Fix various bugs and improve async handling.
|
||||
|
||||
- Update @push.rocks/qenv dependency to ^6.0.5
|
||||
- Fix bug in test: await getEnvVarOnDemand
|
||||
- Improve error handling in getAccountInfo
|
||||
- Simplify fetching publications by refactoring methods in MediumPublication and MediumAccount classes
|
||||
|
||||
## 2024-07-01 - 1.0.5 - fix(core)
|
||||
Fixed module name inconsistencies and documentation links
|
||||
|
||||
|
@ -21,7 +21,7 @@
|
||||
"@types/node": "^20.14.9"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/qenv": "^5.0.2",
|
||||
"@push.rocks/qenv": "^6.0.5",
|
||||
"@push.rocks/smartpromise": "^4.0.2",
|
||||
"@push.rocks/smartrequest": "^2.0.15"
|
||||
},
|
||||
|
110
pnpm-lock.yaml
generated
110
pnpm-lock.yaml
generated
@ -9,8 +9,8 @@ importers:
|
||||
.:
|
||||
dependencies:
|
||||
'@push.rocks/qenv':
|
||||
specifier: ^5.0.2
|
||||
version: 5.0.5
|
||||
specifier: ^6.0.5
|
||||
version: 6.0.5
|
||||
'@push.rocks/smartpromise':
|
||||
specifier: ^4.0.2
|
||||
version: 4.0.4
|
||||
@ -334,8 +334,8 @@ packages:
|
||||
'@push.rocks/lik@6.0.15':
|
||||
resolution: {integrity: sha512-rZxln6l4NAU931MTxnsjy1pue+S3AXtDCidHH/tbkqBtrWIzWuXduo6Nz3zYkndbD64Knyta7F60JRvcOe4XqA==}
|
||||
|
||||
'@push.rocks/qenv@5.0.5':
|
||||
resolution: {integrity: sha512-AUmhbXmi3cHUfpXf7Bb7FmPMZ9d+/mRL1brjR4juGIsOubbpTv52Vv+1VlcRlsuyN7sWNvDEpP/Y8SfoOZzYzg==}
|
||||
'@push.rocks/qenv@6.0.5':
|
||||
resolution: {integrity: sha512-Id/eSKKqSDUGe+0Cp5HEJ58J1iVv1jQseLUMs9kFTPYwG+NJSETUCRsJV50w5cPv8bRFcSkSU+xVbUbOc1p29A==}
|
||||
|
||||
'@push.rocks/smartbrowser@2.0.6':
|
||||
resolution: {integrity: sha512-Ne+KCVhV/DROc1rHRRw59K6h0+LpQAK9fdOUtgDZ7laLPmB/tmnbUh3IuRDNcIY1iVA9pydoobwjnTjVgio9eQ==}
|
||||
@ -370,9 +370,6 @@ packages:
|
||||
'@push.rocks/smartfile-interfaces@1.0.7':
|
||||
resolution: {integrity: sha512-MeOl/200UOvSO4Pgq/DVFiBVZpL9gjOBQM+4XYNjSxda8c6VBvchHAntaFLQUlO8U1ckNaP9i+nMO4O4/0ymyw==}
|
||||
|
||||
'@push.rocks/smartfile@10.0.41':
|
||||
resolution: {integrity: sha512-xOOy0duI34M2qrJZggpk51EHGXmg9+mBL1Q55tNiQKXzfx89P3coY1EAZG8tvmep3qB712QEKe7T+u04t42Kjg==}
|
||||
|
||||
'@push.rocks/smartfile@11.0.21':
|
||||
resolution: {integrity: sha512-ZtF8QPGeR/2MJxtHgJY1+FLrOZ+GTLWqwyDz7Woq1x+K9nb4almA1OpdW5IQPBK3ECHur0ghg5bzzKMuR5ZSzg==}
|
||||
|
||||
@ -406,9 +403,6 @@ packages:
|
||||
'@push.rocks/smartmatch@2.0.0':
|
||||
resolution: {integrity: sha512-MBzP++1yNIBeox71X6VxpIgZ8m4bXnJpZJ4nWVH6IWpmO38MXTu4X0QF8tQnyT4LFcwvc9iiWaD15cstHa7Mmw==}
|
||||
|
||||
'@push.rocks/smartmime@1.0.6':
|
||||
resolution: {integrity: sha512-PHd+I4UcsnOATNg8wjDsSAmmJ4CwQFrQCNzd0HSJMs4ZpiK3Ya91almd6GLpDPU370U4HFh4FaPF4eEAI6vkJQ==}
|
||||
|
||||
'@push.rocks/smartmime@2.0.2':
|
||||
resolution: {integrity: sha512-aXH1sFD73q9cEwPdeSeN7Zxd2aoVt9wE97ILFCW5gORylvm85Hgfq7SYkqykjQzEL8IDJKJF3G78+xcL2rALTg==}
|
||||
|
||||
@ -460,9 +454,6 @@ packages:
|
||||
'@push.rocks/smartstate@2.0.17':
|
||||
resolution: {integrity: sha512-ucx0zq8gWL3MuN4ogibUiGJlEH0of9WyDfxu+4pQySEdaZBJw0/UFkBM8wButkj0dF8w4wsVMN4YwZAdVqsAXg==}
|
||||
|
||||
'@push.rocks/smartstream@2.0.8':
|
||||
resolution: {integrity: sha512-GlF/9cCkvBHwKa3DK4DO5wjfSgqkj6gAS4TrY9uD5NMHu9RQv4WiNrElTYj7iCEpnZgUnLO3tzw1JA3NRIMnnA==}
|
||||
|
||||
'@push.rocks/smartstream@3.0.44':
|
||||
resolution: {integrity: sha512-BZwSUmhVD/CEzRdG/e2UachlyNjshiknqBnwHzIFPIjVKC66UgWX5lYCs2LOwJtuUEpfL1vTMOAYUFNcmPjW9A==}
|
||||
|
||||
@ -685,9 +676,6 @@ packages:
|
||||
'@types/express@4.17.21':
|
||||
resolution: {integrity: sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==}
|
||||
|
||||
'@types/from2@2.3.5':
|
||||
resolution: {integrity: sha512-giavnjf3kNlJnE+HpZA1CQ3UKHxgehzsTuIMGda8pGMbOiLYGVNADEskey44OZcADHm/HOoBany8IV+0x28XFw==}
|
||||
|
||||
'@types/fs-extra@11.0.4':
|
||||
resolution: {integrity: sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ==}
|
||||
|
||||
@ -1125,9 +1113,6 @@ packages:
|
||||
resolution: {integrity: sha512-TG2hpqe4ELx54QER/S3HQ9SRVnQnGBtKUz5bLQWtYAQ+o6GpgMs6sYUvaiJjVxb+UXwhRhAEP3m7LbsIZ77Hmw==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
core-util-is@1.0.3:
|
||||
resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==}
|
||||
|
||||
cors@2.8.5:
|
||||
resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==}
|
||||
engines: {node: '>= 0.10'}
|
||||
@ -1474,9 +1459,6 @@ packages:
|
||||
resolution: {integrity: sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=}
|
||||
engines: {node: '>= 0.6'}
|
||||
|
||||
from2@2.3.0:
|
||||
resolution: {integrity: sha1-i/tVAr3kpNNs/e6gB/zKIdfjgq8=}
|
||||
|
||||
fs-constants@1.0.0:
|
||||
resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==}
|
||||
|
||||
@ -1757,9 +1739,6 @@ packages:
|
||||
resolution: {integrity: sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==}
|
||||
engines: {node: '>=8'}
|
||||
|
||||
isarray@1.0.0:
|
||||
resolution: {integrity: sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=}
|
||||
|
||||
isbinaryfile@5.0.2:
|
||||
resolution: {integrity: sha512-GvcjojwonMjWbTkfMpnVHVqXW/wKMYDfEpY94/8zy8HFMOqb/VL6oeONq9v87q4ttVlaTLnGXnJD4B5B1OTGIg==}
|
||||
engines: {node: '>= 18.0.0'}
|
||||
@ -2378,9 +2357,6 @@ packages:
|
||||
resolution: {integrity: sha512-ASJqOugUF1bbzI35STMBUpZqdfYKlJugy6JBziGi2EE+AL5JPJGSzvpeVXojxrr0ViUYoToUjb5kjSEGf7Y83Q==}
|
||||
engines: {node: '>=14.16'}
|
||||
|
||||
process-nextick-args@2.0.1:
|
||||
resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==}
|
||||
|
||||
progress@2.0.3:
|
||||
resolution: {integrity: sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==}
|
||||
engines: {node: '>=0.4.0'}
|
||||
@ -2443,9 +2419,6 @@ packages:
|
||||
resolution: {integrity: sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==}
|
||||
engines: {node: '>= 0.8'}
|
||||
|
||||
readable-stream@2.3.8:
|
||||
resolution: {integrity: sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==}
|
||||
|
||||
readable-stream@3.6.2:
|
||||
resolution: {integrity: sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==}
|
||||
engines: {node: '>= 6'}
|
||||
@ -2519,9 +2492,6 @@ packages:
|
||||
rxjs@7.8.1:
|
||||
resolution: {integrity: sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg==}
|
||||
|
||||
safe-buffer@5.1.2:
|
||||
resolution: {integrity: sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==}
|
||||
|
||||
safe-buffer@5.2.1:
|
||||
resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==}
|
||||
|
||||
@ -2634,9 +2604,6 @@ packages:
|
||||
resolution: {integrity: sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
string_decoder@1.1.1:
|
||||
resolution: {integrity: sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==}
|
||||
|
||||
string_decoder@1.3.0:
|
||||
resolution: {integrity: sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==}
|
||||
|
||||
@ -3432,10 +3399,11 @@ snapshots:
|
||||
'@types/symbol-tree': 3.2.5
|
||||
symbol-tree: 3.2.4
|
||||
|
||||
'@push.rocks/qenv@5.0.5':
|
||||
'@push.rocks/qenv@6.0.5':
|
||||
dependencies:
|
||||
'@api.global/typedrequest': 3.0.30
|
||||
'@configvault.io/interfaces': 1.0.17
|
||||
'@push.rocks/smartfile': 10.0.41
|
||||
'@push.rocks/smartfile': 11.0.21
|
||||
'@push.rocks/smartlog': 3.0.7
|
||||
'@push.rocks/smartpath': 5.0.18
|
||||
|
||||
@ -3508,25 +3476,6 @@ snapshots:
|
||||
|
||||
'@push.rocks/smartfile-interfaces@1.0.7': {}
|
||||
|
||||
'@push.rocks/smartfile@10.0.41':
|
||||
dependencies:
|
||||
'@push.rocks/lik': 6.0.15
|
||||
'@push.rocks/smartdelay': 3.0.5
|
||||
'@push.rocks/smartfile-interfaces': 1.0.7
|
||||
'@push.rocks/smarthash': 3.0.4
|
||||
'@push.rocks/smartjson': 5.0.20
|
||||
'@push.rocks/smartmime': 1.0.6
|
||||
'@push.rocks/smartpath': 5.0.18
|
||||
'@push.rocks/smartpromise': 4.0.4
|
||||
'@push.rocks/smartrequest': 2.0.22
|
||||
'@push.rocks/smartstream': 2.0.8
|
||||
'@types/fs-extra': 11.0.4
|
||||
'@types/glob': 8.1.0
|
||||
'@types/js-yaml': 4.0.9
|
||||
fs-extra: 11.2.0
|
||||
glob: 10.4.2
|
||||
js-yaml: 4.1.0
|
||||
|
||||
'@push.rocks/smartfile@11.0.21':
|
||||
dependencies:
|
||||
'@push.rocks/lik': 6.0.15
|
||||
@ -3606,11 +3555,6 @@ snapshots:
|
||||
dependencies:
|
||||
matcher: 5.0.0
|
||||
|
||||
'@push.rocks/smartmime@1.0.6':
|
||||
dependencies:
|
||||
'@types/mime-types': 2.1.4
|
||||
mime-types: 2.1.35
|
||||
|
||||
'@push.rocks/smartmime@2.0.2':
|
||||
dependencies:
|
||||
'@types/mime-types': 2.1.4
|
||||
@ -3759,15 +3703,6 @@ snapshots:
|
||||
'@push.rocks/smartrx': 3.0.7
|
||||
'@push.rocks/webstore': 2.0.20
|
||||
|
||||
'@push.rocks/smartstream@2.0.8':
|
||||
dependencies:
|
||||
'@push.rocks/smartpromise': 4.0.4
|
||||
'@push.rocks/smartrx': 3.0.7
|
||||
'@types/from2': 2.3.5
|
||||
'@types/through2': 2.0.41
|
||||
from2: 2.3.0
|
||||
through2: 4.0.2
|
||||
|
||||
'@push.rocks/smartstream@3.0.44':
|
||||
dependencies:
|
||||
'@push.rocks/lik': 6.0.15
|
||||
@ -4110,10 +4045,6 @@ snapshots:
|
||||
'@types/qs': 6.9.15
|
||||
'@types/serve-static': 1.15.7
|
||||
|
||||
'@types/from2@2.3.5':
|
||||
dependencies:
|
||||
'@types/node': 20.14.9
|
||||
|
||||
'@types/fs-extra@11.0.4':
|
||||
dependencies:
|
||||
'@types/jsonfile': 6.1.4
|
||||
@ -4615,8 +4546,6 @@ snapshots:
|
||||
depd: 2.0.0
|
||||
keygrip: 1.1.0
|
||||
|
||||
core-util-is@1.0.3: {}
|
||||
|
||||
cors@2.8.5:
|
||||
dependencies:
|
||||
object-assign: 4.1.1
|
||||
@ -4999,11 +4928,6 @@ snapshots:
|
||||
|
||||
fresh@0.5.2: {}
|
||||
|
||||
from2@2.3.0:
|
||||
dependencies:
|
||||
inherits: 2.0.4
|
||||
readable-stream: 2.3.8
|
||||
|
||||
fs-constants@1.0.0: {}
|
||||
|
||||
fs-extra@11.2.0:
|
||||
@ -5350,8 +5274,6 @@ snapshots:
|
||||
dependencies:
|
||||
is-docker: 2.2.1
|
||||
|
||||
isarray@1.0.0: {}
|
||||
|
||||
isbinaryfile@5.0.2: {}
|
||||
|
||||
isexe@2.0.0: {}
|
||||
@ -6123,8 +6045,6 @@ snapshots:
|
||||
dependencies:
|
||||
parse-ms: 3.0.0
|
||||
|
||||
process-nextick-args@2.0.1: {}
|
||||
|
||||
progress@2.0.3: {}
|
||||
|
||||
property-information@6.5.0: {}
|
||||
@ -6201,16 +6121,6 @@ snapshots:
|
||||
iconv-lite: 0.4.24
|
||||
unpipe: 1.0.0
|
||||
|
||||
readable-stream@2.3.8:
|
||||
dependencies:
|
||||
core-util-is: 1.0.3
|
||||
inherits: 2.0.4
|
||||
isarray: 1.0.0
|
||||
process-nextick-args: 2.0.1
|
||||
safe-buffer: 5.1.2
|
||||
string_decoder: 1.1.1
|
||||
util-deprecate: 1.0.2
|
||||
|
||||
readable-stream@3.6.2:
|
||||
dependencies:
|
||||
inherits: 2.0.4
|
||||
@ -6315,8 +6225,6 @@ snapshots:
|
||||
dependencies:
|
||||
tslib: 2.6.3
|
||||
|
||||
safe-buffer@5.1.2: {}
|
||||
|
||||
safe-buffer@5.2.1: {}
|
||||
|
||||
safer-buffer@2.1.2: {}
|
||||
@ -6466,10 +6374,6 @@ snapshots:
|
||||
emoji-regex: 9.2.2
|
||||
strip-ansi: 7.1.0
|
||||
|
||||
string_decoder@1.1.1:
|
||||
dependencies:
|
||||
safe-buffer: 5.1.2
|
||||
|
||||
string_decoder@1.3.0:
|
||||
dependencies:
|
||||
safe-buffer: 5.2.1
|
||||
|
@ -7,7 +7,7 @@ const testQenv = new Qenv('./', './.nogit/');
|
||||
let testMediumAccount: medium.MediumAccount;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testMediumAccount = new medium.MediumAccount(testQenv.getEnvVarOnDemand('MEDIUM_API_TOKEN'));
|
||||
testMediumAccount = new medium.MediumAccount(await testQenv.getEnvVarOnDemand('MEDIUM_API_TOKEN'));
|
||||
expect(testMediumAccount).toBeInstanceOf(medium.MediumAccount);
|
||||
});
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user