Skip to content

Commit

Permalink
fix: support for passing filter params when post & patch role
Browse files Browse the repository at this point in the history
  • Loading branch information
zoswing authored and vnues committed Dec 17, 2021
1 parent 5afa840 commit 1f66b6c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
41 changes: 26 additions & 15 deletions src/openapi/v1/role.ts
Expand Up @@ -13,6 +13,13 @@ export interface IRole {
number: number; // 人数 不会被修改,创建接口修改
member_limit: number; // 成员上限 不会被修改,创建接口修改
}

// 指定创建、更新频道身份组涉及的字段
export interface IRoleFilter {
name?: number;
color?: number;
hoist?: number;
}
export interface GuildRoles {
guild_id: string; // 频道ID
roles: IRole[]; // 一组频道身份组对象
Expand All @@ -25,6 +32,13 @@ export interface UpdateResult {
guild_id: string; // 频道ID
role: IRole; // 所创建的频道身份组对象
}

// 默认的filter:0 1 代表是否设置 0-否 1-是
export const defaultFilter: IRoleFilter = {
name: 1,
color: 1,
hoist: 1,
};
export default class Role implements RoleAPI {
public request: OpenAPIRequest;
public config: Config;
Expand All @@ -45,17 +59,14 @@ export default class Role implements RoleAPI {
}

// 创建频道身份组
public postRole(guildID: string, role: Omit<IRole, 'id'>): Promise<RestyResponse<UpdateResult>> {
public postRole(
guildID: string,
role: Omit<IRole, 'id'>,
filter = defaultFilter,
): Promise<RestyResponse<UpdateResult>> {
if (role.color === 0) {
role.color = defaultColor;
}
// openapi 上修改哪个字段,就需要传递哪个filter
// 0 1 代表是否设置 0-否 1-是
const filter = {
name: 1,
color: 1,
hoist: 1,
};
const options = {
method: 'POST' as const,
url: getURL('rolesURI'),
Expand All @@ -72,16 +83,16 @@ export default class Role implements RoleAPI {
}

// 修改频道身份组
public patchRole(guildID: string, roleID: string, role: IRole): Promise<RestyResponse<UpdateResult>> {
public patchRole(
guildID: string,
roleID: string,
role: IRole,
filter = defaultFilter,
): Promise<RestyResponse<UpdateResult>> {
if (role.color === 0) {
role.color = defaultColor;
}
// openapi 上修改哪个字段,就需要传递哪个 filter
const filter = {
name: 1,
color: 1,
hoist: 1,
};

const options = {
method: 'PATCH' as const,
url: getURL('roleURI'),
Expand Down
11 changes: 8 additions & 3 deletions src/types/openapi.ts
Expand Up @@ -5,7 +5,7 @@ import DirectMessage, { DirectMessageToCreate, IDirectMessage } from '@src/opena
import { GuildMembersPager, IGuild, IMember } from '@src/openapi/v1/guild';
import { IUser } from '@src/openapi/v1/me';
import { IMessage, IMessageRes, MessagesPager, MessageToCreate } from '@src/openapi/v1/message';
import { GuildRoles, IRole, UpdateResult } from '@src/openapi/v1/role';
import { GuildRoles, IRole, UpdateResult, IRoleFilter } from '@src/openapi/v1/role';
import { RequestOptions, RestyResponse } from 'resty-client';

export type OpenAPIRequest = <T extends Record<any, any> = any>(options: RequestOptions) => Promise<RestyResponse<T>>;
Expand Down Expand Up @@ -80,8 +80,13 @@ export interface AudioAPI {
// RoleAPI 用户组相关接口
export interface RoleAPI {
roles: (guildID: string) => Promise<RestyResponse<GuildRoles>>;
postRole: (guildID: string, role: Omit<IRole, 'id'>) => Promise<RestyResponse<UpdateResult>>;
patchRole: (guildID: string, roleID: string, role: IRole) => Promise<RestyResponse<UpdateResult>>;
postRole: (guildID: string, role: Omit<IRole, 'id'>, filter?: IRoleFilter) => Promise<RestyResponse<UpdateResult>>;
patchRole: (
guildID: string,
roleID: string,
role: IRole,
filter?: IRoleFilter,
) => Promise<RestyResponse<UpdateResult>>;
deleteRole: (guildID: string, roleID: string) => Promise<RestyResponse<any>>;
}

Expand Down

0 comments on commit 1f66b6c

Please sign in to comment.