Skip to content

Commit

Permalink
feat: add CommandInput and CommandResponse as types
Browse files Browse the repository at this point in the history
* feat: add CommandResponse and CommandInput types

Update usage of commands to use CommandResponse instead of CommandResponseTypes

* chore: remove unused package type-fest
  • Loading branch information
matthova committed Apr 23, 2024
1 parent a0a9f2a commit 6f65ef3
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@discord/embedded-app-sdk": "workspace:@discord/embedded-app-sdk@*"
},
"devDependencies": {
"vite": "^5.2.9",
"type-fest": "^2.16.0"
"vite": "^5.2.9"
}
}
6 changes: 3 additions & 3 deletions examples/discord-activity-starter/packages/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {CommandResponse} from '@discord/embedded-app-sdk';
import './style.css';
import {discordSdk} from './discordSdk';
import type {AsyncReturnType} from 'type-fest';

type Auth = AsyncReturnType<typeof discordSdk.commands.authenticate>;
type Auth = CommandResponse<'authenticate'>;
let auth: Auth;

// Once setupDiscordSdk is complete, we can assert that "auth" is initialized
Expand Down Expand Up @@ -121,7 +121,7 @@ async function appendGuildAvatar() {
guildImg.setAttribute(
'src',
// More info on image formatting here: https://discord.com/developers/docs/reference#image-formatting
`https://cdn.discordapp.com/icons/${currentGuild.id}/${currentGuild.icon}.webp?size=128`
`https://cdn.discordapp.com/icons/${currentGuild.id}/${currentGuild.icon}.webp?size=128`,
);
guildImg.setAttribute('width', '128px');
guildImg.setAttribute('height', '128px');
Expand Down
3 changes: 1 addition & 2 deletions examples/react-colyseus/packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"eslint": "^8.18.0",
"eslint-plugin-react-hooks": "^4.6.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"type-fest": "^4.8.3"
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.64",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {discordSdk} from '../discordSdk';
import {LoadingScreen} from '../components/LoadingScreen';
import {getUserAvatarUrl} from '../utils/getUserAvatarUrl';

import type {IGuildsMembersRead, TAuthenticateResponse, TAuthenticatedContext} from '../types';
import type {IGuildsMembersRead, TAuthenticatedContext} from '../types';
import {getUserDisplayName} from '../utils/getUserDisplayName';

const AuthenticatedContext = React.createContext<TAuthenticatedContext>({
Expand Down Expand Up @@ -102,7 +102,7 @@ function useAuthenticatedContextSetup() {
const {access_token} = await response.json();

// Authenticate with Discord client (using the access_token)
const newAuth: TAuthenticateResponse = await discordSdk.commands.authenticate({
const newAuth = await discordSdk.commands.authenticate({
access_token,
});

Expand Down
8 changes: 4 additions & 4 deletions examples/react-colyseus/packages/client/src/types.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {AsyncReturnType} from 'type-fest';
import {discordSdk} from './discordSdk';
import type {CommandResponse} from '@discord/embedded-app-sdk';
import {Client, Room} from 'colyseus.js';
import {State} from '../../server/src/entities/State';

export type TAuthenticateResponse = AsyncReturnType<typeof discordSdk.commands.authenticate>;
export interface IColyseus {
room: Room<State>;
client: Client;
}
export type TAuthenticatedContext = TAuthenticateResponse & {guildMember: IGuildsMembersRead | null} & IColyseus;
export type TAuthenticatedContext = CommandResponse<'authenticate'> & {
guildMember: IGuildsMembersRead | null;
} & IColyseus;

export interface IGuildsMembersRead {
roles: string[];
Expand Down
1 change: 0 additions & 1 deletion examples/sdk-playground/packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^1.3.0",
"eslint-plugin-react-hooks": "^4.6.0",
"type-fest": "^2.16.0",
"typescript": "^5.2.2",
"vite": "^5.2.9"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/sdk-playground/packages/client/src/types.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {CommandResponseTypes} from '@discord/embedded-app-sdk';
export type TAuthenticatedContext = CommandResponseTypes['authenticate'] & {guildMember: IGuildsMembersRead | null};
import type {CommandResponse} from '@discord/embedded-app-sdk';
export type TAuthenticatedContext = CommandResponse<'authenticate'> & {guildMember: IGuildsMembersRead | null};

export interface IGuildsMembersRead {
roles: string[];
Expand Down
19 changes: 0 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export type CommandTypes = ReturnType<typeof commands>;
export type CommandResponseTypes = {
[Name in keyof CommandTypes]: Awaited<ReturnType<CommandTypes[Name]>>;
};
export type CommandResponse<K extends keyof CommandTypes> = Awaited<ReturnType<CommandTypes[K]>>;
export type CommandInputTypes = {
[Name in keyof CommandTypes]: Parameters<CommandTypes[Name]>;
};
export type CommandInput<K extends keyof CommandTypes> = Parameters<CommandTypes[K]>;
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {DiscordSDK} from './Discord';
import type {IDiscordSDK} from './interface';
import type * as Types from './schema/types';
import {CommandTypes, CommandResponseTypes, CommandInputTypes} from './commands';
import {CommandTypes, CommandResponseTypes, CommandResponse, CommandInputTypes, CommandInput} from './commands';
import {Events} from './schema/events';
import * as Common from './schema/common';
import * as Responses from './schema/responses';
Expand Down Expand Up @@ -31,4 +31,13 @@ export {
patchUrlMappings,
attemptRemap,
};
export type {IDiscordSDK, CommandTypes, CommandInputTypes, CommandResponseTypes, ISDKError, Types};
export type {
IDiscordSDK,
CommandTypes,
CommandInput,
CommandInputTypes,
CommandResponse,
CommandResponseTypes,
ISDKError,
Types,
};

0 comments on commit 6f65ef3

Please sign in to comment.