A PhoneGap Build API wrapper in Node

This post is more than 2 years old.

Yesterday I worked on a PhoneGap Build API wrapper for Node. This already exists - https://github.com/germallon/phonegapbuildapi - but I thought it would be a fun experiment. The Build API is real simple so I assumed my code would also be pretty simple. It was... at first.

I began by working on the Read portions of the API. This allows for things like getting apps, icons, downloads, etc. PhoneGap allows you to request a token or pass the authentication information in every hit. I took the easy way out and simply passed the auth with each request. I was able to wrap up most of the logic for all of the API calls with two utility functions:

The core function here is doCall, which expects a path to the API. All the API calls share the same base URL, so I made it simpler by just passing the additional part needed. HTTP calls in Node are a bit more complex than CF because they are asynchronous, but not difficult. You can probably guess what is going on here. I open a request which gets a result object. The result object has a data event which means crap streamed in. I append it to a variable. There is an end event which fires - you guessed it - at the end. I can then just parse the result and fire off a success handler.

So as an example, here is the API to get all apps:

And finally, here is how a Node app could make use of it:

Most of the code I wrote for the read API follows this format - ask for crap and pass a success/fail handler.

So - as I said - this was all pretty simple. I think I burned through the Read API in about 30 minutes or so. It was Unicorns and Rainbows fun all around. Then I began to work on the Write API and hit a brick wall. Why? The Write API, or rather the part I was working on - creating apps - allows you to upload files when defining the new application. You can also point new apps at a repository but I wanted to work on the file version first. (Call me a glutton for punishment - I knew it would be trouble.) Turns out that uploading files is a major pain in the rear. As in - there is no real built in support in the core Node.js libraries. Googling was really difficult as well since almost every result was about processing a file upload, not making a file upload request.

After even more furious Googling (and a quick Diablo 3 break), I found this post. I'd love to actually name the guy or gal - but his About page doesn't actually say who he or she is. Therefore I've decided this person is...

... just because s/he/it was so damn helpful. I incorporated some of his logic into my final code, and while I'm not terribly happy with the mashup, it is working correctly. Here's an example of the call:

And here it is up on the shiny new PhoneGap Build site...

Interestingly - browsers have made file uploads in JavaScript much easier with XHR2. If you haven't seen this in action, check out the excellent HTML5 Rocks article on it.

For folks who want to play with this, I've included the entire pgbuild.js code below. Remember - I've been writing Node.js for about a week - so if you use this in production you have my admiration.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Dave Horan posted on 8/31/2012 at 11:38 PM

I was just talking about something like this to a student yesterday in a Sencha Touch class. Thanks for posting this as I was going to look into this after a project wraps up in a week or so. You run into any issues with the PhoneGap build process when using the included API require'd into the node app? The app size seem reasonable enough for mobile?

Comment 2 by Raymond Camden posted on 9/1/2012 at 12:01 AM

Dave - I can't comment on any trouble using it as I just built it yesterday. I haven't used it in production at all.

As to your very last question - are you asking about the size of PG apps in general?

Comment 3 by Dave Horan posted on 9/1/2012 at 12:18 AM

I was wondering if including Node increased the size of the PhoneGap-base apps compared to others you may have built. Just curious.

Comment 4 by Raymond Camden posted on 9/1/2012 at 12:22 AM

Um - no - that is totally incorrect there. I hope I didn't confuse you.

I'm not including Node in my PhoneGap apps. Node is a server-side technology. You can't do server-side stuff in mobile apps. You can communicate with a server running Node, or CF of course, using Ajax, but you aren't "running" it in the PG app itself.

Comment 5 by Dave Horan posted on 9/1/2012 at 4:45 PM

Cool. Thanks for the clarification. There has been some chatter of trying to get Node apps to run client-side on devices, and upon a quick skim I though this was an interesting solution by "wrapping" it with PhoneGap for cross-platform use. My confusion. Using Node of the server-side is very cool and if you continue down the path I think you'll like what you find. Enjoy.