This repository has been archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from kaimallea/next-dev
Ship latest changes to next channel
- Loading branch information
Showing
19 changed files
with
360 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, unknown> | 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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createReadStream } from 'fs'; | ||
import FormData from 'form-data'; | ||
import { Payload } from './types'; | ||
|
||
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; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/helpers/getAuthorizationHeader.test.ts → src/getAuthorizationHeader.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/helpers/getAuthorizationHeader.ts → src/getAuthorizationHeader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ImgurClient } from '../client'; | ||
import { IMAGE_ENDPOINT } from '../common/endpoints'; | ||
|
||
export interface 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ImgurClient } from '../client'; | ||
import { IMAGE_ENDPOINT } from '../common/endpoints'; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './deleteImage'; | ||
export * from './favoriteImage'; | ||
export * from './getImage'; | ||
export * from './updateImage'; | ||
export * from './upload'; |
Oops, something went wrong.