This release is 13 versions behind 0.1.31 — the latest version of @trakt/api. Jump to latest
The core library that implements Trakt API interactions using ts-rest and zod for type-safe communication and validation.
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
17%
Published
a month ago (0.1.18)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183import { initClient } from 'npm:@ts-rest/core@^3.51.0'; import { traktContract } from './contracts/traktContract.ts'; import { Environment } from './Environment.ts'; export type * from './contracts/calendars/index.ts'; export type * from './contracts/certifications/index.ts'; export type * from './contracts/comments/index.ts'; export * from './contracts/enums/index.ts'; export type * from './contracts/lists/index.ts'; export type * from './contracts/movies/index.ts'; export type * from './contracts/oauth/index.ts'; export type * from './contracts/people/index.ts'; export type * from './contracts/recommendations/index.ts'; export type * from './contracts/scrobble/index.ts'; export type * from './contracts/search/index.ts'; export type * from './contracts/shows/index.ts'; export type * from './contracts/sync/index.ts'; export type * from './contracts/users/index.ts'; export type * from './contracts/watchnow/index.ts'; export { Environment, traktContract }; export type TraktApiOptions = { /** * Trakt API environment target (production, staging, development) */ environment: | Environment | `https://${string}` | `http://localhost:${string}` | 'http://localhost'; /** * Trakt API key (client id from trakt.tv API application) */ apiKey: string; /** * Fetch implementation */ fetch?: typeof fetch; /** * If the request can be cancelled */ cancellable?: boolean; /** * Cancellation id for the request */ cancellationId?: string; }; export type TraktApi = ReturnType<typeof traktApiFactory>; const controllers = new Map<string, AbortController>(); export class AbortError extends Error { constructor(message: string) { super(message); this.name = 'AbortError'; } } function createCancellationHandler(cancellable: boolean, id: string) { if (!cancellable) { return { signal: () => undefined, abort: () => undefined, finalize: () => undefined, }; } function abort() { if (controllers.has(id)) { controllers.get(id)?.abort?.( new AbortError( `New request with id ${id} has been made, in-flight request aborted.`, ), ); } } function finalize() { controllers.delete(id); } return { signal: () => { const controller = new AbortController(); controllers.set(id, controller); return controller.signal; }, abort, finalize, }; } export function abortRequest(matcher: (id: string) => boolean, reason: Error) { for (const [id, controller] of controllers) { if (!matcher(id)) { continue; } controller.abort(reason); } } export function traktApiFactory({ environment, apiKey, fetch = globalThis.fetch, cancellable, cancellationId, }: TraktApiOptions) { return initClient(traktContract, { baseUrl: environment, baseHeaders: { 'Content-Type': 'application/json', 'trakt-api-version': '2', 'trakt-api-key': apiKey, }, api: async ({ path, method, body, headers }) => { cancellationId = cancellationId ?? path.split('?').at(0) ?? ''; const handler = createCancellationHandler( Boolean(cancellable), cancellationId, ); handler.abort(); const result = await fetch(path, { method, headers, body, signal: handler.signal(), }); handler.finalize(); const contentType = result.headers.get('content-type'); if ( contentType?.includes('application/') && contentType?.includes('json') ) { const response = { status: result.status, body: await result.json(), headers: result.headers, }; return response; } if (contentType?.includes('text/')) { return { status: result.status, body: await result.text(), headers: result.headers, }; } return { status: result.status, body: await result.blob(), headers: result.headers, }; }, }); } export function traktApi({ environment = Environment.production, apiKey, fetch, cancellable, cancellationId, }: TraktApiOptions): TraktApi { return traktApiFactory({ environment, apiKey, fetch, cancellable, cancellationId, }); }