Skip to content

Commit ddeb3aa

Browse files
committed
added types
1 parent 073f089 commit ddeb3aa

File tree

7 files changed

+176
-65
lines changed

7 files changed

+176
-65
lines changed

src/Base.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface BundleOptions {
1212
default?: (name: string) => Function;
1313
}
1414

15-
export type QueryReturns = Promise<Record<string, unknown>|Error|null>;
15+
export type APIResponse = Promise<Record<string, unknown>|Error|null>;
1616

1717
export default class Base {
1818
options: Options;
@@ -30,22 +30,23 @@ export default class Base {
3030
return this.options.agent || Base.defaultAgent;
3131
}
3232

33-
async query(url: string, cb?: ((r: AxiosResponse) => Promise<Record<string, unknown>>)): QueryReturns {
33+
async query(url: string, cb?: ((r: AxiosResponse) => Promise<Record<string, unknown>>)): APIResponse {
3434
if (!cb) cb = r => r.data;
3535
const ops: AxiosRequestConfig = this.options?.axiosOptions || {};
3636
if (!ops.headers) ops.headers = {}
3737
ops.headers['User-Agent'] = this.options?.agent || Base.defaultAgent
38+
if (!ops.timeout) ops.timeout = 10000;
3839
if (this.token) ops.headers['Authorization'] = `Bearer ${this.token}`
3940
return await axios.get(url, ops)
4041
.then(cb)
4142
.catch(e => e)
4243
}
4344

4445
/* eslint-disable @typescript-eslint/ban-types */
45-
static bundle(base: Function, items: Record<string, string>, options?: BundleOptions): (options?: Options) => QueryReturns {
46+
static bundle(base: Function, items: Record<string, string>, options?: BundleOptions): (options?: Options) => APIResponse {
4647
const bundler: Record<string, Record<string, Function>> = {};
4748
Object.keys(items).forEach((name) => {
48-
bundler[name] = { value: options?.default ? options.default(items[name]) : (async function (options?: Options): QueryReturns {
49+
bundler[name] = { value: options?.default ? options.default(items[name]) : (async function (options?: Options): APIResponse {
4950
return await base(items[name], options)
5051
}) }
5152
})

src/e6.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Base, { Options, QueryReturns } from './Base';
1+
import Base, { Options, APIResponse } from './Base';
22

33
export type Query = Array<string> | string;
44

@@ -7,7 +7,7 @@ export interface E6Options extends Options {
77
overrideTags?: string;
88
}
99

10-
async function E6Base(endpoint: string, query?: Query, options?: E6Options): QueryReturns {
10+
async function E6Base(endpoint: string, query?: Query, options?: E6Options): APIResponse {
1111
if (typeof query === 'undefined') query = '';
1212
if (Array.isArray(query)) query = query.join(' ')
1313
const base = new Base(options);
@@ -19,12 +19,37 @@ async function E6Base(endpoint: string, query?: Query, options?: E6Options): Que
1919
return await base.query(url, r => r.data?.posts[0]);
2020
}
2121

22-
export default Base.bundle(E6Base, {
22+
/**
23+
* Uses the E621 API to retrieve data of a random post {@link https://e621.net}
24+
* @param options - Library options
25+
* @returns - The response from the API
26+
*/
27+
declare function E621(options?: Options): APIResponse;
28+
29+
/**
30+
* Uses the E926 API to retrieve data of a random post {@link https://e926.net}
31+
* @param options - Library options
32+
* @returns - The response from the API
33+
*/
34+
declare function E926(options?: Options): APIResponse;
35+
36+
export declare interface E6 {
37+
/**
38+
* Uses the E621 API to retrieve data of a random post {@link https://e621.net}
39+
* @param options - Library options
40+
* @returns - The response from the API
41+
*/
42+
(options?: Options): APIResponse;
43+
nsfw: typeof E621;
44+
sfw: typeof E926;
45+
}
46+
47+
export default <E6>Base.bundle(E6Base, {
2348
nsfw: 'e621',
2449
sfw: 'e926'
2550
}, {
2651
default: (name) => {
27-
return async function (query: Query, options?: E6Options): QueryReturns {
52+
return async function (query: Query, options?: E6Options): APIResponse {
2853
return await E6Base(name, query, options)
2954
}
3055
}

src/fox.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import Base, { Options, QueryReturns } from './Base';
1+
import Base, { Options, APIResponse } from './Base';
22

3-
async function Fox(options?: Options): QueryReturns {
3+
/**
4+
* Uses the RandomFox API to retrieve an image of a fox {@link https://randomfox.ca}
5+
* @param options - Library options
6+
* @returns - A random fox image
7+
*/
8+
async function Fox(options?: Options): APIResponse {
49
const base = new Base(options);
510

611
return await base.query('https://randomfox.ca/floof');

0 commit comments

Comments
 (0)