Add types for yt-search (#43336)

This commit is contained in:
cherryblossom000
2020-03-26 10:57:25 -07:00
committed by GitHub
parent 2e7835106b
commit abe4e8758f
4 changed files with 200 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
// Type definitions for yt-search 1.1
// Project: https://github.com/talmobi/yt-search#readme
// Definitions by: cherryblossom <https://github.com/cherryblossom000>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.5
export = search;
declare function search(query: string | search.Options, callback: (err: Error, data: search.SearchResult) => void): void;
declare function search(query: string | search.Options): Promise<search.SearchResult>;
declare function search(query: search.VideoMetadataOptions, callback: (err: Error, data: search.VideoMetadataResult) => void): void;
declare function search(query: search.VideoMetadataOptions): Promise<search.VideoMetadataResult>;
declare function search(query: search.PlaylistMetadataOptions, callback: (err: Error, data: search.PlaylistMetadataResult) => void): void;
declare function search(query: search.PlaylistMetadataOptions): Promise<search.PlaylistMetadataResult>;
declare namespace search {
interface BaseOptions {
pageStart?: number;
pageEnd?: number;
}
interface OptionsWithQuery extends BaseOptions { query: string; }
interface OptionsWithSearch extends BaseOptions { search: string; }
type Options = OptionsWithQuery | OptionsWithSearch;
interface VideoMetadataOptions { videoId: string; }
interface PlaylistMetadataOptions { listId: string; }
interface Author {
name: string;
id: string;
url: string;
userId: string;
userUrl: string;
userName: string;
channelId: string;
channelUrl: string;
channelName: string;
}
interface PlaylistAuthor extends Omit<Author, 'userName' | 'channelName'> {
userText: string;
channelText: string;
}
interface VideoSearchResult {
type: 'video';
title: string;
description: string;
url: string;
videoId: string;
seconds: number;
timestamp: string;
duration: {
seconds: number;
timestamp: string;
};
views: number;
thumbnail: string;
image: string;
ago: string;
author: Author;
}
interface PlaylistSearchResult {
type: 'list';
title: string;
url: string;
listId: string;
videoCountLabel: string;
videoCount: number;
thumbnail: string;
image: string;
author: Author;
}
interface ChannelSearchResult {
type: 'channel';
title: string;
description: string;
url: string;
videoCountLabel: string;
videoCount: number;
thumbnail: string;
name: string;
id: string;
}
interface SearchResult {
videos: VideoSearchResult[];
playlists: PlaylistSearchResult[];
lists: PlaylistSearchResult[];
accounts: ChannelSearchResult[];
channels: ChannelSearchResult[];
}
interface VideoMetadataResult {
title: string;
description: string;
url: string;
videoId: string;
seconds: number;
timestamp: string;
duration: {
seconds: number;
timestamp: string;
};
views: number;
genre: string;
uploadDate: string;
ago: string;
thumbnail: string;
image: string;
author: Author;
}
interface PlaylistItem {
title: string;
videoId: string;
listId: string;
url: string;
thumbnailUrl: string;
thumbnailUrlHQ: string;
owner: string;
author: PlaylistAuthor;
}
interface PlaylistMetadataResult {
title: string;
listId: string;
url: string;
videoCount: number;
views: number;
lastUpdate: string;
thumbnail: string;
items: PlaylistItem[];
author: PlaylistAuthor;
}
}
+24
View File
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"esModuleInterop": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"yt-search-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+33
View File
@@ -0,0 +1,33 @@
import yts from 'yt-search';
// $ExpectType Promise<SearchResult>
yts('');
// $ExpectType void
yts('', (err, data) => {
// $ExpectType Error
err;
// $ExpectType SearchResult
data;
});
// $ExpectType Promise<SearchResult>
yts({ query: '' });
// $ExpectType Promise<SearchResult>
yts({ search: '', pageStart: 2, pageEnd: 5 });
// $ExpectType Promise<VideoMetadataResult>
yts({ videoId: '' });
// $ExpectType Promise<PlaylistMetadataResult>
yts({ listId: '' });
// $ExpectError
yts();
// $ExpectError
yts({});
// $ExpectError
yts({ query: '', videoId: '' });