narou 0.5.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/dist/index.browser.d.ts +30 -0
  2. package/dist/index.browser.d.ts.map +1 -0
  3. package/dist/index.browser.js +119 -0
  4. package/dist/index.browser.js.map +1 -0
  5. package/dist/index.common.d.ts +8 -0
  6. package/dist/index.common.d.ts.map +1 -0
  7. package/dist/index.common.js +17 -0
  8. package/dist/index.common.js.map +1 -0
  9. package/dist/index.d.ts +30 -31
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +120 -113
  12. package/dist/index.js.map +1 -1
  13. package/dist/narou-fetch.d.ts +13 -0
  14. package/dist/narou-fetch.d.ts.map +1 -0
  15. package/dist/narou-fetch.js +130 -0
  16. package/dist/narou-fetch.js.map +1 -0
  17. package/dist/narou-jsonp.d.ts +10 -0
  18. package/dist/narou-jsonp.d.ts.map +1 -0
  19. package/dist/narou-jsonp.js +102 -0
  20. package/dist/narou-jsonp.js.map +1 -0
  21. package/dist/narou-ranking-results.d.ts +8 -8
  22. package/dist/narou-ranking-results.js +2 -2
  23. package/dist/narou-search-results.d.ts +90 -89
  24. package/dist/narou-search-results.d.ts.map +1 -1
  25. package/dist/narou-search-results.js +51 -51
  26. package/dist/narou-search-results.js.map +1 -1
  27. package/dist/narou.d.ts +24 -25
  28. package/dist/narou.d.ts.map +1 -1
  29. package/dist/narou.js +104 -142
  30. package/dist/narou.js.map +1 -1
  31. package/dist/params.d.ts +230 -230
  32. package/dist/params.js +173 -173
  33. package/dist/ranking-history.d.ts +13 -13
  34. package/dist/ranking-history.js +13 -13
  35. package/dist/ranking-history.js.map +1 -1
  36. package/dist/ranking.d.ts +47 -45
  37. package/dist/ranking.d.ts.map +1 -1
  38. package/dist/ranking.js +167 -165
  39. package/dist/ranking.js.map +1 -1
  40. package/dist/search-builder-r18.d.ts +21 -21
  41. package/dist/search-builder-r18.d.ts.map +1 -1
  42. package/dist/search-builder-r18.js +64 -63
  43. package/dist/search-builder-r18.js.map +1 -1
  44. package/dist/search-builder.d.ts +228 -226
  45. package/dist/search-builder.d.ts.map +1 -1
  46. package/dist/search-builder.js +412 -412
  47. package/dist/search-builder.js.map +1 -1
  48. package/dist/util/jsonp.d.ts +14 -0
  49. package/dist/util/jsonp.d.ts.map +1 -0
  50. package/dist/util/jsonp.js +48 -0
  51. package/dist/util/jsonp.js.map +1 -0
  52. package/dist/util/unzipp.d.ts +3 -3
  53. package/dist/util/unzipp.js +56 -56
  54. package/dist/util/unzipp.js.map +1 -1
  55. package/dist/util/url.d.ts +10 -0
  56. package/dist/util/url.d.ts.map +1 -0
  57. package/dist/util/url.js +9 -0
  58. package/dist/util/url.js.map +1 -0
  59. package/dist.es2015/index.browser.js +50 -0
  60. package/dist.es2015/index.browser.js.map +1 -0
  61. package/dist.es2015/index.common.js +5 -0
  62. package/dist.es2015/index.common.js.map +1 -0
  63. package/dist.es2015/index.js +50 -49
  64. package/dist.es2015/index.js.map +1 -1
  65. package/dist.es2015/narou-fetch.js +44 -0
  66. package/dist.es2015/narou-fetch.js.map +1 -0
  67. package/dist.es2015/narou-jsonp.js +22 -0
  68. package/dist.es2015/narou-jsonp.js.map +1 -0
  69. package/dist.es2015/narou-ranking-results.js +1 -1
  70. package/dist.es2015/narou-search-results.js +46 -46
  71. package/dist.es2015/narou-search-results.js.map +1 -1
  72. package/dist.es2015/narou.js +23 -60
  73. package/dist.es2015/narou.js.map +1 -1
  74. package/dist.es2015/params.js +170 -170
  75. package/dist.es2015/ranking-history.js +9 -9
  76. package/dist.es2015/ranking.js +90 -90
  77. package/dist.es2015/ranking.js.map +1 -1
  78. package/dist.es2015/search-builder-r18.js +41 -42
  79. package/dist.es2015/search-builder-r18.js.map +1 -1
  80. package/dist.es2015/search-builder.js +383 -383
  81. package/dist.es2015/search-builder.js.map +1 -1
  82. package/dist.es2015/util/jsonp.js +43 -0
  83. package/dist.es2015/util/jsonp.js.map +1 -0
  84. package/dist.es2015/util/unzipp.js +7 -7
  85. package/dist.es2015/util/url.js +7 -0
  86. package/dist.es2015/util/url.js.map +1 -0
  87. package/dist.esm/index.browser.js +102 -0
  88. package/dist.esm/index.browser.js.map +1 -0
  89. package/dist.esm/index.common.js +5 -0
  90. package/dist.esm/index.common.js.map +1 -0
  91. package/dist.esm/index.js +102 -97
  92. package/dist.esm/index.js.map +1 -1
  93. package/dist.esm/narou-fetch.js +128 -0
  94. package/dist.esm/narou-fetch.js.map +1 -0
  95. package/dist.esm/narou-jsonp.js +100 -0
  96. package/dist.esm/narou-jsonp.js.map +1 -0
  97. package/dist.esm/narou-ranking-results.js +1 -1
  98. package/dist.esm/narou-search-results.js +49 -49
  99. package/dist.esm/narou-search-results.js.map +1 -1
  100. package/dist.esm/narou.js +102 -140
  101. package/dist.esm/narou.js.map +1 -1
  102. package/dist.esm/params.js +170 -170
  103. package/dist.esm/ranking-history.js +9 -9
  104. package/dist.esm/ranking.js +164 -162
  105. package/dist.esm/ranking.js.map +1 -1
  106. package/dist.esm/search-builder-r18.js +62 -61
  107. package/dist.esm/search-builder-r18.js.map +1 -1
  108. package/dist.esm/search-builder.js +410 -410
  109. package/dist.esm/search-builder.js.map +1 -1
  110. package/dist.esm/util/jsonp.js +44 -0
  111. package/dist.esm/util/jsonp.js.map +1 -0
  112. package/dist.esm/util/unzipp.js +52 -52
  113. package/dist.esm/util/url.js +7 -0
  114. package/dist.esm/util/url.js.map +1 -0
  115. package/package-lock.json +3328 -4289
  116. package/package.json +8 -9
  117. package/src/index.browser.ts +63 -0
  118. package/src/index.common.ts +17 -0
  119. package/src/index.ts +22 -23
  120. package/src/narou-fetch.ts +51 -0
  121. package/src/narou-jsonp.ts +29 -0
  122. package/src/narou-ranking-results.ts +9 -9
  123. package/src/narou-search-results.ts +11 -10
  124. package/src/narou.ts +21 -49
  125. package/src/params.ts +280 -280
  126. package/src/ranking.ts +111 -107
  127. package/src/search-builder-r18.ts +1 -2
  128. package/src/search-builder.ts +5 -3
  129. package/src/util/jsonp.ts +66 -0
  130. package/src/util/unzipp.ts +9 -9
  131. package/src/util/url.ts +7 -0
