Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wordpress/api-fetch

Package Overview
Dependencies
Maintainers
23
Versions
192
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/api-fetch

Utility to make WordPress REST API requests.

  • 7.14.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
133K
increased by9.73%
Maintainers
23
Weekly downloads
 
Created

What is @wordpress/api-fetch?

@wordpress/api-fetch is a package designed to facilitate making HTTP requests to the WordPress REST API. It provides a set of utilities to handle common tasks such as authentication, nonce handling, and error handling, making it easier to interact with WordPress data from JavaScript applications.

What are @wordpress/api-fetch's main functionalities?

Basic GET Request

This feature allows you to make a basic GET request to the WordPress REST API to fetch a list of posts.

const apiFetch = require('@wordpress/api-fetch');

apiFetch({ path: '/wp/v2/posts' }).then(posts => {
  console.log(posts);
});

Authenticated Request

This feature demonstrates how to make an authenticated request using a nonce. It shows how to create a new post by sending a POST request.

const apiFetch = require('@wordpress/api-fetch');

apiFetch.use(apiFetch.createNonceMiddleware('your-nonce-here'));

apiFetch({ path: '/wp/v2/posts', method: 'POST', data: { title: 'New Post' } }).then(post => {
  console.log(post);
});

Custom Middleware

This feature shows how to add custom middleware to modify requests. In this example, a custom header is added to all requests.

const apiFetch = require('@wordpress/api-fetch');

const customMiddleware = (options, next) => {
  options.headers = {
    ...options.headers,
    'X-Custom-Header': 'CustomValue'
  };
  return next(options);
};

apiFetch.use(customMiddleware);

apiFetch({ path: '/wp/v2/posts' }).then(posts => {
  console.log(posts);
});

Other packages similar to @wordpress/api-fetch

Keywords

FAQs

Package last updated on 11 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc