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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "narou",
3
- "version": "0.5.0",
3
+ "version": "0.6.2",
4
4
  "description": "Narou API Wrapper",
5
5
  "main": "dist/index.js",
6
+ "browser": "dist/index.browser.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "typings": "dist/index.d.ts",
8
9
  "module": "dist.esm/index.js",
@@ -29,22 +30,20 @@
29
30
  "@types/node": "^13.5.0",
30
31
  "@types/node-fetch": "^2.5.7",
31
32
  "chai": "^4.1.2",
32
- "husky": "^4.2.1",
33
- "lint-staged": "^10.0.2",
34
- "mocha": "^7.0.1",
33
+ "mocha": "^9.1.3",
35
34
  "mocha-typescript": "^1.1.12",
36
35
  "npm-run-all": "^4.1.4",
37
36
  "nyc": "^15.0.0",
38
- "prettier": "^1.15.2",
37
+ "prettier": "^2.4.1",
39
38
  "rimraf": "^3.0.0",
40
- "run-sequence": "^2.2.1",
41
39
  "touch": "^3.1.0",
42
- "ts-node": "^8.6.2",
40
+ "ts-node": "^10.3.0",
43
41
  "tslint": "^6.0.0",
44
42
  "tslint-config-prettier": "^1.16.0",
45
43
  "tslint-plugin-prettier": "^2.0.1",
46
- "typedoc": "^0.19.2",
47
- "typescript": "^4.1.3"
44
+ "typedoc": "^0.22.5",
45
+ "typescript": "^4.4.4",
46
+ "node-fetch": "^2.6.1"
48
47
  },
