Skip to content

Commit

Permalink
web-api(fix): include file or contents types in file_uploads arguments (
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeg authored Feb 12, 2024
1 parent 664db4e commit 738784b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/web-api/src/WebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ export class WebClient extends Methods {
}

// add multiple files data when file_uploads is supplied
if (options.file_uploads) {
if ('file_uploads' in options) {
fileUploads = fileUploads.concat(await getMultipleFileUploadJobs(options, this.logger));
}
return fileUploads;
Expand Down
34 changes: 22 additions & 12 deletions packages/web-api/src/file-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { errorWithCode, ErrorCode } from './errors';
import {
FilesCompleteUploadExternalArguments,
FilesUploadV2Arguments,
FileUploadBinaryContents,
FileUploadStringContents,
FileUploadV2,
FileUploadV2Job,
} from './types/request/files';
Expand Down Expand Up @@ -47,10 +45,16 @@ export async function getFileUploadJob(
...fileUploadJob,
};
}
return {
file: options.file,
...fileUploadJob,
};
if ('file' in options) {
return {
file: options.file,
...fileUploadJob,
};
}
throw errorWithCode(
new Error('Either a file or content field is required for valid file upload. You must supply one'),
ErrorCode.FileUploadInvalidArgumentsError,
);
}

/**
Expand Down Expand Up @@ -82,7 +86,7 @@ export async function getMultipleFileUploadJobs(
options: FilesUploadV2Arguments,
logger: Logger,
): Promise<FileUploadV2Job[]> {
if (options.file_uploads) {
if ('file_uploads' in options) {
// go through each file_upload and create a job for it
return Promise.all(options.file_uploads.map((upload) => {
// ensure no omitted properties included in files_upload entry
Expand All @@ -108,14 +112,20 @@ export async function getMultipleFileUploadJobs(
}
if ('content' in upload) {
return getFileUploadJob({
content: (upload as FileUploadStringContents).content,
content: upload.content,
...uploadJobArgs,
}, logger);
}
return getFileUploadJob({
file: (upload as FileUploadBinaryContents).file,
...uploadJobArgs,
}, logger);
if ('file' in upload) {
return getFileUploadJob({
file: upload.file,
...uploadJobArgs,
}, logger);
}
throw errorWithCode(
new Error('Either a file or content field is required for valid file upload. You must supply one'),
ErrorCode.FileUploadInvalidArgumentsError,
);
}));
}
throw new Error(buildFilesUploadMissingMessage());
Expand Down
2 changes: 2 additions & 0 deletions packages/web-api/src/types/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** Omit all keys K from possible union types T */
export type ExcludeFromUnion<T, K extends string> = T extends T ? Omit<T, K> : never;
12 changes: 9 additions & 3 deletions packages/web-api/src/types/request/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
TraditionalPagingEnabled,
} from './common';
import type { FilesGetUploadURLExternalResponse } from '../response/index';
import type { ExcludeFromUnion } from '../helpers';

interface FileArgument {
/** @description Encoded file ID. */
Expand Down Expand Up @@ -147,10 +148,15 @@ export type FileUploadV2 = FileUpload & {
snippet_type?: string;
};

interface FilesUploadV2ArgumentsMultipleFiles {
file_uploads: ExcludeFromUnion<FileUploadV2, 'channel_id' | 'channels' | 'initial_comment' | 'thread_ts'>[];
}

// https://slack.dev/node-slack-sdk/web-api#upload-a-file
export type FilesUploadV2Arguments = FileUploadV2 & TokenOverridable & {
file_uploads?: Omit<FileUploadV2, 'channel_id' | 'channels' | 'initial_comment' | 'thread_ts'>[];
};
export type FilesUploadV2Arguments = TokenOverridable & (
| FileUploadV2
| (Omit<FileUploadV2, 'file' | 'content'> & FilesUploadV2ArgumentsMultipleFiles)
);

// Helper type intended for internal use in filesUploadV2 client method
// Includes additional metadata required to complete a single file upload job
Expand Down

0 comments on commit 738784b

Please sign in to comment.