npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cronicle-client

v1.1.0

Published

Light Cronicle node client with full TypeScript support

Downloads

174

Readme

Npm Version Build Status Coverage Status Codacy Badge Known Vulnerabilities dependencies Status devDependencies Status

Cronicle client

Light Croncile node client with full TypeScript support

Main features

  • Fully typed api client for Cronicle
  • No dependencies (you need to install your own request-promise library)
  • Helper methods to build Timing objects for scheduling events
  • Type safety extendable for Categories, Plugins and Targets

Install

npm install cronicle-client

--NOTICE--
request-promise is a peer dependency and must be installed by you (>=1.0.0)

--NOTICE--
If you want to use the timing utils, you must also install moment

Quick example

import { CronicleClient, NumberedBoolean, BaseCategories, BaseTargets, getFutureUtcTiming, 
 HttpPluginMethods, basePlugins, CronicleError, TargetAlgorithms} from 'cronicle-client';

const scheduler = new CronicleClient({
  masterUrl: 'http://localhost:3012',
  apiKey: '<your api key>',
});

scheduler.createEvent({
        plugin: basePlugins.urlplug,
        title: 'test event1',
        enabled: NumberedBoolean.TRUE,
        algo: TargetAlgorithms.RANDOM,
        category: BaseCategories.GENERAL,
        target: BaseTargets.GENERAL,
        timing: getFutureUtcTiming('2016-05-26T14:50:50.900Z'),
        timezone: 'Etc/UTC',
        params: {
          method: HttpPluginMethods.POST,
          timeout: '60',
          headers:  'Content-Type: application/json',
          data: JSON.stringify({ a: 1 }),
          url: 'https://requestbin.com',
        },
      })
      .then((data) => {
        console.log(`Created event with id: ${data.id}`);
        return scheduler.runEvent({ id: data.id });
      })
      .then((data) => {
        console.log(`Started event with job id: ${data.ids[0]}`);
      })
      .catch((err: CronicleError) => {
        console.log(`Cronicle error: ${err.code} - ${err.message}`);
      });

Extending with custom types example

import { CronicleClient, IHttpPluginData, IShellPluginData, ITestPluginData, NumberedBoolean,
    getFutureUtcTiming, IPluginNames, CronicleError, TargetAlgorithms } from 'cronicle-client';

interface ICustomPluginData {
  duration: string;
  action: string;
}

interface Plugins {
  // Default plugins
  urlplug: IHttpPluginData;
  shellplug: IShellPluginData;
  testplug: ITestPluginData;
  // Custom plugins
  mycustomplug: ICustomPluginData;
}

enum Categories {
  // Default category
  GENERAL = 'general',
  // Custom categories...
  TEST_CATEGORY = 'cjw6g085901', 
  TEST_CATEGORY2 = 'cjw6l8mnb02',
}

enum Targets {
    // Default targets...
    ALL = 'allgrp',
    MAIN = 'maingrp',
    // Custom targets...
    AWS = 'awsgrp',
    GCP = 'gcpgrp',
}

const plugins: IPluginNames<Plugins> = {
    urlplug: 'urlplug',
    shellplug: 'shellplug',
    testplug: 'testplug',
    mycustomplug: 'mycustomplug',
};

const scheduler = new CronicleClient<Categories, Targets, Plugins>({
  masterUrl: 'http://localhost:3012',
  apiKey: '<your api key>',
});

scheduler.createEvent({
        plugin: plugins.mycustomplug,
        title: 'test event1',
        enabled: NumberedBoolean.TRUE,
        algo: TargetAlgorithms.RANDOM,
        category: Categories.TEST_CATEGORY2,
        target: Targets.AWS,
        timing: getFutureUtcTiming('2016-05-26T14:50:50.900Z'),
        timezone: 'Etc/UTC',
        params: {
          duration: '60',
          action: JSON.stringify({ a: 1 }),
        },
      })
      .then((data) => {
        console.log(`Created event with id: ${data.id}`);
        return scheduler.runEvent({ id: data.id });
      })
      .then((data) => {
        console.log(`Started event with job id: ${data.ids[0]}`);
      })
      .catch((err: CronicleError) => {
        console.log(`Cronicle error: ${err.code} - ${err.message}`);
      });

Documentation

Methods

For all api endpoints documentations, please refer to Cronicle api reference

createEvent

When creating an event, there is no unique restriction on the title/id.
While searching for an event using getEvent, the api allows you to search by title/id, which is great, but as of now (cronicle v0.89) it will return a single result.
This imposes an issue when you don't enforce a unique title/id since you will get a random result (see #186)
Until this behaviour is fixed, you can tell the createEvent method to enforce uniqueness and it will fail if an event with the provided title/id already exists.
Note: if id is provided - it will be used as the unique key, otherwise title will be used.

Error handling

Croncile will always return a valid HTTP response (code 200).
To raise an error, the code property of the response will be different than 0.
In such cases, the current method will be rejected with CronicleError with the proper error message and the code property.

Constructor

Options

| Parameter Name | Description | |----------------|-------------| | masterUrl | The full url to the master Cronicle server | apiKey | The api key to use (make sure it has relevant permissions enabled)

Typings

The client can enforce the proper usage of categories, targets and plugins (with their required parameters).
This is done using optional generics:

| Generics Parameter Name | Description | |--------------------------|-------------| | Categories | Enum containing the ids of the categories available at you Cronicle server (Defaults to BaseCategories) | Targets | Enum containing the ids of the targets available at you Cronicle server (Defaults to BaseTargets) | Plugins | Interface containing mapping between plugin ids and the interface representing their required event params (Defaults to IBasePlugins)

Examples

Example constructor with defaults:

const scheduler = new CronicleClient({
  masterUrl: 'http://localhost:3012',
  apiKey: '<your api key>',
});

Example for setting the categories your server supports:

enum Categories {
  // Default category
  GENERAL = 'general',
  // Custom categories...
  TEST_CATEGORY = 'cjw6g085901', 
  TEST_CATEGORY2 = 'cjw6l8mnb02',
}

Example for setting the targets your server supports:

enum Targets {
    // Default targets...
    ALL = 'allgrp',
    MAIN = 'maingrp',
    // Custom targets...
    AWS = 'awsgrp',
    GCP = 'gcpgrp',
}

Example for setting the plugins your server supports:

interface ICustomPluginData {
  duration: string;
  action: string;
}

interface Plugins {
  // Default plugins
  urlplug: IHttpPluginData;
  shellplug: IShellPluginData;
  testplug: ITestPluginData;
  // Custom plugins
  mycustomplug: ICustomPluginData;
}

Example constructor with overrides:

const scheduler = new CronicleClient<Categories, Targets, Plugins>({
  masterUrl: 'http://localhost:3012',
  apiKey: '<your api key>',
});

Timing utils

To support a wide variety of scheduling, the timing object an be very cumbersome...
To make life easier (at least when you just want to schedule an event for a single future run) you can use the getFutureTiming and getFutureUtcTiming methods:

--NOTICE--
If you want to use the timing utils, you MUST npm install --save moment

Running:

getFutureUtcTiming(moment.utc('2016-05-26T14:50:50.900Z');

Will produce:

{ 
  "years": [ 2016 ],
  "months": [ 5 ],
  "days": [ 26 ],
  "hours": [ 14 ],
  "minutes": [ 50 ]
}

Versions

Cronicle client supports Node 6 LTS and higher.

Contributing

All contributions are happily welcomed!
Please make all pull requests to the master branch from your fork and ensure tests pass locally.