Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: new RESTClient #52

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -63,13 +63,14 @@ rules:
- 2
- order:
- "[alphabetical-properties]"
- "[alphabetical-private-properties]"
- constructor
- update
- "[alphabetical-getters]"
- "[alphabetical-methods]"
- "[alphabetical-conventional-private-methods]"
- "[everything-else]"
- "[alphabetical-private-methods]"
- "[everything-else]"
- "[custom-inspect-method]"
- toString
- toJSON
@@ -89,6 +90,11 @@ rules:
sort: alphabetical
alphabetical-private-methods:
- type: method
private: true
sort: alphabetical
alphabetical-private-properties:
- type: property
private: true
sort: alphabetical
custom-inspect-method:
- name: "[util.inspect.custom]"
1 change: 1 addition & 0 deletions esm.mjs
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ export const {
PrivateThreadChannel,
PublicThreadChannel,
RequestHandler,
RESTClient,
Role,
SequentialBucket,
Shard,
531 changes: 486 additions & 45 deletions index.d.ts

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -39,8 +39,9 @@ Dysnomia.PrivateChannel = require("./lib/structures/PrivateChannel");
Dysnomia.PrivateThreadChannel = require("./lib/structures/PrivateThreadChannel");
Dysnomia.PublicThreadChannel = require("./lib/structures/PublicThreadChannel");
Dysnomia.RequestHandler = require("./lib/rest/RequestHandler");
Dysnomia.RESTClient = require("./lib/rest/RESTClient");
Dysnomia.Role = require("./lib/structures/Role");
Dysnomia.SequentialBucket = require("./lib/util/SequentialBucket");
Dysnomia.SequentialBucket = require("./lib/rest/SequentialBucket");
Dysnomia.Shard = require("./lib/gateway/Shard");
Dysnomia.SharedStream = require("./lib/voice/SharedStream");
Dysnomia.StageChannel = require("./lib/structures/StageChannel");
1,618 changes: 513 additions & 1,105 deletions lib/Client.js

Large diffs are not rendered by default.

35 changes: 5 additions & 30 deletions lib/errors/DiscordHTTPError.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
"use strict";

class DiscordHTTPError extends Error {
constructor(req, res, response, stack) {
super();
constructor(req, res, stack) {
super(`${res.status} ${res.statusText || "Unknown error"} on ${req.method} ${req.path}`);

Object.defineProperty(this, "req", {
enumerable: false,
value: req
});

Object.defineProperty(this, "res", {
enumerable: false,
value: res
});
Object.defineProperty(this, "response", {
enumerable: false,
value: response
});

Object.defineProperty(this, "code", {
enumerable: false,
value: res.statusCode
});
let message = `${res.statusCode} ${res.statusMessage} on ${req.method} ${req.path}`;
const errors = this.flattenErrors(response);
if(errors.length > 0) {
message += "\n " + errors.join("\n ");
}
Object.defineProperty(this, "message", {
enumerable: false,
value: message
value: res.status
});

if(stack) {
@@ -39,25 +27,12 @@ class DiscordHTTPError extends Error {
}

get headers() {
return this.response.headers;
return this.res.headers;
}

get name() {
return this.constructor.name;
}

flattenErrors(errors, keyPrefix = "") {
let messages = [];
for(const fieldName in errors) {
if(!errors.hasOwnProperty(fieldName) || fieldName === "message" || fieldName === "code") {
continue;
}
if(Array.isArray(errors[fieldName])) {
messages = messages.concat(errors[fieldName].map((str) => `${keyPrefix + fieldName}: ${str}`));
}
}
return messages;
}
}

module.exports = DiscordHTTPError;
2 changes: 1 addition & 1 deletion lib/rest/Endpoints.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

const {REST_VERSION} = require("../Constants");

module.exports.BASE_URL = "/api/v" + REST_VERSION;
module.exports.BASE_URL = "https://discord.com/api/v" + REST_VERSION;
module.exports.CDN_URL = "https://cdn.discordapp.com";
module.exports.CLIENT_URL = "https://discord.com";

Loading