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

@openverse/api-client

v0.0.1-a1

Published

Thoroughly typed JavaScript client for the Openverse API.

Downloads

8

Readme

@openverse/api-client

Thoroughly typed JavaScript client for the Openverse API.

Repository NPM Version


Installation

npm install @openverse/api-client

The cross-fetch dependency is optional, and only needed when using the Openverse API client in a setting where fetch is not globally available on window (e.g., server-side). If fetch is globally available, the client will always use it. Otherwise, it will attempt to use cross-fetch.

@openverse/api-client ships its own type definitions. cross-fetch was chosen for Node and browser interoperability instead of directly using node-fetch, because cross-fetch uses types identical in structure to browser fetch, significantly simplifying request and response type management.

cross-fetch will not be used if globalThis.fetch is defined. That means Node's native fetch will be used when available, and browser's native fetch is always used. If you target environments that do not have globalThis.fetch available, cross-fetch is used. To avoid cross-fetch for any reason, polyfill globalThis.fetch to your preferred implementation.

Usage

Requests to the Openverse API are made through a function returned by OpenverseClient. The function accepts a string parameter representing the endpoint's method and route. TypeScript infers the possible query parameters for that endpoint, which are passed as the params property of the second argument.

import { OpenverseClient } from "@openverse/api-client";

const openverse = OpenverseClient();

const images = await openverse("GET v1/images/", {
    params: {
        q: "dogs",
        license: "by-nc-sa",
        source: ["flickr", "wikimedia"],
    },
});

images.body.results.forEach((image) => console.log(image.title));

All responses bear the following properties:

  • body: The API response. For JSON responses, this will be an object. For all others (e.g., thumbnail requests), this will be an untouched ReadableStream (response.body from fetch).
  • meta: An object containing the following information about the request:
    • headers: Response headers
    • status: The status of the response
    • url: The final URL, including query parameters, the client made the request with
    • request: The RequestInit object pased to fetch, including headers and body.

Rate limiting

The requester function does not automatically handle rate limit back-off. To implement this yourself, check the rate limit headers from the response meta.headers.

Authentication

By default, the OpenverseClient function will return an unauthenticated client.

To use an authenticated client, pass a credentials object containing clientId and clientSecret to the OpenverseClient function. The client will automatically request tokens as needed, including upon expiration.

import { OpenverseClient } from "@openverse/api-client";

const authenticatedOpenverse = OpenverseClient({
    credentials: {
        clientId: "...",
        clientSecret: "...",
    },
});

OpenverseClient automatically requests API tokens when authenticated, including eagerly refreshing tokens to avoid halting ongoing requests. This is safe, as the Openverse API does not immediately expire existing tokens when a new one issued. This also means you do not need to share the same token between multiple client instances (e.g., across multiple instances of the same application, as in an application server cluster).

Alternative Openverse API instances

By default, the main Openverse API is used at https://api.openverse.engineering/. Other Openverse API instances may be used by passing baseUrl to the OpenverseClient function:

import { OpenverseClient } from "@openverse/api-client";

const localhostOpenverse = OpenverseClient({
    baseUrl: "localhost:50280",
});

License

@openverse/api-client is distributed under the terms of the GNU Lesser General Public License v3.0 or later license.