wordpress-api-client
TypeScript icon, indicating that this package has built-in type declarations

0.4.9 • Public • Published

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


Damian

💻 📖 💡 🚧 ⚠️

Jon Wolfe

💻 🤔

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 :)

Package Sidebar

Install

npm i wordpress-api-client

Weekly Downloads

60

Version

0.4.9

License

MIT

Unpacked Size

95.8 kB

Total Files

29

Last publish

Collaborators

  • dkress59