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

Commit

Permalink
feat(gallery, account, album, image): implement delete actions
Browse files Browse the repository at this point in the history
scope: client
  • Loading branch information
cacheflow committed Aug 17, 2019
1 parent 1a27da0 commit 545b9e6
Show file tree
Hide file tree
Showing 22 changed files with 232 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,16 @@ test('get() has correct headers', async () => {
method: 'GET',
});
});

test('delete() has correct headers', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('{"success": true}')));
const client = new Client({ access_token: 'abc123' });
await client.delete('https://www.blah.com');
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith('https://www.blah.com', {
headers: {
Authorization: 'Bearer abc123',
},
method: 'DELETE',
});
});
6 changes: 3 additions & 3 deletions src/account/__tests__/getSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { getSettings } from '../getSettings';
import { ACCOUNT_SETTINGS } from '../../endpoints';

const mockGet = jest.fn();
const mockClient = jest.fn().mockImplementation(() => {
const MockClient = jest.fn().mockImplementation(() => {
return {
get: mockGet,
};
});

beforeEach(() => {
mockClient.mockClear();
MockClient.mockClear();
mockGet.mockClear();
});

test('getAccessToken calls the correct endpoint and resolves', () => {
const mockResponse = '{"success":true}';
mockGet.mockReturnValueOnce(Promise.resolve(mockResponse));

const promiseResponse = getSettings(new mockClient());
const promiseResponse = getSettings(new MockClient());

expect(promiseResponse).resolves.toBe(mockResponse);
expect(mockGet).toHaveBeenCalledTimes(1);
Expand Down
25 changes: 25 additions & 0 deletions src/account/__tests__/unblock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unblock from '../unblock';

const mockGet = jest.fn();
const MockClient = jest.fn().mockImplementation(() => {
return {
delete: mockGet,
};
});

beforeEach(() => {
MockClient.mockClear();
mockGet.mockClear();
});

test('unblock calls the correct endpoint and resolves', () => {
const mockResponse = '{"success":true}';
mockGet.mockReturnValueOnce(Promise.resolve(mockResponse));
const promiseResponse = unblock(new MockClient(), 'john');
expect(promiseResponse).resolves.toBe(mockResponse);
expect(mockGet).toHaveBeenCalledTimes(1);
expect(mockGet).toHaveBeenCalledWith(
'https://api.imgur.com/account/v1/john/block',
'GET',
);
});
1 change: 1 addition & 0 deletions src/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './generateAccessToken';
export * from './getSettings';
export * from './unblock';
10 changes: 10 additions & 0 deletions src/account/unblock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from '../client';
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';
import { createUnblockUrl } from '../helpers/';

export default async function unblock(
client: Client,
username: string,
): Promise<ImgurApiResponse | AuthenticationRequiredResponse> {
return client.delete(`${createUnblockUrl(username)}`, 'GET');
}
10 changes: 10 additions & 0 deletions src/account/unfollowTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from '../client';
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';
import { UNFOLLOW_TAG_URI } from '../endpoints';

export default async function unfollowTag(
client: Client,
tagName: string,
): Promise<ImgurApiResponse | AuthenticationRequiredResponse> {
return client.delete(`${UNFOLLOW_TAG_URI}/${tagName}`);
}
26 changes: 26 additions & 0 deletions src/album/__tests__/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import deleteAlbum from '../delete';

const mockDelete = jest.fn();
const MockClient = jest.fn().mockImplementation(() => {
return {
delete: mockDelete,
};
});

beforeEach(() => {
MockClient.mockClear();
mockDelete.mockClear();
});

const mockAlbumHash = `fc1f5de1-f8dc-4ba7-9f17-52ab9ad8c46c`;

test('deleteAlbum calls the correct endpoint and resolves', () => {
const mockResponse = '{"success":true}';
mockDelete.mockReturnValueOnce(Promise.resolve(mockResponse));
const promiseResponse = deleteAlbum(new MockClient(), mockAlbumHash);
expect(promiseResponse).resolves.toBe(mockResponse);
expect(mockDelete).toHaveBeenCalledTimes(1);
expect(mockDelete).toHaveBeenCalledWith(
`https://api.imgur.com/3/album/${mockAlbumHash}`,
);
});
10 changes: 10 additions & 0 deletions src/album/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from '../client';
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';
import { DELETE_ALBUM_URI } from '../endpoints';

export default async function deleteAlbum(
client: Client,
albumHash: string,
): Promise<ImgurApiResponse | AuthenticationRequiredResponse> {
return client.delete(`${DELETE_ALBUM_URI}${albumHash}`);
}
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 './delete';
15 changes: 15 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,19 @@ export class Client {
body: form,
});
}

/**
* Make a DELETE request to the specified Imgur API endpoint with form data
*
* @param endpoint An Imgur API endpoint
* @param method Default method is DELETE. For certain endpoints like unblocking users you
* need to pass a GET.
* @returns A JSON response object
*/

delete(endpoint: string, method: string = 'DELETE'): Promise<any> {
return this.request(endpoint, {
method,
});
}
}
25 changes: 25 additions & 0 deletions src/comment/__tests__/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import deleteComment from '../delete';

const mockDelete = jest.fn();
const MockClient = jest.fn().mockImplementation(() => {
return {
delete: mockDelete,
};
});

beforeEach(() => {
MockClient.mockClear();
mockDelete.mockClear();
});

test('deleteComment calls the correct endpoint and resolves', () => {
const mockResponse = '{"success":true}';
mockDelete.mockReturnValueOnce(Promise.resolve(mockResponse));

const promiseResponse = deleteComment(new MockClient(), '1234');
expect(promiseResponse).resolves.toBe(mockResponse);
expect(mockDelete).toHaveBeenCalledTimes(1);
expect(mockDelete).toHaveBeenCalledWith(
'https://api.imgur.com/3/comment/1234',
);
});
10 changes: 10 additions & 0 deletions src/comment/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from '../client';
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';
import { DELETE_COMMENT_URI } from '../endpoints';

export default async function deleteComment(
client: Client,
commentId: string,
): Promise<ImgurApiResponse | AuthenticationRequiredResponse> {
return client.delete(`${DELETE_COMMENT_URI}${commentId}`);
}
1 change: 1 addition & 0 deletions src/comment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './delete';
7 changes: 7 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export const ACCESS_TOKEN_URI = 'https://api.imgur.com/oauth2/token';
export const UPLOAD_URI = 'https://api.imgur.com/3/upload';
export const ACCOUNT_SETTINGS = 'https://api.imgur.com/3/account/me/settings';
export const DELETE_IMAGE_URI = 'https://api.imgur.com/3/image/';
export const DELETE_ALBUM_URI = 'https://api.imgur.com/3/album/';
export const DELETE_IMAGE_FROM_GALLERY_URI = 'https://api.imgur.com/3/gallery/';
export const ACCOUNT_URI = 'https://api.imgur.com/account/v1/';
export const UNFOLLOW_TAG_URI =
'https://api.imgur.com/3/account/me/follow/tag/';
export const DELETE_COMMENT_URI = 'https://api.imgur.com/3/comment/';
25 changes: 25 additions & 0 deletions src/gallery/__tests__/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import deleteFromGallery from '../delete';

const mockDelete = jest.fn();
const MockClient = jest.fn().mockImplementation(() => {
return {
delete: mockDelete,
};
});

beforeEach(() => {
MockClient.mockClear();
mockDelete.mockClear();
});

test('deleteFromGallery calls the correct endpoint and resolves', () => {
const mockResponse = '{"success":true}';
mockDelete.mockReturnValueOnce(Promise.resolve(mockResponse));
const mockGalleryHash = '37ad6e9a-7995-4042-a179-0380b7291497';
const promiseResponse = deleteFromGallery(new MockClient(), mockGalleryHash);
expect(promiseResponse).resolves.toBe(mockResponse);
expect(mockDelete).toHaveBeenCalledTimes(1);
expect(mockDelete).toHaveBeenCalledWith(
`https://api.imgur.com/3/gallery/${mockGalleryHash}`,
);
});
10 changes: 10 additions & 0 deletions src/gallery/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from '../client';
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';
import { DELETE_IMAGE_FROM_GALLERY_URI } from '../endpoints';

export default async function deleteFromGallery(
client: Client,
galleryHash: string,
): Promise<ImgurApiResponse | AuthenticationRequiredResponse> {
return client.delete(`${DELETE_IMAGE_FROM_GALLERY_URI}${galleryHash}`);
}
1 change: 1 addition & 0 deletions src/gallery/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './delete';
6 changes: 6 additions & 0 deletions src/helpers/__tests__/createUnblockUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import createUnblockUrl from '../createUnblockUrl';

test('createUnblockUrl returns a constructed unblock url', () => {
const unblockUrl = createUnblockUrl('john');
expect(unblockUrl).toBe('https://api.imgur.com/account/v1/john/block');
});
15 changes: 15 additions & 0 deletions src/helpers/createUnblockUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ACCOUNT_URI } from '../endpoints';

/**
* Accepts username of account to
* be unblocked and returns the constructed unblocked URL endpoint
*
* @param username
* @returns https://api.imgur.com/account/v1/{{username}}/block
*/

const createUnblockUrl = (username: string) => {
return `${ACCOUNT_URI}${username}/block`;
};

export default createUnblockUrl;
4 changes: 4 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import createUnblockUrl from './createUnblockUrl';
import createForm from './createForm';

export { createUnblockUrl, createForm };
13 changes: 13 additions & 0 deletions src/image/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Client } from '../client';
import { PathLike } from 'fs';
import { DELETE_IMAGE_URI } from '../endpoints';
import { ImgurApiResponse, AuthenticationRequiredResponse } from '../responses';

type ImageHash = PathLike;

export default async function deleteImage(
client: Client,
imageHash: ImageHash,
): Promise<ImgurApiResponse | AuthenticationRequiredResponse> {
return client.delete(`${DELETE_IMAGE_URI}/${imageHash}`);
}
1 change: 1 addition & 0 deletions src/image/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './upload';
export * from './delete';

0 comments on commit 545b9e6

Please sign in to comment.