Skip to content

Commit

Permalink
ビルド出来るように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
deflis committed Nov 8, 2023
1 parent 4521188 commit 0d2c3c1
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 202 deletions.
5 changes: 0 additions & 5 deletions jest.config.js

This file was deleted.

6 changes: 6 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: "ts-jest",
extensionsToTreatAsEsm: [".ts"],
testEnvironment: "node",
};
189 changes: 63 additions & 126 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "narou",
"version": "0.8.1",
"description": "Narou API Wrapper",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -48,7 +49,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"jest": "^29.7.0",
"node-fetch": "^3.3.2",
"node-fetch": "^2.7.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
Expand Down
18 changes: 5 additions & 13 deletions src/narou-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import nodeFetch from "node-fetch";

import { unzipp } from "./util/unzipp";
import NarouNovel, { NarouParams } from "./narou";

type Fetch = typeof fetch;

/**
* なろう小説APIへのリクエストを実行する
*/
export default class NarouNovelFetch extends NarouNovel {
constructor(private fetch = nodeFetch) {
constructor(private fetch: Fetch = require('node-fetch')) {
super();
}

Expand All @@ -34,15 +34,7 @@ export default class NarouNovelFetch extends NarouNovel {
return (await res.json()) as T;
}

const buffer = await res.buffer();
try {
return await unzipp(buffer);
} catch {
try {
throw JSON.stringify(buffer.toString());
} catch {
throw buffer.toString();
}
}
const buffer = await res.arrayBuffer();
return await unzipp(buffer);
}
}
2 changes: 1 addition & 1 deletion src/ranking-history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parse from "date-fns/parse";
import { parse } from "date-fns";
import { RankingType } from "./params";

const dateFormat = "yyyyMMdd";
Expand Down
3 changes: 1 addition & 2 deletions src/ranking.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NarouRankingResult, RankingResult } from "./narou-ranking-results";
import SearchBuilder, { DefaultSearchResultFields } from "./search-builder";
import addDays from "date-fns/addDays";
import format from "date-fns/format";
import { addDays, format } from "date-fns";
import {
Fields,
GzipLevel,
Expand Down
16 changes: 13 additions & 3 deletions src/util/unzipp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { promisify } from "util";

const gunzipAsync = promisify<InputType, Buffer>(gunzip);

export async function unzipp(data: InputType) {
const buffer = await gunzipAsync(data);
return JSON.parse(buffer.toString());
const decoder = new TextDecoder()
export async function unzipp(data: ArrayBuffer) {
try {
const buffer = await gunzipAsync(data);
try {
return JSON.parse(decoder.decode(buffer));
} catch {
throw decoder.decode(buffer);
}
} catch (e) {
if (typeof e === "string") throw e;
throw decoder.decode(data);
}
}
Loading

0 comments on commit 0d2c3c1

Please sign in to comment.