Files
discord.js/packages/structures/__tests__/mixinClasses.ts
Qjuh 3cff4d7412 feat: @discordjs/structures (#10900)
* chore: init /structures

* feat: base structure

* feat: initial structures design attempt

* refactor(Structure): use unknown to store in kData

* feat(Structure): add Invite

refactor(Structure): patch to _patch

* refactor: symbol names and override location

* fix: don't possibly return 0 if discord borks

Co-authored-by: Synbulat Biishev <signin@syjalo.dev>

* refactor: use getter value instead of api

Co-authored-by: Synbulat Biishev <signin@syjalo.dev>

* refactor: cache createdTimestamp value

Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>

* docs: better docs for what's done so far

* feat: add Mixin

* refactor(User): remove bitfield getters and add displayName

* feat(structures): add Connection

* feat(structures): add Channel base

* refactor(Mixin): trace prototype chain, allow construction

* fix(structures): fix mixin behavior

* fix(structures): data optimization call behavior from perf testing

* feat: channel mixins

* chore: update deps

* feat: channels and mixins

* chore: more typeguard tests

* fix: tests and some other issues

* feat: add ChannelWebhookMixin

* fix: more tests

* chore: tests and docs

* chore: docs

* fix: remove unneccessary omitted

* chore: apply code suggestions

* refactor: change how extended invite works

* fix: type imports

* Apply suggestions from code review

Co-authored-by: Almeida <github@almeidx.dev>

* fix: tests

* chore: add jsdoc

* refactor: apply code suggestions

* fix: don't instantiate sub-structures

* fix: don't do null default twice

* chore: use formatters, add _cache

* chore: lockfile

* chore: move MixinTypes to declaratiion file

* fix: tests

* fix: don't include source d.ts files for docs

* feat: bitfields

* feat: more bitfields

* refactor: remove DirectoryChannel structure

* chore: apply suggestions from code review

* chore: remove unused import

* refactor: use symbol for mixin toJSON, remove _ prefix

* chore: apply suggestions from code review

* refactor: remove bitfield casts

* refactor: remove special case for threadchannel types

* fix: apply code review suggestions

* refactor: bitfields always store bigint

* fix: tests

* chore: apply suggestions from code review

* fix: lint

* refactor: conditional structuredClone

* Apply suggestions from code review

Co-authored-by: ckohen <chaikohen@gmail.com>

* fix: code review errors

* fix: lint

* chore: bump dtypes

* Update packages/structures/cliff.toml

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* docs: link to VideoQualityMode

* chore: typo in comment

* chore: small nits in docs links

* chore: small nits

* docs: forgot one

* chore: update template

* chore: typos and things

* chore: apply suggestions from code review

* fix: tests and typeguards

* chore: don't clone appliedTags

* refactor: use a symbol for patch method

* fix: add missing readonly

* chore: remove todo comment

* refactor: use symbol for clone

* fix: add constraint to DataType

* chore: apply suggestions

* fix: dtypes bump

* chore: fix comment

* chore: add todo comment

* chore: mark bitfield as todo
chore: mark bit field as todo and edit readme

---------

Co-authored-by: ckohen <chaikohen@gmail.com>
Co-authored-by: Synbulat Biishev <signin@syjalo.dev>
Co-authored-by: Almeida <github@almeidx.dev>
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2025-07-12 18:24:30 +00:00

133 lines
3.0 KiB
TypeScript

import { Mixin } from '../src/Mixin.js';
import type { MixinTypes } from '../src/MixinTypes.d.ts';
import { Structure } from '../src/Structure.js';
import { kData, kMixinConstruct, kMixinToJSON, kPatch } from '../src/utils/symbols.js';
export interface APIData {
baseOptimize?: string;
id: string;
mixinOptimize?: string;
property1?: number;
property2?: boolean;
}
export class Base<Omitted extends keyof APIData | '' = ''> extends Structure<APIData, Omitted> {
public static override readonly DataTemplate = {
set baseOptimize(_: unknown) {},
};
public baseOptimize: boolean | null = null;
public constructor(data: APIData) {
super(data);
this.optimizeData(data);
}
public override [kPatch](data: Partial<APIData>) {
super[kPatch](data);
return this;
}
public override optimizeData(data: Partial<APIData>) {
if ('baseOptimize' in data) {
this.baseOptimize = Boolean(data.baseOptimize);
}
}
public get id() {
return this[kData].id;
}
public getId() {
return this.id;
}
public override toJSON() {
const data = super.toJSON();
if (this.baseOptimize) {
data.baseOptimize = String(this.baseOptimize);
}
return data;
}
}
export interface MixinProperty1<Omitted extends keyof APIData | '' = ''> extends Base<Omitted> {
mixinOptimize: boolean | null;
}
export class MixinProperty1 {
public static readonly DataTemplate = {
set mixinOptimize(_: unknown) {},
};
public [kMixinConstruct]() {
this.mixinOptimize = null;
}
public optimizeData(data: Partial<APIData>) {
if ('mixinOptimize' in data) {
this.mixinOptimize = Boolean(data.mixinOptimize);
}
}
public get property1() {
return this[kData].property1;
}
public getProperty1() {
return this.property1;
}
protected [kMixinToJSON](data: Partial<APIData>) {
if (this.mixinOptimize) {
data.mixinOptimize = String(this.mixinOptimize);
}
}
}
export interface MixinProperty2<Omitted extends keyof APIData | '' = ''> extends Base<Omitted> {
constructCalled: boolean;
}
export class MixinProperty2 {
public [kMixinConstruct]() {
this.constructCalled = true;
}
public get property2() {
return this[kData].property2;
}
public getProperty2() {
return this.property2;
}
}
export class ExtendedMixinProperty2 extends MixinProperty2 {
// eslint-disable-next-line @typescript-eslint/class-literal-property-style
public get isExtended() {
return true;
}
}
export interface Mixed extends MixinTypes<Base, [MixinProperty1, MixinProperty2]> {}
export class Mixed extends Base {
public getProperties() {
return { property1: this.property1, property2: this.property2 };
}
}
Mixin(Mixed, [MixinProperty1, MixinProperty2]);
export interface MixedWithExtended extends MixinTypes<Base, [MixinProperty1, ExtendedMixinProperty2]> {}
export class MixedWithExtended extends Base {
public getProperties() {
return {
property1: this.property1,
property2: this.property2,
};
}
}
// Intentionally don't directly mix Property 2
Mixin(MixedWithExtended, [MixinProperty1, ExtendedMixinProperty2]);