revolt-api
Advanced tools
Comparing version
{ | ||
"name": "revolt-api", | ||
"version": "0.8.4", | ||
"version": "0.8.5", | ||
"description": "Revolt API Library", | ||
"main": "dist/index.js", | ||
"module": "esm/index.js", | ||
"type": "module", | ||
"module": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"homepage": "https://developers.revolt.chat", | ||
@@ -12,24 +13,19 @@ "repository": "https://github.com/revoltchat/api.git", | ||
"scripts": { | ||
"build": "REWRITE_ANYOF=1 oapilib && tsc && tsc -p tsconfig.esm.json", | ||
"prepublish": "in-publish && yarn build || echo Skipping build." | ||
"build": "REWRITE_ANYOF=1 oapilib && tsc", | ||
"prepublish": "in-publish && pnpm build || echo Skipping build." | ||
}, | ||
"devDependencies": { | ||
"@insertish/oapi": "0.1.18", | ||
"@types/lodash.defaultsdeep": "^4.6.6", | ||
"@insertish/oapi": "0.2.4", | ||
"in-publish": "^2.0.1", | ||
"openapi-typescript": "^5.2.0", | ||
"typescript": "^4.6.2" | ||
"openapi-typescript": "^5.4.2", | ||
"typescript": "^5.8.2" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.26.1", | ||
"lodash.defaultsdeep": "^4.6.1" | ||
}, | ||
"files": [ | ||
"src", | ||
"dist", | ||
"esm", | ||
"lib", | ||
"OpenAPI.json", | ||
"LICENSE", | ||
"README.md" | ||
] | ||
], | ||
"packageManager": "pnpm@8.13.1+sha512.758bebc6989cb6462c5138129840193b10c258ddcdb0ab43ee0ef9dd87784bc218d6e41cb5beeac362b8ce33f1f8b08025b599933c0bce82050a9a36b8474d43" | ||
} |
534
src/index.ts
// This file was auto-generated by @insertish/oapi! | ||
import Axios from 'axios'; | ||
import type { AxiosRequestConfig } from 'axios'; | ||
import type { APIRoutes } from "./routes.js"; | ||
import defaultsDeep from 'lodash.defaultsdeep'; | ||
export * from "./types.js"; | ||
export * from './types'; | ||
import type { APIRoutes } from './routes'; | ||
import { defaultBaseURL } from "./baseURL.js"; | ||
import { pathResolve, queryParams } from "./params.js"; | ||
import { defaultBaseURL } from './baseURL'; | ||
import { pathResolve, queryParams } from './params'; | ||
type Methods = APIRoutes['method']; | ||
type Methods = APIRoutes["method"]; | ||
type PickRoutes<Method extends Methods> = APIRoutes & { method: Method }; | ||
type GetRoutes = PickRoutes<'get'>; | ||
type PatchRoutes = PickRoutes<'patch'>; | ||
type PutRoutes = PickRoutes<'put'>; | ||
type DeleteRoutes = PickRoutes<'delete'>; | ||
type PostRoutes = PickRoutes<'post'>; | ||
type GetRoutes = PickRoutes<"get">; | ||
type PatchRoutes = PickRoutes<"patch">; | ||
type PutRoutes = PickRoutes<"put">; | ||
type DeleteRoutes = PickRoutes<"delete">; | ||
type PostRoutes = PickRoutes<"post">; | ||
type Count<Str extends string, SubStr extends string, Matches extends null[] = []> = | ||
Str extends `${infer _}${SubStr}${infer After}` ? Count<After, SubStr, [...Matches, null]> : Matches['length']; | ||
type Count< | ||
Str extends string, | ||
SubStr extends string, | ||
Matches extends null[] = [] | ||
> = Str extends `${infer _}${SubStr}${infer After}` | ||
? Count<After, SubStr, [...Matches, null]> | ||
: Matches["length"]; | ||
@@ -31,18 +32,20 @@ /** | ||
export function getPathName(anyPath: string) { | ||
const segments = anyPath.split('/'); | ||
const segments = anyPath.split("/"); | ||
const list = (pathResolve as unknown as Record<string, (string | [string])[]>)[(segments.length - 1).toString()] || []; | ||
for (const entry of list) { | ||
let i = 1; | ||
let copy = [...segments]; | ||
for (i;i<segments.length;i++) { | ||
if (Array.isArray(entry[i - 1])) { | ||
copy[i] = entry[i - 1]; | ||
continue; | ||
} | ||
else if (entry[i - 1] !== segments[i]) break; | ||
} | ||
const list = | ||
(pathResolve as unknown as Record<string, (string | [string])[]>)[ | ||
(segments.length - 1).toString() | ||
] || []; | ||
for (const entry of list) { | ||
let i = 1; | ||
let copy = [...segments]; | ||
for (i; i < segments.length; i++) { | ||
if (Array.isArray(entry[i - 1])) { | ||
copy[i] = entry[i - 1]; | ||
continue; | ||
} else if (entry[i - 1] !== segments[i]) break; | ||
} | ||
if (i === segments.length) return copy.join('/'); | ||
} | ||
if (i === segments.length) return copy.join("/"); | ||
} | ||
} | ||
@@ -53,221 +56,312 @@ | ||
*/ | ||
export interface Options { | ||
/** | ||
* Base URL of the Revolt node | ||
*/ | ||
baseURL: string; | ||
/** | ||
* Authentication used for requests | ||
*/ | ||
authentication: { | ||
rauth?: string | undefined; | ||
revolt?: { token: string } | string | undefined; | ||
}; | ||
export interface Options { | ||
/** | ||
* Base URL of the Revolt node | ||
*/ | ||
baseURL: string; | ||
/** | ||
* Additional headers to apply to requests | ||
*/ | ||
headers?: Record<string, any>; | ||
/** | ||
* Authentication used for requests | ||
*/ | ||
authentication: { | ||
rauth?: string | undefined; | ||
revolt?: { token: string } | string | undefined; | ||
headers?: Record<string, any>; | ||
}; | ||
} | ||
/** | ||
* Request options | ||
*/ | ||
export interface RequestOptions { | ||
/** | ||
* The base URL used for this request | ||
*/ | ||
baseURL?: string; | ||
/** | ||
* The type of response given | ||
*/ | ||
responseType?: "blob" | "json" | "text" | "arrayBuffer"; | ||
/** | ||
* Headers to apply for this request | ||
*/ | ||
headers?: Record<string, any>; | ||
} | ||
/** | ||
* API Client | ||
*/ | ||
export class API { | ||
private baseURL: Options['baseURL']; | ||
private authentication: Options['authentication']; | ||
private baseURL: Options["baseURL"]; | ||
private authentication: Options["authentication"]; | ||
private headers: Options["headers"]; | ||
constructor({ baseURL, authentication }: Partial<Options> = { }) { | ||
this.baseURL = baseURL || defaultBaseURL; | ||
this.authentication = authentication || { }; | ||
} | ||
constructor({ baseURL, authentication, headers }: Partial<Options> = {}) { | ||
this.baseURL = baseURL || defaultBaseURL; | ||
this.authentication = authentication || {}; | ||
this.headers = headers || {}; | ||
} | ||
/** | ||
* 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 { | ||
/** | ||
* Generate authentication options. | ||
*/ | ||
get auth(): Record<string, string> { | ||
if (this.authentication.rauth) { | ||
if (typeof this.authentication.rauth === "string") { | ||
return { | ||
baseURL: this.baseURL, | ||
...this.auth, | ||
"X-Session-Token": this.authentication.rauth, | ||
}; | ||
} | ||
} else if (this.authentication.revolt) { | ||
switch (typeof this.authentication.revolt) { | ||
case "string": { | ||
return { | ||
"X-Bot-Token": this.authentication.revolt, | ||
}; | ||
} | ||
case "object": { | ||
return { | ||
"X-Session-Token": this.authentication.revolt.token, | ||
}; | ||
} | ||
} | ||
} else if (this.authentication.headers) { | ||
return this.authentication.headers; | ||
} | ||
/** | ||
* 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<Method extends Methods, Routes extends PickRoutes<Method>, Path extends Routes['path'], Route extends Routes & { path: Path, parts: Count<Path, '/'> }>(method: Method, path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise<Route['response']> { | ||
let query, body; | ||
let named = getPathName(path); | ||
return {}; | ||
} | ||
// 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, string[]>)[method]; | ||
/** | ||
* Generate config to pass through to API. | ||
*/ | ||
get config(): RequestOptions { | ||
return { | ||
baseURL: this.baseURL, | ||
headers: { | ||
...this.auth, | ||
...this.headers, | ||
}, | ||
}; | ||
} | ||
// 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<any, any>)[parameter] | ||
}; | ||
} else { | ||
body = { | ||
...(body || {}), | ||
[parameter]: (params as Record<any, any>)[parameter] | ||
}; | ||
} | ||
} | ||
/** | ||
* Send any arbitrary request. | ||
* @param method HTTP Method | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Request configuration | ||
* @returns Typed Response Data | ||
*/ | ||
async req< | ||
Method extends Methods, | ||
Routes extends PickRoutes<Method>, | ||
Path extends Routes["path"], | ||
Route extends Routes & { path: Path; parts: Count<Path, "/"> } | ||
>( | ||
method: Method, | ||
path: Path, | ||
params: Route["params"], | ||
config?: RequestOptions | ||
): Promise<Route["response"]> { | ||
let query = new URLSearchParams(); | ||
let body = {} as Record<any, any>; | ||
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, string[]>)[ | ||
method | ||
]; | ||
// Map each parameter to the correct object. | ||
for (const parameter of Object.keys(params)) { | ||
// omit undefined values | ||
if (typeof (params as Record<any, any>)[parameter] !== "undefined") { | ||
if (allowed_query?.includes(parameter)) { | ||
query.append(parameter, (params as Record<any, any>)[parameter]); | ||
} else { | ||
body[parameter] = (params as Record<any, any>)[parameter]; | ||
} | ||
} | ||
} | ||
} | ||
const passbody = ["head", "get"].includes(method) | ||
? undefined | ||
: JSON.stringify(body); | ||
return Axios(path, defaultsDeep({ | ||
method, | ||
params: query, | ||
data: body | ||
}, defaultsDeep( | ||
config, | ||
this.config | ||
))) | ||
.then(res => res.data); | ||
let fetchpath = `${path}?${query.toString()}`; | ||
if (fetchpath.startsWith("/")) { | ||
fetchpath = (config?.baseURL || this.baseURL) + fetchpath; | ||
} | ||
/** | ||
* Send HTTP GET request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
get<Path extends GetRoutes['path'], Route extends GetRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise<Route['response']>; | ||
const fetchdata = await fetch(new URL(fetchpath).toString(), { | ||
method: method.toUpperCase(), | ||
headers: { | ||
...(config?.headers || {}), | ||
...(this.config.headers || {}), | ||
} as HeadersInit, | ||
body: passbody, | ||
}); | ||
return await fetchdata[config?.responseType || "json"](); | ||
} | ||
/** | ||
* Send HTTP GET request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
get<Path extends (GetRoutes & { params: undefined })['path'], Route extends GetRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path): Promise<Route['response']>; | ||
/** | ||
* Send HTTP GET request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
get< | ||
Path extends GetRoutes["path"], | ||
Route extends GetRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>( | ||
path: Path, | ||
params: Route["params"], | ||
config?: RequestOptions | ||
): Promise<Route["response"]>; | ||
get(path: any, params?: any, config?: AxiosRequestConfig): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req('get', path, params, config); | ||
} | ||
/** | ||
* Send HTTP GET request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
get< | ||
Path extends (GetRoutes & { params: undefined })["path"], | ||
Route extends GetRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>(path: Path): Promise<Route["response"]>; | ||
/** | ||
* Send HTTP PATCH request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
patch<Path extends PatchRoutes['path'], Route extends PatchRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise<Route['response']>; | ||
get(path: any, params?: any, config?: RequestOptions): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req("get", path, params, config); | ||
} | ||
/** | ||
* Send HTTP PATCH request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
patch<Path extends (PatchRoutes & { params: undefined })['path'], Route extends PatchRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path): Promise<Route['response']>; | ||
/** | ||
* Send HTTP PATCH request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
patch< | ||
Path extends PatchRoutes["path"], | ||
Route extends PatchRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>( | ||
path: Path, | ||
params: Route["params"], | ||
config?: RequestOptions | ||
): Promise<Route["response"]>; | ||
patch(path: any, params?: any, config?: AxiosRequestConfig): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req('patch', path, params, config); | ||
} | ||
/** | ||
* Send HTTP PATCH request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
patch< | ||
Path extends (PatchRoutes & { params: undefined })["path"], | ||
Route extends PatchRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>(path: Path): Promise<Route["response"]>; | ||
/** | ||
* Send HTTP PUT request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
put<Path extends PutRoutes['path'], Route extends PutRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise<Route['response']>; | ||
/** | ||
* Send HTTP PUT request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
put<Path extends (PutRoutes & { params: undefined })['path'], Route extends PutRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path): Promise<Route['response']>; | ||
patch(path: any, params?: any, config?: RequestOptions): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req("patch", path, params, config); | ||
} | ||
put(path: any, params?: any, config?: AxiosRequestConfig): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req('put', 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 extends PutRoutes["path"], | ||
Route extends PutRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>( | ||
path: Path, | ||
params: Route["params"], | ||
config?: RequestOptions | ||
): Promise<Route["response"]>; | ||
/** | ||
* Send HTTP DELETE request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
delete<Path extends DeleteRoutes['path'], Route extends DeleteRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path, params?: any, config?: AxiosRequestConfig): Promise<Route['response']>; | ||
/** | ||
* Send HTTP DELETE request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @returns Typed Response Data | ||
*/ | ||
delete<Path extends (DeleteRoutes & { params: undefined })['path'], Route extends DeleteRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path, params?: any): Promise<Route['response']>; | ||
/** | ||
* Send HTTP PUT request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
put< | ||
Path extends (PutRoutes & { params: undefined })["path"], | ||
Route extends PutRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>(path: Path): Promise<Route["response"]>; | ||
delete(path: any, params?: any, config?: AxiosRequestConfig): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req('delete', path, params, config); | ||
} | ||
put(path: any, params?: any, config?: RequestOptions): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req("put", 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 extends PostRoutes['path'], Route extends PostRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path, params: Route['params'], config?: AxiosRequestConfig): Promise<Route['response']>; | ||
/** | ||
* Send HTTP POST request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
post<Path extends (PostRoutes & { params: undefined })['path'], Route extends PostRoutes & { path: Path, parts: Count<Path, '/'> }>(path: Path): Promise<Route['response']>; | ||
/** | ||
* Send HTTP DELETE request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @param config Axios configuration | ||
* @returns Typed Response Data | ||
*/ | ||
delete< | ||
Path extends DeleteRoutes["path"], | ||
Route extends DeleteRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>( | ||
path: Path, | ||
params?: any, | ||
config?: RequestOptions | ||
): Promise<Route["response"]>; | ||
post(path: any, params?: any, config?: AxiosRequestConfig): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req('post', path, params, config); | ||
} | ||
/** | ||
* Send HTTP DELETE request. | ||
* @param path Path | ||
* @param params Body or Query Parameters | ||
* @returns Typed Response Data | ||
*/ | ||
delete< | ||
Path extends (DeleteRoutes & { params: undefined })["path"], | ||
Route extends DeleteRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>(path: Path, params?: any): Promise<Route["response"]>; | ||
delete(path: any, params?: any, config?: RequestOptions): Promise<any> { | ||
// @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 extends PostRoutes["path"], | ||
Route extends PostRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>( | ||
path: Path, | ||
params: Route["params"], | ||
config?: RequestOptions | ||
): Promise<Route["response"]>; | ||
/** | ||
* Send HTTP POST request. | ||
* @param path Path | ||
* @returns Typed Response Data | ||
*/ | ||
post< | ||
Path extends (PostRoutes & { params: undefined })["path"], | ||
Route extends PostRoutes & { path: Path; parts: Count<Path, "/"> } | ||
>(path: Path): Promise<Route["response"]>; | ||
post(path: any, params?: any, config?: RequestOptions): Promise<any> { | ||
// @ts-ignore-next-line | ||
return this.req("post", path, params, config); | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
-100%4
-20%Yes
NaN933328
-7.6%28
-17.65%25289
-20.53%1
Infinity%- Removed
- Removed
- Removed
- Removed
- Removed