Back
Close

Cozy Connector Tutorial

doubleface
2,737 views
Previous: intro Next: Save cats

Hello world connector

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

Hello konnector!
'use strict'
const {BaseKonnector, log} = require('cozy-konnector-libs')
module.exports = new BaseKonnector(function init () {
log('info', 'Hello World!')
})
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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 🌸
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants