-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: base class, shallow clone, symbols
- Loading branch information
Showing
4 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { data as kData } from './utils/symbols'; | ||
|
||
export abstract class Structure<DataType, Omitted extends keyof DataType | '' = ''> { | ||
protected [kData]: Readonly<Omit<DataType, Omitted>>; | ||
|
||
protected constructor(data: Readonly<Omit<DataType, Omitted>>) { | ||
// Do not shallow clone data here as subclasses should do it (also allows them to set the constructor to public) | ||
this[kData] = data; | ||
} | ||
|
||
/** | ||
* Update this structure with new data | ||
* @param data - A payload with updated data for this structure | ||
* @returns New structure with patched data | ||
*/ | ||
public patch(data: Readonly<Partial<DataType>>): this { | ||
// @ts-expect-error TS cannot identify the type of this.constructor properly because subclasses | ||
return new this.constructor({ ...this.toJSON(), ...data }) as this; | ||
} | ||
|
||
public toJSON(): DataType { | ||
// This will be DataType provided nothing is omitted, when omits occur, subclass needs to overwrite this. | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
return { ...this[kData] } as DataType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,80 @@ | ||
import type { APIConnection } from 'discord-api-types/v10'; | ||
import { Structure } from '../Structure'; | ||
import { data as kData } from '../utils/symbols'; | ||
|
||
/** | ||
* Represents a user's connection on Discord. | ||
*/ | ||
export class Connection { | ||
export class Connection extends Structure<APIConnection> { | ||
public constructor( | ||
/** | ||
* The raw data received from the API for the connection | ||
*/ | ||
protected raw: APIConnection, | ||
) {} | ||
data: APIConnection, | ||
) { | ||
super({ ...data }); | ||
} | ||
|
||
/** | ||
* The id of the connection account | ||
*/ | ||
public get id() { | ||
return this.raw.id; | ||
return this[kData].id; | ||
} | ||
|
||
/** | ||
* The username of the connection account | ||
*/ | ||
public get name() { | ||
return this.raw.name; | ||
return this[kData].name; | ||
} | ||
|
||
/** | ||
* The type of service this connection is for | ||
*/ | ||
public get type() { | ||
return this.raw.type; | ||
return this[kData].type; | ||
} | ||
|
||
/** | ||
* Whether the connection is revoked | ||
*/ | ||
public get revoked() { | ||
return this.raw.revoked ?? false; | ||
return this[kData].revoked ?? false; | ||
} | ||
|
||
/** | ||
* Any integrations associated with this connection | ||
*/ | ||
public get integrations() { | ||
return this.raw.integrations ?? null; | ||
return this[kData].integrations ?? null; | ||
} | ||
|
||
/** | ||
* Whether the connection is verified | ||
*/ | ||
public get verified() { | ||
return this.raw.verified; | ||
return this[kData].verified; | ||
} | ||
|
||
/** | ||
* Whether friend sync is enabled for this connection | ||
*/ | ||
public get friendSync() { | ||
return this.raw.friend_sync; | ||
return this[kData].friend_sync; | ||
} | ||
|
||
/** | ||
* Whether activities related to this connection are shown in the users presence | ||
*/ | ||
public get showActivity() { | ||
return this.raw.show_activity; | ||
return this[kData].show_activity; | ||
} | ||
|
||
/** | ||
* The visibilty state for this connection | ||
*/ | ||
public get visibility() { | ||
return this.raw.visibility; | ||
return this[kData].visibility; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const data = Symbol('structure:data'); |