e621-client 1.2.3 → 1.4.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/dist/client/E621ApiClient.d.ts +9 -1
- package/dist/client/E621ApiClient.js +32 -0
- package/dist/client/options.d.ts +7 -0
- package/dist/client/options.js +7 -1
- package/dist/types/e621/E621User.d.ts +2 -0
- package/dist/types/e621/E621User.js +2 -0
- package/package.json +1 -1
- package/src/client/E621ApiClient.ts +45 -0
- package/src/client/options.ts +14 -2
- package/src/types/e621/E621User.ts +3 -0
|
@@ -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";
|
package/dist/client/options.d.ts
CHANGED
|
@@ -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;
|
package/dist/client/options.js
CHANGED
|
@@ -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;
|
|
@@ -25,6 +25,7 @@ export interface UserResponse {
|
|
|
25
25
|
can_upload_free: boolean;
|
|
26
26
|
level_string: string;
|
|
27
27
|
avatar_id: number | null;
|
|
28
|
+
blacklisted_tags: string | null;
|
|
28
29
|
}
|
|
29
30
|
export interface E621User {
|
|
30
31
|
id: number;
|
|
@@ -36,6 +37,7 @@ export interface E621User {
|
|
|
36
37
|
artInfo: string;
|
|
37
38
|
avatarId: number | null;
|
|
38
39
|
levelString: string;
|
|
40
|
+
blacklistedTags: string;
|
|
39
41
|
};
|
|
40
42
|
counts: {
|
|
41
43
|
wikiPageVersion: number;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseUser = void 0;
|
|
4
4
|
function parseUser(user) {
|
|
5
|
+
var _a;
|
|
5
6
|
return {
|
|
6
7
|
id: user.id,
|
|
7
8
|
name: user.name,
|
|
@@ -12,6 +13,7 @@ function parseUser(user) {
|
|
|
12
13
|
artInfo: user.profile_artinfo,
|
|
13
14
|
avatarId: user.avatar_id,
|
|
14
15
|
levelString: user.level_string,
|
|
16
|
+
blacklistedTags: (_a = user.blacklisted_tags) !== null && _a !== void 0 ? _a : "",
|
|
15
17
|
},
|
|
16
18
|
counts: {
|
|
17
19
|
wikiPageVersion: user.wiki_page_version_count,
|
package/package.json
CHANGED
|
@@ -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";
|
package/src/client/options.ts
CHANGED
|
@@ -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
|
+
}
|
|
@@ -25,6 +25,7 @@ export interface UserResponse {
|
|
|
25
25
|
can_upload_free: boolean;
|
|
26
26
|
level_string: string;
|
|
27
27
|
avatar_id: number | null;
|
|
28
|
+
blacklisted_tags: string | null;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
export interface E621User {
|
|
@@ -38,6 +39,7 @@ export interface E621User {
|
|
|
38
39
|
artInfo: string;
|
|
39
40
|
avatarId: number | null;
|
|
40
41
|
levelString: string;
|
|
42
|
+
blacklistedTags: string;
|
|
41
43
|
};
|
|
42
44
|
|
|
43
45
|
counts: {
|
|
@@ -78,6 +80,7 @@ export function parseUser(user: UserResponse): E621User {
|
|
|
78
80
|
artInfo: user.profile_artinfo,
|
|
79
81
|
avatarId: user.avatar_id,
|
|
80
82
|
levelString: user.level_string,
|
|
83
|
+
blacklistedTags: user.blacklisted_tags ?? "",
|
|
81
84
|
},
|
|
82
85
|
|
|
83
86
|
counts: {
|