Skip to content

fix(deprecation): add setCloudApiKey method #47

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 1 commit into from
May 19, 2021
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ const clientStub = new dgraph.DgraphClientStub(
const dgraphClient = new dgraph.DgraphClient(clientStub);

//here we pass the API key
dgraphClient.setSlashApiKey("<api-key>");
dgraphClient.setCloudApiKey("<api-key>");
```

**Note:** the `setSlashApiKey` method is deprecated and will be removed in the next release.
**Note:** the `setSlashApiKey` method is deprecated and will be removed in the next release. Instead use `setCloudApiKey` method.

### Login into Dgraph

Expand Down Expand Up @@ -166,10 +166,10 @@ Some Dgraph configurations require extra access tokens.
dgraphClient.setAlphaAuthToken("My secret token value");
```

2. [Slash GraphQL](https://dgraph.io/slash-graphql) requires API key for HTTP access:
2. [Dgraph Cloud](https://cloud.dgraph.io) requires API key for HTTP access:

```js
dgraphClient.setSlashApiKey("Copy the Api Key from Slash GraphQL admin page");
dgraphClient.setCloudApiKey("Copy the Api Key from Dgraph Cloud admin page");
```

### Create https connection
Expand Down
1 change: 1 addition & 0 deletions lib/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export declare class DgraphClient {
alter(op: Operation): Promise<Payload>;
setAlphaAuthToken(authToken: string): void;
setSlashApiKey(apiKey: string): void;
setCloudApiKey(apiKey: string): void;
login(userid: string, password: string): Promise<boolean>;
loginIntoNamespace(userid: string, password: string, namespace?: number): Promise<boolean>;
logout(): void;
Expand Down
5 changes: 4 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ var DgraphClient = (function () {
});
};
DgraphClient.prototype.setSlashApiKey = function (apiKey) {
this.clients.forEach(function (c) { return c.setSlashApiKey(apiKey); });
this.setCloudApiKey(apiKey);
};
DgraphClient.prototype.setCloudApiKey = function (apiKey) {
this.clients.forEach(function (c) { return c.setCloudApiKey(apiKey); });
};
DgraphClient.prototype.login = function (userid, password) {
return __awaiter(this, void 0, void 0, function () {
Expand Down
1 change: 1 addition & 0 deletions lib/clientStub.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export declare class DgraphClientStub {
setAutoRefresh(val: boolean): void;
setAlphaAuthToken(authToken: string): void;
setSlashApiKey(apiKey: string): void;
setCloudApiKey(apiKey: string): void;
private cancelRefreshTimer;
private maybeStartRefreshTimer;
private callAPI;
Expand Down
7 changes: 5 additions & 2 deletions lib/clientStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var errors_1 = require("./errors");
var AUTO_REFRESH_PREFETCH_TIME = 5000;
var ACL_TOKEN_HEADER = "X-Dgraph-AccessToken";
var ALPHA_AUTH_TOKEN_HEADER = "X-Dgraph-AuthToken";
var SLASH_API_KEY_HEADER = "X-Auth-Token";
var DGRAPHCLOUD_API_KEY_HEADER = "X-Auth-Token";
var DgraphClientStub = (function () {
function DgraphClientStub(addr, stubConfig, options) {
if (stubConfig === void 0) { stubConfig = {}; }
Expand Down Expand Up @@ -399,10 +399,13 @@ var DgraphClientStub = (function () {
this.options.headers[ALPHA_AUTH_TOKEN_HEADER] = authToken;
};
DgraphClientStub.prototype.setSlashApiKey = function (apiKey) {
this.setCloudApiKey(apiKey);
};
DgraphClientStub.prototype.setCloudApiKey = function (apiKey) {
if (this.options.headers === undefined) {
this.options.headers = {};
}
this.options.headers[SLASH_API_KEY_HEADER] = apiKey;
this.options.headers[DGRAPHCLOUD_API_KEY_HEADER] = apiKey;
};
DgraphClientStub.prototype.cancelRefreshTimer = function () {
if (this.autoRefreshTimer !== undefined) {
Expand Down
10 changes: 7 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ export class DgraphClient {
}

/**
* @deprecated since v21.3 and will be removed in v21.07 release. For more details, see:
* https://discuss.dgraph.io/t/regarding-slash-cloud-dgraph-endpoints-in-the-clients/13492
* @deprecated since v21.3 and will be removed in v21.07 release.
* Please use {@link setCloudApiKey} instead.
*/

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

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

/**
Expand Down
88 changes: 46 additions & 42 deletions src/clientStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const AUTO_REFRESH_PREFETCH_TIME = 5000;

const ACL_TOKEN_HEADER = "X-Dgraph-AccessToken";
const ALPHA_AUTH_TOKEN_HEADER = "X-Dgraph-AuthToken";
const SLASH_API_KEY_HEADER = "X-Auth-Token";
const DGRAPHCLOUD_API_KEY_HEADER = "X-Auth-Token";

/**
* Stub is a stub/client connecting to a single dgraph server instance.
Expand Down Expand Up @@ -302,56 +302,56 @@ export class DgraphClientStub {
}

public abort(ctx: TxnContext): Promise<TxnContext> {
let url = !this.legacyApi
let url = !this.legacyApi
? `commit?startTs=${ctx.start_ts}&abort=true`
: `abort/${ctx.start_ts}`;

if (ctx?.hash?.length > 0) {
if (!this.legacyApi) {
if (ctx?.hash?.length > 0) {
if (!this.legacyApi) {
url += `&hash=${ctx.hash}`;
}
}
}
}

return this.callAPI(url, { ...this.options, method: "POST" });
return this.callAPI(url, { ...this.options, method: "POST" });
}

public async login(
userid?: string,
password?: string,
refreshToken?: string,
): Promise<boolean> {
if (this.legacyApi) {
throw new Error("Pre v1.1 clients do not support Login methods");
}

const body: { [k: string]: string } = {};
if (
userid === undefined &&
refreshToken === undefined &&
this.refreshToken === undefined
) {
throw new Error(
"Cannot find login details: neither userid/password nor refresh token are specified",
);
}
if (userid === undefined) {
body.refresh_token =
refreshToken !== undefined ? refreshToken : this.refreshToken;
} else {
body.userid = userid;
body.password = password;
}

const res: LoginResponse = await this.callAPI("login", {
...this.options,
method: "POST",
body: JSON.stringify(body),
});
this.accessToken = res.data.accessJWT;
this.refreshToken = res.data.refreshJWT;

this.maybeStartRefreshTimer(this.accessToken);
return true;
if (this.legacyApi) {
throw new Error("Pre v1.1 clients do not support Login methods");
}

const body: { [k: string]: string } = {};
if (
userid === undefined &&
refreshToken === undefined &&
this.refreshToken === undefined
) {
throw new Error(
"Cannot find login details: neither userid/password nor refresh token are specified",
);
}
if (userid === undefined) {
body.refresh_token =
refreshToken !== undefined ? refreshToken : this.refreshToken;
} else {
body.userid = userid;
body.password = password;
}

const res: LoginResponse = await this.callAPI("login", {
...this.options,
method: "POST",
body: JSON.stringify(body),
});
this.accessToken = res.data.accessJWT;
this.refreshToken = res.data.refreshJWT;

this.maybeStartRefreshTimer(this.accessToken);
return true;
}

public async loginIntoNamespace(
Expand Down Expand Up @@ -444,15 +444,19 @@ export class DgraphClientStub {
}

/**
* @deprecated since v21.3 and will be removed in v21.07 release. For more details, see:
* https://discuss.dgraph.io/t/regarding-slash-cloud-dgraph-endpoints-in-the-clients/13492
* @deprecated since v21.3 and will be removed in v21.07 release.
* Please use {@link setCloudApiKey} instead.
*/

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

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

private cancelRefreshTimer() {
Expand Down