Is it possible to install npm package for all proxies?

Not applicable

Hi,

This API documentation explains how to install npm package for a particular proxy. Is it possible to install an npm package in such a was that it's available to all proxies?

Specifically, we would like to use AWS SDK for JS to simplify integration with AWS services. Since this SDK would be utilized by a number of proxies, it'd be preferable to maintain it "globally" rather than at an individual proxy level.

We're using Apigee On-Prem.

Thanks a lot in advance!

0 3 901
3 REPLIES 3

Maybe? I don't know, I have never tried it with a node_module. But Apigee Edge has the concept of a "resource". When you upload a nodejs script, you upload it as a proxy-specific resource. Along with the .ZIP files for modules that you depend on, and maybe other stuff.

There are other types of resources: jsc files, Java files, XSL files, and I think XSD and WSDL too.

All of these can be part of the API Proxy, as you know.

But, there is also the possibility to attach resources to the environment, and to the organization. You cannot (today) manage these through the Admin UI for Apigee Edge, but you can do it through the administrative API. The relevant doc that discusses these is here.

What I would try if I were you, is to zip up the node_module in question, or maybe start with a smaller one, and then manually upload it as a resourcefile to the org or environment (as appropriate). Use the Admin API for this.

Then see if you can require() that module from within multiple distinct proxies with nodejs targets.

EDIT

Yes, it works. For me, it worked for a small module.

Here's how I tested it.

  1. created a small sample module and published it on git.
    This module has one export, "hello", which is a function that accepts a string and returns a string.
  2. wrote a nodejs server that calls require('apigee-sample-module') , and then invokes that hello function.
    The nodejs server also has some other dependencies.
  3. *manually* installed the apigee-sample-module and did not add it to the list of dependencies in package.json .
  4. tested the nodejs server locally. it worked.
  5. zipped the apigee-sample-module, along with its dependencies: actually just one: sprintf-js. The zip looked like this:

    $ zip apigee-sample-module.zip -r node_modules/sprintf-js node_modules/apigee-sample-module 
      adding: node_modules/sprintf-js/ (stored 0%)
      adding: node_modules/sprintf-js/dist/ (stored 0%)
       ....
      adding: node_modules/apigee-sample-module/ (stored 0%)
      adding: node_modules/apigee-sample-module/index.js (deflated 40%)
      adding: node_modules/apigee-sample-module/LICENSE (deflated 65%)
      adding: node_modules/apigee-sample-module/package.json (deflated 75%)
      adding: node_modules/apigee-sample-module/README.md (deflated 23%)
    
  6. uploaded that zip as a resourcefile, like this:

    curl -i -n -X POST $mgmtserver/v1/o/$orgname/resourcefiles\?type=node\&name=apigee-sample-module.zip \
      --header "Content-Type:application/octet-stream" \
      --data-binary @apigee-sample-module.zip
    	
  7. packaged and uploaded the proxy containing the nodejs server and deployed it. "Packaged" here means all the nodejs dependencies, other than the module I mentioned above, were included in a node_modules.zip file, which was in the /node directory of the API Proxy bundle.
  8. Invoked the proxy. It returned a payload that proved it successfully used the apigee-sample-module which was resolved from an org-scoped resourcefile.

@Andrei Filippov -- I've also never tried this. But I did find that the doc states that Node resources can only be scoped to the proxy: http://docs.apigee.com/api-services/content/resource-files#node.

However, this restriction seems to apply only to Node resources. Other kinds of resources (JS, Java, etc) can be scoped to orgs and environments and you can use the Management API to achieve those scopes (not through the UI though).

Not applicable

To elaborate on what my colleagues propose.

Not that I know of. But personally, I'd prefer not sharing modules among multiple API proxies because managing versions could lead to dependency hell. Instead, I'd recommend leveraging a build tool (Grunt or Maven) for resolving NPM dependencies per proxy; in coordination with apigeetool capabilities to upload modules and a headless way or CLI. You can take a look at this post to checkout how other users leverage apigeetool to --upload-modules.