Skip to content

Commit

Permalink
fix(rest): not sending Content-Length header when the body is undefin…
Browse files Browse the repository at this point in the history
…ed (#223)

* 🚑️ fix(rest): not sending Content-Length header when the body is undefined

tbh it's deno's problem, not the harmony's problem

* ✨ feat: skip checking the status code

I mean, it's gonna throw an error if the status is not 2xx

* 🎨 chore: fmt

* 🩹 fix: change to use reason option instead of body

* ✨ feat(guild): add reason arg in <GuildBans>.remove
  • Loading branch information
Helloyunho authored Sep 6, 2021
1 parent f6c5bc2 commit c15255d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
18 changes: 7 additions & 11 deletions src/managers/memberRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,31 @@ export class MemberRolesManager extends BaseChildManager<RolePayload, Role> {
}

async add(role: string | Role, reason?: string): Promise<boolean> {
const res = await this.client.rest.put(
await this.client.rest.put(
GUILD_MEMBER_ROLE(
this.member.guild.id,
this.member.id,
typeof role === 'string' ? role : role.id
),
{ reason },
undefined,
undefined,
true,
undefined,
undefined,
{ reason }
)

return res.response.status === 204
return true
}

async remove(role: string | Role): Promise<boolean> {
const res = await this.client.rest.delete(
await this.client.rest.delete(
GUILD_MEMBER_ROLE(
this.member.guild.id,
this.member.id,
typeof role === 'string' ? role : role.id
),
undefined,
undefined,
undefined,
true
)
)

return res.response.status === 204
return true
}
}
8 changes: 8 additions & 0 deletions src/rest/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export class APIRequest {
: `${this.rest.tokenType} ${this.rest.token}`.trim()
}

// TODO: Remove once https://github.com/denoland/deno/issues/11920 is fixed
if (
(this.method === 'post' || this.method === 'put') &&
(body === undefined || body === null)
) {
headers['Content-Length'] = '0'
}

if (contentType !== undefined) headers['Content-Type'] = contentType

const init: RequestInit = {
Expand Down
17 changes: 9 additions & 8 deletions src/structures/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,32 @@ export class GuildBans extends Base {
const res = await this.client.rest.put(
GUILD_BAN(this.guild.id, typeof user === 'string' ? user : user.id),
{
reason,
delete_message_days: deleteMessagesDays
},
undefined,
null,
true
true,
{ reason }
)
if (res.response.status !== 204) throw new Error('Failed to Add Guild Ban')
}

/**
* Unbans (removes ban from) a User.
* @param user User to unban, ID or User object.
* @param reason Reason for the Unban.
*/
async remove(user: string | User): Promise<boolean> {
const res = await this.client.rest.delete(
async remove(user: string | User, reason?: string): Promise<boolean> {
await this.client.rest.delete(
GUILD_BAN(this.guild.id, typeof user === 'string' ? user : user.id),
undefined,
undefined,
null,
true
undefined,
undefined,
{ reason }
)

if (res.response.status !== 204) return false
else return true
return true
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/structures/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,17 @@ export class Member extends SnowflakeBase {
* Kicks the member.
*/
async kick(reason?: string): Promise<boolean> {
const resp = await this.client.rest.delete(
await this.client.rest.delete(
GUILD_MEMBER(this.guild.id, this.id),
undefined,
undefined,
null,
true,
undefined,
{
reason
}
)
if (resp.ok !== true) return false
else return true
return true
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/structures/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ export class Webhook {

/** Deletes the Webhook. */
async delete(): Promise<boolean> {
const resp = await this.rest.delete(this.url, undefined, 0, undefined, true)
if (resp.response.status !== 204) return false
else return true
await this.rest.delete(this.url)
return true
}

async editMessage(
Expand Down

0 comments on commit c15255d

Please sign in to comment.