From d2a788225542d39a41eba4896cbacbb9321d8276 Mon Sep 17 00:00:00 2001 From: Filip Maj Date: Wed, 15 Nov 2023 17:28:04 -0500 Subject: [PATCH] small refactor of files.* API arguments and finished types tests for these APIs. --- packages/web-api/src/types/request/files.ts | 8 +- .../test/types/methods/files.test-d.ts | 76 +++++++++++++++---- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/packages/web-api/src/types/request/files.ts b/packages/web-api/src/types/request/files.ts index 10837b78b..ccaa26049 100644 --- a/packages/web-api/src/types/request/files.ts +++ b/packages/web-api/src/types/request/files.ts @@ -160,8 +160,7 @@ export interface FilesCommentsDeleteArguments extends FileArgument, TokenOverrid /** @description The ID of the comment to delete. */ id: string; } -// https://api.slack.com/methods/files.remote.add -export interface FilesRemoteAddArguments extends FileType, ExternalIDArgument, TokenOverridable { +interface SharedFile { /** @description Title of the file being shared. */ title: string; /** @description URL of the remote file. */ @@ -174,6 +173,9 @@ export interface FilesRemoteAddArguments extends FileType, ExternalIDArgument, T */ indexable_file_contents?: Buffer | Stream; } + +// https://api.slack.com/methods/files.remote.add +export interface FilesRemoteAddArguments extends SharedFile, FileType, ExternalIDArgument, TokenOverridable {} // Either the encoded file ID or the external ID must be used as an argument. type FileOrExternalID = (FileArgument & { external_id?: never; }) | (ExternalIDArgument & { file?: never; }); // https://api.slack.com/methods/files.remote.info @@ -192,4 +194,4 @@ export type FilesRemoteRemoveArguments = FileOrExternalID & TokenOverridable; // https://api.slack.com/methods/files.remote.share export type FilesRemoteShareArguments = Required & FileOrExternalID & TokenOverridable; // https://api.slack.com/methods/files.remote.update -export type FilesRemoteUpdateArguments = FileOrExternalID & TokenOverridable & FileType & Pick; +export type FilesRemoteUpdateArguments = Partial & FileOrExternalID & FileType & TokenOverridable; diff --git a/packages/web-api/test/types/methods/files.test-d.ts b/packages/web-api/test/types/methods/files.test-d.ts index a24baafb9..1e6675663 100644 --- a/packages/web-api/test/types/methods/files.test-d.ts +++ b/packages/web-api/test/types/methods/files.test-d.ts @@ -5,15 +5,6 @@ const web = new WebClient('TOKEN'); // Reusable files.* API partial argument objects for these tests const file = { id: 'F1234', title: 'Choose Boring Technology' }; -// -- File upload target/destination types: channels, threads or private files -// Some of these use a `channel_id` prop vs. a `channels` prop :/ -// ---- correct destinations -const destinationChannel = { channel_id: 'C1234' }; -const destinationThread = { ...destinationChannel, thread_ts: '1234.456' }; -const destinationChannels = { channels: 'C1234' }; // same as above but different property name -const destinationThreadChannels = { ...destinationChannels, thread_ts: '1234.456' }; -// ---- invalid destinations -const destinationThreadWithMissingChannel = { thread_ts: '1234.567' }; // files.completeUploadExternal // -- sad path @@ -21,7 +12,7 @@ expectError(web.files.completeUploadExternal()); // lacking argument expectError(web.files.completeUploadExternal({})); // empty argument expectError(web.files.completeUploadExternal({ files: [file], - ...destinationThreadWithMissingChannel, // has thread_ts but no channel + thread_ts: '1234.567', // has thread_ts but no channel })); expectError(web.files.completeUploadExternal({ files: [], // must specify at least one file @@ -33,11 +24,12 @@ expectAssignable>([{ }]); expectAssignable>([{ files: [file], - ...destinationChannel, // share to a channel + channel_id: 'C1234', // share to a channel }]); expectAssignable>([{ files: [file], - ...destinationThread, // share to a thread + channel_id: 'C1234', + thread_ts: '1234.567', // share to a thread }]); // files.delete @@ -107,11 +99,11 @@ expectAssignable>([{ content: 'text', // or file contents }]); expectAssignable>([{ - ...destinationChannels, // optionally share to one or more channels + channels: 'C1234', // optionally share to one or more channels content: 'text', }]); expectAssignable>([{ - ...destinationThreadChannels, // or even to a specific thread + channels: 'C1234', // or even to a specific thread content: 'text', }]); @@ -128,11 +120,12 @@ expectAssignable>([{ content: 'text', // or file contents... }]); expectAssignable>([{ - ...destinationChannels, // optionally share to one or more channels + channels: 'C1234', // optionally share to one or more channels content: 'text', }]); expectAssignable>([{ - ...destinationThreadChannels, // or even to a specific thread + channels: 'C1234', + thread_ts: '12345.67', // or even to a specific thread content: 'text', }]); @@ -177,3 +170,54 @@ expectAssignable>([{ expectAssignable>([{ file: 'F1234', }]); + +// files.remote.list +// -- sad path +expectError(web.files.remote.list()); // lacking argument +// -- happy path +expectAssignable>([{}]); // able to call it with empty argument + +// files.remote.remove +// -- sad path +expectError(web.files.remote.remove()); // lacking argument +expectError(web.files.remote.remove({})); // empty argument +expectError(web.files.remote.remove({ external_id: '1234', file: 'F1234' })); // either external ID, or file ID, but not both +// -- happy path +expectAssignable>([{ + external_id: '1234', +}]); +expectAssignable>([{ + file: 'F1234', +}]); + +// files.remote.share +// -- sad path +expectError(web.files.remote.share()); // lacking argument +expectError(web.files.remote.share({})); // empty argument +expectError(web.files.remote.share({ external_id: '1234', file: 'F1234' })); // either external ID, or file ID, but not both +expectError(web.files.remote.share({ channels: 'C1234' })); // missing one of external ID or file ID +// -- happy path +expectAssignable>([{ + channels: 'C123', + external_id: '1234', +}]); +expectAssignable>([{ + channels: 'C123', + file: 'F1234', +}]); + +// files.remote.update +// -- sad path +expectError(web.files.remote.update()); // lacking argument +expectError(web.files.remote.update({})); // empty argument +expectError(web.files.remote.update({ external_id: '1234', file: 'F1234' })); // either external ID, or file ID, but not both +expectError(web.files.remote.update({ title: 'Fear and Loathing in Las Vegas' })); // missing one of external ID or file ID +// -- happy path +expectAssignable>([{ + external_id: '1234', + title: 'Moby Dick', +}]); +expectAssignable>([{ + file: 'F1234', + external_url: 'https://someurl.com', +}]);