From 0488d7b8cceba58f6d66da5592fd5127b2d53eaf Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Sat, 27 Mar 2021 16:52:45 -0400 Subject: [PATCH 1/6] feat: add deleteImage --- README.md | 12 ++++++++++++ src/client.ts | 6 +++++- src/image/deleteImage.test.ts | 15 +++++++++++++++ src/image/deleteImage.ts | 15 +++++++++++++++ src/image/index.ts | 1 + 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/image/deleteImage.test.ts create mode 100644 src/image/deleteImage.ts diff --git a/README.md b/README.md index fa47eabb..e7e14068 100644 --- a/README.md +++ b/README.md @@ -125,3 +125,15 @@ The progress object looks like the following: | `transferred` | total number of bytes transferred thus far | | `total` | total number of bytes to be transferred | | `id` | unique id for the media being transferred; useful when uploading multiple things concurrently | + +### Delete an image + +Requires an image hash or delete hash, which are obtained in an image upload response + +```ts +import { ImgurClient } from 'imgur'; + +const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); + +client.delete('someImageHash'); +``` diff --git a/src/client.ts b/src/client.ts index ee5c0794..cd7649f0 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,7 +1,7 @@ import { EventEmitter } from 'events'; import got, { ExtendOptions, Got } from 'got'; import { getAuthorizationHeader, Credentials } from './helpers'; -import { getImage, upload, Payload } from './image'; +import { deleteImage, getImage, upload, Payload } from './image'; import { IMGUR_API_PREFIX } from './helpers'; type ImgurApiResponse = { @@ -44,6 +44,10 @@ export class ImgurClient extends EventEmitter { return this.gotExtended.extend(options)(url); } + async deleteImage(imageHash: string) { + return deleteImage(this, imageHash); + } + async getImage(imageHash: string) { return getImage(this, imageHash); } diff --git a/src/image/deleteImage.test.ts b/src/image/deleteImage.test.ts new file mode 100644 index 00000000..6bf43183 --- /dev/null +++ b/src/image/deleteImage.test.ts @@ -0,0 +1,15 @@ +import { ImgurClient } from '../client'; +import { deleteImage } from './deleteImage'; + +test('delete works successfully', async () => { + const accessToken = 'abc123'; + const client = new ImgurClient({ accessToken }); + const response = await deleteImage(client, 'CEddrgP'); + expect(response).toMatchInlineSnapshot(` + Object { + "data": true, + "status": 200, + "success": true, + } + `); +}); diff --git a/src/image/deleteImage.ts b/src/image/deleteImage.ts new file mode 100644 index 00000000..3fc6b9a8 --- /dev/null +++ b/src/image/deleteImage.ts @@ -0,0 +1,15 @@ +import { ImgurClient } from '../client'; +import { IMAGE_ENDPOINT } from '../helpers'; + +type DeleteResponse = { + data: true; + success: true; + status: 200; +}; + +export async function deleteImage(client: ImgurClient, imageHash: string) { + const url = `${IMAGE_ENDPOINT}/${imageHash}`; + return (await client + .request(url, { method: 'DELETE' }) + .json()) as DeleteResponse; +} diff --git a/src/image/index.ts b/src/image/index.ts index 00140c15..4173f72b 100644 --- a/src/image/index.ts +++ b/src/image/index.ts @@ -1,2 +1,3 @@ +export * from './deleteImage'; export * from './getImage'; export * from './upload'; From 5a767c7adeabf94a3846046e9d4c39393296435e Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Sat, 27 Mar 2021 23:50:13 -0400 Subject: [PATCH 2/6] feat: add updateImage --- README.md | 37 +++++++++++++++++ src/client.ts | 14 ++++++- src/helpers/createForm.ts | 52 +++++++++++++++++++++++ src/helpers/index.ts | 1 + src/image/index.ts | 1 + src/image/updateImage.test.ts | 77 +++++++++++++++++++++++++++++++++++ src/image/updateImage.ts | 46 +++++++++++++++++++++ src/image/upload.ts | 56 +------------------------ 8 files changed, 228 insertions(+), 56 deletions(-) create mode 100644 src/helpers/createForm.ts create mode 100644 src/image/updateImage.test.ts create mode 100644 src/image/updateImage.ts diff --git a/README.md b/README.md index e7e14068..f7a2de4f 100644 --- a/README.md +++ b/README.md @@ -137,3 +137,40 @@ const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); client.delete('someImageHash'); ``` + +### Update image information + +Update the title and/or description of an image + +```ts +import { ImgurClient } from 'imgur'; + +const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); + +client.updateImage({ + imageHash: 'someImageHash', + title: 'A new title', + description: 'A new description', +}); +``` + +Update multiple images at once: + +```ts +import { ImgurClient } from 'imgur'; + +const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); + +client.updateImage([ + { + imageHash: 'someImageHash', + title: 'A new title', + description: 'A new description', + }, + { + imageHash: 'anotherImageHash', + title: 'A better title', + description: 'A better description', + }, +]); +``` diff --git a/src/client.ts b/src/client.ts index cd7649f0..5fc95dcf 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,8 +1,14 @@ import { EventEmitter } from 'events'; import got, { ExtendOptions, Got } from 'got'; import { getAuthorizationHeader, Credentials } from './helpers'; -import { deleteImage, getImage, upload, Payload } from './image'; -import { IMGUR_API_PREFIX } from './helpers'; +import { + deleteImage, + getImage, + upload, + updateImage, + UpdateImagePayload, +} from './image'; +import { IMGUR_API_PREFIX, Payload } from './helpers'; type ImgurApiResponse = { data: Record; @@ -52,6 +58,10 @@ export class ImgurClient extends EventEmitter { return getImage(this, imageHash); } + updateImage(payload: UpdateImagePayload | UpdateImagePayload[]) { + return updateImage(this, payload); + } + async upload(payload: string | string[] | Payload | Payload[]) { return upload(this, payload); } diff --git a/src/helpers/createForm.ts b/src/helpers/createForm.ts new file mode 100644 index 00000000..2de3a6d2 --- /dev/null +++ b/src/helpers/createForm.ts @@ -0,0 +1,52 @@ +import { createReadStream } from 'fs'; +import FormData from 'form-data'; + +export interface Payload { + image?: string; + video?: string; + type?: 'file' | 'url' | 'base64'; + name?: string; + title?: string; + description?: string; + album?: string; + disable_audio?: '1' | '0'; +} + +export function isVideo(payload: string | Payload) { + if (typeof payload === 'string') { + return false; + } + + return typeof payload.video !== 'undefined' && payload.video; +} + +export function getSource(payload: string | Payload) { + if (typeof payload === 'string') { + return payload; + } + + if (isVideo(payload)) { + return payload.video; + } else { + return payload.image; + } +} + +export function createForm(payload: string | Payload) { + const form = new FormData(); + + if (typeof payload === 'string') { + form.append('image', createReadStream(payload)); + return form; + } + + for (const [key, value] of Object.entries(payload)) { + if (key === 'image' || key === 'video') { + if (!payload.type || payload.type === 'file') + form.append(key, createReadStream(value)); + } else { + form.append(key, value); + } + } + return form; +} diff --git a/src/helpers/index.ts b/src/helpers/index.ts index dc08a9f3..5759324e 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -1,3 +1,4 @@ export * from './getAuthorizationHeader'; export * from './credentials'; export * from './endpoints'; +export * from './createForm'; diff --git a/src/image/index.ts b/src/image/index.ts index 4173f72b..fce49a28 100644 --- a/src/image/index.ts +++ b/src/image/index.ts @@ -1,3 +1,4 @@ export * from './deleteImage'; export * from './getImage'; +export * from './updateImage'; export * from './upload'; diff --git a/src/image/updateImage.test.ts b/src/image/updateImage.test.ts new file mode 100644 index 00000000..150bc44a --- /dev/null +++ b/src/image/updateImage.test.ts @@ -0,0 +1,77 @@ +import { ImgurClient } from '../client'; +import { updateImage } from './updateImage'; + +test('update one image with all props', async () => { + const accessToken = 'abc123'; + const client = new ImgurClient({ accessToken }); + const response = await updateImage(client, { + imageHash: 'abc123', + title: 'new title', + description: 'description', + }); + expect(response).toMatchInlineSnapshot(` + Object { + "data": true, + "status": 200, + "success": true, + } + `); +}); + +test('update one image with title only', async () => { + const accessToken = 'abc123'; + const client = new ImgurClient({ accessToken }); + const response = await updateImage(client, { + imageHash: 'abc123', + title: 'new title', + }); + expect(response).toMatchInlineSnapshot(` + Object { + "data": true, + "status": 200, + "success": true, + } + `); +}); + +test('update one image without title or description', async () => { + const accessToken = 'abc123'; + const client = new ImgurClient({ accessToken }); + const response = updateImage(client, { + imageHash: 'abc123', + }); + expect(response).rejects.toThrowErrorMatchingInlineSnapshot( + `"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, + }, + ] + `); +}); diff --git a/src/image/updateImage.ts b/src/image/updateImage.ts new file mode 100644 index 00000000..00e2dbbe --- /dev/null +++ b/src/image/updateImage.ts @@ -0,0 +1,46 @@ +import { ImgurClient } from '../client'; +import { createForm, IMAGE_ENDPOINT, Payload } from '../helpers'; + +export type UpdateImagePayload = { imageHash: string } & Pick< + Payload, + 'title' | 'description' +>; + +function isValidUpdatePayload(p: UpdateImagePayload) { + return typeof p.title === 'string' || typeof p.description === 'string'; +} + +export async function updateImage( + client: ImgurClient, + payload: UpdateImagePayload | UpdateImagePayload[] +) { + 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); + return client.request(url, { + method: 'POST', + body: form, + resolveBodyOnly: true, + }); + }); + + return await Promise.all(promises); + } + + if (!isValidUpdatePayload(payload)) { + throw new Error('Update requires a title and/or description'); + } + + const url = `${IMAGE_ENDPOINT}/${payload.imageHash}`; + const form = createForm(payload); + return await client.request(url, { + method: 'POST', + body: form, + resolveBodyOnly: true, + }); +} diff --git a/src/image/upload.ts b/src/image/upload.ts index cd815141..f3d96520 100644 --- a/src/image/upload.ts +++ b/src/image/upload.ts @@ -1,58 +1,6 @@ import { ImgurClient } from '../client'; -import { UPLOAD_ENDPOINT } from '../helpers'; -import { createReadStream } from 'fs'; -import FormData from 'form-data'; -import { Progress } from 'got/dist/source'; - -export interface Payload { - image?: string; - video?: string; - type?: 'file' | 'url' | 'base64'; - name?: string; - title?: string; - description?: string; - album?: string; - disable_audio?: '1' | '0'; -} - -function isVideo(payload: string | Payload) { - if (typeof payload === 'string') { - return false; - } - - return typeof payload.video !== 'undefined' && payload.video; -} - -function getSource(payload: string | Payload) { - if (typeof payload === 'string') { - return payload; - } - - if (isVideo(payload)) { - return payload.video; - } else { - return payload.image; - } -} - -function createForm(payload: string | Payload) { - const form = new FormData(); - - if (typeof payload === 'string') { - form.append('image', createReadStream(payload)); - return form; - } - - for (const [key, value] of Object.entries(payload)) { - if (key === 'image' || key === 'video') { - if (!payload.type || payload.type === 'file') - form.append(key, createReadStream(value)); - } else { - form.append(key, value); - } - } - return form; -} +import { createForm, getSource, Payload, UPLOAD_ENDPOINT } from '../helpers'; +import { Progress } from 'got'; export async function upload( client: ImgurClient, From 251a9702a85c6267ccbacc8f36ca5a168fccd334 Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Sat, 27 Mar 2021 23:58:32 -0400 Subject: [PATCH 3/6] feat: add favoriteImage --- README.md | 10 ++++++++++ src/client.ts | 5 +++++ src/image/favoriteImage.test.ts | 15 +++++++++++++++ src/image/favoriteImage.ts | 15 +++++++++++++++ src/image/index.ts | 1 + 5 files changed, 46 insertions(+) create mode 100644 src/image/favoriteImage.test.ts create mode 100644 src/image/favoriteImage.ts 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'; From 94daf35faac4c28f933c5163bb5c85b89fc45e6b Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Sun, 28 Mar 2021 02:42:12 -0400 Subject: [PATCH 4/6] refactor: move helpers into common structure --- src/client.ts | 11 ++--- src/{helpers => common}/endpoints.ts | 0 src/common/types.ts | 47 +++++++++++++++++++ .../createForm.ts => common/utils.ts} | 12 +---- .../getAuthorizationHeader.test.ts | 2 +- src/{helpers => }/getAuthorizationHeader.ts | 6 +-- src/helpers/credentials.ts | 26 ---------- src/helpers/index.ts | 4 -- src/image/deleteImage.ts | 6 +-- src/image/favoriteImage.ts | 2 +- src/image/getImage.ts | 6 +-- src/image/updateImage.ts | 12 +++-- src/image/upload.ts | 5 +- 13 files changed, 73 insertions(+), 66 deletions(-) rename src/{helpers => common}/endpoints.ts (100%) create mode 100644 src/common/types.ts rename src/{helpers/createForm.ts => common/utils.ts} (81%) rename src/{helpers => }/getAuthorizationHeader.test.ts (96%) rename src/{helpers => }/getAuthorizationHeader.ts (90%) delete mode 100644 src/helpers/credentials.ts delete mode 100644 src/helpers/index.ts diff --git a/src/client.ts b/src/client.ts index 550a02fe..8552c80c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; import got, { ExtendOptions, Got } from 'got'; -import { getAuthorizationHeader, Credentials } from './helpers'; +import { getAuthorizationHeader } from './getAuthorizationHeader'; import { deleteImage, favoriteImage, @@ -9,13 +9,8 @@ import { updateImage, UpdateImagePayload, } from './image'; -import { IMGUR_API_PREFIX, Payload } from './helpers'; - -type ImgurApiResponse = { - data: Record; - status: number; - success: boolean; -}; +import { IMGUR_API_PREFIX } from './common/endpoints'; +import { Credentials, Payload } from './common/types'; const USERAGENT = 'imgur/next (https://github.com/kaimallea/node-imgur)'; diff --git a/src/helpers/endpoints.ts b/src/common/endpoints.ts similarity index 100% rename from src/helpers/endpoints.ts rename to src/common/endpoints.ts diff --git a/src/common/types.ts b/src/common/types.ts new file mode 100644 index 00000000..d910a482 --- /dev/null +++ b/src/common/types.ts @@ -0,0 +1,47 @@ +export interface AccessToken { + accessToken: string; +} + +export interface ClientId { + clientId: string; +} + +export interface Login extends ClientId { + username: string; + password: string; +} + +export type Credentials = AccessToken | ClientId | Login; + +export function isAccessToken(arg: any): arg is AccessToken { + return arg.accessToken !== undefined; +} + +export function isClientId(arg: any): arg is ClientId { + return arg.clientId !== undefined; +} + +export function isLogin(arg: any): arg is Login { + return ( + arg.clientId !== undefined && + arg.username !== undefined && + arg.password !== undefined + ); +} + +export interface ImgurApiResponse { + data: Record | string | boolean; + status: number; + success: boolean; +} + +export interface Payload { + image?: string; + video?: string; + type?: 'file' | 'url' | 'base64'; + name?: string; + title?: string; + description?: string; + album?: string; + disable_audio?: '1' | '0'; +} diff --git a/src/helpers/createForm.ts b/src/common/utils.ts similarity index 81% rename from src/helpers/createForm.ts rename to src/common/utils.ts index 2de3a6d2..3ff64a2f 100644 --- a/src/helpers/createForm.ts +++ b/src/common/utils.ts @@ -1,16 +1,6 @@ import { createReadStream } from 'fs'; import FormData from 'form-data'; - -export interface Payload { - image?: string; - video?: string; - type?: 'file' | 'url' | 'base64'; - name?: string; - title?: string; - description?: string; - album?: string; - disable_audio?: '1' | '0'; -} +import { Payload } from './types'; export function isVideo(payload: string | Payload) { if (typeof payload === 'string') { diff --git a/src/helpers/getAuthorizationHeader.test.ts b/src/getAuthorizationHeader.test.ts similarity index 96% rename from src/helpers/getAuthorizationHeader.test.ts rename to src/getAuthorizationHeader.test.ts index 77e916bb..fba4cd23 100644 --- a/src/helpers/getAuthorizationHeader.test.ts +++ b/src/getAuthorizationHeader.test.ts @@ -1,4 +1,4 @@ -import { ImgurClient } from '../client'; +import { ImgurClient } from './client'; import { getAuthorizationHeader } from './getAuthorizationHeader'; test('returns provided access code in bearer header', async () => { diff --git a/src/helpers/getAuthorizationHeader.ts b/src/getAuthorizationHeader.ts similarity index 90% rename from src/helpers/getAuthorizationHeader.ts rename to src/getAuthorizationHeader.ts index 3e8b9fc7..d77ffce6 100644 --- a/src/helpers/getAuthorizationHeader.ts +++ b/src/getAuthorizationHeader.ts @@ -1,6 +1,6 @@ -import { isAccessToken, isClientId, isLogin } from './credentials'; -import { ImgurClient } from '../client'; -import { IMGUR_API_PREFIX, AUTHORIZE_ENDPOINT } from '../helpers'; +import { isAccessToken, isClientId, isLogin } from './common/types'; +import { ImgurClient } from './client'; +import { IMGUR_API_PREFIX, AUTHORIZE_ENDPOINT } from './common/endpoints'; export async function getAuthorizationHeader(client: ImgurClient) { if (isAccessToken(client.credentials)) { diff --git a/src/helpers/credentials.ts b/src/helpers/credentials.ts deleted file mode 100644 index 7acca632..00000000 --- a/src/helpers/credentials.ts +++ /dev/null @@ -1,26 +0,0 @@ -export type AccessToken = { - accessToken: string; -}; - -export type ClientId = { - clientId: string; -}; - -export type Login = ClientId & { - username: string; - password: string; -}; - -export type Credentials = AccessToken | ClientId | Login; - -export function isAccessToken(arg: any): arg is AccessToken { - return arg.accessToken !== undefined; -} - -export function isLogin(arg: any): arg is Login { - return arg.username !== undefined && arg.password !== undefined; -} - -export function isClientId(arg: any): arg is ClientId { - return arg.clientId !== undefined; -} diff --git a/src/helpers/index.ts b/src/helpers/index.ts deleted file mode 100644 index 5759324e..00000000 --- a/src/helpers/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './getAuthorizationHeader'; -export * from './credentials'; -export * from './endpoints'; -export * from './createForm'; diff --git a/src/image/deleteImage.ts b/src/image/deleteImage.ts index 3fc6b9a8..0db8fc4d 100644 --- a/src/image/deleteImage.ts +++ b/src/image/deleteImage.ts @@ -1,11 +1,11 @@ import { ImgurClient } from '../client'; -import { IMAGE_ENDPOINT } from '../helpers'; +import { IMAGE_ENDPOINT } from '../common/endpoints'; -type DeleteResponse = { +export interface DeleteResponse { data: true; success: true; status: 200; -}; +} export async function deleteImage(client: ImgurClient, imageHash: string) { const url = `${IMAGE_ENDPOINT}/${imageHash}`; diff --git a/src/image/favoriteImage.ts b/src/image/favoriteImage.ts index 0d0ca141..4f3f2a43 100644 --- a/src/image/favoriteImage.ts +++ b/src/image/favoriteImage.ts @@ -1,5 +1,5 @@ import { ImgurClient } from '../client'; -import { IMAGE_ENDPOINT } from '../helpers'; +import { IMAGE_ENDPOINT } from '../common/endpoints'; type FavoriteResponse = { data: 'favorited'; diff --git a/src/image/getImage.ts b/src/image/getImage.ts index 4579a636..eca88ae2 100644 --- a/src/image/getImage.ts +++ b/src/image/getImage.ts @@ -1,7 +1,7 @@ import { ImgurClient } from '../client'; -import { IMAGE_ENDPOINT } from '../helpers'; +import { IMAGE_ENDPOINT } from '../common/endpoints'; -type ImageResponse = { +export interface ImageResponse { data?: { id?: string; title?: string | null; @@ -39,7 +39,7 @@ type ImageResponse = { }; success?: boolean; status?: number; -}; +} export async function getImage( client: ImgurClient, diff --git a/src/image/updateImage.ts b/src/image/updateImage.ts index 00e2dbbe..8ddb8297 100644 --- a/src/image/updateImage.ts +++ b/src/image/updateImage.ts @@ -1,10 +1,12 @@ import { ImgurClient } from '../client'; -import { createForm, IMAGE_ENDPOINT, Payload } from '../helpers'; +import { IMAGE_ENDPOINT } from '../common/endpoints'; +import { createForm } from '../common/utils'; +import { Payload } from '../common/types'; -export type UpdateImagePayload = { imageHash: string } & Pick< - Payload, - 'title' | 'description' ->; +export interface UpdateImagePayload + extends Pick { + imageHash: string; +} function isValidUpdatePayload(p: UpdateImagePayload) { return typeof p.title === 'string' || typeof p.description === 'string'; diff --git a/src/image/upload.ts b/src/image/upload.ts index f3d96520..ebcbfdbe 100644 --- a/src/image/upload.ts +++ b/src/image/upload.ts @@ -1,5 +1,8 @@ import { ImgurClient } from '../client'; -import { createForm, getSource, Payload, UPLOAD_ENDPOINT } from '../helpers'; +import { createForm, getSource } from '../common/utils'; +import { Payload } from '../common/types'; +import { UPLOAD_ENDPOINT } from '../common/endpoints'; + import { Progress } from 'got'; export async function upload( From c053ad8466b27215a83e1c1a912509d01f2bbeaa Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Sun, 28 Mar 2021 02:48:30 -0400 Subject: [PATCH 5/6] style: remove async from funcs that don't use await --- src/client.ts | 6 +++--- src/image/getImage.ts | 5 +---- src/image/upload.test.ts | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/client.ts b/src/client.ts index 8552c80c..34fc6a64 100644 --- a/src/client.ts +++ b/src/client.ts @@ -46,7 +46,7 @@ export class ImgurClient extends EventEmitter { return this.gotExtended.extend(options)(url); } - async deleteImage(imageHash: string) { + deleteImage(imageHash: string) { return deleteImage(this, imageHash); } @@ -54,7 +54,7 @@ export class ImgurClient extends EventEmitter { return favoriteImage(this, imageHash); } - async getImage(imageHash: string) { + getImage(imageHash: string) { return getImage(this, imageHash); } @@ -62,7 +62,7 @@ export class ImgurClient extends EventEmitter { return updateImage(this, payload); } - async upload(payload: string | string[] | Payload | Payload[]) { + upload(payload: string | string[] | Payload | Payload[]) { return upload(this, payload); } } diff --git a/src/image/getImage.ts b/src/image/getImage.ts index eca88ae2..2df09dae 100644 --- a/src/image/getImage.ts +++ b/src/image/getImage.ts @@ -41,10 +41,7 @@ export interface ImageResponse { status?: number; } -export async function getImage( - client: ImgurClient, - imageHash: string -): Promise { +export async function getImage(client: ImgurClient, imageHash: string) { const url = `${IMAGE_ENDPOINT}/${imageHash}`; return (await client.request(url).json()) as ImageResponse; } diff --git a/src/image/upload.test.ts b/src/image/upload.test.ts index 2166c54f..33a2ffa5 100644 --- a/src/image/upload.test.ts +++ b/src/image/upload.test.ts @@ -153,7 +153,7 @@ describe('test file uploads', () => { const eventHandler = jest.fn(); client.on('uploadProgress', eventHandler); - const response = await upload(client, { + await upload(client, { video, title: 'trailer for my new stream', description: 'yolo', From daa555c69a555b745b3c64f5df2b8d8a28826b40 Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Sun, 28 Mar 2021 02:59:49 -0400 Subject: [PATCH 6/6] docs(readme): remove repeated import/instantiate example --- README.md | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0689887e..9c82fe45 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ If you don't have any credentials, you'll need to: 1. [Create an Imgur account](https://imgur.com/register) 1. [Register an application](https://api.imgur.com/#registerapp) +### **⚠️ For brevity, the rest of the examples will leave out the import and/or instantiation step.** + ### Upload one or more images and videos You can upload one or more files by simply passing a path to a file or array of paths to multiple files. @@ -100,8 +102,6 @@ Acceptable key/values match what [the Imgur API expects](https://apidocs.imgur.c Instances of `ImgurClient` emit `uploadProgress` events so that you can track progress with event listeners. ```ts -import { ImgurClient } from 'imgur'; - const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); client.on('uploadProgress', (progress) => console.log(progress)); @@ -131,10 +131,6 @@ The progress object looks like the following: Requires an image hash or delete hash, which are obtained in an image upload response ```ts -import { ImgurClient } from 'imgur'; - -const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); - client.delete('someImageHash'); ``` @@ -143,10 +139,6 @@ client.delete('someImageHash'); Update the title and/or description of an image ```ts -import { ImgurClient } from 'imgur'; - -const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); - client.updateImage({ imageHash: 'someImageHash', title: 'A new title', @@ -157,10 +149,6 @@ client.updateImage({ Update multiple images at once: ```ts -import { ImgurClient } from 'imgur'; - -const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); - client.updateImage([ { imageHash: 'someImageHash', @@ -178,9 +166,5 @@ client.updateImage([ Favorite an image: ```ts -import { ImgurClient } from 'imgur'; - -const client = new ImgurClient({ accessToken: process.env.ACCESS_TOKEN }); - client.favoriteImage('someImageHash'); ```