mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-20 20:16:06 -04:00
fix: fetch should fill out partial objects
This commit is contained in:
committed by
Paul Makles
parent
2427c6c32c
commit
daab20dbac
@@ -27,7 +27,7 @@ export class ChannelCollection extends ClassCollection<
|
||||
*/
|
||||
async fetch(id: string): Promise<Channel> {
|
||||
const channel = this.get(id);
|
||||
if (channel) return channel;
|
||||
if (channel && !this.isPartial(id)) return channel;
|
||||
const data = await this.client.api.get(`/channels/${id as ""}`);
|
||||
return this.getOrCreate(data._id, data);
|
||||
}
|
||||
@@ -39,7 +39,7 @@ export class ChannelCollection extends ClassCollection<
|
||||
* @param isNew Whether this object is new
|
||||
*/
|
||||
getOrCreate(id: string, data: API.Channel, isNew = false) {
|
||||
if (this.has(id)) {
|
||||
if (this.has(id) && !this.isPartial(id)) {
|
||||
return this.get(id)!;
|
||||
} else {
|
||||
const instance = new Channel(this, id);
|
||||
|
||||
@@ -14,7 +14,7 @@ export class EmojiCollection extends ClassCollection<Emoji, HydratedEmoji> {
|
||||
*/
|
||||
async fetch(id: string): Promise<Emoji> {
|
||||
const emoji = this.get(id);
|
||||
if (emoji) return emoji;
|
||||
if (emoji && !this.isPartial(id)) return emoji;
|
||||
const data = await this.client.api.get(`/custom/emoji/${id as ""}`);
|
||||
return this.getOrCreate(data._id, data);
|
||||
}
|
||||
@@ -26,7 +26,7 @@ export class EmojiCollection extends ClassCollection<Emoji, HydratedEmoji> {
|
||||
* @param isNew Whether this object is new
|
||||
*/
|
||||
getOrCreate(id: string, data: API.Emoji, isNew = false) {
|
||||
if (this.has(id)) {
|
||||
if (this.has(id) && !this.isPartial(id)) {
|
||||
return this.get(id)!;
|
||||
} else {
|
||||
const instance = new Emoji(this, id);
|
||||
|
||||
@@ -18,7 +18,7 @@ export class MessageCollection extends ClassCollection<
|
||||
*/
|
||||
async fetch(channelId: string, messageId: string): Promise<Message> {
|
||||
const message = this.get(messageId);
|
||||
if (message) return message;
|
||||
if (message && !this.isPartial(messageId)) return message;
|
||||
|
||||
const data = await this.client.api.get(
|
||||
`/channels/${channelId as ""}/messages/${messageId as ""}`
|
||||
@@ -34,7 +34,7 @@ export class MessageCollection extends ClassCollection<
|
||||
* @param isNew Whether this object is new
|
||||
*/
|
||||
getOrCreate(id: string, data: API.Message, isNew = false) {
|
||||
if (this.has(id)) {
|
||||
if (this.has(id) && !this.isPartial(id)) {
|
||||
return this.get(id)!;
|
||||
} else {
|
||||
const instance = new Message(this, id);
|
||||
|
||||
@@ -20,7 +20,7 @@ export class ServerCollection extends ClassCollection<Server, HydratedServer> {
|
||||
*/
|
||||
async fetch(id: string): Promise<Server> {
|
||||
const server = this.get(id);
|
||||
if (server) return server;
|
||||
if (server && !this.isPartial(id)) return server;
|
||||
const data = await this.client.api.get(`/servers/${id as ""}`, {
|
||||
include_channels: true,
|
||||
});
|
||||
@@ -44,7 +44,7 @@ export class ServerCollection extends ClassCollection<Server, HydratedServer> {
|
||||
* @param isNew Whether this object is new
|
||||
*/
|
||||
getOrCreate(id: string, data: API.Server, isNew = false) {
|
||||
if (this.has(id)) {
|
||||
if (this.has(id) && !this.isPartial(id)) {
|
||||
return this.get(id)!;
|
||||
} else {
|
||||
const instance = new Server(this, id);
|
||||
|
||||
@@ -28,6 +28,15 @@ export class ServerMemberCollection extends ClassCollection<
|
||||
return super.get(id.server + id.user);
|
||||
}
|
||||
|
||||
/**
|
||||
* check partial status by composite key
|
||||
* @param id Id
|
||||
* @returns Member
|
||||
*/
|
||||
isPartialByKey(id: API.MemberCompositeKey) {
|
||||
return super.isPartial(id.server + id.user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch server member by Id
|
||||
* @param serverId Server Id
|
||||
@@ -36,7 +45,7 @@ export class ServerMemberCollection extends ClassCollection<
|
||||
*/
|
||||
async fetch(serverId: string, userId: string): Promise<ServerMember> {
|
||||
const member = this.get(serverId + userId);
|
||||
if (member) return member;
|
||||
if (member && !this.isPartial(serverId + userId)) return member;
|
||||
|
||||
const data = (await this.client.api.get(
|
||||
`/servers/${serverId as ""}/members/${userId as ""}`,
|
||||
@@ -55,7 +64,7 @@ export class ServerMemberCollection extends ClassCollection<
|
||||
* @param data Data
|
||||
*/
|
||||
getOrCreate(id: API.MemberCompositeKey, data: API.Member) {
|
||||
if (this.hasByKey(id)) {
|
||||
if (this.hasByKey(id) && !this.isPartialByKey(id)) {
|
||||
return this.getByKey(id)!;
|
||||
} else {
|
||||
const instance = new ServerMember(this, id);
|
||||
|
||||
@@ -28,7 +28,7 @@ export class UserCollection extends ClassCollection<User, HydratedUser> {
|
||||
*/
|
||||
async fetch(id: string): Promise<User> {
|
||||
const user = this.get(id);
|
||||
if (user) return user;
|
||||
if (user && !this.isPartial(id)) return user;
|
||||
const data = await this.client.api.get(`/users/${id as ""}`);
|
||||
return this.getOrCreate(data._id, data);
|
||||
}
|
||||
@@ -40,7 +40,7 @@ export class UserCollection extends ClassCollection<User, HydratedUser> {
|
||||
* @param isNew Whether this object is new
|
||||
*/
|
||||
getOrCreate(id: string, data: API.User) {
|
||||
if (this.has(id)) {
|
||||
if (this.has(id) && !this.isPartial(id)) {
|
||||
return this.get(id)!;
|
||||
} else {
|
||||
const instance = new User(this, id);
|
||||
|
||||
@@ -37,6 +37,7 @@ export class ObjectStorage<T> {
|
||||
*/
|
||||
hydrate(id: string, type: keyof Hydrators, context: unknown, data?: unknown) {
|
||||
if (data) {
|
||||
data = { partial: false, ...data };
|
||||
this.set(id, hydrate(type, data as never, context, true) as T);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user