package/src/ranking.ts CHANGED
@@ -1,107 +1,111 @@
1
- import api from "./narou";
2
- import { NarouRankingResult, RankingResult } from "./narou-ranking-results";
3
- import { Fields } from "./index";
4
- import SearchBuilder from "./search-builder";
5
- import { addDays, format } from "date-fns";
6
- import { RankingParams } from "./params";
7
-
8
- export enum RankingType {
9
- Daily = "d",
10
- Weekly = "w",
11
- Monthly = "m",
12
- Quarterly = "q"
13
- }
14
-
15
- const dateFormat = "yyyyMMdd";
16
-
17
- /**
18
- * ランキングヘルパー
19
- * @class Ranking
20
- */
21
- export default class RankingBuilder {
22
- protected date$: Date;
23
- protected type$: RankingType;
24
-
25
- /**
26
- * constructor
27
- * @private
28
- */
29
- constructor(protected params: Partial<RankingParams> = {}) {
30
- /**
31
- * クエリパラメータ
32
- * @protected
33
- */
34
- this.date$ = addDays(Date.now(), -1);
35
- this.type$ = RankingType.Daily;
36
- }
37
-
38
- date(date: Date) {
39
- this.date$ = date;
40
- return this;
41
- }
42
-
43
- type(type: RankingType) {
44
- this.type$ = type;
45
- return this;
46
- }
47
-
48
- /**
49
- * gzip圧縮する。
50
- *
51
- * 転送量上限を減らすためにも推奨
52
- * @param {number} level gzip圧縮レベル(1~5)
53
- * @return {SearchBuilder} this
54
- */
55
- gzip(level: number) {
56
- this.set({ gzip: level });
57
- return this;
58
- }
59
-
60
- /**
61
- * クエリパラメータをセットする
62
- * @private
63
- * @return {SearchBuilder} this
64
- */
65
- protected set(obj: any) {
66
- Object.assign(this.params, obj);
67
- return this;
68
- }
69
-
70
- /**
71
- * なろう小説APIへのリクエストを実行する
72
- * @returns ランキング
73
- */
74
- execute(): Promise<NarouRankingResult[]> {
75
- const date = format(this.date$, dateFormat);
76
- this.set({ rtype: `${date}-${this.type$}` });
77
- return api.executeRanking(this.params as RankingParams);
78
- }
79
-
80
- async executeWithFields(
81
- fields: Fields | Fields[] = [],
82
- opt?: "weekly"
83
- ): Promise<RankingResult[]> {
84
- const ranking = await this.execute();
85
- const fields$: Fields[] = Array.isArray(fields)
86
- ? fields.length == 0
87
- ? []
88
- : [...fields, Fields.ncode]
89
- : [fields, Fields.ncode];
90
-
91
- const rankingNcodes = ranking.map(({ ncode }) => ncode);
92
- const builder = new SearchBuilder();
93
- builder.fields(fields$);
94
- if (opt) {
95
- builder.opt(opt);
96
- }
97
- builder.ncode(rankingNcodes);
98
- builder.limit(ranking.length);
99
- const result = await builder.execute();
100
-
101
- // TODO: 型的にはNull許容ではないが許容しているのでなんとかする(削除されている小説がある)
102
- return ranking.map<RankingResult>(r => ({
103
- ...r,
104
- ...(result.values.find(novel => novel.ncode == r.ncode) ?? ({} as any))
105
- }));
106
- }
107
- }
1
+ import { NarouRankingResult, RankingResult } from "./narou-ranking-results";
2
+ import { Fields } from "./index";
3
+ import SearchBuilder from "./search-builder";
4
+ import { addDays, format } from "date-fns";
5
+ import { RankingParams } from "./params";
6
+ import NarouNovel from "./narou";
7
+ import NarouNovelFetch from "./narou-fetch";
8
+
9
+ export enum RankingType {
10
+ Daily = "d",
11
+ Weekly = "w",
12
+ Monthly = "m",
13
+ Quarterly = "q",
14
+ }
15
+
16
+ const dateFormat = "yyyyMMdd";
17
+
18
+ /**
19
+ * ランキングヘルパー
20
+ * @class Ranking
21
+ */
22
+ export default class RankingBuilder {
23
+ protected date$: Date;
24
+ protected type$: RankingType;
25
+
26
+ /**
27
+ * constructor
28
+ * @private
29
+ */
30
+ constructor(
31
+ protected params: Partial<RankingParams> = {},
32
+ protected api: NarouNovel
33
+ ) {
34
+ /**
35
+ * クエリパラメータ
36
+ * @protected
37
+ */
38
+ this.date$ = addDays(Date.now(), -1);
39
+ this.type$ = RankingType.Daily;
40
+ }
41
+
42
+ date(date: Date) {
43
+ this.date$ = date;
44
+ return this;
45
+ }
46
+
47
+ type(type: RankingType) {
48
+ this.type$ = type;
49
+ return this;
50
+ }
51
+
52
+ /**
53
+ * gzip圧縮する。
54
+ *
55
+ * 転送量上限を減らすためにも推奨
56
+ * @param {number} level gzip圧縮レベル(1~5)
57
+ * @return {RankingBuilder} this
58
+ */
59
+ gzip(level: number) {
60
+ this.set({ gzip: level });
61
+ return this;
62
+ }
63
+
64
+ /**
65
+ * クエリパラメータをセットする
66
+ * @private
67
+ * @return {RankingBuilder} this
68
+ */
69
+ protected set(obj: any) {
70
+ Object.assign(this.params, obj);
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * なろう小説APIへのリクエストを実行する
76
+ * @returns ランキング
77
+ */
78
+ execute(): Promise<NarouRankingResult[]> {
79
+ const date = format(this.date$, dateFormat);
80
+ this.set({ rtype: `${date}-${this.type$}` });
81
+ return this.api.executeRanking(this.params as RankingParams);
82
+ }
83
+
84
+ async executeWithFields(
85
+ fields: Fields | Fields[] = [],
86
+ opt?: "weekly"
87
+ ): Promise<RankingResult[]> {
88
+ const ranking = await this.execute();
89
+ const fields$: Fields[] = Array.isArray(fields)
90
+ ? fields.length == 0
91
+ ? []
92
+ : [...fields, Fields.ncode]
93
+ : [fields, Fields.ncode];
94
+
95
+ const rankingNcodes = ranking.map(({ ncode }) => ncode);
96
+ const builder = new SearchBuilder({}, this.api);
97
+ builder.fields(fields$);
98
+ if (opt) {
99
+ builder.opt(opt);
100
+ }
101
+ builder.ncode(rankingNcodes);
102
+ builder.limit(ranking.length);
103
+ const result = await builder.execute();
104
+
105
+ // TODO: 型的にはNull許容ではないが許容しているのでなんとかする(削除されている小説がある)
106
+ return ranking.map<RankingResult>((r) => ({
107
+ ...r,
108
+ ...(result.values.find((novel) => novel.ncode == r.ncode) ?? ({} as any)),
109
+ }));
110
+ }
111
+ }
@@ -1,4 +1,3 @@
1
- import api from "./narou";
2
1
  import SearchBuilder from "./search-builder";
3
2
  import INarouSearchResults from "./narou-search-results";
4
3
  import { R18Site } from "./params";
@@ -14,7 +13,7 @@ export default class SearchBuilderR18 extends SearchBuilder {
14
13
  * @returns {Promise<INarouSearchResults>} 検索結果
15
14
  */
16
15
  execute(): Promise<INarouSearchResults> {
17
- return api.executeNovel18(this.params);
16
+ return this.api.executeNovel18(this.params);
18
17
  }
19
18
 
20
19
  r18Site(sites: R18Site | R18Site[]) {
@@ -1,4 +1,6 @@
1
+ import { NarouNovel } from ".";
1
2
  import api from "./narou";
3
+ import NarouNovelFetch from "./narou-fetch";
2
4
  import INarouSearchResults from "./narou-search-results";
3
5
  import { BigGenre, Genre } from "./params";
4
6
  import {
@@ -7,7 +9,7 @@ import {
7
9
  Order,
8
10
  Buntai,
9
11
  NovelType,
10
- GzipLevel
12
+ GzipLevel,
11
13
  } from "./params";
12
14
 
13
15
  /**
@@ -19,7 +21,7 @@ export default class SearchBuilder {
19
21
  * constructor
20
22
  * @private
21
23
  */
22
- constructor(protected params: SearchParams = {}) {}
24
+ constructor(protected params: SearchParams = {}, protected api: NarouNovel) {}
23
25
 
24
26
  /**
25
27
  * a
@@ -423,7 +425,7 @@ export default class SearchBuilder {
423
425
  * @returns {Promise<NarouSearchResults>} 検索結果
424
426
  */
425
427
  execute(): Promise<INarouSearchResults> {
426
- return api.executeNovel(this.params);
428
+ return this.api.executeNovel(this.params);
427
429
  }
428
430
  }
429
431
 
@@ -0,0 +1,66 @@
1
+ /**
2
+ * MIT license
3
+ */
4
+ import nodeURL from "./url";
5
+
6
+ // Callback index.
7
+ let count = 0;
8
+
9
+ type CallbackId<Prefix extends string = string> = `${Prefix}${number}`;
10
+
11
+ declare global {
12
+ interface Window {
13
+ [key: CallbackId]: (data: any) => void;
14
+ }
15
+ }
16
+ export type JsonpOption = {
17
+ prefix?: string;
18
+ param?: string;
19
+ timeout?: number;
20
+ };
21
+ const noop = function () {};
22
+
23
+ export function jsonp<T>(
24
+ url: string,
25
+ { prefix = "__jp", param = "callback", timeout = 15000 }: JsonpOption = {}
26
+ ): Promise<T> {
27
+ return new Promise(function (resolve, reject) {
28
+ const targetChild = document.getElementsByTagName("script").item(0);
29
+ const target = targetChild?.parentNode ?? document.head;
30
+
31
+ const id: CallbackId = `${prefix}${count++}`;
32
+ const cleanup = function () {
33
+ // Remove the script tag.
34
+ if (script && script.parentNode) {
35
+ script.parentNode.removeChild(script);
36
+ }
37
+
38
+ window[id] = noop;
39
+
40
+ if (timer) {
41
+ clearTimeout(timer);
42
+ }
43
+ };
44
+
45
+ const timer =
46
+ timeout > 0
47
+ ? setTimeout(() => {
48
+ cleanup();
49
+ reject(new Error("Timeout"));
50
+ }, timeout)
51
+ : undefined;
52
+
53
+ const callback = (data: T) => {
54
+ cleanup();
55
+ resolve(data);
56
+ };
57
+ window[id] = callback;
58
+
59
+ // Create script.
60
+ const script = document.createElement("script");
61
+ const urlObj = new nodeURL(url);
62
+ urlObj.searchParams.set(param, id);
63
+ script.setAttribute("src", urlObj.toString());
64
+ target.insertBefore(script, targetChild);
65
+ });
66
+ }
@@ -1,9 +1,9 @@
1
- import { gunzip, InputType } from "zlib";
2
- import { promisify } from "util";
3
-
4
- const gunzipAsync = promisify<InputType, Buffer>(gunzip);
5
-
6
- export async function unzipp(data: InputType) {
7
- const buffer = await gunzipAsync(data);
8
- return JSON.parse(buffer.toString());
9
- }
1
+ import { gunzip, InputType } from "zlib";
2
+ import { promisify } from "util";
3
+
4
+ const gunzipAsync = promisify<InputType, Buffer>(gunzip);
5
+
6
+ export async function unzipp(data: InputType) {
7
+ const buffer = await gunzipAsync(data);
8
+ return JSON.parse(buffer.toString());
9
+ }
@@ -0,0 +1,7 @@
1
+ import { URL as nodeURL } from "url";
2
+ let windowURL = null;
3
+ if (typeof window !== "undefined") {
4
+ windowURL = window.URL;
5
+ }
6
+
7
+ export default windowURL ?? nodeURL;