e621-client 1.2.2 → 1.3.0

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/README.md CHANGED
@@ -1,47 +1,47 @@
1
- # e621 Client
2
-
3
- This is a small client to do some stuff with the e621 API. It is created to fit my needs, but you can use it too, if it works for you.
4
-
5
- I can't guarantee it will work in the future, so I recommend you just use another package instead or build a client yourself.
6
-
7
- ### Supported Endpoints
8
-
9
- - `/favorites`
10
- - `/users`
11
- - `/users/:id`
12
- - `/posts`
13
- - `/posts/:id`
14
-
15
- ### Getting Started
16
-
17
- Just create an instance of the client like so:
18
-
19
- ```ts
20
- const client = new E621ApiClient({
21
- client: "Your client name",
22
- isBrowser: false,
23
- });
24
- ```
25
-
26
- Now you can use it to do some stuff:
27
-
28
- ```ts
29
- // not useful but still here (like me)
30
- const posts = await client.getPosts();
31
-
32
- // search posts by tags
33
- const postsWithTags = await client.getPosts({
34
- tags: "male score:>100",
35
- });
36
-
37
- // search users by names (* for wildcards)
38
- const toms = await client.getUsers({
39
- nameMatches: "tom*",
40
- });
41
-
42
- // if you have credentials set, they will be sent with your requests as well:
43
- client.setCredentials({
44
- username: "HornyFurry",
45
- apiKey: "wouldntyouliketoknowweatherboy",
46
- });
47
- ```
1
+ # e621 Client
2
+
3
+ This is a small client to do some stuff with the e621 API. It is created to fit my needs, but you can use it too, if it works for you.
4
+
5
+ I can't guarantee it will work in the future, so I recommend you just use another package instead or build a client yourself.
6
+
7
+ ### Supported Endpoints
8
+
9
+ - `/favorites`
10
+ - `/users`
11
+ - `/users/:id`
12
+ - `/posts`
13
+ - `/posts/:id`
14
+
15
+ ### Getting Started
16
+
17
+ Just create an instance of the client like so:
18
+
19
+ ```ts
20
+ const client = new E621ApiClient({
21
+ client: "Your client name",
22
+ isBrowser: false,
23
+ });
24
+ ```
25
+
26
+ Now you can use it to do some stuff:
27
+
28
+ ```ts
29
+ // not useful but still here (like me)
30
+ const posts = await client.getPosts();
31
+
32
+ // search posts by tags
33
+ const postsWithTags = await client.getPosts({
34
+ tags: "male score:>100",
35
+ });
36
+
37
+ // search users by names (* for wildcards)
38
+ const toms = await client.getUsers({
39
+ nameMatches: "tom*",
40
+ });
41
+
42
+ // if you have credentials set, they will be sent with your requests as well:
43
+ client.setCredentials({
44
+ username: "HornyFurry",
45
+ apiKey: "wouldntyouliketoknowweatherboy",
46
+ });
47
+ ```
@@ -1,7 +1,7 @@
1
1
  import { E621Post } from "../types/e621/E621Post";
2
2
  import { E621User } from "../types/e621/E621User";
3
3
  import { E621ApiHeaders } from "./E621ApiHeaders";
4
- import { GetAllFavoritesOptions, GetFavoritesOptions, GetPostsOptions, GetUsersOptions, Params } from "./options";
4
+ import { GetAllFavoritesOptions, GetFavoritesOptions, GetPostsOptions, GetUsersOptions, Params, PostFavoriteOptions } from "./options";
5
5
  /**
6
6
  * The option object used to initialize the API client.
7
7
  */
@@ -36,6 +36,12 @@ export type GetOptions = {
36
36
  params?: Params;
37
37
  headers?: E621ApiHeaders;
38
38
  };
