-
Notifications
You must be signed in to change notification settings - Fork 12
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 #140 from mykhailodanilenko/feature/homepage-api
Add homepage API calls
- Loading branch information
Showing
17 changed files
with
394 additions
and
287 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import axios, { AxiosInstance } from "axios"; | ||
|
||
export abstract class AxiosInstanceBasedApi { | ||
protected constructor(debugLogging: boolean = false) { | ||
this.createAxiosInstance(); | ||
this.debugLogging = debugLogging; | ||
} | ||
|
||
debugLogging: boolean = false; | ||
// Our Axios instance, https://axios-http.com/docs/instance | ||
instance!: AxiosInstance; | ||
|
||
/** | ||
* Create the Axios instance with app identifier and request/response interceptors | ||
*/ | ||
private createAxiosInstance(): void { | ||
this.instance = axios.create({ | ||
withCredentials: false, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Type": "application/json", | ||
"User-Agent": "OwnTube.tv/1.0.0 (https://app.owntube.tv)", | ||
}, | ||
}); | ||
} | ||
} |
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,33 @@ | ||
import i18n from "../i18n"; | ||
import { AxiosInstanceBasedApi } from "./axiosInstance"; | ||
|
||
/** | ||
* Get available categories from the PeerTube backend `/api/v1/videos/categories` API | ||
* | ||
* @description https://docs.joinpeertube.org/api-rest-reference.html#tag/Video/operation/getCategories | ||
*/ | ||
export class CategoriesApi extends AxiosInstanceBasedApi { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Get a list of available categories from the PeerTube instance | ||
* | ||
* @param [baseURL] - Selected instance url | ||
* @returns List of available categories | ||
*/ | ||
async getCategories(baseURL: string): Promise<Array<{ name: string; id: number }>> { | ||
try { | ||
const response = await this.instance.get<Record<number, string>>("videos/categories", { | ||
baseURL: `https://${baseURL}/api/v1`, | ||
}); | ||
|
||
return Object.entries(response.data).map(([id, name]) => ({ id: Number(id), name })); | ||
} catch (error: unknown) { | ||
throw new Error(i18n.t("errors.failedToFetchAvailableCategories", { error: (error as Error).message })); | ||
} | ||
} | ||
} | ||
|
||
export const CategoriesApiImpl = new CategoriesApi(); |
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,79 @@ | ||
import { VideoChannel } from "@peertube/peertube-types"; | ||
import { Video } from "@peertube/peertube-types/peertube-models/videos/video.model"; | ||
import { GetVideosVideo } from "./models"; | ||
import i18n from "../i18n"; | ||
import { AxiosInstanceBasedApi } from "./axiosInstance"; | ||
import { commonQueryParams } from "./constants"; | ||
|
||
/** | ||
* Get channels from the PeerTube backend `/api/v1/video-channels` API | ||
* | ||
* @description https://docs.joinpeertube.org/api-rest-reference.html#tag/Video-Channels/operation/getVideoChannels | ||
*/ | ||
export class ChannelsApi extends AxiosInstanceBasedApi { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Get a list of channels from the PeerTube instance | ||
* | ||
* @param [baseURL] - Selected instance url | ||
* @returns List of channels | ||
*/ | ||
async getChannels(baseURL: string): Promise<{ data: VideoChannel[]; total: number }> { | ||
try { | ||
const response = await this.instance.get<{ data: VideoChannel[]; total: number }>("video-channels", { | ||
params: { sort: "-createdAt", count: 30 }, | ||
baseURL: `https://${baseURL}/api/v1`, | ||
}); | ||
|
||
return response.data; | ||
} catch (error: unknown) { | ||
throw new Error(i18n.t("errors.failedToFetchTotalVids", { error: (error as Error).message })); | ||
} | ||
} | ||
|
||
/** | ||
* Get a list of videos on an instance channel | ||
* | ||
* @param [baseURL] - Selected instance url | ||
* @param [channelHandle] - Channel handle | ||
* @param [count] - Count of videos to fetch | ||
* @returns List of channel videos | ||
*/ | ||
async getChannelVideos( | ||
baseURL: string, | ||
channelHandle: string, | ||
count: number, | ||
): Promise<{ data: GetVideosVideo[]; total: number }> { | ||
try { | ||
const response = await this.instance.get(`video-channels/${channelHandle}/videos`, { | ||
params: { ...commonQueryParams, sort: "-originallyPublishedAt", count }, | ||
baseURL: `https://${baseURL}/api/v1`, | ||
}); | ||
|
||
return { | ||
data: response.data.data.map((video: Video) => { | ||
return { | ||
uuid: video.uuid, | ||
name: video.name, | ||
category: video.category, | ||
description: video.description, | ||
thumbnailPath: video.thumbnailPath, | ||
duration: video.duration, | ||
channel: video.channel, | ||
publishedAt: video.publishedAt, | ||
originallyPublishedAt: video.originallyPublishedAt, | ||
views: video.views, | ||
}; | ||
}), | ||
total: response.data.total, | ||
}; | ||
} catch (error: unknown) { | ||
throw new Error(i18n.t("errors.failedToFetchTotalVids", { error: (error as Error).message })); | ||
} | ||
} | ||
} | ||
|
||
export const ChannelsApiImpl = new ChannelsApi(); |
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,12 @@ | ||
// Common query parameters for fetching videos that are classified as "local", "non-live", and "Safe-For-Work" | ||
import { VideosCommonQuery } from "@peertube/peertube-types"; | ||
|
||
export const commonQueryParams: VideosCommonQuery = { | ||
start: 0, | ||
count: 15, | ||
sort: "createdAt", | ||
nsfw: "false", | ||
isLocal: true, | ||
isLive: false, | ||
skipCount: false, | ||
}; |
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
Oops, something went wrong.