e621-client 1.1.0 → 1.1.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.
- package/dist/client/E621ApiClient.d.ts +4 -0
- package/dist/client/E621ApiClient.js +26 -1
- package/dist/test.d.ts +1 -0
- package/dist/test.js +23 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +6 -1
- package/package.json +1 -1
- package/src/client/E621ApiClient.ts +8 -3
- package/src/utils.ts +5 -0
|
@@ -31,6 +31,9 @@ export type GetPostsOptions = {
|
|
|
31
31
|
export type GetFavoritesOptions = {
|
|
32
32
|
userId?: string;
|
|
33
33
|
} & Pagination;
|
|
34
|
+
export type GetAllFavoritesOptions = {
|
|
35
|
+
userId?: string;
|
|
36
|
+
};
|
|
34
37
|
export declare class E621ApiClient {
|
|
35
38
|
protected static readonly BASE_URL = "https://e621.net/";
|
|
36
39
|
private readonly client;
|
|
@@ -42,6 +45,7 @@ export declare class E621ApiClient {
|
|
|
42
45
|
getPosts(options?: GetPostsOptions): Promise<E621Post[]>;
|
|
43
46
|
getPostById(id: EntityId): Promise<E621Post | null>;
|
|
44
47
|
getFavorites(options?: GetFavoritesOptions): Promise<E621Post[]>;
|
|
48
|
+
getAllFavorites(options?: GetAllFavoritesOptions, debugLogs?: boolean): Promise<E621Post[]>;
|
|
45
49
|
setCredentials(credentials: Credentials | null): E621ApiClient;
|
|
46
50
|
checkCredentials(credentials?: Credentials): Promise<boolean>;
|
|
47
51
|
get<E>({ route, params, headers, }: GetOptions): Promise<E | null>;
|
|
@@ -66,6 +66,26 @@ class E621ApiClient {
|
|
|
66
66
|
return ((_a = response === null || response === void 0 ? void 0 : response.posts) === null || _a === void 0 ? void 0 : _a.map(E621Post_1.parsePost)) || [];
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
+
getAllFavorites() {
|
|
70
|
+
return __awaiter(this, arguments, void 0, function* (options = {}, debugLogs = false) {
|
|
71
|
+
var _a, _b;
|
|
72
|
+
// max page size on e621 api
|
|
73
|
+
const favorites = [];
|
|
74
|
+
const limit = 300;
|
|
75
|
+
let page = 1;
|
|
76
|
+
while (true) {
|
|
77
|
+
if (debugLogs) {
|
|
78
|
+
const user = (_a = options.userId) !== null && _a !== void 0 ? _a : (_b = this.credentials) === null || _b === void 0 ? void 0 : _b.username;
|
|
79
|
+
console.info(`page ${page} for user ${user}`);
|
|
80
|
+
}
|
|
81
|
+
const posts = yield this.getFavorites(Object.assign(Object.assign({}, options), { page: String(page++), limit: String(limit) }));
|
|
82
|
+
if (posts.length === 0)
|
|
83
|
+
break;
|
|
84
|
+
favorites.push(...posts);
|
|
85
|
+
}
|
|
86
|
+
return favorites;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
69
89
|
// Account
|
|
70
90
|
setCredentials(credentials) {
|
|
71
91
|
this.credentials = credentials;
|
|
@@ -96,7 +116,12 @@ class E621ApiClient {
|
|
|
96
116
|
method: "GET",
|
|
97
117
|
headers: headers.addAuthorization(this.credentials).build(),
|
|
98
118
|
});
|
|
99
|
-
|
|
119
|
+
if (response.ok) {
|
|
120
|
+
return response.json().catch(utils_1.logErrorAndReturnNull);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
100
125
|
});
|
|
101
126
|
}
|
|
102
127
|
buildUrl(route, params = {}) {
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const E621ApiClient_1 = require("./client/E621ApiClient");
|
|
13
|
+
function test() {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const client = new E621ApiClient_1.E621ApiClient({
|
|
16
|
+
isBrowser: false,
|
|
17
|
+
client: "EstoBox (by Tommus621 on e621)",
|
|
18
|
+
});
|
|
19
|
+
const user = yield client.getUserById(918032);
|
|
20
|
+
console.log(user);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
test().finally();
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export declare function ensureArray<T>(value: T | T[]): T[];
|
|
2
2
|
export declare function translateOptions<Options extends Record<string, string>>(options: Options, table: Partial<Options>): Record<string, string>;
|
|
3
|
+
export declare function logErrorAndReturnNull(error: unknown): null;
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.translateOptions = exports.ensureArray = void 0;
|
|
3
|
+
exports.logErrorAndReturnNull = exports.translateOptions = exports.ensureArray = void 0;
|
|
4
4
|
function ensureArray(value) {
|
|
5
5
|
if (Array.isArray(value)) {
|
|
6
6
|
return value;
|
|
@@ -17,3 +17,8 @@ function translateOptions(options, table) {
|
|
|
17
17
|
return params;
|
|
18
18
|
}
|
|
19
19
|
exports.translateOptions = translateOptions;
|
|
20
|
+
function logErrorAndReturnNull(error) {
|
|
21
|
+
console.error(error);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
exports.logErrorAndReturnNull = logErrorAndReturnNull;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { E621Post, parsePost } from "../types/e621/E621Post";
|
|
2
|
-
import { E621User,
|
|
2
|
+
import { E621User, parseUser, UserResponse } from "../types/e621/E621User";
|
|
3
3
|
import {
|
|
4
4
|
GetFavoritesResponse,
|
|
5
5
|
GetPostResponse,
|
|
6
6
|
GetPostsResponse,
|
|
7
7
|
} from "../types/responses";
|
|
8
|
-
import { ensureArray, translateOptions } from "../utils";
|
|
8
|
+
import { ensureArray, logErrorAndReturnNull, translateOptions } from "../utils";
|
|
9
9
|
import { E621ApiHeaders } from "./E621ApiHeaders";
|
|
10
10
|
|
|
11
11
|
export interface E621ApiOptions {
|
|
@@ -153,7 +153,12 @@ export class E621ApiClient {
|
|
|
153
153
|
method: "GET",
|
|
154
154
|
headers: headers.addAuthorization(this.credentials).build(),
|
|
155
155
|
});
|
|
156
|
-
|
|
156
|
+
|
|
157
|
+
if (response.ok) {
|
|
158
|
+
return response.json().catch(logErrorAndReturnNull);
|
|
159
|
+
} else {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
157
162
|
}
|
|
158
163
|
|
|
159
164
|
private buildUrl(route: Route, params: Params = {}): string {
|