npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mogeko/tmdb-request

v1.3.2

Published

A simple wrapper for TMDB APIs

Downloads

1

Readme

tmdb-request.js

A simple wrapper for the The Movie Database API.

It use RFC 6570 URI Template specification to build the API endpoints and Fetch API to get the data.

Installation

Node.js

By install with npm, yarn, pnpm or any other package manager that you use.

pnpm add @mogeko/tmdb-request

Note

Make sure you Node.js environment has Fetch API support. If not, you can use node-fetch or cross-fetch to polyfill it.

If you use framework like Next.js, you should not need to worry about this. They usually have an out-of-the-box Fetch API implementation.

Then using this library / module in your code:

import { parser, request } from "@mogeko/tmdb-request";

Browser

You can also load @mogeko/tmdb-request directly in your browser (from esm.sh):

<script type="module">
  import { parser, request } from "https://esm.sh/@mogeko/tmdb-request";
</script>

Usage

First, You need to apply an API Token from TMDB. It's completely free, you just need to register an account.

We need thanks TMDB provide this great service for us, so please do not abuse the API.

GET request example

This library 1:1 mapping of REST API endpoints in the The Movie Database API Reference.

For example, to get the details of a movie, you would do:

// The default method is GET, so you can omit it.
const result = request("/movie/{movie_id}?language={lang}", {
  headers: {
    authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  },
  movie_id: 10997,
  lang: "en-US",
});

console.log(result);

POST request example

You can also use POST request to interact with TMDB Server.

For example, to rate a movie, you would do:

const result = request("POST /movie/{movie_id}/rating", {
  headers: {
    authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "content-type": "application/json;charset=utf-8",
  },
  body: JSON.stringify(data),
  movie_id: 10997,
});

console.log(result);

Working with axios

If you prefer to use other HTTP client, like axios.

You can use parser function to only parse the URL.

parser("GET /movie/{movie_id}?language={lang}", {
  headers: {
    authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  },
  movie_id: 10997,
  lang: "en-US",
});

It will result like:

{
  baseUrl: "https://api.themoviedb.org/3",
  body: null,
  headers: {
    accept: "application/json",
    authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "user-agent": "Node.js/20.3.1 (Linux x64)",
  },
  method: "GET",
  url: "/movie/10997?language=en-US",
}

Then you can use it with you HTTP client.

omit the route argument

We also allow you to omit the route parameter and only pass the endpoint (opts) parameter.

In this case, we will use the endpoint.url as the route parameter, so the url field is required.

This feature is implemented by TypeScript's function overloading.

// For request function
request({
  url: "GET /movie/{movie_id}?language={lang}",
  headers: {
    authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  },
  movie_id: 10997,
  lang: "en-US",
});

// For parser function
parser({
  url: "POST /movie/{movie_id}/rating",
  headers: {
    authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "content-type": "application/json;charset=utf-8",
  },
  body: JSON.stringify(data),
  movie_id: 10997,
});

API

Both request and parser function have the same arguments. The only difference is that request will send the request and return the result, while parser will only parse the URL and return the request information.

request(route, opts) and parser(route, opts)

| Name | Type | Description | | -------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | route | string | It has to be a string consisting of URL Template and the request method, e.g. GET /movie/{id}. If it’s set to a URL, only the method defaults to GET. | | opts.method | string | Required unless it was set in the route parameter. HTTP request method, but only GET, POST and DELETE are supported. Defaults to GET. | | opts.headers | object | Custom headers to send with the request. Only authorization header is required. By default, it will set accept to application/json and user-agent to suitably value. | | opts.baseUrl | string | The custom base URL for the request. It will be prepended to the URL (parsed from the route parameter) when we make the request. Defaults to https://api.themoviedb.org/3. | | opts.body | string | The request body. It will be sent as-is in a POST request. For GET and DELETE request, it will always be null. Defaults to null. | | otp.url | undefined | Unrecommended. To be honest, it is an useless field, because it will be overwritten by the route parameter always. You should NEVER use it. |

request(endpoint) and parser(endpoint)

| Name | Type | Description | | ------------------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | endpoint.url | string | In this case, the url field is required. We will use it to parse the URL and request method. It is equivalent to the route parameter in the previous chapter. | | endpoint.baseUrl | string | Same as the previous article. | | endpoint.method | string | Same as the previous article. | | endpoint.headers | object | Same as the previous article. | | endpoint.body | string | Same as the previous article. |

License

The code in this project is released under the MIT License.