brianjgeiger / osf.io-js-api-client

OSF.IO JS API Client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OSF.io v2 API Client — Javascript Edition

Usage

Download and include the dist/osfclient.min.js in your project and follow the examples below.

Examples

Setup the client

var client = new osfClient(
	'https://api.osf.io/v2/',
	{
		username: 'cosLogin@cos.io',
		password: 'password'
	}
);

Create a node

var nodeData = {
  title: 'api test node',
  description: 'very description, much words',
  category: 'project'
};
var newNode;
client.nodes().create(nodeData)
	.then(
		function(response) {
			newNode = response;
		}
	);

Update a node

var updatedNode;
var nodeId = 'xyz123';
client.nodes(nodeId).update({title: 'A New Title'})
	.then(
		function(response) {
			updatedNode = resp;
		}
	);

Get an array of nodes

var arrayOfNodes;
client.nodes().get()
	.then(
		function(response) {
			arrayOfNodes = response;
		}
	);

Get an array of public nodes

var arrayOfPublicNodes;
client.nodes().get({ query: 'filter[public]': true })
	.then(
		function(response) {
			arrayOfPublicNodes = response;
		}
	);

Get a specific node

var myNodeId = 'xyz123';
var myNode;
client.nodes(myNodeId).get()
	.then(
		function(response) {
			myNode = response;
		}
	);

Update a user

var userId = '123xyz';
var updatedUser;
client.users(userId).update({given_name: 'Bob'})
	.then(
		function(response) {
			updatedUser = response;
		}
	);

Get the nodes that belong to a user

var userId = '123xyz';
var user;
var nodes;
client.users(userId).get()
	.then(
		function(response) {
			user = response;
		}
	);
user.data.magicRelationships.get()
	.then(
		function(response) {
			nodes = response;
		}
	);

Development

The client is written in ES6 and transpiled to ES5 using Babel. It's http request handling is build ontop of isomorphic-fetch so it is compatible with node and the browser.

  • Checkout the repository
  • Install dependencies: npm install
  • Tests: npm run test
  • Lint: npm run lint
  • Watchify: npm run watch
  • Build unminified: npm run build
  • Build distribution: npm run build-dist

Running tests

The tests require an authenticated user. They also point to the OSF running on your local machine. Both the user credentials and the base url of the API are located in test/common.es6.js. Please change these to match your environment.

Roadmap

  • Try to remove isomorphic-fetch because it's huge
  • Rename magicRelationships to something else
  • Add more file abilities
  • Hydrate "links" section as well as relationships
  • Use ES6 generators for pagination
  • More unit tests, specifically focusing on un-happy path scenerios

About

OSF.IO JS API Client

License:Other


Languages

Language:JavaScript 100.0%