0.1.7 • Published 1 year ago

@grace-studio/next-wordpress v0.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@grace-studio/next-wordpress

npm version

Middle layer to connect a Next.js application with WordPress Rest API.

Note: Requires Next.js 13 or later. Uses next-gen Fetch API in both browser and in Node.js.

Installation

npm i @grace-studio/next-wordpress

or

yarn add @grace-studio/next-wordpress

Usage example

import {
  NextWordPress,
  NextWordPressConfig,
} from '@grace-studio/next-wordpress';

const config: NextWordPressConfig = {
  // required
  apiUrl: process.env.NEXT_PUBLIC_WORDPRESS_API_URL!,

  // optional auth information for 'apiKey' och 'basic' auth.
  // apiKey
  auth: {
    type: 'apiKey',
    apiKey: 'any api key string',
  },
  // basic
  auth: {
    type: 'basic',
    user: 'username',
    password: 'password',
  },

  // optional header information
  headers: {
    someKey: 'somevalue',
  },

  // add verbose logging
  verbose: true,
};

// Create instance of NextWordPress
export const api = NextWordPress.create(config);

// Get posts from custom post type 'partner'
// And specify which fields to be populated by the API.
type Partner = {
  id: number;
  link: string;
  acf: Record<string, any>;
};

// All arrays in the returned data has a generated '_key' field that can be used for the 'key' prop in React loops in rendering.
const partners = await api.get.item<Partner>({
  path: 'partner',
  params: {
    _fields: 'id,link,acf',
  },
});

// Get specific post of type partner with id = 3
const partner = await api.get.singleItem<Partner>({
  path: 'partner',
  id: 3,
  params: {
    _fields: 'id,link,acf',
  },
});

More information about what params can be used and how can be found in the WordPress Rest API documentation.

0.1.7

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.2

1 year ago

0.1.0

1 year ago

0.1.1

1 year ago

0.0.5

1 year ago

0.0.6

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago