This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
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 #162 from unsplash/feature/topics
Add topics endpoints
- Loading branch information
Showing
7 changed files
with
242 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { handleFeedResponse } from '../../helpers/feed'; | ||
import { compactDefined, flow } from '../../helpers/fp'; | ||
import * as Query from '../../helpers/query'; | ||
import { createRequestGenerator } from '../../helpers/request'; | ||
import { castResponse } from '../../helpers/response'; | ||
import { OrientationParam, PaginationParams } from '../../types/request'; | ||
import * as Photo from '../photos/types'; | ||
import * as Topic from './types'; | ||
|
||
type TopicIdOrSlug = { | ||
topicIdOrSlug: string; | ||
}; | ||
|
||
const BASE_TOPIC_PATH = '/topics'; | ||
const getTopicPath = ({ topicIdOrSlug }: TopicIdOrSlug) => `${BASE_TOPIC_PATH}/${topicIdOrSlug}`; | ||
const getTopicPhotosPath = flow(getTopicPath, topicPath => `${topicPath}/photos`); | ||
|
||
type TopicOrderBy = 'latest' | 'oldest' | 'position' | 'featured'; | ||
|
||
export const list = createRequestGenerator({ | ||
handleRequest: ({ | ||
page, | ||
perPage, | ||
orderBy, | ||
topicIdsOrSlugs, | ||
}: Omit<PaginationParams, 'orderBy'> & { | ||
/** | ||
* default: `position` | ||
*/ | ||
orderBy?: TopicOrderBy; | ||
topicIdsOrSlugs?: string[]; | ||
}) => ({ | ||
pathname: BASE_TOPIC_PATH, | ||
query: compactDefined({ | ||
...Query.getFeedParams({ page, perPage }), | ||
ids: topicIdsOrSlugs?.join(','), | ||
order_by: orderBy, | ||
}), | ||
}), | ||
handleResponse: handleFeedResponse<Topic.Basic>(), | ||
}); | ||
|
||
export const get = createRequestGenerator({ | ||
handleRequest: ({ topicIdOrSlug }: TopicIdOrSlug) => ({ | ||
pathname: getTopicPath({ topicIdOrSlug }), | ||
query: {}, | ||
}), | ||
handleResponse: castResponse<Topic.Full>(), | ||
}); | ||
|
||
export const getPhotos = createRequestGenerator({ | ||
handleRequest: ({ | ||
topicIdOrSlug, | ||
orientation, | ||
...feedParams | ||
}: TopicIdOrSlug & PaginationParams & OrientationParam) => ({ | ||
pathname: getTopicPhotosPath({ topicIdOrSlug }), | ||
query: compactDefined({ | ||
...Query.getFeedParams(feedParams), | ||
orientation, | ||
}), | ||
}), | ||
handleResponse: handleFeedResponse<Photo.Basic>(), | ||
}); |
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,31 @@ | ||
import { NonEmptyArray } from '../../helpers/typescript'; | ||
import { Entity } from '../../types/entities'; | ||
|
||
import * as Photo from '../photos/types'; | ||
import * as User from '../users/types'; | ||
|
||
export interface Basic extends Entity { | ||
cover_photo: Photo.Basic | null; | ||
current_user_contributions: Photo.VeryBasic[]; | ||
description: string | null; | ||
ends_at: string | null; | ||
featured: boolean; | ||
links: { | ||
self: string; | ||
html: string; | ||
photos: string; | ||
}; | ||
owners: NonEmptyArray<User.Basic>; | ||
preview_photos: Photo.VeryBasic[] | null; | ||
published_at: string; | ||
starts_at: string; | ||
status: 'open' | 'closed'; | ||
slug: string; | ||
title: string; | ||
total_photos: number; | ||
updated_at: string; | ||
} | ||
|
||
export interface Full extends Basic { | ||
top_contributors: User.Basic[]; | ||
} |
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