Skip to content

Commit

Permalink
#327: Add Buffer type support using formdata-node package
Browse files Browse the repository at this point in the history
- Implemented `Buffer` to `File` conversion for attachment uploads.
- Added `attachment.mimeType` an optional property.
- Added automatic MIME type detection based on file extensions.
  • Loading branch information
MrRefactoring committed Jan 3, 2025
1 parent 9ca3605 commit 293cc52
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 3 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"code:formatting": "npm run replace:all && npm run prettier && npm run lint:fix"
},
"devDependencies": {
"@types/mime-types": "^2.1.4",
"@types/node": "^18.19.69",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^8.19.0",
Expand All @@ -73,6 +74,7 @@
"dependencies": {
"axios": "^1.7.9",
"formdata-node": "^6.0.3",
"mime-types": "^2.1.35",
"tslib": "^2.8.1"
}
}
1 change: 1 addition & 0 deletions src/serviceDesk/parameters/attachTemporaryFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Attachment {
filename: string;
file: Buffer | ReadableStream | string | Blob | File;
mimeType?: string;
}

export interface AttachTemporaryFile {
Expand Down
10 changes: 9 additions & 1 deletion src/serviceDesk/serviceDesk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FormData } from 'formdata-node';
import * as mime from 'mime-types';
import * as Models from './models';
import * as Parameters from './parameters';
import { Callback } from '../callback';
Expand Down Expand Up @@ -116,7 +117,14 @@ export class ServiceDesk {
const formData = new FormData();
const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment];

attachments.forEach(attachment => formData.append('file', attachment.file, attachment.filename));
attachments.forEach(attachment => {
const mimeType = attachment.mimeType ?? (mime.lookup(attachment.filename) || undefined);
const file = Buffer.isBuffer(attachment.file)
? new File([attachment.file], attachment.filename, { type: mimeType })
: attachment.file;

formData.append('file', file, attachment.filename);
});

const config: RequestConfig = {
url: `/rest/servicedeskapi/servicedesk/${parameters.serviceDeskId}/attachTemporaryFile`,
Expand Down
10 changes: 9 additions & 1 deletion src/version2/issueAttachments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FormData } from 'formdata-node';
import * as mime from 'mime-types';
import * as Models from './models';
import * as Parameters from './parameters';
import { Callback } from '../callback';
Expand Down Expand Up @@ -425,7 +426,14 @@ export class IssueAttachments {
const formData = new FormData();
const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment];

attachments.forEach(attachment => formData.append('file', attachment.file, attachment.filename));
attachments.forEach(attachment => {
const mimeType = attachment.mimeType ?? (mime.lookup(attachment.filename) || undefined);
const file = Buffer.isBuffer(attachment.file)
? new File([attachment.file], attachment.filename, { type: mimeType })

Check failure on line 432 in src/version2/issueAttachments.ts

View workflow job for this annotation

GitHub Actions / Integration Tests (18.x)

tests/integration/version2/issueAttachments.test.ts > should add attachment

ReferenceError: File is not defined ❯ src/version2/issueAttachments.ts:432:15 ❯ IssueAttachments.addAttachment src/version2/issueAttachments.ts:429:17 ❯ tests/integration/version2/issueAttachments.test.ts:35:47
: attachment.file;

formData.append('file', file, attachment.filename);
});

const config: RequestConfig = {
url: `/rest/api/2/issue/${parameters.issueIdOrKey}/attachments`,
Expand Down
1 change: 1 addition & 0 deletions src/version2/parameters/addAttachment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Attachment {
filename: string;
file: Buffer | ReadableStream | string | Blob | File;
mimeType?: string;
}

export interface AddAttachment {
Expand Down
10 changes: 9 additions & 1 deletion src/version3/issueAttachments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FormData } from 'formdata-node';
import * as mime from 'mime-types';
import * as Models from './models';
import * as Parameters from './parameters';
import { Callback } from '../callback';
Expand Down Expand Up @@ -425,7 +426,14 @@ export class IssueAttachments {
const formData = new FormData();
const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment];

attachments.forEach(attachment => formData.append('file', attachment.file, attachment.filename));
attachments.forEach(attachment => {
const mimeType = attachment.mimeType ?? (mime.lookup(attachment.filename) || undefined);
const file = Buffer.isBuffer(attachment.file)
? new File([attachment.file], attachment.filename, { type: mimeType })
: attachment.file;

formData.append('file', file, attachment.filename);
});

const config: RequestConfig = {
url: `/rest/api/3/issue/${parameters.issueIdOrKey}/attachments`,
Expand Down
1 change: 1 addition & 0 deletions src/version3/parameters/addAttachment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Attachment {
filename: string;
file: Buffer | ReadableStream | string | Blob | File;
mimeType?: string;
}

export interface AddAttachment {
Expand Down

0 comments on commit 293cc52

Please sign in to comment.