Design
Scope
@skiffyard/tmdb is a typed client for The Movie Database™ API: the v3 catalogue (search, discovery, films, series, seasons, episodes, people, collections, companies, networks, keywords, reviews, images, videos, watch providers, trending, change feeds, lookups by external id) and v4 user authentication, account collections and custom lists, all through one Client. Replies are folded into structs.
Not in scope:
- Downloading images. The deck builds image and video URLs (
/configurationsizes checked per image kind); fetching the bytes is the caller's choice. - Caching and rate-limit bookkeeping. TMDB™'s limits are answered by retrying 429 with backoff; a cache belongs to the application.
- Endpoints without a typed verb.
call,callPost,callPutandcallDeletereach them and return decoded JSON.
Decisions
- A deck, not core. TMDB is one vendor's API; the deck builds on the bundled
rest,httpandurimodules. - One
Clientvalue, copied by builders. Credentials, language, region, adult flag, timeout, retries, backoff and verbose mode live inClient; everywith*builder returns a changed copy.client,clientFromApiKeyandclientFromEnv(TMDB_ACCESS_TOKEN,TMDB_API_KEY,TMDB_USER_TOKEN,TMDB_LANGUAGE,TMDB_REGION,TMDB_VERBOSE) construct one. - Credentials. A user token, when set, is preferred over the read access token because it carries both scopes; either goes in an
Authorization: Bearerheader. The legacyapi_keyquery parameter is used only when no bearer token exists, and works on/3only. v4 verbs refuse locally when the bearer token or user token they need is missing, rather than drawing a 401. - Topics layout.
src/tmdb.jincludes one topic per API area (core,model,images,search,discover,movies,shows,people,catalog,browse,auth,account,userlists), each with an overlay beside it. - Discover filters in deck terms.
DiscoverFilterstores the deck's key names and translates them per catalogue at request time (release date for films, first air date for series;titleornamesorts). A criterion the catalogue lacks, such as a network on a film, throws instead of being silently ignored by TMDB. Only theSORT_*constants are translated; another sort string is sent verbatim. - Body verbs bypass
rest. v4 removals put a JSON body onDELETE, whichrest.deletecannot send, so body verbs usehttp.senddirectly. - Pages capped at the API's own limit. TMDB serves at most
MAX_PAGE(500) pages and answers page 501 with a 400; the deck refuses it locally and stops paging at 500 whatevertotal_pagessays. The*Allhelpers take a page budget. - Bundles through
append_to_response.movieBundle,showBundleandpersonBundlefetch a record with its sub-resources in one request; their paged members are page 1. Series appendaggregate_credits, becausecreditsholds only the regular cast. - Lenient JSON reading, strict arguments. TMDB writes absent values as
nulland mixes number and string ids, so readers default to "", 0 or false and stringify numeric ids. Arguments are checked before any request: ids must be positive (season and episode numbers may be 0, the specials), datesYYYY-MM-DD, image sizes from the configuration's list for that kind. - Media kinds.
MediaKindnames the wire values (tvfor a series,tv_season,tv_episode). Lists hold a kind plus an id because the film and series id spaces overlap.
Errors
- Every error of kind
"tmdb"is aTmdbError:statusis the HTTP status of a refusal,statusCodeTMDB's ownstatus_codefrom the body (7 an invalid key, 34 not found); both are 0 for a bad argument, a missing credential or a transport failure. - A refusal's message names the method, URL,
HTTP <status>and TMDB'sstatus_message; with no message in the body it gives a hint for the status instead. - A reply over the transport's size cap stays kind
"limit". - A transport failure (refused, reset, unreachable, DNS, TLS, a close before any reply) is the
httpmodule'snet.NetError, kind"net", passed on unchanged so itsreason,sentandrepliedstay readable. - Any other failure of the exchange, such as a timeout, is rewrapped as a
TmdbErrorwith status 0.
Security and limits
- HTTPS only, to
API_BASE(https://api.themoviedb.org); there is no plain-http option. - Caller-supplied paths for the
call*verbs are checked: a leading slash is required, and//,..,?,#and whitespace are refused, so a path cannot send the bearer token to another host. Account ids are checked the same way, and an all-digit id is refused (v4 wants the hex account object id). - Transport policy per request: idle timeout
DEFAULT_TIMEOUT_MS(15 s),DEFAULT_RETRIES(2) retries of 429 and 5xx withDEFAULT_BACKOFF_MS(500 ms) backoff, at most 3 redirects, replies capped atMAX_RESPONSE_BYTES(16 MiB). - Verbose mode prints each request line with
api_key,access_token,request_tokenandsession_idredacted; the bearer token travels in a header and is never printed.
Testing and validation
- Network-free overlays (243 tests) cover builders, argument checks, headers, query strings, the verbose transcript, error interpretation, the discover translation table, URL building and every JSON fold, fed with reply bodies taken from TMDB's documentation and kept inline in the overlays.
- The verbs themselves are thin and are not exercised against a server, fake or live.
- Every example needs a live credential, so CI only parses and lints them.
examples/lists.jwrites to the account (creates, edits and deletes a list);examples/auth.jruns the approval flow interactively.
Known limitations and open questions
- No loopback fake covers the request path end to end; transport behaviour is tested through its helpers (one test points the GET path at a closed loopback port).
- Genre ids differ between films and series, and image size lists differ per image kind; the deck checks sizes but leaves genre ids to the caller.
Dependencies on the interpreter
- Floor
jennifer >=0.28.0(the calls 0.29.0 removes are replaced by their 0.28.0 forms), the release that carries every library and module the deck uses; the default binary for the verbs (jennifer-tinystubsnet). - The bundled
rest,http(options, retries, size cap) andurimodules. - Thrown structs classified by kind (
jennifer6abf345, the error-struct work of M28.51 to M28.57), and transport failures asnet.NetError(M28.65).