Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #180 from kaimallea/next-dev
Browse files Browse the repository at this point in the history
feat(album): add getAlbum
  • Loading branch information
kaimallea authored Apr 26, 2021
2 parents 3ef9248 + 349db38 commit 1b2c55d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,9 @@ Additionally, the following advanced search query options can be set (NOTE: if a
| `q_not` | optional | Exclude results matching this string |
| `q_type` | optional | Show results for any file type, `jpg` \| `png` \| `gif` \| `anigif` (animated gif) \| `album` |
| `q_size_px` | optional | Size ranges, `small` (500 pixels square or less) \| `med` (500 to 2,000 pixels square) \| `big` (2,000 to 5,000 pixels square) \| `lrg` (5,000 to 10,000 pixels square) \| `huge` (10,000 square pixels and above) |

### Get album info

```ts
const album = await client.getAlbum('XtMnA');
```
38 changes: 38 additions & 0 deletions src/album/getAlbum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ImgurClient } from '../client';
import { getAlbum } from './getAlbum';

test('returns an album response', async () => {
const accessToken = 'abc123';
const client = new ImgurClient({ accessToken });
const response = await getAlbum(client, 'XtMnA');
expect(response).toMatchInlineSnapshot(`
Object {
"data": Object {
"description": "Dank memes",
"id": "XtMnA",
"image_count": 22,
"images": Array [
Object {
"datetime": 1316635799,
"description": null,
"id": "2dAns",
"link": "https://i.imgur.com/2dAns.gif",
"title": null,
"type": "image/gif",
},
Object {
"datetime": 1316635800,
"description": null,
"id": "snAd2",
"link": "https://i.imgur.com/2dAns.jpg",
"title": null,
"type": "image/jpeg",
},
],
"title": "Meme album",
},
"status": 200,
"success": true,
}
`);
});
11 changes: 11 additions & 0 deletions src/album/getAlbum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ImgurClient } from '../client';
import { ALBUM_ENDPOINT } from '../common/endpoints';
import { ImgurApiResponse, AlbumData } from '../common/types';

export async function getAlbum(
client: ImgurClient,
albumHash: string
): Promise<ImgurApiResponse<AlbumData>> {
const url = `${ALBUM_ENDPOINT}/${albumHash}`;
return (await client.request(url).json()) as ImgurApiResponse<AlbumData>;
}
1 change: 1 addition & 0 deletions src/album/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getAlbum';
6 changes: 6 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import {
searchGallery,
SearchGalleryOptions,
} from './gallery';
import { getAlbum } from './album';
import { IMGUR_API_PREFIX } from './common/endpoints';
import {
AlbumData,
Credentials,
GalleryData,
ImageData,
Expand Down Expand Up @@ -74,6 +76,10 @@ export class ImgurClient extends EventEmitter {
return favoriteImage(this, imageHash);
}

getAlbum(albumHash: string): Promise<ImgurApiResponse<AlbumData>> {
return getAlbum(this, albumHash);
}

getGallery(options: GalleryOptions): Promise<ImgurApiResponse<GalleryData>> {
return getGallery(this, options);
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const API_VERSION = '3';

export const AUTHORIZE_ENDPOINT = 'oauth2/authorize';

export const ALBUM_ENDPOINT = `${API_VERSION}/album`;

export const IMAGE_ENDPOINT = `${API_VERSION}/image`;

export const UPLOAD_ENDPOINT = `${API_VERSION}/upload`;
Expand Down
26 changes: 23 additions & 3 deletions src/mocks/handlers/album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,34 @@ const AuthenticationRequiredResponse = {

const SuccessResponse = {
data: {
id: 'ybqNtEF',
deletehash: 'KCsF6XvjfqpImI8',
id: 'XtMnA',
title: 'Meme album',
description: 'Dank memes',
image_count: 22,
images: [
{
id: '2dAns',
title: null,
description: null,
datetime: 1316635799,
type: 'image/gif',
link: 'https://i.imgur.com/2dAns.gif',
},
{
id: 'snAd2',
title: null,
description: null,
datetime: 1316635800,
type: 'image/jpeg',
link: 'https://i.imgur.com/2dAns.jpg',
},
],
},
success: true,
status: 200,
};

export const postHandler: Handler = (req, res, ctx) => {
export const getHandler: Handler = (req, res, ctx) => {
if (!req.headers.has('authorization')) {
return res(ctx.status(401), ctx.json(AuthenticationRequiredResponse));
}
Expand Down
2 changes: 1 addition & 1 deletion src/mocks/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export const handlers = [
rest.get('https://api.imgur.com/3/credits', credits.getHandler),

// album
rest.post('https://api.imgur.com/3/album', album.postHandler),
rest.get('https://api.imgur.com/3/album/:id', album.getHandler),
];

0 comments on commit 1b2c55d

Please sign in to comment.