Skip to content

Commit

Permalink
feat(createalbum): adds the createAlbum method for creating new albums
Browse files Browse the repository at this point in the history
  • Loading branch information
KenEucker committed Apr 21, 2022
1 parent ac52088 commit 4834011
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
6 changes: 4 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ const run = async (client) => {
const imageStream = createReadStream(join(__dirname, 'small.jpg'));
const videoStream = createReadStream(join(__dirname, 'small.mp4'));

await client.upload({ album, image: imageStream, type: 'stream' }).then(console.log);
await client.upload({ album, image: videoStream, type: 'stream' }).then(console.log);
await client.createAlbum('test').then(console.log)

// await client.upload({ album, image: imageStream, type: 'stream' }).then(console.log);
// await client.upload({ album, image: videoStream, type: 'stream' }).then(console.log);

}

Expand Down
5 changes: 5 additions & 0 deletions src/album/createAlbum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test imgur album creation', () => {
test('no tests ready because issues', async () => {
expect(true).toBe(true);
});
});
28 changes: 28 additions & 0 deletions src/album/createAlbum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ImgurClient } from '../client';
import { ALBUM_ENDPOINT } from '../common/endpoints';
import { ImgurApiResponse, AlbumData } from '../common/types';
import { createForm, getImgurApiResponseFromResponse } from '../common/utils';

export async function createAlbum(
client: ImgurClient,
title?: string,
description?: string
): Promise<ImgurApiResponse<AlbumData>> {
const form = createForm({ title, description });
const response = await client
.request({
url: ALBUM_ENDPOINT,
headers: form.getHeaders(),
method: 'POST',
data: form,
})
.catch((e) => e.response);

if (response.data?.success && response.data?.data) {
response.data.data.title = title;
response.data.data.description = description;
}
return getImgurApiResponseFromResponse(
response
) as ImgurApiResponse<AlbumData>;
}
1 change: 1 addition & 0 deletions src/album/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './createAlbum';
export * from './getAlbum';
9 changes: 8 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
searchGallery,
SearchGalleryOptions,
} from './gallery';
import { getAlbum } from './album';
import { getAlbum, createAlbum } from './album';
import { getAccount, getAlbums, getAlbumsIds } from './account';
import { IMGUR_API_PREFIX } from './common/endpoints';
import {
Expand Down Expand Up @@ -108,6 +108,13 @@ export class ImgurClient extends EventEmitter {
return getAlbums(this, account, page);
}

createAlbum(
title?: string,
description?: string
): Promise<ImgurApiResponse<AlbumData>> {
return createAlbum(this, title, description);
}

getAlbumsIds(
account: string,
page?: number
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createForm(payload: string | Payload): FormData {
if (supportedUploadObjectTypes.indexOf(payload.type as string) !== -1) {
form.append(key, payload);
}
} else {
} else if (value) {
form.append(key, value);
}
}
Expand Down

0 comments on commit 4834011

Please sign in to comment.