39
+ export type PostOptions = {
40
+ route: Route;
41
+ params?: Params;
42
+ body?: string;
43
+ headers?: E621ApiHeaders;
44
+ };
39
45
  export interface Credentials {
40
46
  username: string;
41
47
  apiKey: string;
@@ -56,8 +62,10 @@ export declare class E621ApiClient {
56
62
  getPostById(id: EntityId): Promise<E621Post | null>;
57
63
  getFavorites(options?: GetFavoritesOptions): Promise<E621Post[]>;
58
64
  getAllFavorites(options?: GetAllFavoritesOptions): Promise<E621Post[]>;
65
+ makeFavorite(options: PostFavoriteOptions): Promise<boolean>;
59
66
  setCredentials(credentials: Credentials | null): E621ApiClient;
60
67
  checkCredentials(credentials?: Credentials): Promise<boolean>;
61
68
  get<E>({ route, params, headers, }: GetOptions): Promise<E | null>;
69
+ post<E>({ route, params, body, headers, }: PostOptions): Promise<E | null>;
62
70
  private buildUrl;
63
71
  }
@@ -73,15 +73,15 @@ class E621ApiClient {
73
73
  var _a, _b;
74
74
  // max page size on e621 api
75
75
  const favorites = [];
76
- const limit = 300;
76
+ const limit = 320;
77
77
  let page = 1;
78
78
  while (true) {
79
79
  if (this.debugLogs) {
80
80
  const user = (_a = options.userId) !== null && _a !== void 0 ? _a : (_b = this.credentials) === null || _b === void 0 ? void 0 : _b.username;
81
- console.info(`page ${page} for user ${user}`);
81
+ console.info("User", user, "page", page);
82
82
  }
83
83
  const posts = yield this.getFavorites(Object.assign(Object.assign({}, options), { page: String(page++), limit: String(limit) }));
84
- if (posts.length === 0)
84
+ if (posts.length === 0 || posts.length < limit)
85
85
  break;
86
86
  favorites.push(...posts);
87
87
  yield (0, utils_1.delay)(this.batchRateLimit);
@@ -89,6 +89,16 @@ class E621ApiClient {
89
89
  return favorites;
90
90
  });
91
91
  }
92
+ makeFavorite(options) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const response = yield this.post({
95
+ route: "favorites",
96
+ headers: new E621ApiHeaders_1.E621ApiHeaders().addFormContentType(),
97
+ body: new URLSearchParams({ post_id: options.postId }).toString(),
98
+ });
99
+ return Boolean(response);
100
+ });
101
+ }
92
102
  // Account
93
103
  setCredentials(credentials) {
94
104
  this.credentials = credentials;
@@ -125,6 +135,28 @@ class E621ApiClient {
125
135
  }
126
136
  });
127
137
  }
