diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2197832 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "node" diff --git a/README.md b/README.md index b4f1b96..221ba75 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,40 @@ # gapi-client +[![Build Status](https://travis-ci.org/kevinschaich/gapi-client.svg?branch=master)](https://travis-ci.org/kevinschaich/gapi-client) -A *very* thin Node wrapper for Google's client-side javascript V3 API. +A *very* thin Node wrapper for Google's client-side javascript V3 API. For `gapi` documentation, see [Google API Client Reference](https://developers.google.com/api-client-library/javascript/reference/referencedocs) and for `API_KEY`, see [Google Developers Console](https://console.developers.google.com/). -## Example +## Install ```bash npm install --save gapi-client ``` -`index.js:` +## Example ```javascript -import gapi from 'gapi-client'; - -//On load, called to load the auth2 library and API client library. -gapi.load('client:auth2', initClient); - -// Initialize the API client library -function initClient() { - gapi.client.init({ - discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"], - clientId: 'YOUR_CLIENT_ID', - scope: 'https://www.googleapis.com/auth/drive.metadata.readonly' - }).then(function () { - // do stuff with loaded APIs - console.log('it worked'); - }); +import gapi from 'gapi-client' + +// Example : Auth2 (synchronous) +const init = () => gapi.client.init( + discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'], + clientId: 'YOUR_CLIENT_ID', + scope: 'https://www.googleapis.com/auth/drive.metadata.readonly' +}) + +gapi.load('client:auth2', init) + +// Example : Youtube (synchronous) +const init = { + client: () => gapi.client.load('youtube', 'v3', init.youtube), + youtube: () => gapi.client.setApiKey('YOUR_API_KEY') } +gapi.load('client', init.client) + +// Example : Youtube (promisified) +new Promise(resolve => gapi.load('client', resolve)) + .then(() => new Promise(resolve => gapi.client.load('youtube', 'v3', resolve))) + .then(() => gapi.client.setApiKey('YOUR_API_KEY')) ``` ## License diff --git a/index.js b/index.js index af1628f..1a0f09c 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,14 @@ var gapi=window.gapi=window.gapi||{};gapi._bs=new Date().getTime();(function(){/* gapi.loader.OBJECT_CREATE_TEST_OVERRIDE &&*/ -var g=window,h=document,m=g.location,n=function(){},q=/\[native code\]/,u=function(a,b,c){return a[b]=a[b]||c},aa=function(a){for(var b=0;baa.call(b,e)&&c.push(e)}return c},U=function(){var a=A.nonce;if(void 0!==a)return a&&a===String(a)&&a.match(S)?a:A.nonce=null;var b=u(A,"us",[]);if(!b||!b.length)return A.nonce=null;for(var c=h.getElementsByTagName(R),d=0,e=c.length;d")}},V=function(a){var b=h.createElement(R);b.setAttribute("src",a);a=U();null!==a&&b.setAttribute("nonce",a);b.async="true";(a=h.getElementsByTagName(R)[0])?a.parentNode.insertBefore(b,a):(h.head||h.body||h.documentElement).appendChild(b)},qa=function(a,b){var c=b&&b._c;if(c)for(var d=0;daa.call(b,e)&&c.push(e)}return c},U=function(){var a=A.nonce;if(void 0!==a)return a&&a===String(a)&&a.match(S)?a:A.nonce=null;var b=u(A,"us",[]);if(!b||!b.length)return A.nonce=null;for(var c=h.getElementsByTagName(R),d=0,e=c.length;d")}},V=function(a){var b=h.createElement(R);b.setAttribute("src",a);a=U();null!==a&&b.setAttribute("nonce",a);b.async="true";(a=h.getElementsByTagName(R)[0])?a.parentNode.insertBefore(b,a):(h.head||h.body||h.documentElement).appendChild(b)},qa=function(a,b){var c=b&&b._c;if(c)for(var d=0;d new Promise(resolve => setTimeout(resolve, i * 500)) + .then(() => fetch('https://apis.google.com/js/api.js')) + ) + ) + .then(responses => Promise.all(responses.map(response => response.text()))) + .then(contents => contents.map(content => content + "\n\nmodule.exports = gapi;")) + .then(distants => distants.map(distant => md5(distant))) + .then(checksums => expect(md5(local), JSON.stringify(checksums)).to.be.oneOf(checksums)) + }) + + it('should have a load property', function(){ + expect(gapi).to.have.property('load') + }) + + it('should be able to load client', function(done){ + gapi.load('client', done) + }) +}) diff --git a/upgrade.js b/upgrade.js new file mode 100644 index 0000000..c0c0464 --- /dev/null +++ b/upgrade.js @@ -0,0 +1,19 @@ +const fetch = require('node-fetch') +const md5 = require('md5') +const fs = require('fs') + +fetch('https://apis.google.com/js/api.js') + .then(response => response.text()) + .then(content => content + "\n\nmodule.exports = gapi;") + .then(distant => { + var local = fs.readFileSync('index.js') + var checksum = md5(distant) + + if (checksum !== md5(local)) { + fs.writeFileSync('index.js', distant) + console.log('Writing new version :', checksum) + } else { + console.log('Current version is up to date !') + } + }) + .catch(error => console.log('Error during download :', error.message))