Giter Site home page Giter Site logo

wp-headless / fetch Goto Github PK

View Code? Open in Web Editor NEW
53.0 2.0 3.0 2.23 MB

Isomorphic Wordpress API client and React hooks - super tiny, super fast.

License: MIT License

JavaScript 100.00%
es6 isomorphic wordpress-api api-client sdk headless headless-cms wordpress javascript create-react-app

fetch's Introduction

wp-headless

drone Coverage Status npm Bundle size

(Currently in alpha release - use in production at your own risk)

Fetch

A Wordpress API client that works both in the browser and in Node. Tiny footprint, > 95% code coverage, browser tested down to IE11, tree shakable CJS and ES6 builds, expressive syntax.

Why?

There are great alternatives such as wpapi and yllet although both of these projects have issues:

  • Long unresolved browser issues
  • Bloated packages size
  • No tree-shakable ESM or CJS build available
  • Opinionated API that attempts to do more then is needed.
  • Lack of automated browser testing and coverage

We intend to build and support a lean and well tested packages that fit into the modern ES6 javascript ecosystem.

Client

The fundamental tool in the wp-headless ecosystem is the API client.

Installation

Yarn

yarn add @wp-headless/client

NPM

npm install @wp-headless/client

Usage

Creating a client instance bound to the endpoint of your Wordpress install:

import Client from '@wp-headless/client';

const client = new Client('https://demo.wp-api.org/wp-json');

Fetching posts:

// post with id 123
const post = await client.posts().get(123);
// post with slug 'hello-world'
const post = await client.posts().slug('hello-world');
// All posts
const posts = await client.posts().get();
// filtered posts
const posts = await client.posts().get({
  per_page: 10,
  orderby: 'title',
  search: 'Dog fetches bone'
});

Fetching pages is the same as above, simply change the resource endpoint as follows:

// page with id 456
const page = await client.pages().get(456);

Resources

The client provides the following API resource methods:

  • client.categories()
  • client.comments()
  • client.media()
  • client.statuses()
  • client.posts()
  • client.pages()
  • client.settings()
  • client.tags()
  • client.taxonomies()
  • client.types()
  • client.users()

These resource methods are simply syntax sugar for setting the path and namespace to an API resource. Therefore the following are equivalent:

const posts = await client.posts().get(123);
const post = await client.get('posts/123');

Adding custom resource methods is easy (example WooCommerce REST API), the following would fetch the enpoint http://demo.wp-api.org/wp-json/wc/v2/products:

client.products = () => client.namespace('wc/v2').resource('products');

const products = await client.products().get();

Of course you could simply also do the following:

const product = await client.namespace('wc/v2').get('products/123');

As you can see building requests is as simple as setting the namespace(), resource() and the HTTP method; get(), post(), put(), patch() or delete().

HTTP methods

Client instances also provide access to HTTP methods to access API resources.

client.get(); // Http GET
client.create(); // Http POST
client.update(); // Http PATCH
client.delete(); // Http DELETE

For example:

// create a post
const post = client.posts().create({
  title: 'Dog fetches ball',
  content: '<p>then he brings it back</p>'
});
// update the post
const post = client.posts().update(post.id, {
  excerpt: 'Its just what dogs do...'
});
// delete the post
client.posts().delete(post.id);

Request parameters

You can pass request parameters as an object to any of the above methods:

// REQUEST URI https://demo.wp-api.org/wp-json/wp/v2/posts
// REQUEST BODY { title: 'Hello doggy', content: 'fetch a bone' }
const post = client.posts().create({
  title: 'Hello doggy',
  content: 'fetch a bone'
});

Or with a get request

// REQUEST URI https://demo.wp-api.org/wp-json/wp/v2/posts?per_page=10&status=draft
const post = client.posts().get({
  per_page: 10,
  status: 'draft'
});

Its also possible to set global params that will be sent with each request:

// Sets a single param key/value
client.param('per_page', 20);

// Merges an object with current global param values
client.param({
  per_page: 20,
  orderby: 'title'
});

To retrieve global params:

// Single value
client.param('source'); // wp-headless

