Skip to content

Commit

Permalink
Change typing script and improve JSDoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
md-y committed Sep 16, 2021
1 parent 880283f commit 5b00f7a
Show file tree
Hide file tree
Showing 16 changed files with 2,321 additions and 1,933 deletions.
418 changes: 414 additions & 4 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "mangadex-full-api",
"version": "5.5.1",
"version": "5.6.0",
"description": "A MangaDex api based around the official API.",
"main": "./src/index.js",
"types": "./types/index.d.ts",
"scripts": {
"test": "mocha",
"gen-types": "tsc && node ./types/compress-types.js"
"gen-types": "dts-bundle-generator --out-file=./types/index.d.ts --external-types=node --no-banner ./src/index.js"
},
"engines": {
"node": ">=10.12.0"
Expand All @@ -30,7 +30,8 @@
},
"homepage": "https://github.com/md-y/mangadex-full-api#readme",
"devDependencies": {
"mocha": "^8.4.0",
"typescript": "^4.1.3"
"@types/node": "^16.9.1",
"dts-bundle-generator": "^5.9.0",
"mocha": "^8.4.0"
}
}
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ Relationship.registerType('cover_art', Cover);

/**
* A shortcut for resolving all relationships in an array
* @param {Relationship[]} relationshipArray
* @returns {Promise}
* @template T
* @param {Relationship<T>[]} relationshipArray
* @returns {Promise<T[]>}
*/
function resolveArray(relationshipArray) {
function resolveArray(relationshipArray) {
return Relationship.resolveAll(relationshipArray);
}
exports.resolveArray = resolveArray;
3 changes: 3 additions & 0 deletions src/internal/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* https://api.mangadex.org/docs.html#section/Static-data/Manga-links-data
*/
class Links {
/**
* @param {Object.<string, string>} linksObject
*/
constructor(linksObject) {
if (!linksObject) {
this.availableLinks = [];
Expand Down
5 changes: 4 additions & 1 deletion src/internal/localizedstring.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
class LocalizedString {
/**
* Global locale setting
* @private
* @ignore
* @type {String}
*/
static locale = 'en';

/**
* @param {Object.<string, string>} stringObject
*/
constructor(stringObject) {
if (!stringObject) {
this.availableLocales = [];
Expand Down
15 changes: 9 additions & 6 deletions src/internal/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Represents a relationship from one Mangadex object to another such as a manga, author, etc via its id.
* @template ResolveType
*/
class Relationship {
static types = {};
Expand Down Expand Up @@ -30,7 +31,7 @@ class Relationship {
/**
* This function must be called to return the proper and complete object representation of this relationship.
* Essentially, it calls and returns Manga.get(), Author.get(), Cover.get(), etc.
* @returns {Promise<Manga|Author|Chapter|User|Group|List|Cover>}
* @returns {Promise<ResolveType>}
*/
resolve() {
if (this.id === undefined || this.type === undefined) throw new Error('Invalid Relationship object');
Expand All @@ -40,11 +41,11 @@ class Relationship {

/**
* Returns an array of converted objects from a Mangadex Relationships Array
* @private
* @ignore
* @param {String} type
* @param {Object[]} dataArray
* @param {Object} caller
* @returns {Object[]}
* @returns {Relationship<ResolveType>}
*/
static convertType(type, dataArray, caller) {
if (!(dataArray instanceof Array)) return [];
Expand All @@ -71,7 +72,7 @@ class Relationship {
/**
* Provides a constructor for a relationship type at run-time.
* Should only be called in index.js
* @private
* @ignore
* @param {String} name
* @param {Object} classObject
*/
Expand All @@ -83,8 +84,10 @@ class Relationship {

/**
* Resolves an array of relationships
* @private
* @param {Relationship[]} relationshipArray
* @ignore
* @template T
* @param {Relationship<T>[]} relationshipArray
* @returns {Promise<T[]>}
*/
static resolveAll(relationshipArray) {
if (relationshipArray.length === 0) return [];
Expand Down
4 changes: 2 additions & 2 deletions src/internal/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Tag {
}

/**
* @private
* @ignore
* @returns {Promise<Tag[]>}
*/
static async getAllTags() {
Expand All @@ -75,7 +75,7 @@ class Tag {
}

/**
* @private
* @ignore
* @param {String} indentity
* @returns {Promise<Tag>}
*/
Expand Down
12 changes: 6 additions & 6 deletions src/internal/uploadsession.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ class UploadSession {

/**
* Relationship of the target manga
* @type {Relationship}
* @type {Relationship<import('../index').Manga>}
*/
this.manga = Relationship.convertType('manga', res.data.relationships, this).pop();

/**
* Relationships to the groups attributed to this chapter
* @type {Relationship}
* @type {Relationship<import('../index').Group>}
*/
this.groups = Relationship.convertType('group', res.data.relationships, this);

/**
* Relationship to the uploader (the current user)
* @type {Relationship}
* @type {Relationship<import('../index').User>}
*/
this.uploader = Relationship.convertType('user', res.data.relationships, this).pop();

Expand Down Expand Up @@ -78,7 +78,7 @@ class UploadSession {
/**
* Requests MD to start an upload session
* @param {String|Manga} manga
* @param {...String|Group|Relationship} groups
* @param {...String|import('../index').Group|Relationship<import('../index').Group>} groups
* @returns {UploadSession}
*/
static async open(manga, ...groups) {
Expand Down Expand Up @@ -111,7 +111,7 @@ class UploadSession {
}

/**
* @private
* @ignore
* @typedef {Object} PageFileObject
* @property {Buffer} PageFileObject.data
* @property {'jpeg'|'png'|'gif'} [PageFileObject.type]
Expand Down Expand Up @@ -158,7 +158,7 @@ class UploadSession {
}

/**
* @private
* @ignore
* @typedef {Object} ChapterDraftObject
* @property {String} ChapterDraftObject.volume
* @property {String} ChapterDraftObject.chapter
Expand Down
7 changes: 3 additions & 4 deletions src/structure/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const Util = require('../util.js');
const Relationship = require('../internal/relationship.js');
const Manga = require('./manga.js');

/**
* Represents an author or artist
Expand Down Expand Up @@ -61,13 +60,13 @@ class Author {

/**
* Manga this author/artist has been attributed to
* @type {Relationship[]}
* @type {Relationship<import('../index').Manga>[]}
*/
this.manga = Relationship.convertType('manga', context.data.relationships, this);
}

/**
* @private
* @ignore
* @typedef {Object} AuthorParameterObject
* @property {String} [AuthorParameterObject.name]
* @property {String[]} [AuthorParameterObject.ids] Max of 100 per request
Expand All @@ -92,7 +91,7 @@ class Author {

/**
* Gets multiple authors
* @param {...String|Author|Relationship} ids
* @param {...String|Author|Relationship<Author>} ids
* @returns {Promise<Author[]>}
*/
static getMultiple(...ids) {
Expand Down
20 changes: 9 additions & 11 deletions src/structure/chapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const Util = require('../util.js');
const AuthUtil = require('../auth.js');
const Relationship = require('../internal/relationship.js');
const Group = require('./group.js');
const Manga = require('./manga.js');

/**
* Represents a chapter with readable pages
Expand Down Expand Up @@ -110,25 +108,25 @@ class Chapter {

/**
* The scanlation groups that are attributed to this chapter
* @type {Relationship[]}
* @type {Relationship<import('../index').Group>[]}
*/
this.groups = Relationship.convertType('scanlation_group', context.data.relationships, this);

/**
* The manga this chapter belongs to
* @type {Relationship}
* @type {Relationship<import('../index').Manga>}
*/
this.manga = Relationship.convertType('manga', context.data.relationships, this).pop();

/**
* The user who uploaded this chapter
* @type {Relationship}
* @type {Relationship<import('../index').User>}
*/
this.uploader = Relationship.convertType('user', context.data.relationships, this).pop();
}

/**
* @private
* @ignore
* @typedef {Object} ChapterParameterObject
* @property {String} [ChapterParameterObject.title]
* @property {String} [ChapterParameterObject.createdAtSince] DateTime string with following format: YYYY-MM-DDTHH:MM:SS
Expand All @@ -147,9 +145,9 @@ class Chapter {
* @property {String[]} [ChapterParameterObject.ids] Max of 100 per request
* @property {Number} [ChapterParameterObject.limit] Not limited by API limits (more than 100). Use Infinity for maximum results (use at your own risk)
* @property {Number} [ChapterParameterObject.offset]
* @property {String[]|Group[]} [ChapterParameterObject.groups]
* @property {String|User|Relationship} [ChapterParameterObject.uploader]
* @property {String|Manga|Relationship} [ChapterParameterObject.manga]
* @property {String[]|import('../index').Group[]} [ChapterParameterObject.groups]
* @property {String|import('../index').User|Relationship<import('../index').User>} [ChapterParameterObject.uploader]
* @property {String|import('../index').Manga|Relationship<import('../index').Manga>} [ChapterParameterObject.manga]
* @property {String[]} [ChapterParameterObject.volume]
* @property {String} [ChapterParameterObject.chapter]
*/
Expand All @@ -162,14 +160,14 @@ class Chapter {
* @returns {Promise<Chapter[]>}
*/
static search(searchParameters = {}, includeSubObjects = false) {
if (typeof searchParameters === 'string') searchParameters = { title: searchParameters };
if (typeof searchParameters === 'string') searchParameters = { title: searchParameters, groups };
if (includeSubObjects) searchParameters.includes = ['scanlation_group', 'manga', 'user'];
return Util.apiCastedRequest('/chapter', Chapter, searchParameters);
}

/**
* Gets multiple chapters
* @param {...String|Chapter|Relationship} ids
* @param {...String|Chapter|Relationship<Chapter>} ids
* @returns {Promise<Chapter[]>}
*/
static getMultiple(...ids) {
Expand Down
16 changes: 7 additions & 9 deletions src/structure/cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const Relationship = require('../internal/relationship.js');
const Util = require('../util.js');
const Manga = require('./manga.js');
const User = require('./user.js');

/**
* Represents the cover art of a manga volume
Expand Down Expand Up @@ -57,14 +55,14 @@ class Cover {

/**
* Manga this is a cover for
* @type {Relationship}
* @type {Relationship<import('../index').Manga>}
*/
this.manga = Relationship.convertType('manga', context.data.relationships, this).pop();
if (!this.manga) this.manga = null;

/**
* The user who uploaded this cover
* @type {Relationship}
* @type {Relationship<import('../index').User>}
*/
this.uploader = Relationship.convertType('user', context.data.relationships, this).pop();
if (!this.uploader) this.uploader = null;
Expand Down Expand Up @@ -99,13 +97,13 @@ class Cover {
}

/**
* @private
* @ignore
* @typedef {Object} CoverParameterObject
* @property {Number} [CoverParameterObject.limit] Not limited by API limits (more than 100). Use Infinity for maximum results (use at your own risk)
* @property {Number} [CoverParameterObject.offset]
* @property {String[]|Manga[]} [CoverParameterObject.manga] Manga ids (limited to 100 per request)
* @property {String[]|import('../index').Manga[]} [CoverParameterObject.manga] Manga ids (limited to 100 per request)
* @property {String[]|Cover[]} [CoverParameterObject.ids] Covers ids (limited to 100 per request)
* @property {String[]|User[]} [CoverParameterObject.uploaders] User ids (limited to 100 per request)
* @property {String[]|import('../index').User[]} [CoverParameterObject.uploaders] User ids (limited to 100 per request)
* @property {Object} [CoverParameterObject.order]
* @property {'asc'|'desc'} [CoverParameterObject.order.createdAt]
* @property {'asc'|'desc'} [CoverParameterObject.order.updatedAt]
Expand All @@ -126,7 +124,7 @@ class Cover {

/**
* Gets multiple covers
* @param {...String|Cover|Relationship} ids
* @param {...String|Cover|Relationship<Cover>} ids
* @returns {Promise<Cover[]>}
*/
static getMultiple(...ids) {
Expand All @@ -147,7 +145,7 @@ class Cover {

/**
* Get an array of manga's covers
* @param {...String|Manga|Relationship} manga
* @param {...String|import('../index').Manga|Relationship<import('../index').Manga>} manga
* @returns {Promise<Cover[]>}
*/
static getMangaCovers(...manga) {
Expand Down
9 changes: 4 additions & 5 deletions src/structure/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const Util = require('../util.js');
const AuthUtil = require('../auth.js');
const Relationship = require('../internal/relationship.js');
const User = require('./user.js');

/**
* Represents a scanlation group
Expand Down Expand Up @@ -98,19 +97,19 @@ class Group {

/**
* This group's leader
* @type {Relationship}
* @type {Relationship<import('../index').User>}
*/
this.leader = Relationship.convertType('leader', context.data.relationships, this).pop();

/**
* Array of this group's members
* @type {Relationship[]}
* @type {Relationship<import('../index').User>[]}
*/
this.members = Relationship.convertType('member', context.data.relationships, this);
}

/**
* @private
* @ignore
* @typedef {Object} GroupParameterObject
* @property {String} [GroupParameterObject.name]
* @property {String[]} [GroupParameterObject.ids] Max of 100 per request
Expand All @@ -133,7 +132,7 @@ class Group {

/**
* Gets multiple groups
* @param {...String|Group|Relationship} ids
* @param {...String|Group|Relationship<Group>} ids
* @returns {Promise<Group[]>}
*/
static getMultiple(...ids) {
Expand Down
Loading

0 comments on commit 5b00f7a

Please sign in to comment.