revolt-api
Advanced tools
Comparing version 0.5.3-rc.1 to 0.5.3-rc.1-patch.0
@@ -1,4 +0,27 @@ | ||
export * from './types'; | ||
import Axios from 'axios'; | ||
import { pathResolve, queryParams } from './params'; | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.API = exports.getPathName = void 0; | ||
// This file was auto-generated by @insertish/oapi! | ||
const axios_1 = __importDefault(require("axios")); | ||
const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep")); | ||
__exportStar(require("./types"), exports); | ||
const baseURL_1 = require("./baseURL"); | ||
const params_1 = require("./params"); | ||
/** | ||
@@ -9,5 +32,6 @@ * Get the specific path name of any given path. | ||
*/ | ||
export function getPathName(anyPath) { | ||
function getPathName(anyPath) { | ||
var _a; | ||
const segments = anyPath.split('/'); | ||
const list = pathResolve[(segments.length - 1).toString()] ?? []; | ||
const list = (_a = params_1.pathResolve[(segments.length - 1).toString()]) !== null && _a !== void 0 ? _a : []; | ||
for (const entry of list) { | ||
@@ -28,22 +52,49 @@ let i = 1; | ||
} | ||
exports.getPathName = getPathName; | ||
/** | ||
* Revolt API Client | ||
* API Client | ||
*/ | ||
export class RevoltAPI { | ||
class API { | ||
constructor({ baseURL, authentication } = {}) { | ||
this.baseURL = baseURL ?? 'https://api.revolt.chat'; | ||
this.authentication = authentication; | ||
this.baseURL = baseURL !== null && baseURL !== void 0 ? baseURL : baseURL_1.defaultBaseURL; | ||
this.authentication = authentication !== null && authentication !== void 0 ? authentication : {}; | ||
} | ||
/** | ||
* Generate authentication options. | ||
*/ | ||
get auth() { | ||
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() { | ||
return { | ||
baseURL: this.baseURL, | ||
headers: typeof this.authentication === 'string' | ||
? { 'X-Bot-Token': this.authentication } | ||
: typeof this.authentication === 'object' | ||
? { 'X-Session-Token': this.authentication.token } | ||
: undefined | ||
}; | ||
return Object.assign({ baseURL: this.baseURL }, this.auth); | ||
} | ||
@@ -63,3 +114,3 @@ /** | ||
if (named && typeof params === 'object') { | ||
const route = queryParams[named]; | ||
const route = params_1.queryParams[named]; | ||
const allowed_query = route[method]; | ||
@@ -69,22 +120,14 @@ // Map each parameter to the correct object. | ||
if (allowed_query.includes(parameter)) { | ||
query = { | ||
...(query ?? {}), | ||
[parameter]: params[parameter] | ||
}; | ||
query = Object.assign(Object.assign({}, (query !== null && query !== void 0 ? query : {})), { [parameter]: params[parameter] }); | ||
} | ||
else { | ||
body = { | ||
...(body ?? {}), | ||
[parameter]: params[parameter] | ||
}; | ||
body = Object.assign(Object.assign({}, (body !== null && body !== void 0 ? body : {})), { [parameter]: params[parameter] }); | ||
} | ||
} | ||
} | ||
return Axios(path, { | ||
...this.config, | ||
...config, | ||
return (0, axios_1.default)(path, (0, lodash_defaultsdeep_1.default)({ | ||
method, | ||
params: query, | ||
data: body | ||
}) | ||
}, (0, lodash_defaultsdeep_1.default)(config, this.config))) | ||
.then(res => res.data); | ||
@@ -108,1 +151,2 @@ } | ||
} | ||
exports.API = API; |
{ | ||
"name": "revolt-api", | ||
"version": "0.5.3-rc.1", | ||
"description": "Revolt API typings", | ||
"version": "0.5.3-rc.1-patch.0", | ||
"description": "Revolt API Library", | ||
"main": "dist/index.js", | ||
"type": "module", | ||
"homepage": "https://developers.revolt.chat", | ||
@@ -12,16 +11,16 @@ "repository": "https://gitlab.insrt.uk/revolt/api.git", | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc-watch --onSuccess \"node --experimental-specifier-resolution=node dist\"", | ||
"routes": "node scripts/generate_routes.js", | ||
"exports": "node scripts/generate_exports.js", | ||
"convert": "openapi-typescript OpenAPI.json --output src/schema.ts && node scripts/generate_routes.js && node scripts/generate_exports.js" | ||
"build": "oapilib && tsc", | ||
"prepublish": "in-publish && yarn build || echo Skipping build." | ||
}, | ||
"devDependencies": { | ||
"tsc-watch": "^4.4.0", | ||
"typescript": "^4.3.5" | ||
"@types/lodash.defaultsdeep": "^4.6.6", | ||
"in-publish": "^2.0.1", | ||
"openapi-typescript": "^5.2.0", | ||
"typescript": "^4.6.2" | ||
}, | ||
"dependencies": { | ||
"@insertish/oapi": "^0.1.6", | ||
"axios": "^0.26.1", | ||
"openapi-typescript": "^5.2.0" | ||
"lodash.defaultsdeep": "^4.6.1" | ||
} | ||
} |
@@ -5,26 +5,36 @@ # Revolt API | ||
This package contains typings for objects in the [Revolt API](https://developers.revolt.chat/api/) and code for generating the OpenAPI specification. | ||
This package contains typings for objects in the [Revolt API](https://developers.revolt.chat/api/) and a fully typed API request builder. | ||
For most cases, if not all, you should only be concerned with `revolt-api/types`. | ||
### Example Usage | ||
If you just need access to types: | ||
```typescript | ||
import type { User } from 'revolt-api/types/Users'; | ||
import type { User } from 'revolt-api'; | ||
``` | ||
### Tip (for development) | ||
If you want to send requests: | ||
For faster compile times when working on API routes, comment out the categories you don't care about. | ||
```typescript | ||
import { API } from 'revolt-api'; | ||
```ts | ||
/// src/routes/index.ts | ||
export async function load() { | ||
// await import('./core.js'); | ||
// await import('./users.js'); | ||
// await import('./channels.js'); | ||
await import('./servers.js'); | ||
} | ||
// Initialise a new API client: | ||
const client = new API(); | ||
await load(); | ||
// or with authentication: | ||
const client = new API({ authentication: { revolt: 'bot-token' } }); | ||
// Make requests with ease: | ||
client.get('/users/@me') | ||
// Fully typed responses! | ||
.then(user => user.username); | ||
// No need to worry about the details: | ||
let channel_id = "some channel id"; | ||
client.post(`/channels/${channel_id}/messages`, { | ||
// Parameters given are fully typed as well! | ||
content: "some content" | ||
}); | ||
``` | ||
For more details on how this works, see the [README of @insertish/oapi](https://github.com/insertish/oapi#example). |
@@ -0,7 +1,11 @@ | ||
// 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 { Session } from './types'; | ||
import type { APIRoutes } from './routes'; | ||
import Axios, { AxiosRequestConfig } from 'axios'; | ||
import { defaultBaseURL } from './baseURL'; | ||
import { pathResolve, queryParams } from './params'; | ||
@@ -45,3 +49,3 @@ | ||
*/ | ||
export interface Options { | ||
export interface Options { | ||
/** | ||
@@ -54,18 +58,55 @@ * Base URL of the Revolt node | ||
*/ | ||
authentication: Session | string | undefined; | ||
authentication: { | ||
rauth?: string | undefined; | ||
revolt?: { token: string } | string | undefined; | ||
}; | ||
} | ||
/** | ||
* Revolt API Client | ||
* API Client | ||
*/ | ||
export class RevoltAPI { | ||
private baseURL: string; | ||
private authentication: Session | string | undefined; | ||
export class API { | ||
private baseURL: Options['baseURL']; | ||
private authentication: Options['authentication']; | ||
constructor({ baseURL, authentication }: Partial<Options> = { }) { | ||
this.baseURL = baseURL ?? 'https://api.revolt.chat'; | ||
this.authentication = authentication; | ||
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. | ||
@@ -76,7 +117,3 @@ */ | ||
baseURL: this.baseURL, | ||
headers: typeof this.authentication === 'string' | ||
? { 'X-Bot-Token': this.authentication } | ||
: typeof this.authentication === 'object' | ||
? { 'X-Session-Token': this.authentication.token } | ||
: undefined | ||
...this.auth, | ||
}; | ||
@@ -118,9 +155,10 @@ } | ||
return Axios(path, { | ||
...this.config, | ||
...config, | ||
return Axios(path, defaultsDeep({ | ||
method, | ||
params: query, | ||
data: body | ||
}) | ||
}, defaultsDeep( | ||
config, | ||
this.config | ||
))) | ||
.then(res => res.data); | ||
@@ -127,0 +165,0 @@ } |
@@ -1,3 +0,3 @@ | ||
// This file was auto-generated! | ||
// This file was auto-generated by @insertish/oapi! | ||
export const pathResolve = {"1":[[""]],"2":[["users","@me"],["users",["{target}"]],["users","dms"],["bots","create"],["bots",["{target}"]],["bots","@me"],["channels",["{target}"]],["channels","create"],["servers","create"],["servers",["{target}"]],["invites",["{target}"]],["onboard","hello"],["onboard","complete"],["push","subscribe"],["push","unsubscribe"],["sync","unreads"]],"3":[["users","@me","username"],["users",["{target}"],"default_avatar"],["users",["{target}"],"profile"],["users",["{target}"],"dm"],["users",["{target}"],"mutual"],["users",["{username}"],"friend"],["users",["{target}"],"friend"],["users",["{target}"],"block"],["bots",["{target}"],"invite"],["channels",["{target}"],"members"],["channels",["{target}"],"invites"],["channels",["{target}"],"messages"],["channels",["{target}"],"search"],["channels",["{_target}"],"join_call"],["servers",["{target}"],"ack"],["servers",["{target}"],"channels"],["servers",["{target}"],"members"],["servers",["{target}"],"bans"],["servers",["{target}"],"invites"],["servers",["{target}"],"roles"],["auth","account","create"],["auth","account","reverify"],["auth","account",""],["auth","account","reset_password"],["auth","session","login"],["auth","session","logout"],["auth","session","all"],["auth","session",["{id}"]],["sync","settings","fetch"],["sync","settings","set"]],"4":[["channels",["{target}"],"ack",["{message}"]],["channels",["{_target}"],"messages","stale"],["channels",["{target}"],"messages",["{msg}"]],["channels",["{target}"],"recipients",["{member}"]],["channels",["{target}"],"permissions",["{role_id}"]],["channels",["{target}"],"permissions","default"],["servers",["{target}"],"members",["{member}"]],["servers",["{server}"],"members",["{target}"]],["servers",["{server}"],"bans",["{target}"]],["servers",["{target}"],"roles",["{role_id}"]],["servers",["{target}"],"permissions",["{role_id}"]],["servers",["{target}"],"permissions","default"],["auth","account","change","password"],["auth","account","change","email"],["auth","account","verify",["{code}"]]]}; | ||
export const queryParams = {"/":{"get":[]},"/users/@me":{"get":[],"patch":[]},"/users/{target}":{"get":[]},"/users/@me/username":{"patch":[]},"/users/{target}/default_avatar":{"get":[]},"/users/{target}/profile":{"get":[]},"/users/dms":{"get":[]},"/users/{target}/dm":{"get":[]},"/users/{target}/mutual":{"get":[]},"/users/{username}/friend":{"put":[]},"/users/{target}/friend":{"delete":[]},"/users/{target}/block":{"put":[],"delete":[]},"/bots/create":{"post":[]},"/bots/{target}/invite":{"get":[],"post":[]},"/bots/{target}":{"get":[],"delete":[],"patch":[]},"/bots/@me":{"get":[]},"/channels/{target}/ack/{message}":{"put":[]},"/channels/{target}":{"get":[],"delete":[],"patch":[]},"/channels/{target}/members":{"get":[]},"/channels/{target}/invites":{"post":[]},"/channels/{target}/messages":{"get":["limit","before","after","sort","nearby","include_users"],"post":[]},"/channels/{target}/search":{"post":[]},"/channels/{_target}/messages/stale":{"post":[]},"/channels/{target}/messages/{msg}":{"get":[],"delete":[],"patch":[]},"/channels/create":{"post":[]},"/channels/{target}/recipients/{member}":{"put":[],"delete":[]},"/channels/{_target}/join_call":{"post":[]},"/channels/{target}/permissions/{role_id}":{"put":[]},"/channels/{target}/permissions/default":{"put":[]},"/servers/create":{"post":[]},"/servers/{target}":{"get":[],"delete":[],"patch":[]},"/servers/{target}/ack":{"put":[]},"/servers/{target}/channels":{"post":[]},"/servers/{target}/members":{"get":[]},"/servers/{target}/members/{member}":{"get":[],"delete":[]},"/servers/{server}/members/{target}":{"patch":[]},"/servers/{server}/bans/{target}":{"put":[],"delete":[]},"/servers/{target}/bans":{"get":[]},"/servers/{target}/invites":{"get":[]},"/servers/{target}/roles":{"post":[]},"/servers/{target}/roles/{role_id}":{"delete":[],"patch":[]},"/servers/{target}/permissions/{role_id}":{"put":[]},"/servers/{target}/permissions/default":{"put":[]},"/invites/{target}":{"get":[],"post":[],"delete":[]},"/auth/account/create":{"post":[]},"/auth/account/reverify":{"post":[]},"/auth/account/":{"get":[]},"/auth/account/change/password":{"patch":[]},"/auth/account/change/email":{"patch":[]},"/auth/account/verify/{code}":{"post":[]},"/auth/account/reset_password":{"post":[],"patch":[]},"/auth/session/login":{"post":[]},"/auth/session/logout":{"post":[]},"/auth/session/all":{"get":[],"delete":["revoke_self"]},"/auth/session/{id}":{"delete":[],"patch":[]},"/onboard/hello":{"get":[]},"/onboard/complete":{"post":[]},"/push/subscribe":{"post":[]},"/push/unsubscribe":{"post":[]},"/sync/settings/fetch":{"post":[]},"/sync/settings/set":{"post":["timestamp"]},"/sync/unreads":{"get":[]}}; |
@@ -1,2 +0,2 @@ | ||
// This file was auto-generated! | ||
// This file was auto-generated by @insertish/oapi! | ||
import { paths } from './schema'; | ||
@@ -3,0 +3,0 @@ export type APIRoutes = |
@@ -0,1 +1,2 @@ | ||
// This file was auto-generated by @insertish/oapi! | ||
import { components } from './schema'; | ||
@@ -2,0 +3,0 @@ export type RevoltConfig = components['schemas']['RevoltConfig']; |
@@ -7,4 +7,4 @@ { | ||
// "incremental": true, /* Enable incremental compilation */ | ||
"target": "es2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ | ||
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ | ||
"target": "es2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ | ||
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ | ||
// "lib": [], /* Specify library files to be included in the compilation. */ | ||
@@ -48,3 +48,3 @@ // "allowJs": true, /* Allow javascript files to be compiled. */ | ||
/* Module Resolution Options */ | ||
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ | ||
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ | ||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ | ||
@@ -51,0 +51,0 @@ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12182
40
1
0
354121
3
4
16
No
+ Added@insertish/oapi@^0.1.6
+ Addedlodash.defaultsdeep@^4.6.1
+ Added@insertish/oapi@0.1.18(transitive)
+ Addedlodash.defaultsdeep@4.6.1(transitive)
+ Addedtypescript@4.9.5(transitive)
- Removedopenapi-typescript@^5.2.0