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

wordpress-api-client

v0.4.9

Published

WordPress-API Client for JS/TS

Downloads

380

Readme

WordPress-API Client

npm version WordPress TypeScript JavaScript Jest Coverage Reliability Rating Maintainability Rating

A typed JavaScript client for your WordPress REST API. Super simple yet highly extensible.

This library covers all built-in WP REST API routes and can easily be extended with custom routes. Fully integrated with Advanced Custom Fields.


Installation

Depending on the package manager of your choice:

yarn add wordpress-api-client
npm install wordpress-api-client

Quick Start

If you only need to access public REST routes from a vanilla WordPress installation, all you need is:

import WpApiClient from 'wordpress-api-client'
export const client = new WpApiClient('https://my-wordpress-website.com')

The next example shows how this bare setup, from above, will already cover most of your needs:

import WpApiClient, { WPCategory, WPPage, WPPost } from 'wordpress-api-client'

async function getContent(): Promise<{
    aboutPage: WPPage
    contactPage: WPPage
    frontPage: WPPage
    categories: WPCategory[]
    recent25posts: WPPost[]
}> {
    const client = new WpApiClient('https://my-wordpress-website.com')

    const [aboutPage, contactPage, frontPage] = await client.page().find(12, 23, 34)
    const categories = await client.postCategory().find()
    const recent25posts = await client.posts().find(new URLSearchParams({
        order: 'desc',
        per_page: '25',
    }))

    return { frontPage, aboutPage, contactPage, categories, recent25Posts }
}

If you would like to extend the client, adding post types and REST end points is as easy as you would expect (example from the demo project):

import { CustomPost, WPMenu, WPProduct } from './types'
import WpApiClient, { DefaultEndpointWithRevision } from 'wordpress-api-client'

const EP_PRODUCTS = 'wp/v2/products'
const EP_MENU = 'demo-plugin/v1/menu'

export class WpClient extends WpApiClient {
    constructor() {
        super('http://localhost:8080', {
            auth: {
                type: 'basic',
                password: 'password',
                username: 'admin',
            },
        })
    }

    post<P = CustomPost>(): DefaultEndpointWithRevision<P> {
        return super.post<P>()
    }

    public product(): DefaultEndpointWithRevision<WPProduct> {
        return this.addPostType<WPProduct>(EP_PRODUCTS, true)
    }

    menu = this.createEndpointCustomGet<WPMenu>(EP_MENU)
}

With this WpClient class, extended from this package's WpApiClient class, you have full access to your WordPress's REST API, including your custom post types, custom end points and Advanced Custom Fields:

import { WpClient } from './wp-client'
const client = new WpClient()

// custom end points
await client.menu()

await client.product().find()
await client.product().create()
await client.product().update()
await client.product().delete()
await client.product().revision().find()
await client.product().revision().create()
await client.product().revision().update()
await client.product().revision().delete()

// default end points
await client.siteSettings.find()
await client.taxonomy().find()
await client.page().find()
await client...

Documentation

Feel free to report an issue if you are having trouble and the documentation is not helping.

Contributors ✨

Contributing Guidelines

This project follows the all-contributors specification. Contributions of any kind welcome!

Please fork the upstream repository into your own account and apply your changes to a new branch. Make sure to add tests, if necessary, before opening a PR against the upstream main-branch.

Comment on an issue or on a PR and the all-contributors bot will add you to the list above :)