// This file was auto-generated by @insertish/oapi! import Axios from 'axios'; import type { AxiosRequestConfig } from 'axios'; import defaultsDeep from 'lodash.defaultsdeep'; export * from './types'; import type { APIRoutes } from './routes'; import { defaultBaseURL } from './baseURL'; import { pathResolve, queryParams } from './params'; type Methods = APIRoutes['method']; type PickRoutes = APIRoutes & { method: Method }; type GetRoutes = PickRoutes<'get'>; type PatchRoutes = PickRoutes<'patch'>; type PutRoutes = PickRoutes<'put'>; type DeleteRoutes = PickRoutes<'delete'>; type PostRoutes = PickRoutes<'post'>; type Count = Str extends `${infer _}${SubStr}${infer After}` ? Count : Matches['length']; /** * Get the specific path name of any given path. * @param anyPath Any path * @returns Specific path */ export function getPathName(anyPath: string) { const segments = anyPath.split('/'); const list = (pathResolve as unknown as Record)[(segments.length - 1).toString()] || []; for (const entry of list) { let i = 1; let copy = [...segments]; for (i;i = { }) { this.baseURL = baseURL || defaultBaseURL; this.authentication = authentication || { }; } /** * Generate authentication options. */ get auth(): AxiosRequestConfig { if (this.authentication.rauth) { if (typeof this.authentication.rauth === 'string') { return { headers: { 'X-Session-Token': this.authentication.rauth } } } } else if (this.authentication.revolt) { switch (typeof this.authentication.revolt) { case 'string': { return { headers: { 'X-Bot-Token': this.authentication.revolt } } } case 'object': { return { headers: { 'X-Session-Token': this.authentication.revolt.token } } } } } return { }; } /** * Generate config to pass through to API. */ get config(): AxiosRequestConfig { return { baseURL: this.baseURL, ...this.auth, }; } /** * Send any arbitrary request. * @param method HTTP Method * @param path Path * @param params Body or Query Parameters * @param config Axios configuration * @returns Typed Response Data */ req, Path extends Routes['path'], Route extends Routes & { path: Path, parts: Count }>(method: Method, path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise { let query, body; let named = getPathName(path); // If we are aware of this route, then match the parameters given. if (named && typeof params === 'object') { const route = queryParams[named as keyof typeof queryParams]; const allowed_query = (route as unknown as Record)[method]; // Map each parameter to the correct object. for (const parameter of Object.keys(params)) { if (allowed_query?.includes(parameter)) { query = { ...(query || {}), [parameter]: (params as Record)[parameter] }; } else { body = { ...(body || {}), [parameter]: (params as Record)[parameter] }; } } } return Axios(path, defaultsDeep({ method, params: query, data: body }, defaultsDeep( config, this.config ))) .then(res => res.data); } /** * Send HTTP GET request. * @param path Path * @param params Body or Query Parameters * @param config Axios configuration * @returns Typed Response Data */ get }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise; /** * Send HTTP GET request. * @param path Path * @returns Typed Response Data */ get }>(path: Path): Promise; get(path: any, params?: any, config?: AxiosRequestConfig): Promise { // @ts-ignore-next-line return this.req('get', path, params, config); } /** * Send HTTP PATCH request. * @param path Path * @param params Body or Query Parameters * @param config Axios configuration * @returns Typed Response Data */ patch }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise; /** * Send HTTP PATCH request. * @param path Path * @returns Typed Response Data */ patch }>(path: Path): Promise; patch(path: any, params?: any, config?: AxiosRequestConfig): Promise { // @ts-ignore-next-line return this.req('patch', path, params, config); } /** * Send HTTP PUT request. * @param path Path * @param params Body or Query Parameters * @param config Axios configuration * @returns Typed Response Data */ put }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise; /** * Send HTTP PUT request. * @param path Path * @returns Typed Response Data */ put }>(path: Path): Promise; put(path: any, params?: any, config?: AxiosRequestConfig): Promise { // @ts-ignore-next-line return this.req('put', path, params, config); } /** * Send HTTP DELETE request. * @param path Path * @param params Body or Query Parameters * @param config Axios configuration * @returns Typed Response Data */ delete }>(path: Path, params?: any, config?: AxiosRequestConfig): Promise; /** * Send HTTP DELETE request. * @param path Path * @param params Body or Query Parameters * @returns Typed Response Data */ delete }>(path: Path, params?: any): Promise; delete(path: any, params?: any, config?: AxiosRequestConfig): Promise { // @ts-ignore-next-line return this.req('delete', path, params, config); } /** * Send HTTP POST request. * @param path Path * @param params Body or Query Parameters * @param config Axios configuration * @returns Typed Response Data */ post }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise; /** * Send HTTP POST request. * @param path Path * @returns Typed Response Data */ post }>(path: Path): Promise; post(path: any, params?: any, config?: AxiosRequestConfig): Promise { // @ts-ignore-next-line return this.req('post', path, params, config); } }