Safe-js: Promise based version of safe javascript APIs

Having spent the morning playing around with the APIs and trying to get an app going, I’ve extracted the API as it is in the latest demo app into its own package.

This is purely for my own understanding so far. (and inspired by GitHub - eblanshey/safenet: Low-level javascript API wrapper for communicating with the SAFE Network launcher ). I wanted to get something going with the latest launcher APIs.


npm i safe-js


It’s been refactored to be promise based and I’ve simplified away the angular code there too.

Right now auth work and create file is returning true. I haven’t had chance to test the rest yet (that’s my first npm package, was a bit confused with publishing etc; so this will be buggy). But if you want to pull and play, and PR with solutions to any bugs, that would be most welcome.

Anyway, I plan to update this as I find the problems that will exist in there. But I thought it might be useful for other devs even as it is now.

20 Likes

If this is anything along the lines of making the APIs accessible for automating actions - especially from a command line, that is very welcome and timely!.. On my to do list still is getting a route to action that isn’t just the Demo App.

There are obviously nodejs examples at Introduction and I had limited success with those… half way up a learning curve there.

It’s pretty much just a wrapper lib for the readme functions, but up to date.

Purely extracted from the demo app.

You should be able to at least get the auth going with a small node.js script, for example. I have an app I’m using this in which I’ll be putting on github later, when it’s not a total mess. I need to implement this in there first.

4 Likes

I’ve updated the readme with a couple of examples: GitHub - joshuef/safe-js

But if you want to test the various endpoints, I’d recommend grabbing this extension which lets you easily formulate the requests with headers and relevant body parts. That’s what I’ve been using to check my requests etc.

2 Likes

Great stuff @joshuef :slight_smile: I’ve also been using Promises in my pure JavaScript implementation (non-nodejs) of a remoteStorage back-end that uses the SAFE-NFS API so it might make sense to extract this as a wrapper at some point then we’ll have both node and pure JavaScript options. I still get confused by Promises though, so maybe not just yet :wink:

So like you I have to do more work before it will be much use for anyone to build on. Interim code like ours is though still helpful - I think just having some code to look at is useful as well (so I do push my remoteStorage backend to github from time to time - file safestore.js - though I have completely rewritten it since the last push!).

When I go back to playing with launcher I’ll be keen to take a look at what you created. I also implemented experimental stuff in that using Promises and I think MaidSafe should consider switching over (from callbacks) especially as Promises are now part of the JS browser standard. Note to @Krishna_Kumar! :slight_smile: Maybe we can all build on your safe-js repo?

4 Likes

Yeh, ideally, you could use this instead of having to rewrite the API calls.

The isomorphic fetch is nicely done and makes it simple to doYourFunction().then( doSomethingElse ), so much nicer than the callback workflow (imo).

This is pretty basic representations of the API so far, so hopefully you would be able to incorporate it easily enough. If you need any help implementing this or any promise stuff, or suggestions / PRs for this, just drop me a message!

2 Likes

Small update/bump to the package there. Now modifyFileContents is promising properly.

I can write/read to safe in an app. WOOOO.

That’s my dev for the day. Ha, already about 4 hours longer than I planned. Just… one… more… tweak! :smiley:

3 Likes

Nice. FYI, I also have a promised-based impl at GitHub - cretz/safeclient.js: JS Client to the SAFE Launcher API if you get stuck. It is not updated for the new API version however.

5 Likes

ah nice!

And with documentation. :thumbsup:

2 Likes