This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanga.ts
109 lines (79 loc) · 2.69 KB
/
manga.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
ChapterSource, IChapter,
IChapterResolver, IChapterResolverDependency, IImageResolver, IImageResolverDependency, IInfoResolverDependency,
ImageCollection,
IManga,
IMangaInfoResolver,
IMangaObject, IReadOnlyManga,
MangaInfo
} from "./interface";
export type MangaObjectDependencies = IImageResolverDependency & IChapterResolverDependency & IInfoResolverDependency;
export class MangaObject implements IMangaObject, IReadOnlyManga {
private _chapters: ChapterSource[];
private _info: Promise<MangaInfo>;
private _chapterResolver: IChapterResolver;
private _infoResolver: IMangaInfoResolver;
private _imageResolver: IImageResolver;
get name() {
return this._manga.name;
}
get status() {
return this._manga.status;
}
get mature() {
return this._manga.mature;
}
get image() {
return this._manga.image;
}
get src() {
return this._src;
}
manga(): IManga {
return {...this._manga};
}
constructor(dependencies: MangaObjectDependencies, private _src: string, private _manga: IManga) {
this._chapterResolver = dependencies.chapterResolver;
this._infoResolver = dependencies.infoResolver;
this._imageResolver = dependencies.imageResolver;
}
async chapters(): Promise<IChapter[]> {
const chapters = await this._chapterResolver.chapters(this._src);
this._chapters = chapters;
return chapters.map(x => ({
chap_number: x.chap_number,
volume: x.volume,
name: x.name,
language: x.language,
scanlator: x.scanlator,
dateAdded: x.dateAdded,
licensed: x.licensed
}));
}
async images(chapter: IChapter | string | number): Promise<ImageCollection> {
const chap_number = typeof chapter === "string"
? chapter
: typeof chapter === "number"
? chapter.toString()
: !!chapter.chap_number
? chapter.chap_number
: undefined;
if (!chap_number) {
throw new Error("Invalid chapter provider");
}
// resolve chapters if not resolved yet
if (!this._chapters) {
await this.chapters();
}
const chapters = this._chapters;
const chap = chapters.find(x => x.chap_number === chap_number);
if (!chap) {
throw new Error("Chapter not found");
}
const src = chap.src;
return this._imageResolver.images(src);
}
info(): Promise<MangaInfo> {
return this._info || (this._info = this._infoResolver.info(this._src));
}
}