Skip to content

Commit

Permalink
feat: backport to axios for complatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Oct 7, 2022
1 parent 30d74f8 commit 43603f6
Show file tree
Hide file tree
Showing 5 changed files with 831 additions and 815 deletions.
783 changes: 0 additions & 783 deletions .yarn/releases/yarn-3.2.3.cjs

This file was deleted.

801 changes: 801 additions & 0 deletions .yarn/releases/yarn-3.2.4.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"

yarnPath: .yarn/releases/yarn-3.2.3.cjs
yarnPath: .yarn/releases/yarn-3.2.4.cjs
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xhayper/discord-rpc",
"version": "1.0.8",
"version": "1.0.9",
"description": "a fork of discordjs/RPC",
"main": "dist/index.js",
"author": "xhayper",
Expand Down Expand Up @@ -28,8 +28,8 @@
"prepare": "husky install"
},
"dependencies": {
"axios": "^1.1.2",
"discord-api-types": "^0.37.11",
"undici": "^5.10.0",
"ws": "^8.9.0"
},
"devDependencies": {
Expand All @@ -45,5 +45,5 @@
"engines": {
"node": ">=14.17.0"
},
"packageManager": "[email protected].3"
"packageManager": "[email protected].4"
}
54 changes: 26 additions & 28 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { APIApplication, OAuth2Scopes } from "discord-api-types/v10";
import type { HttpMethod, ResponseData } from "undici/types/dispatcher";
import { type FormatFunction, IPCTransport } from "./transport/IPC";
import { WebSocketTransport } from "./transport/WebSocket";
import type { TypedEmitter } from "./utils/TypedEmitter";
import { ClientUser } from "./structures/ClientUser";
import { request, type FormData } from "undici";
import axios, { AxiosResponse, Method } from "axios";
import { RPCError } from "./utils/RPCError";
import { EventEmitter } from "node:events";
import crypto from "node:crypto";
Expand Down Expand Up @@ -174,17 +173,18 @@ export class Client extends (EventEmitter as new () => TypedEmitter<ClientEvents
/**
* @hidden
*/
async fetch(
method: HttpMethod,
async fetch<R = any>(
method: Method | string,
path: string,
req?: { body?: string | Buffer | Uint8Array | null | FormData; query?: URLSearchParams; headers?: any }
): Promise<ResponseData> {
req?: { data?: any; query?: string; headers?: any }
): Promise<AxiosResponse<R>> {
const url = new URL(`https://discord.com/api${path}`);
if (req?.query) for (const [key, value] of req.query) url.searchParams.append(key, value);

return await request(url, {
return await axios({
url: url.toString(),
method,
body: req?.body ?? null,
data: req?.data ?? null,
headers: {
...(req?.headers ?? {}),
...(this.accessToken ? { Authorization: `${this.tokenType} ${this.accessToken}` } : {})
Expand Down Expand Up @@ -223,19 +223,19 @@ export class Client extends (EventEmitter as new () => TypedEmitter<ClientEvents
if (this.debug) console.log("CLIENT | Refreshing access token!");

this.hanleAccessTokenResponse(
await (
(
await this.fetch("POST", "/oauth2/token", {
body: new URLSearchParams({
data: new URLSearchParams({
client_id: this.clientId,
client_secret: this.clientSecret ?? "",
grant_type: "refresh_token",
refresh_token: this.refreshToken ?? ""
}).toString(),
}),
headers: {
"content-type": "application/x-www-form-urlencoded"
}
})
).body.json()
).data
);
}

Expand All @@ -260,18 +260,16 @@ export class Client extends (EventEmitter as new () => TypedEmitter<ClientEvents

if (options.useRPCToken) {
rpcToken = (
await (
await this.fetch("POST", "/oauth2/token/rpc", {
body: new URLSearchParams({
client_id: this.clientId,
client_secret: this.clientSecret ?? ""
}).toString(),
headers: {
"content-type": "application/x-www-form-urlencoded"
}
})
).body.json()
).rpc_token;
await this.fetch("POST", "/oauth2/token/rpc", {
data: new URLSearchParams({
client_id: this.clientId,
client_secret: this.clientSecret ?? ""
}),
headers: {
"content-type": "application/x-www-form-urlencoded"
}
})
).data.rpc_token;
}

const { code } = (
Expand All @@ -285,20 +283,20 @@ export class Client extends (EventEmitter as new () => TypedEmitter<ClientEvents
).data;

this.hanleAccessTokenResponse(
await (
(
await this.fetch("POST", "/oauth2/token", {
body: new URLSearchParams({
data: new URLSearchParams({
client_id: this.clientId,
client_secret: this.clientSecret ?? "",
redirect_uri: options.redirect_uri ?? "",
grant_type: "authorization_code",
code
}).toString(),
}),
headers: {
"content-type": "application/x-www-form-urlencoded"
}
})
).body.json()
).data
);
}

Expand Down

0 comments on commit 43603f6

Please sign in to comment.