UNPKG

@re621/zestyapi

Version:

Comprehensive JS wrapper for e621.net API

87 lines (86 loc) 3.57 kB
import APIResponse from "../responses/APIResponse"; import ZestyAPI from "../ZestyAPI"; import { FormattedResponse, ResponseStatus } from "./RequestQueue"; import { PrimitiveMap, StringMap } from "./UtilType"; export default class Endpoint<T extends APIResponse> { protected api: ZestyAPI; protected endpoint: string; protected searchParams: string[]; protected searchParamAliases: { [prop: string]: string; }; constructor(api: ZestyAPI); find(search?: SearchParams): Promise<FormattedResponse<T>>; /** * Parses the SearchParams and finds values that * need to be split off into QueryParams. * @param {SearchParams} search Search params * @returns {QueryParams} Query params */ protected splitQueryParams(search?: SearchParams): QueryParams; /** * Validates both sets of parameters and returns a prepared * map that can be plugged into `flattenParams()`. * @param {SearchParams} search Search parameters * @param {QueryParams} query Query parameters * @returns Validated results * @throws {MalformedRequestError} If the errors in the parameters are irreconcilable */ protected validateParams(search?: SearchParams, query?: QueryParams): PrimitiveMap; /** * Validates the search parameters for the `find()` methods. * @param {SearchParams} params Search parameters * @returns {SearchParams} Validated parameters */ protected validateSearchParams(params?: SearchParams): SearchParams; /** * Validates the query parameters for the `find()` methods. * @param {QueryParams} params Query parameters * @returns {QueryParams} Validated parameters */ protected validateQueryParams(params?: QueryParams): QueryParams; /** * Shortcut method for making a response in case the search parameters are malformed or missing * @param {bool} array True if the output expects an array, false otherwise * @returns API Response */ protected static makeMalformedRequestResponse(): Promise<FormattedResponse<any>>; /** * Converts a value in a SearchParams format to an object with string values * @param {PrimitiveMap} params Original object * @param {string} separator Array join separator * @param {StringMap} keyReplacement Substitutions for key names * @returns Flattened object */ protected static flattenParams(params: PrimitiveMap, separator?: string, keyReplacement?: StringMap): StringMap; /** * Validates the raw API response and returns a consistent response * @param {ResponseStatus} status First portion of the API response * @param {T} data Second part of the API response * @returns */ protected static formatAPIResponse<T extends APIResponse>(status: ResponseStatus, data: T[]): FormattedResponse<T>; } /** * Search parameters for the `find()` methods. * Empty by default. Extend this interface to add more. */ export interface SearchParams extends PrimitiveMap, QueryParams { } /** * Query parameters for the `find()` methods. * Include the result limit and page number common for all endpoints */ export interface QueryParams extends PrimitiveMap { /** * Number of posts on the page. * Number between 1 and 320, defaults to 75 */ limit?: number | 75 | 320; /** * Page number. Two possible formats: * - Number between 1 and 750, defaults to 1 * - String, prefixed with either `a` (after)` or `b` (before), followed by an ID */ page?: number | string; }