|
| 1 | +import type { RecursiveRecord } from '~/utils/typescript.utils'; |
| 2 | + |
| 3 | +import { |
| 4 | + type BaseOptions, |
| 5 | + type BaseQuery, |
| 6 | + type BaseRequest, |
| 7 | + type BaseTemplate, |
| 8 | + type BaseTemplateOptions, |
| 9 | + ClientEndpoint, |
| 10 | + type ResponseOrTypedResponse, |
| 11 | +} from '~/services/common/base-client'; |
| 12 | + |
| 13 | +export type TmdbClientSettings = { |
| 14 | + /** The domain name (e.g. https://api4.thetmdb.com) */ |
| 15 | + endpoint: string; |
| 16 | + /** The consumer client identifier */ |
| 17 | + useragent: string; |
| 18 | + /** The app read-only api key */ |
| 19 | + readToken: string; |
| 20 | + /** token time-to-live (15 minutes) */ |
| 21 | + requestTokenTTL: number; |
| 22 | +}; |
| 23 | + |
| 24 | +export type TmdbClientAuthentication = { |
| 25 | + /** User access token if there is an active session */ |
| 26 | + accessToken?: string; |
| 27 | + /** The app read-only api key */ |
| 28 | + readToken?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +export type TmdbParamPagination = { |
| 32 | + page?: number; |
| 33 | +}; |
| 34 | + |
| 35 | +export type TmdbApiParam = RecursiveRecord; |
| 36 | +export type TmdbClientOptions = BaseOptions<TmdbClientSettings, TmdbApiResponse>; |
| 37 | + |
| 38 | +export type TmdbApiResponseData<T = unknown> = T & { |
| 39 | + status_code: number; |
| 40 | + status_message: string; |
| 41 | + success: boolean; |
| 42 | +}; |
| 43 | + |
| 44 | +export type TmdbApiPagination = { |
| 45 | + page?: number; |
| 46 | + total_pages?: number; |
| 47 | + total_results?: number; |
| 48 | +}; |
| 49 | + |
| 50 | +export type TmdbApiResponsePageData<T = unknown> = TmdbApiPagination & { |
| 51 | + results: T[]; |
| 52 | +}; |
| 53 | + |
| 54 | +export type TmdbPaginatedData<T = unknown> = { |
| 55 | + data: T[]; |
| 56 | + pagination: TmdbApiPagination; |
| 57 | +}; |
| 58 | + |
| 59 | +export type TmdbApiResponse<T = unknown> = ResponseOrTypedResponse<T>; |
| 60 | + |
| 61 | +export type TmdbApiQuery<T = unknown> = BaseQuery<BaseRequest, T>; |
| 62 | +export type TmdbApiTemplateOptions = BaseTemplateOptions & { |
| 63 | + /** If the method requires user authentication */ |
| 64 | + auth?: boolean; |
| 65 | +}; |
| 66 | + |
| 67 | +export type TmdbApiTemplate<Parameter extends TmdbApiParam = TmdbApiParam> = BaseTemplate<Parameter, TmdbApiTemplateOptions>; |
| 68 | + |
| 69 | +export interface TmdbClientEndpoint<Parameter extends TmdbApiParam = Record<string, never>, Response = unknown> { |
| 70 | + (param?: Parameter, init?: BodyInit): Promise<TmdbApiResponse<Response>>; |
| 71 | +} |
| 72 | + |
| 73 | +// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging |
| 74 | +export class TmdbClientEndpoint< |
| 75 | + Parameter extends TmdbApiParam = Record<string, never>, |
| 76 | + Response = unknown, |
| 77 | + Cache extends boolean = true, |
| 78 | +> extends ClientEndpoint<Parameter, Response, Cache, TmdbApiTemplateOptions> {} |
| 79 | + |
| 80 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any -- this is a recursive type |
| 81 | +export type ITmdbApi<Parameter extends TmdbApiParam = any, Response = unknown, Cache extends boolean = boolean> = { |
| 82 | + [key: string]: TmdbClientEndpoint<Parameter, Response, Cache> | ITmdbApi<Parameter>; |
| 83 | +}; |
0 commit comments