From 0d1bdd75b303a16bbcde7656a37f51f26fd2cb96 Mon Sep 17 00:00:00 2001 From: thcolin Date: Sat, 6 May 2017 18:31:26 +0200 Subject: [PATCH 1/4] Upgrade gapi version to f57af9a798d1e39a43f62aeefd014d5a --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 Date: Sat, 6 May 2017 18:32:52 +0200 Subject: [PATCH 2/4] Add upgrade and test scripts --- package.json | 14 ++++++++++++-- test.js | 33 +++++++++++++++++++++++++++++++++ upgrade.js | 19 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test.js create mode 100644 upgrade.js diff --git a/package.json b/package.json index 9878e86..5ed9b41 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "A very thin Node wrapper for Google's client-side javascript V3 API", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "upgrade": "node upgrade.js", + "test": "./node_modules/mocha/bin/mocha -r jsdom-global/register" }, "repository": { "type": "git", @@ -21,5 +22,14 @@ "bugs": { "url": "https://github.com/kevinschaich/gapi-client/issues" }, - "homepage": "https://github.com/kevinschaich/gapi-client#readme" + "homepage": "https://github.com/kevinschaich/gapi-client#readme", + "devDependencies": { + "chai": "^3.5.0", + "jsdom": "^9.12.0", + "jsdom-global": "^2.1.1", + "md5": "^2.2.1", + "mocha": "^3.3.0", + "node-fetch": "^1.6.3" + }, + "dependencies": {} } diff --git a/test.js b/test.js new file mode 100644 index 0000000..4392eae --- /dev/null +++ b/test.js @@ -0,0 +1,33 @@ +const expect = require('chai').expect +const fetch = require('node-fetch') +const md5 = require('md5') +const fs = require('fs') +const gapi = require('./index.js') + +describe('gapi', function(){ + this.timeout(10000) + + // impossible : travis-ci get different checksum as local : 272a52e59d1f98a4bf2409e77a029b62 - d3b6759a32936b3951340c194d448a70 (05/06/17) + xit('should have same md5 checksum as [distant](https://apis.google.com/js/api.js)', function(){ + var local = fs.readFileSync('index.js') + + // repeat n times because api.js can give different script versions (and so diffrent md5) + return Promise.all(Array.from(Array(10).keys()) + .map(i => 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)) From 9fbf0ec2d042a791e0883b41b5824ce042e01873 Mon Sep 17 00:00:00 2001 From: thcolin Date: Sat, 6 May 2017 18:33:50 +0200 Subject: [PATCH 3/4] Improve README documentation --- README.md | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b4f1b96..c609a43 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,39 @@ # 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 From b01946fd85aac5d8efe208c77b77a05111d6a9cb Mon Sep 17 00:00:00 2001 From: thcolin Date: Sat, 6 May 2017 18:34:11 +0200 Subject: [PATCH 4/4] Add travis-ci --- .travis.yml | 3 +++ README.md | 1 + 2 files changed, 4 insertions(+) create mode 100644 .travis.yml 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 c609a43..221ba75 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # 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. 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/).