Potion
A TypeScript client for APIs written in Flask-Potion.
Installation
$(node bin)/jspm install potion
Usage
Before you use this package, make sure you include reflect-metadata and a shim for ES6/7 features (core-js has the most comprehensive collection of shims and I advise using it).
Furthermore, this package has multiple implementations available, it can be used as:
Note that any routes created with Route.<method>
and the following methods on Item
return a Promise:
.save()
.update()
.destroy()
.query()
.fetch()
Standalone
TODO
AngularJS
If you decide to use this package as a AngularJS module, use the following example as a starting point:
import angular from 'angular';
import 'potion/angularjs';
import {Item, Route} from 'potion/angularjs';
angular
.module('myApp', ['potion'])
.config(['potionProvider', (potionProvider) => {
potionProvider.config({prefix: ''});
}])
.factory('User', ['potion', (potion) => {
class User extends Item {
static names = Route.GET('/names');
attributes = Route.GET('/attributes');
name: string;
}
potion.register('/user', User);
return User;
}])
.controller('MyAppController', ['User', (User) => {
const user1 = User.fetch(1);
const names = User.names();
user1.then((user) => {
user.attributes().then((attrs) => {
console.log(attrs);
});
});
const users = User.query();
user1.then((user) => {
const name = 'John Foo Doe';
user.update({name});
});
user1.then((user) => {
user.destroy({name});
});
const name = 'Foo Bar';
let user2 = User.create({name});
user2.save();
}]);
Contributing
Clone the repository git clone https://github.com/biosustain/potion-node
, install all the deps (npm install
, $(npm bin)/typings install
, $(npm bin)/jspm install
) and start hacking.
Make sure that the builds and tests will run successfully, before you make a pull request. Follow the next steps:
- use
npm run build
to build the .ts
files and see if any errors have occurred; - run the tests using
npm test
(if you wish to run tests on file change, use $(npm bin)/karma start karma.config.js
.); - lint the code with
npm run lint
.
Note: If you add new files or remove files, make sure to edit the "files"
field in tsconfig.json
:
"files": [
"node_modules/typescript/lib/lib.es6.d.ts",
"typings/main.d.ts",
"src/angularjs.ts",
"src/fetch.ts",
"src/base.ts",
"src/utils.ts"
]