A thin client for accessing the Client API.
- Dynalist home page
- Official API documentation
- Client Developer page for generating a Secret Token to access the API
# NPM:
npm install dynalist-js
# Yarn:
yarn add dynalist-js
Require the module, then create an instance with the API Secret Token.
const Client = require('dynalist-js');
const dyn = new Client('<my developer api token>');
The API token can also be set/changed afterwards:
dyn.setToken('<my developer api token>');
All API methods return a promise object that can be used with .then
or async/await.
All API methods can be called with a callback function as the last parameter.
Callback functions are always called with err
and data
parameters.
err
is either null
or an object with the attributes _code
and _msg
as described in the API documentation.
data
is an object representing the JSON response without modifications.
// Using promise interface
dyn.listFiles()
.then(response => console.log)
.catch(error => console.log);
// Using async/await
async function getMyFiles() {
const data = await dyn.listFiles();
// …
}
// With callback
dyn.listFiles(function(err, data) {
// …
});
You can use dyn.buildNodeTree(data.files)
to create a hierarchical tree of folders and files.
let changes = [
// …
];
dyn.editFile(changes, function(err, data) {
// …
});
let file_id = '…';
dyn.readDocument(file_id, function(err, data) {
// …
});
You can use dyn.buildNodeTree(data.nodes)
to create a hierarchical tree of the content.
let file_ids = ['…'];
dyn.checkForDocumentUpdates(file_ids, function(err, data) {
// …
});
let file_id = '…';
let changes = [
// …
];
dyn.editDocument(file_id, changes, function(err, data) {
// …
});
See API documentation for formatting changes
.
let content = 'Call Dana';
let options = {
index: -1,
// See API documentation for more options:
// https://apidocs.dynalist.io/#send-to-inbox
};
dyn.sendToInbox(content, options, function(err, data) {
// …
});
options
can be omitted when not needed:
dyn.sendToInbox('Call Fox', function(err, data) {
// …
});
This API method is not yet implemented.
Some helpers to massage data. They work only on local data.
let file_id = 'fe7a87a626f241b18ef30661';
let link_to_document = dyn.util.buildUrl(file_id);
// => https://dynalist.io/d/fe7a87a626f241b18ef30661
let node_id = '7be6403186fb8a7ed11931ed';
let link_to_node = dyn.util.buildUrl(file_id, node_id);
// => https://dynalist.io/d/fe7a87a626f241b18ef30661#z=7be6403186fb8a7ed11931ed
The document tree and the node tree inside a file are returned from the API as a flat array.
This method converts that array into an object with each node's id as the key.
dyn.readDocument('my document id', function(err, doc) {
const nodeMap = dyn.util.buildNodeMap(doc.nodes);
// Now the nodes can be accessed by using their ID as the key:
console.log(nodeMap['7be6403186fb8a7ed11931ed']);
});
Converts a flat array of nodes into a tree based on children
associations.
dyn.readDocument(documentId, function(err, doc) {
const tree = dyn.util.buildNodeTree(doc.nodes);
console.log(JSON.stringify(tree, 0, 4));
});
Rudimentary tests exist but could be extended by a lot. They are written with mocha
and should
.
The tests require a working API token and the ID of a file that can be used by the tests. The file will be modified, therefore it should not contain actually important information. Also note that frequent test runs may run into the API's rate limiting.
There is no option to run tests only offline yet. Tests WILL modify data in your account.
# Install dependencies
npm install
# Run test, passing in API token and file ID as environment variables
API_TOKEN=my_api_token FILE_ID=my_test_file npm test
# If you want to pass options to mocha you need to call mocha directly:
API_TOKEN=my_api_token FILE_ID=my_test_file ./node_modules/.bin/mocha --grep "Client#listFiles"