e621-client 1.0.3 → 1.0.5

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
@@ -34,7 +34,7 @@ const postsWithTags = await client.getPosts({
34
34
  tags: "male score:>100",
35
35
  });
36
36
 
37
- // search users by names (_ for wildcards)
37
+ // search users by names (* for wildcards)
38
38
  const toms = await client.getUsers({
39
39
  nameMatches: "tom*",
40
40
  });
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
- {
2
- "name": "e621-client",
3
- "version": "1.0.3",
4
- "main": "dist/index.js",
5
- "scripts": {
6
- "build": "tsc"
7
- },
8
- "keywords": [],
9
- "author": "Tommus",
10
- "license": "ISC",
11
- "description": "A simple client for the e621 API",
12
- "devDependencies": {
13
- "@types/node": "^20.14.2",
14
- "prettier": "^3.3.1",
15
- "ts-node": "^10.9.2",
16
- "typescript": "^5.4.5"
17
- }
18
- }
1
+ {
2
+ "name": "e621-client",
3
+ "version": "1.0.5",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "scripts": {
7
+ "build": "tsc"
8
+ },
9
+ "keywords": [],
10
+ "author": "Tommus",
11
+ "license": "ISC",
12
+ "description": "A simple client for the e621 API",
13
+ "devDependencies": {
14
+ "@types/node": "^20.14.2",
15
+ "prettier": "^3.3.1",
16
+ "ts-node": "^10.9.2",
17
+ "typescript": "^5.4.5"
18
+ }
19
+ }
@@ -1,49 +0,0 @@
1
- import { E621Post } from "../types/e621/E621Post";
2
- import { E621User } from "../types/e621/E621User";
3
- import { E621ApiHeaders } from "./E621ApiHeaders";
4
- export interface E621ApiOptions {
5
- client: string;
6
- isBrowser: boolean;
7
- credentials?: Credentials;
8
- }
9
- export type GetOptions = {
10
- route: Route;
11
- params?: Params;
12
- headers?: E621ApiHeaders;
13
- };
14
- export interface Credentials {
15
- username: string;
16
- apiKey: string;
17
- }
18
- export type EntityId = string | number;
19
- export type Route = string | number | (string | number)[];
20
- export type Params = Record<string, string>;
21
- export type Pagination = {
22
- limit?: string;
23
- page?: string;
24
- };
25
- export type GetUsersOptions = {
26
- nameMatches?: string;
27
- } & Params & Pagination;
28
- export type GetPostsOptions = {
29
- tags?: string;
30
- } & Pagination;
31
- export type GetFavoritesOptions = {
32
- userId?: string;
33
- } & Pagination;
34
- export declare class E621ApiClient {
35
- protected static readonly BASE_URL = "https://e621.net/";
36
- private readonly client;
37
- private readonly isBrowser;
38
- private credentials;
39
- constructor(options: E621ApiOptions);
40
- getUsers(options?: GetUsersOptions): Promise<E621User[]>;
41
- getUserById(id: EntityId): Promise<E621User | null>;
42
- getPosts(options?: GetPostsOptions): Promise<E621Post[]>;
43
- getPostById(id: EntityId): Promise<E621Post | null>;
44
- getFavorites(options?: GetFavoritesOptions): Promise<E621Post[]>;
45
- setCredentials(credentials: Credentials | null): E621ApiClient;
46
- checkCredentials(credentials?: Credentials): Promise<boolean>;
47
- get<E>({ route, params, headers, }: GetOptions): Promise<E | null>;
48
- private buildUrl;
49
- }
@@ -1,112 +0,0 @@
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
- exports.E621ApiClient = void 0;
13
- const E621Post_1 = require("../types/e621/E621Post");
14
- const E621User_1 = require("../types/e621/E621User");
15
- const utils_1 = require("../utils");
16
- const E621ApiHeaders_1 = require("./E621ApiHeaders");
17
- class E621ApiClient {
18
- constructor(options) {
19
- this.credentials = null;
20
- this.client = options.client;
21
- this.isBrowser = options.isBrowser;
22
- this.credentials = options.credentials || null;
23
- }
24
- // Users
25
- getUsers() {
26
- return __awaiter(this, arguments, void 0, function* (options = {}) {
27
- const users = yield this.get({
28
- route: "users",
29
- params: (0, utils_1.translateOptions)(options, {
30
- nameMatches: "search[name_matches]",
31
- }),
32
- });
33
- return (users === null || users === void 0 ? void 0 : users.map(E621User_1.parseUser)) || [];
34
- });
35
- }
36
- getUserById(id) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- const user = yield this.get({ route: ["users", id] });
39
- return user ? (0, E621User_1.parseUser)(user) : null;
40
- });
41
- }
42
- // Posts
43
- getPosts() {
44
- return __awaiter(this, arguments, void 0, function* (options = {}) {
45
- const response = yield this.get({
46
- route: "posts",
47
- params: options,
48
- });
49
- return (response === null || response === void 0 ? void 0 : response.posts.map(E621Post_1.parsePost)) || [];
50
- });
51
- }
52
- getPostById(id) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const response = yield this.get({ route: ["posts", id] });
55
- return (response === null || response === void 0 ? void 0 : response.post) ? (0, E621Post_1.parsePost)(response.post) : null;
56
- });
57
- }
58
- // Favorites
59
- getFavorites() {
60
- return __awaiter(this, arguments, void 0, function* (options = {}) {
61
- var _a;
62
- const response = yield this.get({
63
- route: "favorites",
64
- params: (0, utils_1.translateOptions)(options, { userId: "user_id" }),
65
- });
66
- return ((_a = response === null || response === void 0 ? void 0 : response.posts) === null || _a === void 0 ? void 0 : _a.map(E621Post_1.parsePost)) || [];
67
- });
68
- }
69
- // Account
70
- setCredentials(credentials) {
71
- this.credentials = credentials;
72
- return this;
73
- }
74
- checkCredentials(credentials) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- if (credentials) {
77
- this.setCredentials(credentials);
78
- }
79
- const headers = new E621ApiHeaders_1.E621ApiHeaders();
80
- if (!this.isBrowser) {
81
- headers.addUserAgent(this.client);
82
- }
83
- return yield fetch(this.buildUrl("favorites"), {
84
- headers: headers.addAuthorization(this.credentials).build(),
85
- })
86
- .then((res) => res.ok)
87
- .catch(() => false);
88
- });
89
- }
90
- get(_a) {
91
- return __awaiter(this, arguments, void 0, function* ({ route, params, headers = new E621ApiHeaders_1.E621ApiHeaders(), }) {
92
- if (!this.isBrowser) {
93
- headers.addUserAgent(this.client);
94
- }
95
- const response = yield fetch(this.buildUrl(route, params), {
96
- method: "GET",
97
- headers: headers.addAuthorization(this.credentials).build(),
98
- });
99
- return response.json().catch(() => null);
100
- });
101
- }
102
- buildUrl(route, params = {}) {
103
- const queryParams = new URLSearchParams(params);
104
- const url = E621ApiClient.BASE_URL + (0, utils_1.ensureArray)(route).join("/") + ".json";
105
- if (this.isBrowser) {
106
- queryParams.set("_client", this.client);
107
- }
108
- return queryParams.size > 0 ? `${url}?${queryParams}` : url;
109
- }
110
- }
111
- exports.E621ApiClient = E621ApiClient;
112
- E621ApiClient.BASE_URL = "https://e621.net/";
@@ -1,10 +0,0 @@
1
- import { Credentials } from "./E621ApiClient";
2
- export declare class E621ApiHeaders {
3
- private readonly value;
4
- constructor(value?: Record<string, string>);
5
- addAuthorization(credentials: Credentials | null): E621ApiHeaders;
6
- addFormContentType(): E621ApiHeaders;
7
- addUserAgent(userAgent: string): E621ApiHeaders;
8
- build(): Record<string, string>;
9
- protected static buildAuthorization({ username, apiKey }: Credentials): string;
10
- }
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.E621ApiHeaders = void 0;
4
- class E621ApiHeaders {
5
- constructor(value = {}) {
6
- this.value = value;
7
- }
8
- addAuthorization(credentials) {
9
- if (credentials) {
10
- this.value.Authorization = E621ApiHeaders.buildAuthorization(credentials);
11
- }
12
- return this;
13
- }
14
- addFormContentType() {
15
- this.value["Content-Type"] =
16
- "application/x-www-form-urlencoded; charset=UTF-8";
17
- return this;
18
- }
19
- addUserAgent(userAgent) {
20
- this.value["User-Agent"] = userAgent;
21
- return this;
22
- }
23
- build() {
24
- return this.value;
25
- }
26
- static buildAuthorization({ username, apiKey }) {
27
- const creds = `${username}:${apiKey}`;
28
- if (canUseWindow()) {
29
- return `Basic ${window.btoa(creds)}`;
30
- }
31
- else if (canUseBuffer()) {
32
- return `Basic ${Buffer.from(creds).toString("base64")}`;
33
- }
34
- else {
35
- throw new Error("Environment does not support base64 encoding");
36
- }
37
- }
38
- }
39
- exports.E621ApiHeaders = E621ApiHeaders;
40
- function canUseWindow() {
41
- return typeof window !== "undefined";
42
- }
43
- function canUseBuffer() {
44
- return typeof Buffer !== "undefined" && typeof Buffer.from === "function";
45
- }
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./client/E621ApiClient";
2
- export * from "./types/e621/E621User";
3
- export * from "./types/e621/E621Post";
package/dist/index.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./client/E621ApiClient"), exports);
18
- __exportStar(require("./types/e621/E621User"), exports);
19
- __exportStar(require("./types/e621/E621Post"), exports);
@@ -1,133 +0,0 @@
1
- export interface PostResponse {
2
- id: number;
3
- created_at: string;
4
- updated_at: string;
5
- file: {
6
- width: number;
7
- height: number;
8
- ext: string;
9
- size: number;
10
- md5: string;
11
- url: string;
12
- };
13
- preview: {
14
- width: number;
15
- height: number;
16
- url: string | null;
17
- };
18
- sample: {
19
- has: boolean;
20
- width: number;
21
- height: number;
22
- url: string | null;
23
- };
24
- score: {
25
- up: number;
26
- down: number;
27
- total: number;
28
- };
29
- tags: {
30
- general: string[];
31
- artist: string[];
32
- copyright: string[];
33
- character: string[];
34
- species: string[];
35
- invalid: string[];
36
- meta: string[];
37
- lore: string[];
38
- };
39
- locked_tags: string[];
40
- change_seq: number;
41
- flags: {
42
- pending: boolean;
43
- flagged: boolean;
44
- note_locked: boolean;
45
- status_locked: boolean;
46
- rating_locked: boolean;
47
- deleted: boolean;
48
- };
49
- rating: string;
50
- fav_count: number;
51
- sources: string[];
52
- pools: number[];
53
- relationships: {
54
- parent_id: number | null;
55
- has_children: boolean;
56
- has_active_children: boolean;
57
- children: number[];
58
- };
59
- approver_id: number | null;
60
- uploader_id: number;
61
- description: string;
62
- comment_count: number;
63
- is_favorited: boolean;
64
- has_notes: boolean;
65
- duration: number | null;
66
- }
67
- export interface E621Post {
68
- id: number;
69
- createdAt: Date;
70
- updatedAt: Date;
71
- file: {
72
- width: number;
73
- height: number;
74
- ext: string;
75
- size: number;
76
- md5: string;
77
- url: string;
78
- };
79
- preview: {
80
- width: number;
81
- height: number;
82
- url: string | null;
83
- };
84
- sample: {
85
- has: boolean;
86
- width: number;
87
- height: number;
88
- url: string | null;
89
- };
90
- score: {
91
- up: number;
92
- down: number;
93
- total: number;
94
- };
95
- tags: {
96
- general: string[];
97
- artist: string[];
98
- copyright: string[];
99
- character: string[];
100
- species: string[];
101
- invalid: string[];
102
- meta: string[];
103
- lore: string[];
104
- };
105
- lockedTags: string[];
106
- changeSeq: number;
107
- flags: {
108
- pending: boolean;
109
- flagged: boolean;
110
- noteLocked: boolean;
111
- statusLocked: boolean;
112
- ratingLocked: boolean;
113
- deleted: boolean;
114
- };
115
- rating: string;
116
- favCount: number;
117
- sources: string[];
118
- pools: number[];
119
- relationships: {
120
- parentId: number | null;
121
- hasChildren: boolean;
122
- hasActiveChildren: boolean;
123
- children: number[];
124
- };
125
- approverId: number | null;
126
- uploaderId: number;
127
- description: string;
128
- commentCount: number;
129
- isFavorited: boolean;
130
- hasNotes: boolean;
131
- duration: number | null;
132
- }
133
- export declare function parsePost(post: PostResponse): E621Post;
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parsePost = void 0;
4
- function parsePost(post) {
5
- return {
6
- id: post.id,
7
- createdAt: new Date(post.created_at),
8
- updatedAt: new Date(post.updated_at),
9
- file: post.file,
10
- preview: post.preview,
11
- sample: post.sample,
12
- score: post.score,
13
- tags: post.tags,
14
- lockedTags: post.locked_tags,
15
- changeSeq: post.change_seq,
16
- flags: {
17
- pending: post.flags.pending,
18
- flagged: post.flags.flagged,
19
- noteLocked: post.flags.note_locked,
20
- statusLocked: post.flags.status_locked,
21
- ratingLocked: post.flags.rating_locked,
22
- deleted: post.flags.deleted,
23
- },
24
- rating: post.rating,
25
- favCount: post.fav_count,
26
- sources: post.sources,
27
- pools: post.pools,
28
- relationships: {
29
- parentId: post.relationships.parent_id,
30
- hasChildren: post.relationships.has_children,
31
- hasActiveChildren: post.relationships.has_active_children,
32
- children: post.relationships.children,
33
- },
34
- approverId: post.approver_id,
35
- uploaderId: post.uploader_id,
36
- description: post.description,
37
- commentCount: post.comment_count,
38
- isFavorited: post.is_favorited,
39
- hasNotes: post.has_notes,
40
- duration: post.duration,
41
- };
42
- }
43
- exports.parsePost = parsePost;
@@ -1,63 +0,0 @@
1
- export interface UserResponse {
2
- wiki_page_version_count: number;
3
- artist_version_count: number;
4
- pool_version_count: number;
5
- forum_post_count: number;
6
- comment_count: number;
7
- flag_count: number;
8
- favorite_count: number;
9
- positive_feedback_count: number;
10
- neutral_feedback_count: number;
11
- negative_feedback_count: number;
12
- upload_limit: number;
13
- profile_about: string;
14
- profile_artinfo: string;
15
- id: number;
16
- created_at: string;
17
- name: string;
18
- level: number;
19
- base_upload_limit: number;
20
- post_upload_count: number;
21
- post_update_count: number;
22
- note_update_count: number;
23
- is_banned: boolean;
24
- can_approve_posts: boolean;
25
- can_upload_free: boolean;
26
- level_string: string;
27
- avatar_id: number | null;
28
- }
29
- export interface E621User {
30
- id: number;
31
- profile: {
32
- name: string;
33
- level: number;
34
- about: string;
35
- artInfo: string;
36
- avatarId: number | null;
37
- createdAt: Date;
38
- levelString: string;
39
- };
40
- counts: {
41
- wikiPageVersion: number;
42
- artistVersion: number;
43
- poolVersion: number;
44
- forumPost: number;
45
- comment: number;
46
- flag: number;
47
- favorite: number;
48
- positiveFeedback: number;
49
- neutralFeedback: number;
50
- negativeFeedback: number;
51
- };
52
- limits: {
53
- upload: number;
54
- baseUpload: number;
55
- postUpload: number;
56
- postUpdate: number;
57
- noteUpdate: number;
58
- };
59
- isBanned: boolean;
60
- canApprovePosts: boolean;
61
- canUploadFree: boolean;
62
- }
63
- export declare function parseUser(user: UserResponse): E621User;
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseUser = void 0;
4
- function parseUser(user) {
5
- return {
6
- id: user.id,
7
- profile: {
8
- name: user.name,
9
- level: user.level,
10
- about: user.profile_about,
11
- artInfo: user.profile_artinfo,
12
- avatarId: user.avatar_id,
13
- createdAt: new Date(user.created_at),
14
- levelString: user.level_string,
15
- },
16
- counts: {
17
- wikiPageVersion: user.wiki_page_version_count,
18
- artistVersion: user.artist_version_count,
19
- poolVersion: user.pool_version_count,
20
- forumPost: user.forum_post_count,
21
- comment: user.comment_count,
22
- flag: user.flag_count,
23
- favorite: user.favorite_count,
24
- positiveFeedback: user.positive_feedback_count,
25
- neutralFeedback: user.neutral_feedback_count,
26
- negativeFeedback: user.negative_feedback_count,
27
- },
28
- limits: {
29
- upload: user.upload_limit,
30
- baseUpload: user.base_upload_limit,
31
- postUpload: user.post_upload_count,
32
- postUpdate: user.post_update_count,
33
- noteUpdate: user.note_update_count,
34
- },
35
- isBanned: user.is_banned,
36
- canApprovePosts: user.can_approve_posts,
37
- canUploadFree: user.can_upload_free,
38
- };
39
- }
40
- exports.parseUser = parseUser;
@@ -1,10 +0,0 @@
1
- import { PostResponse } from "./e621/E621Post";
2
- export type GetFavoritesResponse = {
3
- posts: PostResponse[] | null;
4
- };
5
- export type GetPostResponse = {
6
- post: PostResponse | null;
7
- };
8
- export type GetPostsResponse = {
9
- posts: PostResponse[];
10
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare function ensureArray<T>(value: T | T[]): T[];
2
- export declare function translateOptions<Options extends Record<string, string>>(options: Options, table: Partial<Options>): Record<string, string>;
package/dist/utils.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translateOptions = exports.ensureArray = void 0;
4
- function ensureArray(value) {
5
- if (Array.isArray(value)) {
6
- return value;
7
- }
8
- return [value];
9
- }
10
- exports.ensureArray = ensureArray;
11
- function translateOptions(options, table) {
12
- var _a;
13
- const params = {};
14
- for (const [key, value] of Object.entries(options)) {
15
- params[(_a = table[key]) !== null && _a !== void 0 ? _a : key] = value;
16
- }
17
- return params;
18
- }
19
- exports.translateOptions = translateOptions;