Google-api-javascript-client: Publish an NPM package

Created on 3 May 2018  路  6Comments  路  Source: google/google-api-javascript-client

Hi, is this project scheduled to be published as an NPM package?

All 6 comments

It should. That's the established way to do it.

Hey @caesarsol,

Does this help? https://github.com/google/google-api-nodejs-client

npm install googleapis

Please let me know if you need more help.

Thanks!

It is possible to npm install gapi. If I am not mistaken, this will install https://github.com/phated/node-gapi, which saw its latest commit in 2012 and is marked on Github by its author as no longer maintained. I think there really needs to be an official package, even if just because otherwise people will mistakenly think this incredibly outdated one is the official one.
On the other hand, it looks like that one is at least suited for non-browser clients running NodeJS. The library as linked to in the samples of this repository (the actual library itself is not even here, only a placeholder Javascript file) is not; it relies heavily on the window and document variables that are defined by default in a browser. So that would be an issue if publishing to a place mostly aimed at NodeJS devs.

The library referenced by StevenEWright seems to be Google's official API client for NodeJS. It's not a perfect copy of this library; class hierarchies and call signatures differ pretty strongly from this library's. Its documentation is also challenging to figure out, particularly as it lacks examples. On the plus side, it has submodules for specific libraries, which this library does not have. It has the same capabilities as this library (apart from the fact that this library only works in browsers and the Node library also works in Node), is reasonably intuitive and logical to use and works well as far as I can tell. It is probably the closest thing Google will ever make to a Node-compatible and NPM-installable version of this library. The downvotes look undeserved as far as I can tell, and I am curious as to their motivation.

I've look at this. With the way the client is written, it's impossible to make the client public without a full rewrite. The gapi/client uses Google-internal tools in lots of places (probably due to it's age).

At best, I could make an effort to mirror a few read-only files that would have the internal comments stripped.

@grant it might be helpful, can you share those files?

I'm trying to make Google Sheets API for JavaScript more TypeScript-friendly: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37847

And I'm having issues with POST requests, such as batchUpdate.
It uses request parameter in the discovery docs to define POST request body.

The problem is that I'm not sure how JavaScript library expects this parameter.
Both approaches work:

// NodeJS docs example
batchUpdate({
  spreadsheetId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  resource: {
    requests: [  ],
  },
});

```js
// JavaScript docs example
batchUpdate({
spreadsheetId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
}, {
requests: [ ],
});

I've found that NodJS library uses this to get check request body:
```js
const resource = params.requestBody ? params.requestBody : params.resource;

And now I wonder what are valid ways to pass request body in JavaScript library. I want to know the implementation to define all valid ways of passing the request body.

I'll try to look into minified sources, but it's quite challenging.
So, any original files will be of great help :)

UPDATE:
I think that I have an answer now, after debugging JavaScript library.
Here's the simplified version of the library code (at least, how I understood it):

// over-simplified version
var prepareRequest = function (
  currentRequestSchema, // docs discovery batchUpdate info
  commonRequestSchema, // docs discovery common info
  arg1, // batchUpdate first argument
  arg2, // batchUpdate second argument
) {
  let body;
  // ...
  arg1 = deleteAllKnownParameters(arg1, currentRequestSchema.parameters, commonRequestSchema.parameters); // extracts all known parameters from arg1 and leaves only unknown ones
  // ...
  if (
    Object.keys(arg1).length == 1 // if arg1 has exactly one unknown property
    && arg1.hasOwnProperty('resource') // and this property is "resource"
  ) {
    body = arg1.resource; // use it as a request body
  }

  if (body == null) {
    body = arg2; // if arg1 was null, use second argument
  }
};

So, basically both approaches are supported by the library. And I can't find any other approach (like using requestBody in NodeJS lib).
Hopefully, it'll be helpful to anybody.

Was this page helpful?
0 / 5 - 0 ratings