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 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
261 additions
and
9 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
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
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ImgurClient } from '../client'; | ||
import { getGallery, GalleryOptions, constructGalleryUrl } from './getGallery'; | ||
|
||
test('constructGalleryUrl', () => { | ||
expect( | ||
constructGalleryUrl({} as GalleryOptions).pathname | ||
).toMatchInlineSnapshot(`"/3/gallery/hot/viral"`); | ||
|
||
expect( | ||
constructGalleryUrl({ section: 'hot' }).pathname | ||
).toMatchInlineSnapshot(`"/3/gallery/hot/viral"`); | ||
|
||
expect( | ||
constructGalleryUrl({ section: 'hot', sort: 'top' }).pathname | ||
).toMatchInlineSnapshot(`"/3/gallery/hot/top"`); | ||
|
||
expect( | ||
constructGalleryUrl({ section: 'top', window: 'day' }).pathname | ||
).toMatchInlineSnapshot(`"/3/gallery/top/viral/day"`); | ||
|
||
expect( | ||
constructGalleryUrl({ section: 'user', sort: 'rising' }).pathname | ||
).toMatchInlineSnapshot(`"/3/gallery/user/rising"`); | ||
|
||
const { href, pathname, search } = constructGalleryUrl({ | ||
section: 'user', | ||
sort: 'rising', | ||
showViral: true, | ||
mature: false, | ||
album_previews: true, | ||
}); | ||
expect(pathname).toMatchInlineSnapshot(`"/3/gallery/user/rising"`); | ||
expect(search).toMatchInlineSnapshot( | ||
`"?showViral=true&mature=false&album_previews=true"` | ||
); | ||
expect(href).toMatchInlineSnapshot( | ||
`"https://api.imgur.com/3/gallery/user/rising?showViral=true&mature=false&album_previews=true"` | ||
); | ||
}); | ||
|
||
test('returns an image response', async () => { | ||
const accessToken = 'abc123'; | ||
const client = new ImgurClient({ accessToken }); | ||
const response = await getGallery(client, { section: 'hot' }); | ||
expect(response).toMatchInlineSnapshot(` | ||
Object { | ||
"data": Array [ | ||
Object { | ||
"description": "gallery-description", | ||
"id": "ans7sd", | ||
"images": Array [ | ||
Object { | ||
"description": null, | ||
"id": "4yMKKLTz", | ||
"link": "https://i.imgur.com/4yMKKLTz.jpg", | ||
"title": null, | ||
}, | ||
], | ||
"link": "https://imgur.com/a/abc123", | ||
"title": "gallery-title", | ||
}, | ||
], | ||
"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,85 @@ | ||
import { ImgurClient } from '../client'; | ||
import { GALLERY_ENDPOINT, IMGUR_API_PREFIX } from '../common/endpoints'; | ||
import { GalleryData } from '../common/types'; | ||
|
||
export type CommonSectionProps = { | ||
sort?: 'viral' | 'top' | 'time'; | ||
page?: number; | ||
}; | ||
|
||
export type HotSection = CommonSectionProps & { | ||
section: 'hot'; | ||
}; | ||
|
||
export type TopSection = CommonSectionProps & { | ||
section: 'top'; | ||
window?: 'day' | 'week' | 'month' | 'year' | 'all'; | ||
}; | ||
|
||
export type UserSection = Omit<CommonSectionProps, 'sort'> & { | ||
section: 'user'; | ||
sort?: 'viral' | 'top' | 'time' | 'rising'; | ||
}; | ||
|
||
export type SectionOptions = HotSection | TopSection | UserSection; | ||
|
||
export type PresentationOptions = { | ||
showViral?: boolean; | ||
mature?: boolean; | ||
album_previews?: boolean; | ||
}; | ||
|
||
export type GalleryOptions = SectionOptions & PresentationOptions; | ||
|
||
const defaultOptions: GalleryOptions = { | ||
section: 'hot', | ||
sort: 'viral', | ||
}; | ||
|
||
export function constructGalleryUrl(options: GalleryOptions) { | ||
const mergedOptions = Object.assign({}, defaultOptions, options); | ||
|
||
let uri = `${mergedOptions.section}`; | ||
|
||
if (mergedOptions.sort) { | ||
uri += `/${mergedOptions.sort}`; | ||
} | ||
|
||
if (mergedOptions.section === 'top' && mergedOptions.window) { | ||
uri += `/${mergedOptions.window}`; | ||
} | ||
|
||
if (mergedOptions.page) { | ||
uri += `/${mergedOptions.page}`; | ||
} | ||
|
||
const url = new URL(`${IMGUR_API_PREFIX}/${GALLERY_ENDPOINT}/${uri}`); | ||
|
||
if (mergedOptions.showViral !== undefined) { | ||
url.searchParams.append('showViral', mergedOptions.showViral.toString()); | ||
} | ||
|
||
if (mergedOptions.mature !== undefined) { | ||
url.searchParams.append('mature', mergedOptions.mature.toString()); | ||
} | ||
|
||
if (mergedOptions.album_previews !== undefined) { | ||
url.searchParams.append( | ||
'album_previews', | ||
mergedOptions.album_previews.toString() | ||
); | ||
} | ||
|
||
return url; | ||
} | ||
|
||
export async function getGallery( | ||
client: ImgurClient, | ||
options: GalleryOptions = defaultOptions | ||
) { | ||
const { pathname } = constructGalleryUrl(options); | ||
// since we're using prefixUrl with got, we have to remove the starting slash or it'll throw | ||
const finalPathname = pathname.slice(1); | ||
|
||
return (await client.request(finalPathname).json()) as GalleryData; | ||
} |
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 @@ | ||
export * from './getGallery'; |