2.0.7 • Published 6 years ago

google-doc-viewer-helper v2.0.7

Weekly downloads
374
License
MIT
Repository
-
Last release
6 years ago

Google Doc Viewer Helper Utils

Google provides a Doc Viewer that leverages their Google Docs platform to show a certain amount of file formats it is compatible with. This library is to ensure we can generate valid Google Doc Viewer URLs, and serves as a utility to know if files are a valid format, that can be leveraged in applications such as ensuring we don't unnecessarily try to route users to an embedded Google Doc Viewer that can't open the file requested anyway.

Getting Started

npm i google-doc-viewer-helper -S

Import in your application:

import gDocHelper from 'google-doc-viewer-helper';

To generate a generic Google Doc URL, leverage

gDocHelper#generateGoogleDocUrlWithParams(params, viewerOverride)

parametertypeoptionsdefaultValuedescription
paramsobjectn/anullAll parameters to be used in a Google Doc url
viewerOverridestring'default'/'gview''default'Which viewer implementation Google Docs will use to open the document
const testFileUrl = 'http://www.fakeurl.com/myTest.pdf';
const myParams = {
    embedded: false,
    url: testUrl,
};
gDocHelper.generateGoogleDocUrlWithParams(myParams);
// => https://docs.google.com/viewer?embedded=false&url=http%3A%2F%2Fwww.fakeurl.com%2FmyTest.pdf

gDocHelper.generateGoogleDocUrlWithParams(myParams, 'gview');
// => https://docs.google.com/gview?embedded=false&url=http%3A%2F%2Fwww.fakeurl.com%2FmyTest.pdf

You can also use shorthand to generate embedded Google doc urls (Most common usecase). gDocHelper#generateGoogleDocUrlWithParams(urlToEmbed, additionalParams, viewerOverride)

parametertypeoptionsdefaultValuedescription
urlToEmbedstringn/anullURL to file we wish viewer to open
additionalParamsobjectn/anullAdditional parameters for the Google Doc URL (WARNING: Can override default params)
viewerOverridestring'default'/'gview''default'Which viewer implementation Google Docs will use to open the document (WARNING: 'gview' is deprecated, but supports some special cases. An error will be written to the console when selecting this viewer.)
const testFileUrl = 'http://www.fakeurl.com/myTest.pdf';
gDocHelper.generateEmbeddedGoogleDocUrl(testFileUrl);
// => https://docs.google.com/viewer?embedded=true&url=http%3A%2F%2Fwww.fakeurl.com%2FmyTest.pdf

You can also simply check if a specific file format can be opened by Google Docs Viewer gDocHelper#isSupportedFormat(urlToValidate)

parametertypedescription
urlToValidatestringURL to file we wish to validate as being supported by Google docs
gDocHelper.isSupportedFormat('http://www.fakeurl.com/myTest.pptx');
// => true

gDocHelper.isSupportedFormat('http://www.fakeurl.com/myTest.xlsx');
// => true

gDocHelper.isSupportedFormat('http://www.fakeurl.com/myTest');
// => false

gDocHelper.isSupportedFormat('http://www.fakeurl.com/myTest.bat');
// => false

Running tests

We leverage mocha and chai for testing, and cover common usecases as part of simple unit testing.

npm test

TODO

  • Remove lodash dependency
  • Expose supported formats in case they want to be listed or used elsewhere
  • Expose supported viewers and their requirements.
    • Enforce requirements when using these viewers

Dependencies

NONE!

Authors

  • Doel L Gonzalez

License

This project is licensed under the MIT License

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

1.0.1

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.0

6 years ago