@brimble/consul 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/query.js ADDED
@@ -0,0 +1,244 @@
1
+ const errors = require("./errors");
2
+ const utils = require("./utils");
3
+
4
+ class Query {
5
+ constructor(consul) {
6
+ this.consul = consul;
7
+ }
8
+
9
+ /**
10
+ * Lists all queries
11
+ */
12
+ async list(opts) {
13
+ opts = utils.normalizeKeys(opts);
14
+ opts = utils.defaults(opts, this.consul._defaults);
15
+
16
+ const req = {
17
+ name: "query.list",
18
+ path: "/query",
19
+ };
20
+
21
+ utils.options(req, opts);
22
+
23
+ return await this.consul._get(req, utils.body);
24
+ }
25
+
26
+ /**
27
+ * Create a new query
28
+ */
29
+ async create(opts) {
30
+ if (typeof opts === "string") {
31
+ opts = { service: { service: opts } };
32
+ }
33
+
34
+ opts = utils.normalizeKeys(opts);
35
+ opts = utils.defaults(opts, this.consul._defaults);
36
+
37
+ const req = {
38
+ name: "query.create",
39
+ path: "/query",
40
+ query: {},
41
+ type: "json",
42
+ };
43
+
44
+ this._params(req, opts);
45
+
46
+ if (!req.body.Service || !req.body.Service.Service) {
47
+ throw this.consul._err(errors.Validation("service required"), req);
48
+ }
49
+
50
+ utils.options(req, opts, { near: true });
51
+
52
+ return await this.consul._post(req, utils.body);
53
+ }
54
+
55
+ /**
56
+ * Gets a given query
57
+ */
58
+ async get(opts) {
59
+ if (typeof opts === "string") {
60
+ opts = { query: opts };
61
+ }
62
+
63
+ opts = utils.normalizeKeys(opts);
64
+ opts = utils.defaults(opts, this.consul._defaults);
65
+
66
+ const req = {
67
+ name: "query.get",
68
+ path: "/query/{query}",
69
+ params: { query: opts.query },
70
+ query: {},
71
+ };
72
+
73
+ if (!opts.query) {
74
+ throw this.consul._err(errors.Validation("query required"), req);
75
+ }
76
+
77
+ utils.options(req, opts);
78
+
79
+ return await this.consul._get(req, utils.bodyItem);
80
+ }
81
+
82
+ /**
83
+ * Update existing query
84
+ */
85
+ async update(opts) {
86
+ opts = utils.normalizeKeys(opts);
87
+ opts = utils.defaults(opts, this.consul._defaults);
88
+
89
+ const req = {
90
+ name: "query.update",
91
+ path: "/query/{query}",
92
+ params: { query: opts.query },
93
+ query: {},
94
+ type: "json",
95
+ };
96
+
97
+ if (!opts.query) {
98
+ throw this.consul._err(errors.Validation("query required"), req);
99
+ }
100
+
101
+ this._params(req, opts);
102
+
103
+ if (!req.body.Service || !req.body.Service.Service) {
104
+ throw this.consul._err(errors.Validation("service required"), req);
105
+ }
106
+
107
+ utils.options(req, opts, { near: true });
108
+
109
+ return await this.consul._put(req, utils.empty);
110
+ }
111
+
112
+ /**
113
+ * Destroys a given query
114
+ */
115
+ async destroy(opts) {
116
+ if (typeof opts === "string") {
117
+ opts = { query: opts };
118
+ }
119
+
120
+ opts = utils.normalizeKeys(opts);
121
+ opts = utils.defaults(opts, this.consul._defaults);
122
+
123
+ const req = {
124
+ name: "query.destroy",
125
+ path: "/query/{query}",
126
+ params: { query: opts.query },
127
+ query: {},
128
+ };
129
+
130
+ if (!opts.query) {
131
+ throw this.consul._err(errors.Validation("query required"), req);
132
+ }
133
+
134
+ utils.options(req, opts);
135
+
136
+ return await this.consul._delete(req, utils.empty);
137
+ }
138
+
139
+ /**
140
+ * Executes a given query
141
+ */
142
+ async execute(opts) {
143
+ if (typeof opts === "string") {
144
+ opts = { query: opts };
145
+ }
146
+
147
+ opts = utils.normalizeKeys(opts);
148
+ opts = utils.defaults(opts, this.consul._defaults);
149
+
150
+ const req = {
151
+ name: "query.execute",
152
+ path: "/query/{query}/execute",
153
+ params: { query: opts.query },
154
+ query: {},
155
+ };
156
+
157
+ if (!opts.query) {
158
+ throw this.consul._err(errors.Validation("query required"), req);
159
+ }
160
+
161
+ utils.options(req, opts);
162
+
163
+ return await this.consul._get(req, utils.body);
164
+ }
165
+
166
+ /**
167
+ * Explain a given query
168
+ */
169
+ async explain(opts) {
170
+ if (typeof opts === "string") {
171
+ opts = { query: opts };
172
+ }
173
+
174
+ opts = utils.normalizeKeys(opts);
175
+ opts = utils.defaults(opts, this.consul._defaults);
176
+
177
+ const req = {
178
+ name: "query.explain",
179
+ path: "/query/{query}/explain",
180
+ params: { query: opts.query },
181
+ query: {},
182
+ };
183
+
184
+ if (!opts.query) {
185
+ throw this.consul._err(errors.Validation("query required"), req);
186
+ }
187
+
188
+ utils.options(req, opts);
189
+
190
+ return await this.consul._get(req, utils.bodyItem);
191
+ }
192
+
193
+ /**
194
+ * Generate body for query create and update
195
+ */
196
+ _params(req, opts) {
197
+ const body = req.body || {};
198
+
199
+ if (opts.name) body.Name = opts.name;
200
+ if (opts.session) body.Session = opts.session;
201
+ if (opts.token) {
202
+ body.Token = opts.token;
203
+ delete opts.token;
204
+ }
205
+ if (opts.near) body.Near = opts.near;
206
+ if (opts.template) {
207
+ const template = utils.normalizeKeys(opts.template);
208
+ if (template.type || template.regexp) {
209
+ body.Template = {};
210
+ if (template.type) body.Template.Type = template.type;
211
+ if (template.regexp) body.Template.Regexp = template.regexp;
212
+ }
213
+ }
214
+ if (opts.service) {
215
+ const service = utils.normalizeKeys(opts.service);
216
+ body.Service = {};
217
+ if (service.service) body.Service.Service = service.service;
218
+ if (service.failover) {
219
+ const failover = utils.normalizeKeys(service.failover);
220
+ if (typeof failover.nearestn === "number" || failover.datacenters) {
221
+ body.Service.Failover = {};
222
+ if (typeof failover.nearestn === "number") {
223
+ body.Service.Failover.NearestN = failover.nearestn;
224
+ }
225
+ if (failover.datacenters) {
226
+ body.Service.Failover.Datacenters = failover.datacenters;
227
+ }
228
+ }
229
+ }
230
+ if (typeof service.onlypassing === "boolean") {
231
+ body.Service.OnlyPassing = service.onlypassing;
232
+ }
233
+ if (service.tags) body.Service.Tags = service.tags;
234
+ }
235
+ if (opts.dns) {
236
+ const dns = utils.normalizeKeys(opts.dns);
237
+ if (dns.ttl) body.DNS = { TTL: dns.ttl };
238
+ }
239
+
240
+ req.body = body;
241
+ }
242
+ }
243
+
244
+ exports.Query = Query;
@@ -0,0 +1,92 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface CreateOptions extends CommonOptions {
4
+ dc?: string;
5
+ lockdelay?: string;
6
+ node?: string;
7
+ name?: string;
8
+ checks?: string[];
9
+ nodechecks?: string[];
10
+ servicechecks?: { id: string; namespace?: string }[];
11
+ behavior?: "release" | "delete";
12
+ ttl?: string;
13
+ }
14
+
15
+ interface CreateResult {
16
+ ID: string;
17
+ }
18
+
19
+ interface DestroyOptions extends CommonOptions {
20
+ id: string;
21
+ dc?: string;
22
+ ns?: string;
23
+ }
24
+
25
+ type DestroyResult = boolean;
26
+
27
+ interface InfoOptions extends CommonOptions {
28
+ id: string;
29
+ dc?: string;
30
+ }
31
+
32
+ interface InfoResult {
33
+ ID: string;
34
+ Name: string;
35
+ Node: string;
36
+ LockDelay: number;
37
+ Behavior: "release" | "delete";
38
+ TTL: string;
39
+ NodeChecks: string[] | null;
40
+ ServiceChecks: string[] | null;
41
+ CreateIndex: number;
42
+ ModifyIndex: number;
43
+ }
44
+
45
+ type GetOptions = InfoOptions;
46
+
47
+ type GetResult = InfoResult;
48
+
49
+ interface NodeOptions extends CommonOptions {
50
+ node: string;
51
+ dc?: string;
52
+ }
53
+
54
+ type NodeResult = InfoResult[];
55
+
56
+ interface ListOptions extends CommonOptions {
57
+ dc?: string;
58
+ }
59
+
60
+ type ListResult = InfoResult[];
61
+
62
+ interface RenewOptions extends CommonOptions {
63
+ id: string;
64
+ dc?: string;
65
+ }
66
+
67
+ type RenewResult = InfoResult[];
68
+
69
+ declare class Session {
70
+ constructor(consul: Consul);
71
+
72
+ consul: Consul;
73
+
74
+ create(options?: CreateOptions): Promise<CreateResult>;
75
+
76
+ destroy(options: DestroyOptions): Promise<DestroyResult>;
77
+ destroy(id: string): Promise<DestroyResult>;
78
+
79
+ info(options: InfoOptions): Promise<InfoResult>;
80
+ info(id: string): Promise<InfoResult>;
81
+
82
+ get(options: GetOptions): Promise<GetResult>;
83
+ get(id: string): Promise<GetResult>;
84
+
85
+ node(options: NodeOptions): Promise<NodeResult>;
86
+ node(node: string): Promise<NodeResult>;
87
+
88
+ list(options?: ListOptions): Promise<ListResult>;
89
+
90
+ renew(options: RenewOptions): Promise<RenewResult>;
91
+ renew(id: string): Promise<RenewResult>;
92
+ }
package/lib/session.js ADDED
@@ -0,0 +1,164 @@
1
+ const errors = require("./errors");
2
+ const utils = require("./utils");
3
+
4
+ class Session {
5
+ constructor(consul) {
6
+ this.consul = consul;
7
+ }
8
+
9
+ /**
10
+ * Creates a new session
11
+ */
12
+ async create(opts) {
13
+ opts = utils.normalizeKeys(opts);
14
+ opts = utils.defaults(opts, this.consul._defaults);
15
+
16
+ const req = {
17
+ name: "session.create",
18
+ path: "/session/create",
19
+ query: {},
20
+ type: "json",
21
+ body: {},
22
+ };
23
+
24
+ if (opts.lockdelay) req.body.LockDelay = opts.lockdelay;
25
+ if (opts.name) req.body.Name = opts.name;
26
+ if (opts.node) req.body.Node = opts.node;
27
+ if (opts.checks) req.body.Checks = opts.checks;
28
+ if (opts.behavior) req.body.Behavior = opts.behavior;
29
+ if (opts.ttl) req.body.TTL = opts.ttl;
30
+
31
+ utils.options(req, opts);
32
+
33
+ return await this.consul._put(req, utils.body);
34
+ }
35
+
36
+ /**
37
+ * Destroys a given session
38
+ */
39
+ async destroy(opts) {
40
+ if (typeof opts === "string") {
41
+ opts = { id: opts };
42
+ }
43
+
44
+ opts = utils.normalizeKeys(opts);
45
+ opts = utils.defaults(opts, this.consul._defaults);
46
+
47
+ const req = {
48
+ name: "session.destroy",
49
+ path: "/session/destroy/{id}",
50
+ params: { id: opts.id },
51
+ query: {},
52
+ };
53
+
54
+ if (!opts.id) {
55
+ throw this.consul._err(errors.Validation("id required"), req);
56
+ }
57
+
58
+ utils.options(req, opts);
59
+
60
+ return await this.consul._put(req, utils.empty);
61
+ }
62
+
63
+ /**
64
+ * Queries a given session
65
+ */
66
+ async info(opts) {
67
+ if (typeof opts === "string") {
68
+ opts = { id: opts };
69
+ }
70
+
71
+ opts = utils.normalizeKeys(opts);
72
+ opts = utils.defaults(opts, this.consul._defaults);
73
+
74
+ const req = {
75
+ name: "session.info",
76
+ path: "/session/info/{id}",
77
+ params: { id: opts.id },
78
+ query: {},
79
+ };
80
+
81
+ if (!opts.id) {
82
+ throw this.consul._err(errors.Validation("id required"), req);
83
+ }
84
+
85
+ utils.options(req, opts);
86
+
87
+ return await this.consul._get(req, utils.bodyItem);
88
+ }
89
+
90
+ get(opts) {
91
+ return this.info(opts);
92
+ }
93
+
94
+ /**
95
+ * Lists sessions belonging to a node
96
+ */
97
+ async node(opts) {
98
+ if (typeof opts === "string") {
99
+ opts = { node: opts };
100
+ }
101
+
102
+ opts = utils.normalizeKeys(opts);
103
+ opts = utils.defaults(opts, this.consul._defaults);
104
+
105
+ const req = {
106
+ name: "session.node",
107
+ path: "/session/node/{node}",
108
+ params: { node: opts.node },
109
+ };
110
+
111
+ if (!opts.node) {
112
+ throw this.consul._err(errors.Validation("node required"), req);
113
+ }
114
+
115
+ utils.options(req, opts);
116
+
117
+ return await this.consul._get(req, utils.body);
118
+ }
119
+
120
+ /**
121
+ * Lists all the active sessions
122
+ */
123
+ async list(opts) {
124
+ opts = utils.normalizeKeys(opts);
125
+ opts = utils.defaults(opts, this.consul._defaults);
126
+
127
+ const req = {
128
+ name: "session.list",
129
+ path: "/session/list",
130
+ };
131
+
132
+ utils.options(req, opts);
133
+
134
+ return await this.consul._get(req, utils.body);
135
+ }
136
+
137
+ /**
138
+ * Renews a TTL-based session
139
+ */
140
+ async renew(opts) {
141
+ if (typeof opts === "string") {
142
+ opts = { id: opts };
143
+ }
144
+
145
+ opts = utils.normalizeKeys(opts);
146
+ opts = utils.defaults(opts, this.consul._defaults);
147
+
148
+ const req = {
149
+ name: "session.renew",
150
+ path: "/session/renew/{id}",
151
+ params: { id: opts.id },
152
+ };
153
+
154
+ if (!opts.id) {
155
+ throw this.consul._err(errors.Validation("id required"), req);
156
+ }
157
+
158
+ utils.options(req, opts);
159
+
160
+ return await this.consul._put(req, utils.body);
161
+ }
162
+ }
163
+
164
+ exports.Session = Session;
@@ -0,0 +1,19 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface LeaderOptions extends CommonOptions {}
4
+
5
+ type LeaderResult = string;
6
+
7
+ interface PeersOptions extends CommonOptions {}
8
+
9
+ type PeersResult = string[];
10
+
11
+ declare class Status {
12
+ constructor(consul: Consul);
13
+
14
+ consul: Consul;
15
+
16
+ leader(options?: LeaderOptions): Promise<LeaderResult>;
17
+
18
+ peers(options?: PeersOptions): Promise<PeersResult>;
19
+ }
package/lib/status.js ADDED
@@ -0,0 +1,43 @@
1
+ const utils = require("./utils");
2
+
3
+ class Status {
4
+ constructor(consul) {
5
+ this.consul = consul;
6
+ }
7
+
8
+ /**
9
+ * Returns the current Raft leader.
10
+ */
11
+ async leader(opts) {
12
+ opts = utils.normalizeKeys(opts);
13
+ opts = utils.defaults(opts, this.consul._defaults);
14
+
15
+ const req = {
16
+ name: "status.leader",
17
+ path: "/status/leader",
18
+ };
19
+
20
+ utils.options(req, opts);
21
+
22
+ return await this.consul._get(req, utils.body);
23
+ }
24
+
25
+ /**
26
+ * Returns the current Raft peer set
27
+ */
28
+ async peers(opts) {
29
+ opts = utils.normalizeKeys(opts);
30
+ opts = utils.defaults(opts, this.consul._defaults);
31
+
32
+ const req = {
33
+ name: "status.peers",
34
+ path: "/status/peers",
35
+ };
36
+
37
+ utils.options(req, opts);
38
+
39
+ return await this.consul._get(req, utils.body);
40
+ }
41
+ }
42
+
43
+ exports.Status = Status;
@@ -0,0 +1,50 @@
1
+ import { CommonOptions, Consul } from "./consul";
2
+
3
+ interface KVOption {
4
+ verb: string;
5
+ key: string;
6
+ value?: string;
7
+ flags?: number;
8
+ index?: number;
9
+ session?: string;
10
+ namespace?: string;
11
+ }
12
+
13
+ interface NodeOption {
14
+ verb: string;
15
+ node: any;
16
+ }
17
+
18
+ interface ServiceOption {
19
+ verb: string;
20
+ node: string;
21
+ service: any;
22
+ }
23
+
24
+ interface CheckOption {
25
+ verb: string;
26
+ check: any;
27
+ }
28
+
29
+ type Operation = KVOption | NodeOption | ServiceOption | CheckOption;
30
+
31
+ interface CreateOptions extends CommonOptions {
32
+ operations: Operation[];
33
+ }
34
+
35
+ interface CreateResult {
36
+ Results?: Record<"KV" | "Node" | "Service" | "Check", any>[];
37
+ Errors?: any[];
38
+ }
39
+
40
+ declare class Transaction {
41
+ constructor(consul: Consul);
42
+
43
+ consul: Consul;
44
+
45
+ create(options: CreateOptions): Promise<CreateResult>;
46
+ create(
47
+ operations: Operation[],
48
+ options: CreateOptions,
49
+ ): Promise<CreateResult>;
50
+ }
@@ -0,0 +1,58 @@
1
+ const errors = require("./errors");
2
+ const utils = require("./utils");
3
+
4
+ class Transaction {
5
+ constructor(consul) {
6
+ this.consul = consul;
7
+ }
8
+
9
+ /**
10
+ * Create
11
+ */
12
+ async create(operations) {
13
+ let opts;
14
+ switch (arguments.length) {
15
+ case 2:
16
+ // create(operations, opts)
17
+ opts = utils.clone(arguments[1]);
18
+ opts.operations = operations;
19
+ break;
20
+ case 1:
21
+ // create(operations)
22
+ opts = { operations };
23
+ break;
24
+ default:
25
+ throw this.consul._err(
26
+ errors.Validation(
27
+ "a list of operations are required as first arguments",
28
+ ),
29
+ { name: "Transaction.create" },
30
+ );
31
+ }
32
+
33
+ opts = utils.normalizeKeys(opts);
34
+ opts = utils.defaults(opts, this.consul._defaults);
35
+
36
+ const req = {
37
+ name: "Transaction.create",
38
+ path: "/txn",
39
+ params: {},
40
+ query: {},
41
+ type: "json",
42
+ body: opts.operations,
43
+ };
44
+
45
+ if (!(Array.isArray(opts.operations) && opts.operations.length > 0)) {
46
+ throw this.consul._err(
47
+ errors.Validation("operations must be an array with at least one item"),
48
+ req,
49
+ );
50
+ }
51
+
52
+ utils.options(req, opts);
53
+
54
+ return await this.consul._put(req, utils.body);
55
+ }
56
+ }
57
+
58
+ exports.Transaction = Transaction;