Skip to content

Commit 1c3546f

Browse files
author
Paul Korzhyk
committed
New release. Green tests. Almost no ts errors
1 parent 7f473cf commit 1c3546f

19 files changed

+46
-222
lines changed

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module.exports = {
2+
globals: {
3+
"ts-jest": {
4+
diagnostics: false,
5+
},
6+
},
27
testEnvironment: "node",
38
transform: {
49
".ts": "ts-jest"

lib/client.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"use strict";
22
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
return new (P || (P = Promise))(function (resolve, reject) {
54
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
65
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
87
step((generator = generator.apply(thisArg, _arguments || [])).next());
98
});
109
};

lib/clientStub.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ var __assign = (this && this.__assign) || function () {
1111
return __assign.apply(this, arguments);
1212
};
1313
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1514
return new (P || (P = Promise))(function (resolve, reject) {
1615
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1716
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
1918
step((generator = generator.apply(thisArg, _arguments || [])).next());
2019
});
2120
};
@@ -271,7 +270,7 @@ var DgraphClientStub = (function () {
271270
version: "1.0.x",
272271
}];
273272
}
274-
return [2, __assign(__assign({}, JSON.parse(text)), { health: "OK" })];
273+
return [2, __assign({}, JSON.parse(text), { health: "OK" })];
275274
}
276275
});
277276
});

lib/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ export * from "./clientStub";
33
export * from "./client";
44
export * from "./txn";
55
export * from "./errors";
6-
export * from "./zero";

lib/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ __export(require("./clientStub"));
77
__export(require("./client"));
88
__export(require("./txn"));
99
__export(require("./errors"));
10-
__export(require("./zero"));

lib/txn.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"use strict";
22
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
return new (P || (P = Promise))(function (resolve, reject) {
54
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
65
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
87
step((generator = generator.apply(thisArg, _arguments || [])).next());
98
});
109
};

lib/zero.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/zero.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dgraph-js-http",
3-
"version": "1.1.0-rc21",
3+
"version": "2003.0.0",
44
"description": "A javascript HTTP client for Dgraph",
55
"license": "Apache-2.0",
66
"repository": {

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DgraphClientStub } from "./clientStub";
22
import { ERR_NO_CLIENTS } from "./errors";
33
import { Txn } from "./txn";
4-
import { Operation, Payload, UiKeywords, TxnOptions, Response } from "./types";
4+
import { Operation, Payload, Response, TxnOptions, UiKeywords } from "./types";
55
import { stringifyMessage } from "./util";
66

77
/**
@@ -96,14 +96,14 @@ export class DgraphClient {
9696
* getHealth gets client or cluster health
9797
*/
9898
public async getHealth(all: boolean = true): Promise<Response> {
99-
return await this.anyClient().getHealth(all);
99+
return this.anyClient().getHealth(all);
100100
}
101101

102102
/**
103103
* getState gets cluster state
104104
*/
105105
public async getState(): Promise<Response> {
106-
return await this.anyClient().getState();
106+
return this.anyClient().getState();
107107
}
108108

109109
/**

src/clientStub.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ export class DgraphClientStub {
4141

4242
public async detectApiVersion(): Promise<string> {
4343
const health = await this.health();
44-
this.legacyApi = health.version.startsWith("1.0.");
45-
return health.version;
44+
// tslint:disable-next-line
45+
const version: string = health.version || health[0].version;
46+
this.legacyApi = version.startsWith("1.0.");
47+
return version;
4648
}
4749

4850
public alter(op: Operation): Promise<Payload> {
@@ -296,35 +298,20 @@ export class DgraphClientStub {
296298
return this.callAPI("ui/keywords", {});
297299
}
298300

299-
300301
/**
301302
* Gets instance or cluster health, based on the all param
302-
*
303-
* @param {boolean} [all=false] Whether to get the health of the full cluster
304-
* @returns {Promise<Response>} Health in JSON
305-
* @memberof DgraphClientStub
306303
*/
307304
public async getHealth(all: boolean = false): Promise<Response> {
308-
const url = "health" + (all? "?all" : "");
309-
310-
return await this.callAPI(url, {
311-
method: "GET",
312-
});
305+
return this.callAPI(`health${all ? "?all" : ""}`, {});
313306
}
314307

315308
/**
316309
* Gets the state of the cluster
317-
*
318-
* @returns {Promise<Response>} State in JSON
319-
* @memberof DgraphClientStub
320310
*/
321311
public async getState(): Promise<Response> {
322-
return await this.callAPI("state", {
323-
method: "GET",
324-
});
312+
return this.callAPI("state", {});
325313
}
326314

