Skip to content

Commit

Permalink
feat: openExternalLink returns a { opened: boolean } result (#282)
Browse files Browse the repository at this point in the history
* Add a response type for openExternalLink that returns a boolean opened value

* revert the package version change

* adding a fallback to OpenExternalLinkResponse for older clients

* fix: linting fix
  • Loading branch information
HamzaAtDiscord authored Nov 19, 2024
1 parent 2cb879a commit d3bd375
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/commands/openExternalLink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Commands} from '../schema/common';
import {EmptyResponse} from '../schema/responses';
import {OpenExternalLinkResponse} from '../schema/responses';
import {TSendCommand} from '../schema/types';
import {commandFactory} from '../utils/commandFactory';

Expand All @@ -11,4 +11,8 @@ export interface OpenExternalLinkInput {
*
*/
export const openExternalLink = (sendCommand: TSendCommand) =>
commandFactory<OpenExternalLinkInput, typeof EmptyResponse>(sendCommand, Commands.OPEN_EXTERNAL_LINK, EmptyResponse);
commandFactory<OpenExternalLinkInput, typeof OpenExternalLinkResponse>(
sendCommand,
Commands.OPEN_EXTERNAL_LINK,
OpenExternalLinkResponse,
);
2 changes: 1 addition & 1 deletion src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const commandsMockDefault: IDiscordSDK['commands'] = {
startPurchase: () => Promise.resolve([]),
setConfig: () => Promise.resolve({use_interactive_pip: false}),
userSettingsGetLocale: () => Promise.resolve({locale: ''}),
openExternalLink: () => Promise.resolve(null),
openExternalLink: () => Promise.resolve({opened: false}),
encourageHardwareAcceleration: () => Promise.resolve({enabled: true}),
captureLog: () => Promise.resolve(null),
setOrientationLockState: () => Promise.resolve(null),
Expand Down
9 changes: 7 additions & 2 deletions src/schema/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ChannelTypesObject,
ReceiveFramePayload,
} from './common';
import {zodCoerceUnhandledValue} from '../utils/zodUtils';
import {fallbackToDefault, zodCoerceUnhandledValue} from '../utils/zodUtils';
import {Schemas, AuthenticateResponseSchema, InitiateImageUploadResponseSchema} from '../generated/schemas';
import assertUnreachable from '../utils/assertUnreachable';

Expand Down Expand Up @@ -106,6 +106,10 @@ export const GetChannelPermissionsResponse = zod.object({
permissions: zod.bigint().or(zod.string()),
});

export const OpenExternalLinkResponse = fallbackToDefault(
zod.object({opened: zod.boolean().or(zod.null())}).default({opened: null}),
);

export {InitiateImageUploadResponseSchema as InitiateImageUploadResponse};

/**
Expand Down Expand Up @@ -162,8 +166,9 @@ function parseResponseData({cmd, data}: zod.infer<typeof ResponseFrame>) {
return SubscribeResponse.parse(data);
case Commands.USER_SETTINGS_GET_LOCALE:
return UserSettingsGetLocaleResponse.parse(data);
// Empty Responses
case Commands.OPEN_EXTERNAL_LINK:
return OpenExternalLinkResponse.parse(data);
// Empty Responses
case Commands.SET_ORIENTATION_LOCK_STATE:
case Commands.SET_CERTIFIED_DEVICES:
case Commands.SEND_ANALYTICS_EVENT:
Expand Down

0 comments on commit d3bd375

Please sign in to comment.