Skip to content

Commit

Permalink
revise some interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Jun 1, 2019
1 parent c079333 commit 79f786c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Changed some interfaces names to more suitable ones
- Changed error class name to a more suitable one

## v0.1.0
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ If you want to use the timing utils, you must also install `moment`
## Quick example

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

const scheduler = new CronicleClient({
masterUrl: 'http://localhost:3012',
Expand All @@ -47,9 +47,10 @@ scheduler.createEvent({
plugin: basePlugins.urlplug,
title: 'test event1',
enabled: NumberedBoolean.TRUE,
algo: TargetAlgorithms.RANDOM,
category: BaseCategories.GENERAL,
target: BaseTargets.GENERAL,
timing: getUtcTiming('2016-05-26T14:50:50.900Z'),
timing: getFutureUtcTiming('2016-05-26T14:50:50.900Z'),
timezone: 'Etc/UTC',
params: {
method: HttpPluginMethods.POST,
Expand All @@ -75,7 +76,7 @@ scheduler.createEvent({

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

interface ICustomPluginData {
duration: string;
Expand Down Expand Up @@ -124,9 +125,10 @@ scheduler.createEvent({
plugin: plugins.mycustomplug,
title: 'test event1',
enabled: NumberedBoolean.TRUE,
algo: TargetAlgorithms.RANDOM,
category: Categories.TEST_CATEGORY2,
target: Targets.AWS,
timing: getUtcTiming('2016-05-26T14:50:50.900Z'),
timing: getFutureUtcTiming('2016-05-26T14:50:50.900Z'),
timezone: 'Etc/UTC',
params: {
duration: '60',
Expand Down Expand Up @@ -251,14 +253,14 @@ const scheduler = new CronicleClient<Categories, Targets, Plugins>({
To support a wide variety of scheduling, the [timing object](https://github.com/jhuckaby/Cronicle#event-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
`getTiming` and `getUtcTiming` methods:
`getFutureTiming` and `getFutureUtcTiming` methods:

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

Running:
```typescript
getUtcTiming(moment.utc('2016-05-26T14:50:50.900Z');
getFutureUtcTiming(moment.utc('2016-05-26T14:50:50.900Z');
```
Will produce:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cronicle-client",
"version": "0.1.0",
"version": "0.2.0",
"description": "Light Cronicle node client with full TypeScript support",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
37 changes: 19 additions & 18 deletions src/CronicleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ import {IBasePlugins} from './plugins';

const DEFAULT_API_VERSION = 'v1';

export interface ISchedulerOptions {
masterUrl: string;
apiKey: string;
apiVersion?: string;
}

const enum HttpMethods {
POST = 'POST',
GET = 'GET',
}

interface IBodyOrQuery {
[key: string]: any;
}

export enum BaseTargets {
ALL = 'allgrp',
MAIN = 'maingrp',
Expand All @@ -41,8 +39,10 @@ export enum BaseCategories {
GENERAL = 'general',
}

interface IBody {
[key: string]: any;
export interface ICronicleClientOptions {
masterUrl: string;
apiKey: string;
apiVersion?: string;
}

export class CronicleClient<Categories extends string = BaseCategories,
Expand All @@ -53,7 +53,7 @@ export class CronicleClient<Categories extends string = BaseCategories,
private readonly _baseUrl: string;
private readonly _apiVersion: string;

constructor(opts: ISchedulerOptions) {
constructor(opts: ICronicleClientOptions) {
if (!opts.masterUrl) {
throw new Error('cronicle master url is required');
}
Expand Down Expand Up @@ -130,9 +130,9 @@ export class CronicleClient<Categories extends string = BaseCategories,
return this._executeRequest('get_schedule', HttpMethods.GET, req);
}

private _executeRequest<T extends IBasicResponse>(op: string, method: HttpMethods, bodyOrQuery?: IBody)
: Promise<T> {
return Promise.resolve(request(this._buildRequest(op, method, bodyOrQuery)))
private _executeRequest<T extends IBasicResponse>(operation: string, httpMethod: HttpMethods,
bodyOrQuery?: IBodyOrQuery): Promise<T> {
return Promise.resolve(request(this._buildRequest(operation, httpMethod, bodyOrQuery)))
.then((response: T | IErrorResponse) => {
if (response.code !== 0) {
return Promise.reject(new CronicleError(response as IErrorResponse));
Expand All @@ -141,17 +141,18 @@ export class CronicleClient<Categories extends string = BaseCategories,
});
}

private _buildRequest(op: string, method: HttpMethods, bodyOrQuery?: IBody) {
private _buildRequest(operation: string, httpMethod: HttpMethods, bodyOrQuery?: IBodyOrQuery) {
return {
url: this._getMethodUrl(op, method === HttpMethods.GET ? bodyOrQuery : undefined),
method,
body: method === HttpMethods.GET ? undefined : bodyOrQuery,
url: this._getMethodUrl(operation, httpMethod === HttpMethods.GET ? bodyOrQuery : undefined),
method: httpMethod,
body: httpMethod === HttpMethods.GET ? undefined : bodyOrQuery,
json: true,
headers: this._headers,
};
}

private _getMethodUrl(op: string, query?: IBody) {
return `${this._baseUrl}/${op}/${this._apiVersion}${query ? '?' : ''}${query ? qs.stringify(query) : ''}`;
private _getMethodUrl(operation: string, query?: IBodyOrQuery) {
const queryString = `${query ? '?' : ''}${query ? qs.stringify(query) : ''}`;
return `${this._baseUrl}/${operation}/${this._apiVersion}${queryString}`;
}
}
4 changes: 2 additions & 2 deletions src/dataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {NumberedBoolean} from './helperTypes';

export enum Algorithms {
export enum TargetAlgorithms {
RANDOM = 'random',
RR = 'round_robin',
LEAST_CPU = 'least_cpu',
Expand Down Expand Up @@ -215,7 +215,7 @@ export interface ICreatedEvent<Plugin extends keyof Plugins,
}

export interface IEvent<Plugin extends keyof Plugins, Plugins, Targets extends string, Categories extends string> {
algo: Algorithms;
algo: TargetAlgorithms;
catch_up: NumberedBoolean;
category: Categories;
chain: string;
Expand Down
2 changes: 1 addition & 1 deletion src/helperTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type RecursivePartial<T> = {
T[P];
};

export type PluginNameType<Plugin, Plugins> = Plugin extends keyof Plugins ? Plugin : never;
type PluginNameType<Plugin, Plugins> = Plugin extends keyof Plugins ? Plugin : never;

export type IPluginNames<Plugins> = {
[k in keyof Plugins]: PluginNameType<k, Plugins>;
Expand Down
4 changes: 2 additions & 2 deletions src/timingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const _getTiming = (date: Moment): ITiming => {
};
};

export const getTiming = (date: Moment | Date | string): ITiming => {
export const getFutureTiming = (date: Moment | Date | string): ITiming => {
const moment = _getMoment();
if (!moment.isMoment(date)) {
date = moment(date);
}
return _getTiming(date);
};

export const getUtcTiming = (date: Moment | Date | string): ITiming => {
export const getFutureUtcTiming = (date: Moment | Date | string): ITiming => {
const moment = _getMoment();
if (!moment.isMoment(date)) {
date = moment.utc(date);
Expand Down
16 changes: 8 additions & 8 deletions test/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import {
getUtcTiming,
getTiming,
getFutureUtcTiming,
getFutureTiming,
CronicleClient,
CronicleError,
NumberedBoolean,
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('cronicle client', () => {
describe('utc timing helpers ', () => {

it('should create timing object from moment', () => {
const timing = getUtcTiming(moment.utc('2016-05-26T14:50:50.900Z'));
const timing = getFutureUtcTiming(moment.utc('2016-05-26T14:50:50.900Z'));
expect(timing.years![0]).to.eq(2016);
expect(timing.months![0]).to.eq(5);
expect(timing.days![0]).to.eq(26);
Expand All @@ -49,7 +49,7 @@ describe('cronicle client', () => {
});

it('should create timing object from date', () => {
const timing = getUtcTiming(new Date('2016-05-26T14:50:50.900Z'));
const timing = getFutureUtcTiming(new Date('2016-05-26T14:50:50.900Z'));
expect(timing.years![0]).to.eq(2016);
expect(timing.months![0]).to.eq(5);
expect(timing.days![0]).to.eq(26);
Expand All @@ -58,7 +58,7 @@ describe('cronicle client', () => {
});

it('should create timing object from string', () => {
const timing = getUtcTiming('2016-05-26T14:50:50.900Z');
const timing = getFutureUtcTiming('2016-05-26T14:50:50.900Z');
expect(timing.years![0]).to.eq(2016);
expect(timing.months![0]).to.eq(5);
expect(timing.days![0]).to.eq(26);
Expand All @@ -79,7 +79,7 @@ describe('cronicle client', () => {
});

it('should create timing object from moment', () => {
const timing = getTiming(moment('2016-05-26T14:50:50.900Z'));
const timing = getFutureTiming(moment('2016-05-26T14:50:50.900Z'));
expect(timing.years![0]).to.eq(2016);
expect(timing.months![0]).to.eq(5);
expect(timing.days![0]).to.eq(26);
Expand All @@ -88,7 +88,7 @@ describe('cronicle client', () => {
});

it('should create timing object from date', () => {
const timing = getTiming(new Date('2016-05-26T14:50:50.900Z'));
const timing = getFutureTiming(new Date('2016-05-26T14:50:50.900Z'));
expect(timing.years![0]).to.eq(2016);
expect(timing.months![0]).to.eq(5);
expect(timing.days![0]).to.eq(26);
Expand All @@ -97,7 +97,7 @@ describe('cronicle client', () => {
});

it('should create timing object from string', () => {
const timing = getTiming('2016-05-26T14:50:50.900Z');
const timing = getFutureTiming('2016-05-26T14:50:50.900Z');
expect(timing.years![0]).to.eq(2016);
expect(timing.months![0]).to.eq(5);
expect(timing.days![0]).to.eq(26);
Expand Down

0 comments on commit 79f786c

Please sign in to comment.