These are chat archives for WP-API/node-wpapi

16th
Nov 2017
Qikker Online
@qikkeronline
Nov 16 2017 15:08
probably a stupid question
(trying to use the node-wpapi within a Vue application)
right now on each page template we are requiring the api and creating a new instance of it
Qikker Online
@qikkeronline
Nov 16 2017 15:13
var WPAPI = require('wpapi')
  var wp = new WPAPI({

    endpoint: 'http://vue-wp.qikkerlocal.nl/wp-json'

  })
like that
is there a way we can avoid having to create a new instance of it on each page template? would like to just be able to require it, and make calls without having to define the endpoint on each template
K Adam White
@kadamwhite
Nov 16 2017 15:17
I usually make a module called api.js, which defines the site client instance and exports methods that I can use in my router or templates
Qikker Online
@qikkeronline
Nov 16 2017 15:18
awesome, thanks! do you have an example you could share?
K Adam White
@kadamwhite
Nov 16 2017 15:18
I haven’t done enough with Vue to know whether there’s a “best” way to integrate fetching into the system, but for React I’d traditionally have a service module that I’d require from my Redux sagas or asynchronous actions...
Unfortunately nothing public
But in principle, the template should have some mechanism (by router or by whatever sort of componentWillMount-type hook that Vue exposes) to ask for the data, but shouldn’t have access to the API object directly
import WPAPI from ‘wpapi’;
const site = new WPAPI({ /* … */ });

export function getPosts( page ) {
  return site.posts().page( page ).get();
}
that sort of thing is fairly common

but you could also just

import WPAPI from ‘wpapi’;
const site = new WPAPI({ /* … */ });

export default site;

then import the configured instance

K Adam White
@kadamwhite
Nov 16 2017 15:23
hope that helps! and sorry to all I haven’t been around much, was out of the office for a few weeks to get married then i’ve been sick since I got back -_-
Qikker Online
@qikkeronline
Nov 16 2017 15:33
thanks, that helped
while i'm here
what does auto-discovery do exactly? i have some trouble trying to understand it
and last question: the library only allows for authentication via basic auth and cookies right? where basic auth is not safe enough for production, and where cookie auth requires the frontend to be served from the wp theme
(as our f/e is not living inside the wp theme but seperately, on a different subdomain even)