49
48
  "scripts": {
50
49
  "build": "npm-run-all check clean build:tsc",
@@ -0,0 +1,63 @@
1
+ import NarouNovelJsonp from "./narou-jsonp";
2
+ import NarouNovel from "./narou";
3
+ import SearchBuilder from "./search-builder";
4
+ import SearchBuilderR18 from "./search-builder-r18";
5
+ import RankingBuilder from "./ranking";
6
+ import { formatRankingHistory, RankingHistoryResult } from "./ranking-history";
7
+
8
+ export * from "./index.common";
9
+ export { NarouNovelJsonp };
10
+
11
+ const narouNovelJsonp = new NarouNovelJsonp();
12
+
13
+ /**
14
+ * 検索
15
+ * @param {string} [word] - 検索ワード
16
+ * @returns {SearchBuilder}
17
+ */
18
+ export function search(
19
+ word: string = "",
20
+ api: NarouNovel = narouNovelJsonp
21
+ ): SearchBuilder {
22
+ const builder = new SearchBuilder({}, api);
23
+ if (word != "") builder.word(word);
24
+ return builder;
25
+ }
26
+
27
+ /**
28
+ * 検索
29
+ * @param {string} [word] - 検索ワード
30
+ * @returns {SearchBuilder}
31
+ */
32
+ export function searchR18(
33
+ word: string = "",
34
+ api: NarouNovel = narouNovelJsonp
35
+ ): SearchBuilderR18 {
36
+ const builder = new SearchBuilderR18({}, api);
37
+ if (word != "") builder.word(word);
38
+ return builder;
39
+ }
40
+
41
+ export function ranking(api: NarouNovel = narouNovelJsonp): RankingBuilder {
42
+ const builder = new RankingBuilder({}, api);
43
+ return builder;
44
+ }
45
+
46
+ export async function rankingHistory(
47
+ ncode: string,
48
+ api: NarouNovel = narouNovelJsonp
49
+ ): Promise<RankingHistoryResult[]> {
50
+ const result = await api.executeRankingHistory({ ncode });
51
+ if (Array.isArray(result)) {
52
+ return result.map(formatRankingHistory);
53
+ } else {
54
+ throw new Error(result);
55
+ }
56
+ }
57
+
58
+ export default {
59
+ search,
60
+ ranking,
61
+ searchR18,
62
+ rankingHistory,
63
+ };
@@ -0,0 +1,17 @@
1
+ import NarouNovel from "./narou";
2
+ import { RankingHistoryResult } from "./ranking-history";
3
+
4
+ export { NarouNovel, RankingHistoryResult };
5
+ export { RankingType } from "./ranking";
6
+ export {
7
+ Fields,
8
+ Order,
9
+ BigGenre,
10
+ BigGenreNotation,
11
+ Genre,
12
+ GenreNotation,
13
+ R18Site,
14
+ R18SiteNotation,
15
+ } from "./params";
16
+ export { NarouSearchResult } from "./narou-search-results";
17
+ export { NarouRankingResult, RankingResult } from "./narou-ranking-results";
package/src/index.ts CHANGED
@@ -1,31 +1,26 @@
1
1
  import NarouNovel from "./narou";
2
+ import NarouNovelFetch from "./narou-fetch";
3
+ import NarouNovelJsonp from "./narou-jsonp";
2
4
  import RankingBuilder from "./ranking";
3
5
  import { formatRankingHistory, RankingHistoryResult } from "./ranking-history";
4
6
  import SearchBuilder from "./search-builder";
5
7
  import SearchBuilderR18 from "./search-builder-r18";
6
8
 
7
- export { NarouNovel, RankingHistoryResult };
8
- export { RankingType } from "./ranking";
9
- export {
10
- Fields,
11
- Order,
12
- BigGenre,
13
- BigGenreNotation,
14
- Genre,
15
- GenreNotation,
16
- R18Site,
17
- R18SiteNotation
18
- } from "./params";
19
- export { NarouSearchResult } from "./narou-search-results";
20
- export { NarouRankingResult, RankingResult } from "./narou-ranking-results";
9
+ export * from "./index.common";
10
+ export { NarouNovelFetch, NarouNovelJsonp };
11
+
12
+ const narouNovelFetch = new NarouNovelFetch();
21
13
 
22
14
  /**
23
15
  * 検索
24
16
  * @param {string} [word] - 検索ワード
25
17
  * @returns {SearchBuilder}
26
18
  */
27
- export function search(word: string = ""): SearchBuilder {
28
- var builder = new SearchBuilder();
19
+ export function search(
20
+ word: string = "",
21
+ api: NarouNovel = narouNovelFetch
22
+ ): SearchBuilder {
23
+ const builder = new SearchBuilder({}, api);
29
24
  if (word != "") builder.word(word);
30
25
  return builder;
31
26
  }
@@ -35,21 +30,25 @@ export function search(word: string = ""): SearchBuilder {
35
30
  * @param {string} [word] - 検索ワード
36
31
  * @returns {SearchBuilder}
37
32
  */
38
- export function searchR18(word: string = ""): SearchBuilderR18 {
39
- var builder = new SearchBuilderR18();
33
+ export function searchR18(
34
+ word: string = "",
35
+ api: NarouNovel = narouNovelFetch
36
+ ): SearchBuilderR18 {
37
+ const builder = new SearchBuilderR18({}, api);
40
38
  if (word != "") builder.word(word);
41
39
  return builder;
42
40
  }
43
41
 
44
- export function ranking(): RankingBuilder {
45
- const builder = new RankingBuilder();
42
+ export function ranking(api: NarouNovel = narouNovelFetch): RankingBuilder {
43
+ const builder = new RankingBuilder({}, api);
46
44
  return builder;
47
45
  }
48
46
 
49
47
  export async function rankingHistory(
50
- ncode: string
48
+ ncode: string,
49
+ api: NarouNovel = narouNovelFetch
51
50
  ): Promise<RankingHistoryResult[]> {
52
- const result = await NarouNovel.executeRankingHistory({ ncode });
51
+ const result = await api.executeRankingHistory({ ncode });
53
52
  if (Array.isArray(result)) {
54
53
  return result.map(formatRankingHistory);
55
54
  } else {
@@ -61,5 +60,5 @@ export default {
61
60
  search,
62
61
  ranking,
63
62
  searchR18,
64
- rankingHistory
63
+ rankingHistory,
65
64
  };
@@ -0,0 +1,51 @@
1
+ import nodeFetch from "node-fetch";
2
+ import nodeURL from "./util/url";
3
+
4
+ import { unzipp } from "./util/unzipp";
5
+ import NarouNovel, { NarouParams } from "./narou";
6
+
7
+ /**
8
+ * なろう小説APIへのリクエストを実行する
9
+ * @class NarouNovel
10
+ * @private
11
+ */
12
+ export default class NarouNovelFetch extends NarouNovel {
13
+ constructor(private fetch = nodeFetch) {
14
+ super();
15
+ }
16
+
17
+ protected async execute<T>(
18
+ params: NarouParams,
19
+ endpoint: string
20
+ ): Promise<T> {
21
+ const query = { ...params, out: "json" };
22
+
23
+ if (query.gzip === undefined) {
24
+ query.gzip = 5;
25
+ }
26
+ const url = new nodeURL(endpoint);
27
+
28
+ Object.entries(query).forEach(([key, value]) => {
29
+ if (value) {
30
+ url.searchParams.append(key, value.toString());
31
+ }
32
+ });
33
+
34
+ const res = await this.fetch(url);
35
+
36
+ if (query.gzip === 0) {
37
+ return await res.json();
38
+ }
39
+
40
+ const buffer = await res.buffer();
41
+ try {
42
+ return await unzipp(buffer);
43
+ } catch {
44
+ try {
45
+ throw JSON.stringify(buffer.toString());
46
+ } catch {
47
+ throw buffer.toString();
48
+ }
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,29 @@
1
+ import nodeURL from "./util/url";
2
+
3
+ import NarouNovel, { NarouParams } from "./narou";
4
+ import { jsonp } from "./util/jsonp";
5
+
6
+ /**
7
+ * なろう小説APIへのリクエストを実行する
8
+ * @class NarouNovel
9
+ * @private
10
+ */
11
+ export default class NarouNovelJsonp extends NarouNovel {
12
+ protected async execute<T>(
13
+ params: NarouParams,
14
+ endpoint: string
15
+ ): Promise<T> {
16
+ const query = { ...params, out: "jsonp" };
17
+ query.gzip = 0;
18
+
19
+ const url = new nodeURL(endpoint);
20
+
21
+ Object.entries(query).forEach(([key, value]) => {
22
+ if (value) {
23
+ url.searchParams.append(key, value.toString());
24
+ }
25
+ });
26
+
27
+ return await jsonp(url.toString());
28
+ }
29
+ }
@@ -1,9 +1,9 @@
1
- import { NarouSearchResult } from "./narou-search-results";
2
-
3
- export interface NarouRankingResult {
4
- ncode: string;
5
- rank: number;
6
- pt: number;
7
- }
8
-
9
- export interface RankingResult extends NarouSearchResult, NarouRankingResult {}
1
+ import { NarouSearchResult } from "./narou-search-results";
2
+
3
+ export interface NarouRankingResult {
4
+ ncode: string;
5
+ rank: number;
6
+ pt: number;
7
+ }
8
+
9
+ export interface RankingResult extends NarouSearchResult, NarouRankingResult {}
@@ -3,7 +3,7 @@
3
3
  * @class NarouSearchResults
4
4
  */
5
5
 
6
- import { SearchParams } from "./params";
6
+ import { booleanNumber, Genre, R18Site, SearchParams } from "./params";
7
7
 
8
8
  export default class NarouSearchResults {
9
9
  allcount: number;
@@ -93,23 +93,24 @@ export interface NarouSearchResult {
93
93
  userid: number;
94
94
  writer: string;
95
95
  story: string;
96
- genre: number;
96
+ nocgenre: R18Site;
97
+ genre: Genre;
97
98
  keyword: string;
98
99
  general_firstup: string;
99
100
  general_lastup: string;
100
101
  novel_type: number;
101
102
  noveltype: number;
102
- end: number;
103
+ end: booleanNumber;
103
104
  general_all_no: number;
104
105
  length: number;
105
106
  time: number;
106
- isstop: number;
107
- isr15: number;
108
- isbl: number;
109
- isgl: number;
110
- iszankoku: number;
111
- istensei: number;
112
- istenni: number;
107
+ isstop: booleanNumber;
108
+ isr15: booleanNumber;
109
+ isbl: booleanNumber;
110
+ isgl: booleanNumber;
111
+ iszankoku: booleanNumber;
112
+ istensei: booleanNumber;
113
+ istenni: booleanNumber;
113
114
  pc_or_k: number;
114
115
  global_point: number;
115
116
  daily_point: number;
package/src/narou.ts CHANGED
@@ -1,83 +1,55 @@
1
- import fetch from "node-fetch";
2
- import { URL } from "url";
3
-
4
1
  import { NarouRankingResult } from "./narou-ranking-results";
5
2
  import NarouSearchResults from "./narou-search-results";
6
3
  import { RankingHistoryParams, RankingParams, SearchParams } from "./params";
7
4
  import { RankingHistoryRawResult } from "./ranking-history";
8
- import { unzipp } from "./util/unzipp";
9
5
 
10
- type NarouParams = SearchParams | RankingParams | RankingHistoryParams;
6
+ export type NarouParams = SearchParams | RankingParams | RankingHistoryParams;
11
7
 
12
8
  /**
13
9
  * なろう小説APIへのリクエストを実行する
14
10
  * @class NarouNovel
15
11
  * @private
16
12
  */
17
- export default class NarouNovel {
13
+ export default abstract class NarouNovel {
18
14
  /**
19
15
  * なろう小説APIへの検索リクエストを実行する
20
16
  * @param params クエリパラメータ
21
17
  * @param endpoint APIエンドポイント
22
18
  * @returns {Promise<NarouSearchResults>} 検索結果
23
19
  */
24
- static async execute(
20
+ protected abstract execute<T>(
25
21
  params: NarouParams,
26
- endpoint = "http://api.syosetu.com/novelapi/api/"
27
- ): Promise<any> {
28
- const query = { ...params, out: "json" };
29
-
30
- if (query.gzip === undefined) {
31
- query.gzip = 5;
32
- }
33
- const url = new URL(endpoint);
34
-
35
- Object.entries(query).forEach(([key, value]) => {
36
- if (value) {
37
- url.searchParams.append(key, value.toString());
38
- }
39
- });
40
-
41
- const res = await fetch(url);
22
+ endpoint: string
23
+ ): Promise<T>;
42
24
 
43
- if (query.gzip === 0) {
44
- return await res.json();
45
- }
46
-
47
- const buffer = await res.buffer();
48
- try {
49
- return await unzipp(buffer);
50
- } catch {
51
- try {
52
- return JSON.stringify(buffer.toString());
53
- } catch {
54
- return buffer.toString();
55
- }
56
- }
57
- }
58
-
59
- static async executeSearch(
60
- params: any,
25
+ async executeSearch(
26
+ params: NarouParams,
61
27
  endpoint = "http://api.syosetu.com/novelapi/api/"
62
28
  ): Promise<NarouSearchResults> {
63
29
  return new NarouSearchResults(await this.execute(params, endpoint), params);
64
30
  }
65
31
 
66
- static executeNovel(params: SearchParams): Promise<NarouSearchResults> {
67
- return this.executeSearch(params, "http://api.syosetu.com/novelapi/api/");
32
+ async executeNovel(params: SearchParams): Promise<NarouSearchResults> {
33
+ return await this.executeSearch(
34
+ params,
35
+ "http://api.syosetu.com/novelapi/api/"
36
+ );
68
37
  }
69
38
 
70
- static executeNovel18(params: SearchParams): Promise<NarouSearchResults> {
71
- return this.executeSearch(params, "http://api.syosetu.com/novel18api/api/");
39
+ async executeNovel18(params: SearchParams): Promise<NarouSearchResults> {
40
+ return await this.executeSearch(
41
+ params,
42
+ "http://api.syosetu.com/novel18api/api/"
43
+ );
72
44
  }
73
45
 
74
- static executeRanking(params: RankingParams): Promise<NarouRankingResult[]> {
75
- return this.execute(params, "http://api.syosetu.com/rank/rankget/");
46
+ async executeRanking(params: RankingParams): Promise<NarouRankingResult[]> {
47
+ return await this.execute(params, "http://api.syosetu.com/rank/rankget/");
76
48
  }
77
49
 
78
- static executeRankingHistory(
50
+ async executeRankingHistory(
79
51
  params: RankingHistoryParams
80
52
  ): Promise<RankingHistoryRawResult[]> {
81
- return this.execute(params, "http://api.syosetu.com/rank/rankin/");
53
+ return await this.execute(params, "http://api.syosetu.com/rank/rankin/");
82
54
  }
83
55
  }