NPM Lightweight Wrapper for API

Sort:
andyruwruw

Hi all,

I was writing a Node.js wrapper for the API and figured I'd make it open source and publish to NPM.

https://www.npmjs.com/package/chess-web-api

It includes a priority queue for requests.  So even if you accidentally add multiple requests at a time, you won't get hit with an error for parallel requests. You just pass in a reference to the method you want from the wrapper, a callback function, any parameters, options and it's priority. Runs through each one as soon as the previous is complete.

I also incorporated their header "If-None-Match" with the "etag" into a function called "ifChanged", where you can request any of the methods with an "etag". Always returns an object with a boolean property "changed", and if it has changed, the data as well.

Let me know if you see any errors or if there's anything I should add! I'll try to make sure it's updated when changes are made to the API.

Hope someone finds it useful!

zzvbm

Wow great work thumbup.png I will check that out

zzvbm

I've got a question to your queue. I can call a method as callback method, but can I give it additional parameters as well?

You use printResults(response) as callback function. If I use a similar method which then should save the response.body in my service, I can't access 'this' anymore. I wanted to pass a reference of 'this' as parameter, but I can't get it to work.

Basically:

chessAPI.dispatch(chessAPI.getPlayer, printResults(response, myReferenceToThisHere), ["andyruwruw"], {}, 1);

That would also allow me to have only one function for all my queue requests and I could call it with different parameters to distinguish the results I get with a switch case or if statements.

If something similar is already possible, please let me know wink.png

chessAPI.dispatch(chessAPI.getPlayer, printResults(response, 'getPlayer'), ["andyruwruw"], {}, 1);
chessAPI.dispatch(chessAPI.getPlayerStats, printResults(response, 'getPlayerStats'), ["andyruwruw"], {}, 1);
zzvbm

ah I just gave it an anonymous function as parameter thumbup.png

andyruwruw

So sorry for the late reply! I've been a bit afk this weekend. Glad you got it to work! 

I can add additional functionality as well to allow you to pass in some parameters for the callback function as well, and let you know when that's live.

andyruwruw

I went ahead and updated the wrapper with the option of callback parameters for "dispatch" function! Documentation is updated as well.

They're passed in as an array just like the other parameters. The function uses the first array as parameters for the ChessAPI method, and the second array as parameters added to the callback function after the response object. So if you don't have any parameters to pass in to the ChessAPI method then be sure to add an empty array!

Lmk if you have any more questions or added features you'd find useful.

zzvbm

Wow, thank you so much for the fast addition of this! 

I really like your wrapper, makes working with the API so much more comfortable.

andyruwruw

I'm glad it's going to good use!