Skip to content

Commit

Permalink
initial draft of v6 -- replace request peer-dependency with axios
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPaulBrewer committed Aug 1, 2020
1 parent c0c3ae2 commit f1515f7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 49 deletions.
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const upload = require('uploader-for-google-drive-resumable-upload-url');

const folderMimeType = 'application/vnd.google-apps.folder';

function extensions(drive, request, rootFolderId, spaces, salt) {
function extensions({drive, axios, rootFolderId, spaces, salt}) {
const x = {};

function addNew(meta) {
Expand Down Expand Up @@ -314,7 +314,7 @@ function extensions(drive, request, rootFolderId, spaces, salt) {
sourceStream: localStream,
mimeType,
url,
request
axios
};
const result = await(upload(params));
addNew(result);
Expand Down Expand Up @@ -373,23 +373,23 @@ x.upload2 = upload2;
return x;
}

function decorate(drive, auth, request, salt) {
function decorate({drive, auth, axios, salt}) {
// drive is delivered from googleapis frozen, so we'll refreeze after adding extensions
const extras = {};
extras.x = extensions(drive, request, 'root', 'drive', salt);
extras.x = extensions({drive, axios, rootFolderId:'root', spaces:'drive', salt});
if (auth) extras.x.auth = auth;
extras.x.appDataFolder = extensions(drive, request, 'appDataFolder', 'appDataFolder', salt);
extras.x.appDataFolder = extensions({drive, axios, rootFolderId:'appDataFolder', spaces:'appDataFolder', salt});
return Object.freeze(Object.assign({}, drive, extras));
}

function decoratedGoogleDrive(googleapis, request, keys, tokens, salt) {
if (!googleapis)
throw Boom.badImplementation("googleapis not defined");
if (!googleapis.auth)
throw Boom.badImplementation("googleapis.auth not defined");
if (!googleapis.auth.OAuth2)
throw Boom.badImplementation("googleapis.auth.OAuth2 not defined");
const OAuth2 = googleapis.auth.OAuth2;
function decoratedGoogleDrive({google, axios, keys, tokens, salt}) {
if (!google)
throw Boom.badImplementation("google not defined");
if (!google.auth)
throw Boom.badImplementation("google.auth not defined");
if (!google.auth.OAuth2)
throw Boom.badImplementation("google.auth.OAuth2 not defined");
const OAuth2 = google.auth.OAuth2;
const auth = new OAuth2(keys.key, keys.secret, keys.redirect);
// possible patch for googleapis 23.0.0 missing .setCredentials bug
// see https://github.com/google/google-api-nodejs-client/issues/869
Expand All @@ -399,10 +399,10 @@ function decoratedGoogleDrive(googleapis, request, keys, tokens, salt) {
} else {
auth.credentials = tokens;
}
const drive = googleapis.drive({ version: 'v3', auth });
const drive = google.drive({ version: 'v3', auth });
if (typeof(drive) !== 'object')
throw Boom.badImplementation("drive is not an object, got: " + typeof(drive));
return decorate(drive, auth, request, salt);
return decorate({drive, auth, axios, salt});
}

decoratedGoogleDrive.decorate = decorate;
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
"homepage": "https://github.com/DrPaulBrewer/decorated-google-drive#readme",
"devDependencies": {
"assert": "^2.0.0",
"eslint": "^6.8.0",
"googleapis": "^47.0.0",
"mocha": "^7.0.1",
"request": "^2.88.2",
"axios": "^0.19.2",
"eslint": "^7.6.0",
"googleapis": "^58.0.0",
"mocha": "^8.1.0",
"should": "^13.2.3",
"string-to-stream": "^3.0.1"
},
"dependencies": {
"boom": "^7.3.0",
"p-reduce": "^2.1.0",
"search-string-for-google-drive": "^1.0.0",
"uploader-for-google-drive-resumable-upload-url": "^1.1.0"
"uploader-for-google-drive-resumable-upload-url": "^2.0.0"
}
}
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const assert = require('assert');
require('should');
const {google} = require('googleapis');
const request = require('request');
const axios = require('axios');
const fs = require('fs');
const str = require('string-to-stream');
const Boom = require('boom');
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('decorated-google-drive:', function () {
describe(' initializing ', function () {
it('should not throw an error', function () {
function init() {
drive = driveZ(google, request, keys, tokens, salt);
drive = driveZ({google, axios, keys, tokens, salt});
}
init.should.not.throw();
});
Expand Down
8 changes: 4 additions & 4 deletions testDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- [ initializing ](#decorated-google-drive-initializing-)
- [ drive.x.aboutMe ](#decorated-google-drive-drivexaboutme-)
- [ drive.x.hexid ](#decorated-google-drive-drivexhexid-)
- [ drive.x.appDataFolder.upload2: upload a string to appDataFolder ](#decorated-google-drive-drivexappdatafolderupload2-upload-a-string-to-appdatafolder-)
- [ drive.x.appDataFolder.upload2: upload a string ...-1-2-3 to appDataFolder ](#decorated-google-drive-drivexappdatafolderupload2-upload-a-string-1-2-3-to-appdatafolder-)
- [ drive.x.upload2: upload a file README.md to Drive folder /path/to/test/Files](#decorated-google-drive-drivexupload2-upload-a-file-readmemd-to-drive-folder-pathtotestfiles)
- [ after drive.x.upload2 ](#decorated-google-drive-after-drivexupload2-)
- [ drive.x.upload2: upload test/test.zip to Drive folder /path/to/test/Files](#decorated-google-drive-drivexupload2-upload-testtestzip-to-drive-folder-pathtotestfiles)
Expand All @@ -20,7 +20,7 @@ should not throw an error.

```js
function init() {
drive = driveZ(google, request, keys, tokens, salt);
drive = driveZ({google, axios, keys, tokens, salt});
}
init.should.not.throw();
```
Expand Down Expand Up @@ -93,8 +93,8 @@ async function(){
}
```

<a name="decorated-google-drive-drivexappdatafolderupload2-upload-a-string-to-appdatafolder-"></a>
## drive.x.appDataFolder.upload2: upload a string to appDataFolder
<a name="decorated-google-drive-drivexappdatafolderupload2-upload-a-string-1-2-3-to-appdatafolder-"></a>
## drive.x.appDataFolder.upload2: upload a string ...-1-2-3 to appDataFolder
uploading the string to appDataFolder file myaccount should resolve with expected file metadata.

```js
Expand Down
46 changes: 23 additions & 23 deletions testResults.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@
✓ drive should not be undefined
✓ drive.x should be an object
drive.x.aboutMe
✓ should return the test users email address (403ms)
✓ should return a storageQuota object with properties limit, usage (161ms)
✓ drive.about.get still works, as well, and the outputs match (175ms)
✓ should return the test users email address (518ms)
✓ should return a storageQuota object with properties limit, usage (193ms)
✓ drive.about.get still works, as well, and the outputs match (162ms)
drive.x.hexid
✓ should return a 64 char hex id (212ms)
✓ should consistently return the same 64 char hex when called 3 times (201ms)
✓ should return a 64 char hex id (132ms)
✓ should consistently return the same 64 char hex when called 3 times (157ms)
drive.x.appDataFolder.upload2: upload a string ...-1-2-3 to appDataFolder
✓ uploading the string to appDataFolder file myaccount should resolve with expected file metadata
✓ drive.x.appDataFolder.searcher should report there is exactly one myaccount file in the folder and it should match upload file id
✓ drive.x.appDataFolder.contents should resolve to contents Hello-World-Test-1-2-3
drive.x.upload2: upload a file README.md to Drive folder /path/to/test/Files
✓ uploading the README.md file to /path/to/test/Files/README.md should resolve with expected file metadata
✓ the parents[0] folder should have the name 'Files' (158ms)
✓ searching the parents[0] folder for README.md find a file with matching id (267ms)
✓ the parents[0] folder should have the name 'Files' (138ms)
✓ searching the parents[0] folder for README.md find a file with matching id (270ms)
after drive.x.upload2
✓ searching root for anything should yield folder 'path' with .isFolder===true (215ms)
✓ searching root for folders should yield folder 'path' with .isFolder===true (267ms)
✓ searching root for non-folders should be empty (221ms)
✓ searching all folders for any non-trashed file should be non-empty and include file README.md in results (243ms)
✓ searching all folders or a file with appProperties: { 'role': 'documentation' } should be empty (153ms)
✓ checking existence of /path/to/test/Files/README.md with drive.x.findPath should yield expected file metadata (1074ms)
✓ checking existence of /path/to/test should yield expected folder metadata (1205ms)
✓ checking existence on wrong path should throw Boom.notfound (216ms)
✓ downloading content with drive.x.download should yield contents string including 'License: MIT' (1189ms)
✓ updating README.md appProperties to {'role': 'documentation'} should succeed (1820ms)
✓ searching all folders or a file with appProperties: { 'role': 'documentation' } should find README.md (207ms)
✓ drive.x.upload2 uploading the file again with {clobber:false} will throw Boom.conflict error because file already exists (987ms)
✓ searching root for anything should yield folder 'path' with .isFolder===true (413ms)
✓ searching root for folders should yield folder 'path' with .isFolder===true (414ms)
✓ searching root for non-folders should be empty (392ms)
✓ searching all folders for any non-trashed file should be non-empty and include file README.md in results (409ms)
✓ searching all folders or a file with appProperties: { 'role': 'documentation' } should be empty (200ms)
✓ checking existence of /path/to/test/Files/README.md with drive.x.findPath should yield expected file metadata (1233ms)
✓ checking existence of /path/to/test should yield expected folder metadata (619ms)
✓ checking existence on wrong path should throw Boom.notfound (208ms)
✓ downloading content with drive.x.download should yield contents string including 'License: MIT' (2041ms)
✓ updating README.md appProperties to {'role': 'documentation'} should succeed (2253ms)
✓ searching all folders or a file with appProperties: { 'role': 'documentation' } should find README.md (412ms)
✓ drive.x.upload2 uploading the file again with {clobber:false} will throw Boom.conflict error because file already exists (1424ms)
drive.x.upload2: upload test/test.zip to Drive folder /path/to/test/Files
✓ uploading the test.zip file to /path/to/test/Files/test.zip should resolve with expected file metadata and md5 match
create folder /path/to/test2
Expand All @@ -46,11 +46,11 @@
use folderId of /path/to/test2 to upload test.zip
✓ uploading the test.zip file to /path/to/test2/test.zip should resolve with expected file metadata and md5 match
cleanup via drive.x.janitor
✓ janitor hopefully deletes the README.md file(s) OK and resolves correctly (1695ms)
✓ drive.x.findPath will throw Boom.notFound if the file was successfully deleted (974ms)
✓ janitor will throw an error if told to delete an invalid file (173ms)
✓ janitor hopefully deletes the README.md file(s) OK and resolves correctly (1634ms)
✓ drive.x.findPath will throw Boom.notFound if the file was successfully deleted (1406ms)
✓ janitor will throw an error if told to delete an invalid file (119ms)
✓ janitor should not throw an error if given an empty filelist
✓ final cleanup: delete the path folder and check non-existence (885ms)
✓ final cleanup: delete the path folder and check non-existence (1546ms)


40 passing (30s)
Expand Down

0 comments on commit f1515f7

Please sign in to comment.