-
Notifications
You must be signed in to change notification settings - Fork 2
/
CronicleClient.ts
157 lines (135 loc) · 5.57 KB
/
CronicleClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
import * as qs from 'querystring';
import * as request from 'request-promise';
import {CronicleError} from './CronicleError';
import {
IAbortJobRequest,
IBasicResponse,
ICreateEventRequest,
ICreateEventResponse, IDeleteEventRequest, IErrorResponse,
IGetEventRequest,
IGetEventResponse, IGetHistoryRequest, IGetHistoryResponse,
IGetJobStatusRequest,
IGetJobStatusResponse, IGetScheduleRequest, IGetScheduleResponse,
IRunEventRequest,
IRunEventResponse,
IUpdateEventRequest,
IUpdateJobRequest,
} from './requestResponseTypes';
import {IBasePlugins} from './plugins';
const enum HttpMethods {
POST = 'POST',
GET = 'GET',
}
interface IBodyOrQuery {
[key: string]: any;
}
export enum BaseTargets {
ALL = 'allgrp',
MAIN = 'maingrp',
}
export enum BaseCategories {
GENERAL = 'general',
}
export interface ICronicleClientOptions {
masterUrl: string;
apiKey: string;
}
export class CronicleClient<Categories extends string = BaseCategories,
Targets extends string = BaseTargets,
Plugins = IBasePlugins> {
private readonly _headers: any;
private readonly _baseUrl: string;
constructor(opts: ICronicleClientOptions) {
if (!opts.masterUrl) {
throw new Error('cronicle master url is required');
}
if (!opts.apiKey) {
throw new Error('cronicle api key is required');
}
this._baseUrl = `${opts.masterUrl.replace(/\/$/, '')}/api/app`;
this._headers = {
'X-API-Key': opts.apiKey,
};
}
public createEvent<Plugin extends keyof Plugins>(
req: ICreateEventRequest<Plugin, Plugins, Targets, Categories>, enforceUnique?: boolean)
: Promise<ICreateEventResponse> {
return Promise.resolve()
.then(() => {
if (!enforceUnique) {
return Promise.resolve();
}
return this.getEvent(req.id ? {id: req.id} : {title: req.title})
.catch(() => {
return Promise.resolve();
})
.then((resp) => {
if (resp) {
return Promise.reject(new CronicleError({
code: 'unique',
description: 'event already exists',
}));
}
return Promise.resolve();
});
})
.then(() => {
return this._executeRequest('create_event', HttpMethods.POST, req);
});
}
public getEvent<Plugin extends keyof Plugins = any>(
req: IGetEventRequest): Promise<IGetEventResponse<Plugin, Plugins, Targets, Categories>> {
return this._executeRequest('get_event', HttpMethods.POST, req);
}
public getJobStatus<Plugin extends keyof Plugins = any>(
req: IGetJobStatusRequest): Promise<IGetJobStatusResponse<Plugin, Plugins, Targets, Categories>> {
return this._executeRequest('get_job_status', HttpMethods.GET, req);
}
public runEvent<Plugin extends keyof Plugins = any>(
req: IRunEventRequest<Plugin, Plugins, Targets, Categories>): Promise<IRunEventResponse> {
return this._executeRequest('run_event', HttpMethods.POST, req);
}
public updateEvent<Plugin extends keyof Plugins = any>(
req: IUpdateEventRequest<Plugin, Plugins, Targets, Categories>): Promise<IBasicResponse> {
return this._executeRequest('update_event', HttpMethods.POST, req);
}
public updateJob(req: IUpdateJobRequest): Promise<IBasicResponse> {
return this._executeRequest('update_job', HttpMethods.POST, req);
}
public deleteEvent(req: IDeleteEventRequest): Promise<IBasicResponse> {
return this._executeRequest('delete_event', HttpMethods.POST, req);
}
public abortJob(req: IAbortJobRequest): Promise<IBasicResponse> {
return this._executeRequest('abort_job', HttpMethods.POST, req);
}
public getSchedule(req?: IGetScheduleRequest): Promise<IGetScheduleResponse<any, Plugins, Targets, Categories>> {
return this._executeRequest('get_schedule', HttpMethods.GET, req);
}
public getHistory(req?: IGetHistoryRequest): Promise<IGetHistoryResponse<any, Plugins, Targets, Categories>> {
return this._executeRequest('get_history', HttpMethods.GET, req);
}
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));
}
return Promise.resolve(response as T);
});
}
private _buildRequest(operation: string, httpMethod: HttpMethods, bodyOrQuery?: IBodyOrQuery) {
return {
url: this._getMethodUrl(operation, httpMethod === HttpMethods.GET ? bodyOrQuery : undefined),
method: httpMethod,
body: httpMethod === HttpMethods.GET ? undefined : bodyOrQuery,
json: true,
headers: this._headers,
};
}
private _getMethodUrl(operation: string, query?: IBodyOrQuery) {
const queryString = `${query ? '?' : ''}${query ? qs.stringify(query) : ''}`;
return `${this._baseUrl}/${operation}/v1${queryString}`;
}
}