Skip to content

Commit

Permalink
fix(update,upload): removes the support for array payloads and resolv…
Browse files Browse the repository at this point in the history
…es issues with updateImage
  • Loading branch information
KenEucker committed Jan 27, 2022
1 parent e8aa619 commit 51db842
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 111 deletions.
8 changes: 2 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,11 @@ export class ImgurClient extends EventEmitter {
return getImage(this, imageHash);
}

updateImage(
payload: UpdateImagePayload | UpdateImagePayload[]
): Promise<ImgurApiResponse<boolean> | ImgurApiResponse<boolean>[]> {
updateImage(payload: UpdateImagePayload): Promise<ImgurApiResponse<boolean>> {
return updateImage(this, payload);
}

upload(
payload: Payload | Payload[]
): Promise<ImgurApiResponse<ImageData> | ImgurApiResponse<ImageData>[]> {
upload(payload: Payload): Promise<ImgurApiResponse<ImageData>> {
return upload(this, payload);
}
}
1 change: 0 additions & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export function getImgurApiResponseFromResponse(
status = 500;
success = false;
} else if (responseIsSuccess) {
success = response.data.success;
status = response.data.status;
data = response.data.data.error
? response.data.data.error
Expand Down
31 changes: 0 additions & 31 deletions src/image/updateImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,3 @@ test('update one image without title or description', async () => {
`"Update requires a title and/or description"`
);
});

test('update multiple images, receive multiple response', async () => {
const accessToken = 'abc123';
const client = new ImgurClient({ accessToken });
const response = await updateImage(client, [
{
imageHash: 'meme123',
title: 'dank meme',
description: 'the dankiest of dank memes',
},
{
imageHash: 'lol123',
title: 'this is funny',
description: '🤣',
},
]);
expect(response).toMatchInlineSnapshot(`
Array [
Object {
"data": true,
"status": 200,
"success": true,
},
Object {
"data": true,
"status": 200,
"success": true,
},
]
`);
});
57 changes: 17 additions & 40 deletions src/image/updateImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,27 @@ function isValidUpdatePayload(p: UpdateImagePayload) {

export async function updateImage(
client: ImgurClient,
payload: UpdateImagePayload | UpdateImagePayload[]
): Promise<ImgurApiResponse<boolean> | ImgurApiResponse<boolean>[]> {
if (Array.isArray(payload)) {
const promises = payload.map((p: UpdateImagePayload) => {
if (!isValidUpdatePayload(p)) {
throw new Error('Update requires a title and/or description');
}

const url = `${IMAGE_ENDPOINT}/${p.imageHash}`;
const form = createForm(p);
/* eslint no-async-promise-executor: 0 */
return new Promise(async function (resolve) {
return resolve(
getImgurApiResponseFromResponse(
await client
.request({
url,
method: 'POST',
data: form,
headers: form.getHeaders(),
})
.catch((e) => e.response)
) as ImgurApiResponse<boolean>
);
}) as Promise<ImgurApiResponse<boolean>>;
});

return await Promise.all(promises).then();
}

payload: UpdateImagePayload
): Promise<ImgurApiResponse<boolean>> {
if (!isValidUpdatePayload(payload)) {
throw new Error('Update requires a title and/or description');
}

const url = `${IMAGE_ENDPOINT}/${payload.imageHash}`;
const form = createForm(payload);
return getImgurApiResponseFromResponse(
await client
.request({
url,
method: 'POST',
data: form,
headers: form.getHeaders(),
})
.catch((e) => e)
) as ImgurApiResponse<boolean>;
/* eslint no-async-promise-executor: 0 */
return new Promise(async (resolve) => {
return resolve(
getImgurApiResponseFromResponse(
await client
.request({
url,
method: 'POST',
data: form,
headers: form.getHeaders(),
})
.catch((e) => e.response)
) as ImgurApiResponse<boolean>
);
}) as Promise<ImgurApiResponse<boolean>>;
}
61 changes: 28 additions & 33 deletions src/image/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,33 @@ import { UPLOAD_ENDPOINT, IMAGE_ENDPOINT } from '../common/endpoints';

export async function upload(
client: ImgurClient,
payload: Payload | Payload[]
): Promise<ImgurApiResponse<ImageData> | ImgurApiResponse<ImageData>[]> {
payload = Array.isArray(payload) ? payload : [payload];
const promises = payload.map((p: Payload) => {
const form = createForm(p);
const image = p.image as any;
const filename =
typeof image === 'string' ? image : image.path ?? image.name;
const isVideo =
p.type === 'stream' &&
filename &&
(filename.indexOf('.mp4') !== -1 || filename.indexOf('.avi') !== -1);
const url = isVideo ? UPLOAD_ENDPOINT : IMAGE_ENDPOINT;
payload: Payload
): Promise<ImgurApiResponse<ImageData>> {
const form = createForm(payload);
const image = payload.image as any;
const filename = typeof image === 'string' ? image : image.path ?? image.name;
const isVideo =
payload.type === 'stream' &&
filename &&
(filename.indexOf('.mp4') !== -1 || filename.indexOf('.avi') !== -1);
const url = isVideo ? UPLOAD_ENDPOINT : IMAGE_ENDPOINT;

/* eslint no-async-promise-executor: 0 */
return new Promise(async (resolve) => {
resolve(
getImgurApiResponseFromResponse(
await client
.request({
url,
method: 'POST',
data: form,
headers: form.getHeaders(),
onUploadProgress: (progressEvent) => {
client.emit('uploadProgress', { ...progressEvent });
},
})
.catch((e) => e.response)
) as ImgurApiResponse<ImageData>
);
}) as Promise<ImgurApiResponse<ImageData>>;
});
return await Promise.all(promises);
/* eslint no-async-promise-executor: 0 */
return new Promise(async (resolve) => {
return resolve(
getImgurApiResponseFromResponse(
await client
.request({
url,
method: 'POST',
data: form,
headers: form.getHeaders(),
onUploadProgress: (progressEvent) => {
client.emit('uploadProgress', { ...progressEvent });
},
})
.catch((e) => e.response)
) as ImgurApiResponse<ImageData>
);
}) as Promise<ImgurApiResponse<ImageData>>;
}

0 comments on commit 51db842

Please sign in to comment.