Cozy Connector Tutorial

doubleface
260 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Hello world connector

Here is the traditional hello world code, but for a connector!

Hello konnector!
1
2
3
4
5
6
7
8
'use strict'
const {BaseKonnector, log} = require('cozy-konnector-libs')
module.exports = new BaseKonnector(function init () {
log('info', 'Hello World!')
})
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If you run it, you will see the hello world message in the standard output as a JSON string. The cozy stack can interpret those messages and this will be useful later.

If you want to run this connector outside tech.io, this is not hard either provided you have NodeJS 6 or better installed :

Create a directory, then run :

npm init
npm install cozy-konnector-libs

Create an index.js file in this directory and put the code of the connector in it.

Then run :

node_modules/.bin/cozy-konnector-standalone

Your hello world konnector should work!

BaseKonnector ( fetch, options )

This connector does not do much, but it is the occasion to learn about the features of the BaseKonnector:

  • Every connector must inherit from the BaseKonnector
  • It fetches the information about the linked account in the cozy (or in ./konnector-dev-config.json in standalone mode)
  • It calls the function passed as argument in its constructor with an object argument containing all the parameters for the connector (none is needed this time) after doing all the previous steps and waits for a promise as a return value

You can change the fields value in konnector-dev-config.json and see that it is passed as first argument in the init function.

log ( type, message, label )

  • type: Can be info, error, warning or debug. There is a color associated to each type.

  • message is the item we want to log and can be of any type which can be JSON.stringified.

  • label is optional and must be a string

  • In production, messages are outputted in JSON. The cozy-stack can interpret certain logs to show information to the user while the konnector fetches data
  • In development/standalone, it outputs colored text 🌸
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content