Custom endpoints – fluent-wp-client
0 Stars
View on GitHub
Overview
Section titled “Overview”| Content type | API |
|---|---|
Custom post type in wp/v2 | content('resource') |
Custom taxonomy in wp/v2 | terms('resource') |
| Plugin/custom namespace | request({ endpoint: '/wp-json/...' }) |
| Full transport control | request(...) |
Custom post types
Section titled “Custom post types”Assume a CPT with rest_base: 'books'.
const books = wp.content('books');
const list = await books.list({ perPage: 20 });const allBooks = await books.listAll();const paged = await books.listPaginated({ page: 2, perPage: 25 });const book = await books.item('my-book');const embedded = await books.item('my-book', { embed: true });const created = await books.create({ title: 'Book title', status: 'draft' });await books.update(created.id, { status: 'publish' });await books.delete(created.id, { force: true });Response validation
Section titled “Response validation”Validate custom endpoint payloads in application code after the request resolves:
import { z } from 'zod';
const bookSchema = z.object({ id: z.number(), slug: z.string(), status: z.string(), type: z.literal('book'),});
const rawBook = await wp.content('books').item('my-book');const book = bookSchema.parse(rawBook);Sparse CPTs
Section titled “Sparse CPTs”Use WordPressCustomPost<T> or postLikeWordPressSchema when a CPT may omit title, content, or excerpt.
import type { WordPressCustomPost } from 'fluent-wp-client';import { postLikeWordPressSchema } from 'fluent-wp-client/zod';
const rawArtifact = await wp.content('artifacts').item('test-artifact-001');const artifact = postLikeWordPressSchema.parse(rawArtifact);Embed and extraction
Section titled “Embed and extraction”import { getEmbeddedTerms, getEmbeddedAuthor } from 'fluent-wp-client';
const book = await books.item('test-book-001', { embed: ['author', 'wp:term'] });const author = getEmbeddedAuthor(book);const genres = getEmbeddedTerms(book, 'genre');Gutenberg blocks
Section titled “Gutenberg blocks”Use fluent-wp-client/blocks when the CPT exposes content.raw:
import { withBlocks } from 'fluent-wp-client/blocks';
const wpBlocks = withBlocks(wp);const blocks = await wpBlocks.content('books').item('test-book-001').blocks().get();Custom taxonomies
Section titled “Custom taxonomies”Assume rest_base: 'genre'.
const genres = wp.terms('genre');
const list = await genres.list({ perPage: 100 });const allGenres = await genres.listAll();
const created = await genres.create({ name: 'Science Fiction' });await genres.update(created.id, { name: 'Sci-Fi' });await genres.delete(created.id, { force: true });Low-level requests
Section titled “Low-level requests”const { data, response } = await wp.request({ endpoint: '/wp-json/my-plugin/v1/sync', method: 'POST', body: { mode: 'delta' },});Use the same auth approach as core endpoints. See the auth guide.
Pagination
Section titled “Pagination”Error handling
Section titled “Error handling”WordPressClientError— base class for all runtime failures withkind,operation,endpoint,retryableWordPressHttpError— HTTP failures withstatus,wpCode,wpMessage,responseBody- Use
instanceofto narrow, orerror.kindfor serialization - Prefer schema validation for writes so regressions fail fast