Skip to content

upgrade dependencies #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
487 changes: 487 additions & 0 deletions .eslintrc.js

Large diffs are not rendered by default.

7,789 changes: 5,931 additions & 1,858 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 24 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,40 @@
"clean": "rimraf coverage",
"build": "tsc -p tsconfig.release.json",
"build:watch": "tsc -w -p tsconfig.release.json",
"lint": "tslint -t stylish --project \"tsconfig.json\"",
"lint:fix": "tslint --fix -t stylish --project \"tsconfig.json\"",
"lint": "eslint -c .eslintrc.js --ext .ts .",
"lint:fix": "eslint . --ext .ts --fix",
"pretest": "npm run lint",
"test": "npm run test-only",
"test-only": "jest --coverage --runInBand",
"test:watch": "jest --watch",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/node": "^18.11.18",
"coveralls": "^3.1.0",
"jest": "^29.4.3",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.5",
"tslint": "^6.1.3",
"tslint-microsoft-contrib": "^6.2.0",
"typescript": "^4.9.5"
"@types/jest": "^29.5.12",
"@types/node": "^20.14.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/eslint-plugin-tslint": "^7.0.2",
"@typescript-eslint/parser": "^7.13.0",
"coveralls": "^3.1.1",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-jsdoc": "^48.2.9",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-react": "^7.34.2",
"eslint-plugin-security": "^3.0.0",
"eslint-plugin-unicorn": "^53.0.0",
"jest": "^29.7.0",
"rimraf": "^5.0.7",
"ts-jest": "^29.1.5",
"typescript": "^5.4.5"
},
"dependencies": {
"eslint-plugin-lodash": "^7.4.0",
"isomorphic-fetch": "^3.0.0",
"jsonwebtoken": "^9.0.0"
"jsonwebtoken": "^9.0.2"
},
"prettier": {
"arrowParens": "avoid",
Expand Down
29 changes: 12 additions & 17 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class DgraphClient {
* The client can be backed by multiple connections (to the same server, or
* multiple servers in a cluster).
*/
constructor(...clients: DgraphClientStub[]) {
public constructor(...clients: DgraphClientStub[]) {
if (clients.length === 0) {
throw ERR_NO_CLIENTS;
}
Expand Down Expand Up @@ -53,28 +53,26 @@ export class DgraphClient {
return c.alter(op);
}

public setAlphaAuthToken(authToken: string) {
public setAlphaAuthToken(authToken: string): void {
this.clients.forEach((c: DgraphClientStub) =>
c.setAlphaAuthToken(authToken),
);
}

/**
* @deprecated since v21.3 and will be removed in v21.07 release.
* Please use {@link setCloudApiKey} instead.
* Please use {@link setCloudApiKey} instead.
*/

public setSlashApiKey(apiKey: string) {
public setSlashApiKey(apiKey: string): void {
this.setCloudApiKey(apiKey);
}

public setCloudApiKey(apiKey: string) {
this.clients.forEach((c: DgraphClientStub) => c.setCloudApiKey(apiKey));
public setCloudApiKey(apiKey: string): void {
this.clients.forEach((c: DgraphClientStub) =>
c.setCloudApiKey(apiKey),
);
}

/**
* login obtains access tokens from Dgraph Server
*/
public async login(userid: string, password: string): Promise<boolean> {
this.debug(`Login request:\nuserid: ${userid}`);

Expand All @@ -85,15 +83,11 @@ export class DgraphClient {
/**
* loginIntoNamespace obtains access tokens from Dgraph Server for the particular userid & namespace
*/
public async loginIntoNamespace(
userid: string,
password: string,
namespace?: number,
): Promise<boolean> {
public async loginIntoNamespace(userid: string, password: string, namespace?: number): Promise<boolean> {
this.debug(`Login request:\nuserid: ${userid}`);

const c = this.anyClient();
return c.loginIntoNamespace(userid, password, namespace); // tslint:disable-line no-unsafe-any
return c.loginIntoNamespace(userid, password, namespace); // eslint:disable-line no-unsafe-any
}

/**
Expand Down Expand Up @@ -142,7 +136,8 @@ export class DgraphClient {
*/
public debug(msg: string): void {
if (this.debugMode) {
console.log(msg); // tslint:disable-line no-console
// eslint-disable-next-line no-console
console.log(msg);
}
}

Expand Down
88 changes: 40 additions & 48 deletions src/clientStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const DGRAPHCLOUD_API_KEY_HEADER = "X-Auth-Token";
export class DgraphClientStub {
private readonly addr: string;
private readonly options: Options;
// tslint:disable-next-line no-any
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
private readonly jsonParser: (text: string) => any;
private legacyApi: boolean;
private accessToken: string;
Expand All @@ -42,13 +42,12 @@ export class DgraphClientStub {
addr?: string,
stubConfig: {
legacyApi?: boolean;
// tslint:disable-next-line no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jsonParser?(text: string): any;
} = {},
options: Options = {},
) {
if (addr === undefined) {
// tslint:disable-next-line no-http-string
this.addr = "http://localhost:8080";
} else {
this.addr = addr;
Expand All @@ -60,13 +59,13 @@ export class DgraphClientStub {
this.jsonParser =
stubConfig.jsonParser !== undefined
? stubConfig.jsonParser
: // tslint:disable-next-line no-unsafe-any
JSON.parse.bind(JSON);
: // eslint-disable-next-line @typescript-eslint/tslint/config
JSON.parse.bind(JSON);
}

public async detectApiVersion(): Promise<string> {
const health = await this.getHealth();
// tslint:disable-next-line no-unsafe-any no-string-literal
// eslint-disable-next-line @typescript-eslint/tslint/config, @typescript-eslint/dot-notation
let version: string = health["version"] || health[0].version;
if (version === undefined) {
version = "1.0.x";
Expand Down Expand Up @@ -205,19 +204,19 @@ export class DgraphClientStub {
) {
body = `{
${
mu.setNquads === undefined
? ""
: `set {
mu.setNquads === undefined
? ""
: `set {
${mu.setNquads}
}`
}
}
${
mu.deleteNquads === undefined
? ""
: `delete {
mu.deleteNquads === undefined
? ""
: `delete {
${mu.deleteNquads}
}`
}
}
}`;
} else if (mu.mutation !== undefined) {
body = mu.mutation;
Expand Down Expand Up @@ -250,7 +249,7 @@ export class DgraphClientStub {
let nextDelim = "?";
if (mu.startTs > 0) {
url +=
(!this.legacyApi ? `?startTs=` : `/`) + mu.startTs.toString();
(!this.legacyApi ? "?startTs=" : "/") + mu.startTs.toString();
nextDelim = "&";
}

Expand Down Expand Up @@ -428,15 +427,15 @@ export class DgraphClientStub {
return this.callAPI("state", this.options);
}

public setAutoRefresh(val: boolean) {
public setAutoRefresh(val: boolean): void {
if (!val) {
this.cancelRefreshTimer();
}
this.autoRefresh = val;
this.maybeStartRefreshTimer(this.accessToken);
}

public setAlphaAuthToken(authToken: string) {
public setAlphaAuthToken(authToken: string): void{
if (this.options.headers === undefined) {
this.options.headers = {};
}
Expand All @@ -445,51 +444,46 @@ export class DgraphClientStub {

/**
* @deprecated since v21.3 and will be removed in v21.07 release.
* Please use {@link setCloudApiKey} instead.
* Please use {@link setCloudApiKey} instead.
*/

public setSlashApiKey(apiKey: string) {
public setSlashApiKey(apiKey: string): void {
this.setCloudApiKey(apiKey);
}

public setCloudApiKey(apiKey: string) {
public setCloudApiKey(apiKey: string): void {
if (this.options.headers === undefined) {
this.options.headers = {};
}
this.options.headers[DGRAPHCLOUD_API_KEY_HEADER] = apiKey;
}

private cancelRefreshTimer() {
private cancelRefreshTimer(): void {
if (this.autoRefreshTimer !== undefined) {
// tslint:disable-next-line
clearTimeout(<any>this.autoRefreshTimer);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
clearTimeout((this.autoRefreshTimer as any));
this.autoRefreshTimer = undefined;
}
}

private maybeStartRefreshTimer(accessToken?: string) {
private maybeStartRefreshTimer(accessToken?: string): void {
if (accessToken === undefined || !this.autoRefresh) {
return;
}
this.cancelRefreshTimer();

const timeToWait = Math.max(
2000,
// tslint:disable-next-line no-unsafe-any
(<{ exp: number }>jwt.decode(accessToken)).exp * 1000 -
// eslint-disable-next-line @typescript-eslint/tslint/config
(jwt.decode(accessToken) as { exp: number }).exp * 1000 -
Date.now() -
AUTO_REFRESH_PREFETCH_TIME,
);

// tslint:disable-next-line no-unsafe-any no-any
this.autoRefreshTimer = <number>(
(<unknown>(
setTimeout(
() => (this.refreshToken !== undefined ? this.login() : 0),
timeToWait,
)
))
);
// eslint-disable-next-line @typescript-eslint/tslint/config
this.autoRefreshTimer = (setTimeout(
() => (this.refreshToken !== undefined ? this.login() : 0),
timeToWait,
) as unknown) as number;
}

private async callAPI<T>(path: string, config: Config): Promise<T> {
Expand All @@ -499,40 +493,38 @@ export class DgraphClientStub {
config.headers[ACL_TOKEN_HEADER] = this.accessToken;
}

// tslint:disable-next-line no-unsafe-any
// eslint-disable-next-line @typescript-eslint/tslint/config
const response = await fetch(url, config);

// tslint:disable-next-line no-unsafe-any
// eslint-disable-next-line @typescript-eslint/tslint/config
if (response.status >= 300 || response.status < 200) {
// tslint:disable-next-line no-unsafe-any
// eslint-disable-next-line @typescript-eslint/tslint/config
throw new HTTPError(response);
}

let json;
// tslint:disable-next-line no-unsafe-any
// eslint-disable-next-line @typescript-eslint/tslint/config
const responseText: string = await response.text();

try {
// tslint:disable-next-line no-unsafe-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json = this.jsonParser(responseText);
} catch (e) {
if (config.acceptRawText) {
return <T>(<unknown>responseText);
return (responseText as unknown) as T;
}
const err: ErrorNonJson = <ErrorNonJson>(
new Error("Response is not JSON")
);
const err: ErrorNonJson = new Error("Response is not JSON") as ErrorNonJson;
err.responseText = responseText;
throw err;
}
// tslint:disable-next-line no-unsafe-any
const errors = (<{ errors: APIResultError[] }>json).errors;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errors = (json as { errors: APIResultError[] }).errors;

if (errors !== undefined) {
throw new APIError(url, errors);
}

return <T>json;
return json as T;
}

private getURL(path: string): string {
Expand Down
23 changes: 14 additions & 9 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,32 @@ export const ERR_BEST_EFFORT_REQUIRED_READ_ONLY = new Error(
export class CustomError extends Error {
public readonly name: string;

constructor(message?: string) {
public constructor(message?: string) {
super(message);

// tslint:disable no-any no-unsafe-any
this.name = new.target.name;

// fix the extended error prototype chain because typescript __extends implementation can't
const setPrototypeOf: Function = (<any>Object).setPrototypeOf;
/* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/tslint/config,
@typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
const setPrototypeOf: (o: any, proto: object | null) => any = (Object as any).setPrototypeOf;
setPrototypeOf !== undefined
? setPrototypeOf(this, new.target.prototype)
: ((<any>this).__proto__ = new.target.prototype);
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-proto
: ((this as any).__proto__ = new.target.prototype);

// try to remove contructor from stack trace
const captureStackTrace: Function = (<any>Error).captureStackTrace;
// try to remove constructor from stack trace
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const captureStackTrace: (targetObject: any, constructorOpt?: any) => void = (Error as any).captureStackTrace;
if (captureStackTrace !== undefined) {
captureStackTrace(this, this.constructor);
}
// tslint:enable no-any no-unsafe-any
/* eslint-enable @typescript-eslint/ban-types, @typescript-eslint/tslint/config,
@typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
}
}


export interface APIResultError {
code: string;
message: string;
Expand All @@ -50,7 +55,7 @@ export class APIError extends CustomError {
public readonly url: string;
public readonly errors: APIResultError[];

constructor(url: string, errors: APIResultError[]) {
public constructor(url: string, errors: APIResultError[]) {
super(errors.length > 0 ? errors[0].message : "API returned errors");
this.url = url;
this.errors = errors;
Expand All @@ -63,7 +68,7 @@ export class APIError extends CustomError {
export class HTTPError extends CustomError {
public readonly errorResponse: Response;

constructor(response: Response) {
public constructor(response: Response) {
super(`Invalid status code = ${response.status}`);
this.errorResponse = response;
}
Expand Down
Loading
Loading