138
+ post(_a) {
139
+ return __awaiter(this, arguments, void 0, function* ({ route, params, body, headers = new E621ApiHeaders_1.E621ApiHeaders(), }) {
140
+ if (!this.isBrowser) {
141
+ headers.addUserAgent(this.client);
142
+ }
143
+ const url = this.buildUrl(route, params);
144
+ const response = yield fetch(url, {
145
+ method: "POST",
146
+ headers: headers.addAuthorization(this.credentials).build(),
147
+ body,
148
+ });
149
+ if (response.ok) {
150
+ return response.json();
151
+ }
152
+ else if (response.status === 404) {
153
+ return null;
154
+ }
155
+ else {
156
+ throw new Error(`Request to ${route} failed with status code ${response.status}: "${response.statusText}"`);
157
+ }
158
+ });
159
+ }
128
160
  buildUrl(route, params = {}) {
129
161
  const queryParams = new URLSearchParams(params);
130
162
  const url = E621ApiClient.BASE_URL + (0, utils_1.ensureArray)(route).join("/") + ".json";
@@ -21,5 +21,12 @@ export type GetFavoritesApiOptions = {
21
21
  export type GetAllFavoritesOptions = {
22
22
  userId?: string;
23
23
  };
24
+ export type PostFavoriteOptions = {
25
+ postId: string;
26
+ };
27
+ export type PostFavoriteApiOptions = {
28
+ post_id: string;
29
+ };
24
30
  export declare function translateGetUserOptions(options: GetUsersOptions): GetUserApiOptions;
25
31
  export declare function translateGetFavoritesOptions(options: GetFavoritesOptions): GetFavoritesApiOptions;
32
+ export declare function translatePostFavoriteOptions(options: PostFavoriteOptions): PostFavoriteApiOptions;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translateGetFavoritesOptions = exports.translateGetUserOptions = void 0;
3
+ exports.translatePostFavoriteOptions = exports.translateGetFavoritesOptions = exports.translateGetUserOptions = void 0;
4
4
  const utils_1 = require("../utils");
5
5
  function translateGetUserOptions(options) {
6
6
  return (0, utils_1.translateOptions)(options, {
@@ -14,3 +14,9 @@ function translateGetFavoritesOptions(options) {
14
14
  });
15
15
  }
16
16
  exports.translateGetFavoritesOptions = translateGetFavoritesOptions;
17
+ function translatePostFavoriteOptions(options) {
18
+ return (0, utils_1.translateOptions)(options, {
19
+ postId: "post_id",
20
+ });
21
+ }
22
+ exports.translatePostFavoriteOptions = translatePostFavoriteOptions;
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
- {
2
- "name": "e621-client",
3
- "version": "1.2.2",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "scripts": {
7
- "test": "jest",
8
- "build": "tsc",
9
- "prepublishOnly": "tsc"
10
- },
11
- "keywords": [],
12
- "author": "Tommus",
13
- "license": "ISC",
14
- "description": "A simple client for the e621 API",
15
- "devDependencies": {
16
- "@types/jest": "^29.5.12",
17
- "@types/node": "^20.14.2",
18
- "jest": "^29.7.0",
19
- "prettier": "^3.3.1",
20
- "ts-jest": "^29.2.3",
21
- "ts-node": "^10.9.2",
22
- "typescript": "^5.4.5"
23
- }
24
- }
1
+ {
2
+ "name": "e621-client",
3
+ "version": "1.3.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "scripts": {
7
+ "test": "jest",
8
+ "build": "tsc",
9
+ "prepublishOnly": "tsc"
10
+ },
11
+ "keywords": [],
12
+ "author": "Tommus",
13
+ "license": "ISC",
14
+ "description": "A simple client for the e621 API",
15
+ "devDependencies": {
16
+ "@types/jest": "^29.5.12",
17
+ "@types/node": "^20.14.2",
18
+ "jest": "^29.7.0",
19
+ "prettier": "^3.3.1",
20
+ "ts-jest": "^29.2.3",
21
+ "ts-node": "^10.9.2",
22
+ "typescript": "^5.4.5"
23
+ }
24
+ }
@@ -13,6 +13,7 @@ import {
13
13
  GetPostsOptions,
14
14
  GetUsersOptions,
15
15
  Params,
16
+ PostFavoriteOptions,
16
17
  translateGetFavoritesOptions,
17
18
  translateGetUserOptions,
18
19
  } from "./options";
@@ -57,6 +58,13 @@ export type GetOptions = {
57
58
  headers?: E621ApiHeaders;
58
59
  };
59
60
 
61
+ export type PostOptions = {
62
+ route: Route;
63
+ params?: Params;
64
+ body?: string;
65
+ headers?: E621ApiHeaders;
66
+ };
67
+
60
68
  export interface Credentials {
61
69
  username: string;
62
70
  apiKey: string;
@@ -113,7 +121,7 @@ export class E621ApiClient {
113
121
 
114
122
  // Favorites
115
123
  public async getFavorites(
116
- options: GetFavoritesOptions = {},
124
+ options: GetFavoritesOptions = {}
117
125
  ): Promise<E621Post[]> {
118
126
  const response = await this.get<GetFavoritesResponse>({
119
127
  route: "favorites",
@@ -123,17 +131,17 @@ export class E621ApiClient {
123
131
  }
124
132
 
125
133
  public async getAllFavorites(
126
- options: GetAllFavoritesOptions = {},
134
+ options: GetAllFavoritesOptions = {}
127
135
  ): Promise<E621Post[]> {
128
136
  // max page size on e621 api
129
137
  const favorites: E621Post[] = [];
130
- const limit = 300;
138
+ const limit = 320;
131
139
  let page = 1;
132
140
 
133
141
  while (true) {
134
142
  if (this.debugLogs) {
135
143
  const user = options.userId ?? this.credentials?.username;
136
- console.info(`page ${page} for user ${user}`);
144
+ console.info("User", user, "page", page);
137
145
  }
138
146
 
139
147
  const posts = await this.getFavorites({
@@ -142,7 +150,7 @@ export class E621ApiClient {
142
150
  limit: String(limit),
143
151
  });
144
152
 
145
- if (posts.length === 0) break;
153
+ if (posts.length === 0 || posts.length < limit) break;
146
154
  favorites.push(...posts);
147
155
  await delay(this.batchRateLimit);
148
156
  }
@@ -150,6 +158,15 @@ export class E621ApiClient {
150
158
  return favorites;
151
159
  }
152
160
 
161
+ public async makeFavorite(options: PostFavoriteOptions): Promise<boolean> {
162
+ const response = await this.post({
163
+ route: "favorites",
164
+ headers: new E621ApiHeaders().addFormContentType(),
165
+ body: new URLSearchParams({ post_id: options.postId }).toString(),
166
+ });
167
+ return Boolean(response);
168
+ }
169
+
153
170
  // Account
154
171
  public setCredentials(credentials: Credentials | null): E621ApiClient {
155
172
  this.credentials = credentials;
@@ -187,7 +204,35 @@ export class E621ApiClient {
187
204
  return null;
188
205
  } else {
189
206
  throw new Error(
190
- `Request to ${route} failed with status code ${response.status}: "${response.statusText}"`,
207
+ `Request to ${route} failed with status code ${response.status}: "${response.statusText}"`
208
+ );
209
+ }
210
+ }
211
+
212
+ public async post<E>({
213
+ route,
214
+ params,
215
+ body,
216
+ headers = new E621ApiHeaders(),
217
+ }: PostOptions): Promise<E | null> {
218
+ if (!this.isBrowser) {
219
+ headers.addUserAgent(this.client);
220
+ }
221
+
222
+ const url = this.buildUrl(route, params);
223
+ const response = await fetch(url, {
224
+ method: "POST",
225
+ headers: headers.addAuthorization(this.credentials).build(),
226
+ body,
227
+ });
228
+
229
+ if (response.ok) {
230
+ return response.json();
231
+ } else if (response.status === 404) {
232
+ return null;
233
+ } else {
234
+ throw new Error(
235
+ `Request to ${route} failed with status code ${response.status}: "${response.statusText}"`
191
236
  );
192
237
  }
193
238
  }
@@ -16,8 +16,12 @@ export type GetFavoritesOptions = { userId?: string } & Pagination;
16
16
  export type GetFavoritesApiOptions = { user_id?: string } & Pagination;
17
17
  export type GetAllFavoritesOptions = { userId?: string };
18
18
 
19
+ // POST /favorites
20
+ export type PostFavoriteOptions = { postId: string };
21
+ export type PostFavoriteApiOptions = { post_id: string };
22
+
19
23
  export function translateGetUserOptions(
20
- options: GetUsersOptions,
24
+ options: GetUsersOptions
21
25
  ): GetUserApiOptions {
22
26
  return translateOptions(options, {
23
27
  nameMatches: "search[name_matches]",
@@ -25,9 +29,17 @@ export function translateGetUserOptions(
25
29
  }
26
30
 
27
31
  export function translateGetFavoritesOptions(
28
- options: GetFavoritesOptions,
32
+ options: GetFavoritesOptions
29
33
  ): GetFavoritesApiOptions {
30
34
  return translateOptions(options, {
31
35
  userId: "user_id",
32
36
  });
33
37
  }
38
+
39
+ export function translatePostFavoriteOptions(
40
+ options: PostFavoriteOptions
41
+ ): PostFavoriteApiOptions {
42
+ return translateOptions(options, {
43
+ postId: "post_id",
44
+ }) as PostFavoriteApiOptions;
45
+ }
package/demo.ts DELETED
@@ -1,12 +0,0 @@
1
- import { E621ApiClient } from "./src/client/E621ApiClient";
2
-
3
- async function demo() {
4
- const client = new E621ApiClient({
5
- isBrowser: false,
6
- client: "EstoBox (by Tommus621 on e621)",
7
- });
8
- const favs = await client.getFavorites({ userId: "964986", page: "2" });
9
- console.log(favs.length);
10
- }
11
-
12
- demo().finally();