// All values
client.params;

Embed data

WordPress API supports embedding of resources and instead of having to provide ?_embed=true as a param on every request you can simpley use embed() before any request methods.

More about WordPress API embedding can you read here.

const posts = await client
  .posts()
  .embed()
  .get();

Or globally for all requests:

client.param('_embed', true);

const posts = await client.posts().get(); // now embeded

Syntactical sugar (helper functions)

Sometimes its helpful to have expressive methods that wrap the underlying api, making code more readable and clean.

slug(string: slug)

Slug method is a shortcut to fetch a single post, custom post or page via its post_name (slug) attribute.

const post = client.page().slug('sample-page');
// ...equivalent to
const post = client
  .page()
  .get({ slug: 'sample-page', per_page: 1 })
  .then(posts => posts[0]);

more

We endevour to add a minimal set of sugar to this library, keeping it small, lean and bloat free is imperative.

Transport layers

The architecture of Fetch allows you to specify your own transport layer such as fetch or axios. This allows devs to use a library that they are familiar with, and perhaps are already using in their app, saving bundle size.

Fetch

The client uses the Fetch API Standard to make requests. This is supported in all modern browsers and newer versions of Node.

To support older browsers you will have to implement a polyfill such as isomorphic-fetch or (isomorphic-unfetch)[https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch]:

yarn add @wp-headless/client isomorphic-unfetch
import 'isomorphic-unfetch';
import Client from '@wp-headless/client';

const client = new Client('https://demo.wp-api.org/wp-json');

Others

If you would like to use a different transport layer such as axios or superagent you only need to write an adapter that adheres to interface of the Transport class found in the client package. To use this layer pass your custom transport as the second argument to the Client:

import 'isomorphic-unfetch';
import Client from '@wp-headless/client';
import AxiosTransport from 'my-custom-axios-transport';

const transport = new AxiosTransport();

const client = new Client('https://demo.wp-api.org/wp-json', transport);

React

We provide (and reccomend) React hooks that take care of your entire darta fetching life cycle. From fetching initial data, caching, optamistic updates, mutating and refetching - its all covered in an incredibly easy to use hook api. Thanks in a large part to Zeit's swr

Installation

Yarn

yarn add @wp-headless/react

NPM

npm install @wp-headless/react

Usage

import React from 'react';
import { usePost } from '@wp-headless/react';

export const Post = ({ postId }) => {
  const { post, fetching } = usePost(postId);
  return <h1>{post.title.rendered}</h1>;
};

Examples

Examples of usage in a real world application can be found in the examples folder.

Thanks

BrowserStack Logo

Thanks to BrowserStack for lending us their amazing infrastructure to give us automated browser coverage

BrowserStack Logo

Thanks to Drone an incredible pure docker CI/CD platform built on golang for building our stack!

fetch's People

Contributors

andrewmclagan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fetch's Issues

Rewrite to Typescript

Hi @andrewmclagan.

What do you think to migrate this project to Typescript at the beginning level and before adding new features if needed?
I can help you to migrate if you agree.

Is this still working?

Hello, I cloned this repo locally and inside example/create-react-app I run npm install, npm start. I get this error:

Failed to compile.

For the selected environment is no default script chunk format available:
JSONP Array push can be chosen when 'document' or 'importScripts' is available.
CommonJs exports can be chosen when 'require' or node builtins are available.
Make sure that your 'browserslist' includes only platforms that support these features or select an appropriate 'target' to allow selecting a chunk format by default. Alternatively specify the 'output.chunkFormat' directly.

I also tried to include this library into my project, but I am getting TS error (which I don't know how to fix)

error - TypeError: Url must be a string. Received undefined

image

Can someone pinpoint me to what to do? Is there a Codesandbox example of this library?

Access to Yllet npm org

Since you’re pull request and alpha releases we can’t control yllet org on npm and we would be very happy if could give me access or delete the org. We can’t release anything new.

We have tried to contact you by email and asking you in the pull request and tried to reach out to npm support without any luck.

ylletjs/yllet#29

That’s why we creating a issue. This can be fixed very easy.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.