327-
328315
public setAutoRefresh(val: boolean) {
329316
if (!val) {
330317
this.cancelRefreshTimer();
@@ -335,7 +322,7 @@ export class DgraphClientStub {
335322

336323
private cancelRefreshTimer() {
337324
if (this.autoRefreshTimer !== undefined) {
338-
clearTimeout(<any>this.autoRefreshTimer);
325+
clearTimeout(this.autoRefreshTimer);
339326
this.autoRefreshTimer = undefined;
340327
}
341328
}
@@ -348,11 +335,12 @@ export class DgraphClientStub {
348335

349336
const timeToWait = Math.max(
350337
2000,
351-
jwt.decode(accessToken).exp * 1000 - Date.now()
338+
(<{exp: number}>jwt.decode(accessToken)).exp * 1000 - Date.now()
352339
- AUTO_REFRESH_PREFETCH_TIME);
353340

354-
this.autoRefreshTimer = <any>setTimeout(
355-
() => this.refreshToken && this.login(),
341+
// tslint:disable-next-line no-unsafe-any
342+
this.autoRefreshTimer = setTimeout(
343+
() => this.refreshToken !== undefined ? this.login() : 0,
356344
timeToWait,
357345
);
358346
}
@@ -364,20 +352,25 @@ export class DgraphClientStub {
364352
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
365353
}
366354

355+
// tslint:disable-next-line no-unsafe-any
367356
const response = await fetch(url, config);
368357

358+
// tslint:disable-next-line no-unsafe-any
369359
if (response.status >= 300 || response.status < 200) {
360+
// tslint:disable-next-line no-unsafe-any
370361
throw new Error(`Invalid status code = ${response.status}`);
371362
}
372363

364+
// tslint:disable-next-line no-unsafe-any
373365
const json = await response.json();
374-
const errors = (<{ errors: APIResultError[] }><any>json).errors; // tslint:disable-line no-any
366+
// tslint:disable-next-line no-unsafe-any
367+
const errors = (<{ errors: APIResultError[] }>json).errors;
375368

376369
if (errors !== undefined) {
377370
throw new APIError(url, errors);
378371
}
379372

380-
return json;
373+
return <T>json;
381374
}
382375

383376
private getURL(path: string): string {

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@ export * from "./client";
1111
export * from "./txn";
1212

1313
// Export error constants.
14-
export * from "./errors"
15-
16-
// Export DgraphZero class
17-
export * from "./zero"
14+
export * from "./errors";

src/txn.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DgraphClient } from "./client";
2-
import { ERR_ABORTED, ERR_FINISHED, ERR_BEST_EFFORT_REQUIRED_READ_ONLY } from "./errors";
2+
import { ERR_ABORTED, ERR_BEST_EFFORT_REQUIRED_READ_ONLY, ERR_FINISHED } from "./errors";
33
import { Assigned, Mutation, Request, Response, TxnContext, TxnOptions } from "./types";
44
import {
55
isAbortedError,
@@ -27,20 +27,20 @@ export class Txn {
2727
private finished: boolean = false;
2828
private mutated: boolean = false;
2929

30-
constructor(dc: DgraphClient, options?: TxnOptions) {
30+
constructor(dc: DgraphClient, options: TxnOptions = {}) {
3131
this.dc = dc;
3232

33-
if (options && options.bestEffort && !options.readOnly) {
34-
this.dc.debug('Client attempted to query using best-effort without setting the transaction to read-only');
33+
if (options.bestEffort && !options.readOnly) {
34+
this.dc.debug("Client attempted to query using best-effort without setting the transaction to read-only");
3535
throw ERR_BEST_EFFORT_REQUIRED_READ_ONLY;
3636
}
3737

3838
this.ctx = {
3939
start_ts: 0,
4040
keys: [],
4141
preds: [],
42-
readOnly: options && options.readOnly,
43-
bestEffort: options && options.bestEffort,
42+
readOnly: options.readOnly,
43+
bestEffort: options.bestEffort,
4444
};
4545
}
4646

@@ -73,7 +73,7 @@ export class Txn {
7373
timeout: this.dc.getQueryTimeout(),
7474
debug: options.debug,
7575
readOnly: this.ctx.readOnly,
76-
bestEffort: this.ctx.bestEffort
76+
bestEffort: this.ctx.bestEffort,
7777
};
7878
if (vars !== undefined) {
7979
const varsObj: { [k: string]: string } = {};

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ export interface Latency {
7777
}
7878

7979
export interface TxnOptions {
80-
readOnly?: boolean,
81-
bestEffort?: boolean
82-
}
80+
readOnly?: boolean;
81+
bestEffort?: boolean;
82+
}

0 commit comments

Comments
 (0)