diff --git a/README.md b/README.md index f7a2de4f..0689887e 100644 --- a/README.md +++ b/README.md @@ -174,3 +174,13 @@ client.updateImage([ }, ]); ``` + +Favorite an image: + +```ts +import { ImgurClient } from 'imgur'; + +const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); + +client.favoriteImage('someImageHash'); +``` diff --git a/src/client.ts b/src/client.ts index 5fc95dcf..550a02fe 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3,6 +3,7 @@ import got, { ExtendOptions, Got } from 'got'; import { getAuthorizationHeader, Credentials } from './helpers'; import { deleteImage, + favoriteImage, getImage, upload, updateImage, @@ -54,6 +55,10 @@ export class ImgurClient extends EventEmitter { return deleteImage(this, imageHash); } + favoriteImage(imageHash: string) { + return favoriteImage(this, imageHash); + } + async getImage(imageHash: string) { return getImage(this, imageHash); } diff --git a/src/image/favoriteImage.test.ts b/src/image/favoriteImage.test.ts new file mode 100644 index 00000000..bdc3911f --- /dev/null +++ b/src/image/favoriteImage.test.ts @@ -0,0 +1,15 @@ +import { ImgurClient } from '../client'; +import { favoriteImage } from './favoriteImage'; + +test('favorite works successfully', async () => { + const accessToken = 'abc123'; + const client = new ImgurClient({ accessToken }); + const response = await favoriteImage(client, 'CEddrgP'); + expect(response).toMatchInlineSnapshot(` + Object { + "data": "favorited", + "status": 200, + "success": true, + } + `); +}); diff --git a/src/image/favoriteImage.ts b/src/image/favoriteImage.ts new file mode 100644 index 00000000..0d0ca141 --- /dev/null +++ b/src/image/favoriteImage.ts @@ -0,0 +1,15 @@ +import { ImgurClient } from '../client'; +import { IMAGE_ENDPOINT } from '../helpers'; + +type FavoriteResponse = { + data: 'favorited'; + success: true; + status: 200; +}; + +export async function favoriteImage(client: ImgurClient, imageHash: string) { + const url = `${IMAGE_ENDPOINT}/${imageHash}/favorite`; + return (await client + .request(url, { method: 'POST' }) + .json()) as FavoriteResponse; +} diff --git a/src/image/index.ts b/src/image/index.ts index fce49a28..d0a9a330 100644 --- a/src/image/index.ts +++ b/src/image/index.ts @@ -1,4 +1,5 @@ export * from './deleteImage'; +export * from './favoriteImage'; export * from './getImage'; export * from './updateImage'; export * from './upload';