e621-client 1.2.3 → 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.
@@ -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
  }
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "e621-client",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -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;
@@ -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;
@@ -192,6 +209,34 @@ export class E621ApiClient {
192
209
  }
193
210
  }
194
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}"`
236
+ );
237
+ }
238
+ }
239
+
195
240
  private buildUrl(route: Route, params: Params = {}): string {
196
241
  const queryParams = new URLSearchParams(params);
197
242
  const url = E621ApiClient.BASE_URL + ensureArray(route).join("/") + ".json";
@@ -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
+ }