Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.

Commit cf306a5

Browse files
author
Paul Korzhyk
committed
Allow custom HTTP headers
1 parent 3d2161f commit cf306a5

File tree

4 files changed

+29
-334
lines changed

4 files changed

+29
-334
lines changed

lib/clientStub.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,14 @@ var AUTO_REFRESH_PREFETCH_TIME = 5000;
5454
var DgraphClientStub = (function () {
5555
function DgraphClientStub(addr, stubConfig, options) {
5656
if (stubConfig === void 0) { stubConfig = {}; }
57+
if (options === void 0) { options = {}; }
5758
if (addr === undefined) {
5859
this.addr = "http://localhost:8080";
5960
}
6061
else {
6162
this.addr = addr;
6263
}
63-
if (options === undefined) {
64-
this.options = {};
65-
}
66-
else {
67-
this.options = options;
68-
}
64+
this.options = options;
6965
this.legacyApi = !!stubConfig.legacyApi;
7066
this.jsonParser =
7167
stubConfig.jsonParser !== undefined
@@ -108,7 +104,7 @@ var DgraphClientStub = (function () {
108104
return this.callAPI("alter", __assign(__assign({}, this.options), { method: "POST", body: body }));
109105
};
110106
DgraphClientStub.prototype.query = function (req) {
111-
var headers = {};
107+
var headers = Object.assign({}, this.options.headers !== undefined ? this.options.headers : {});
112108
if (req.vars !== undefined) {
113109
if (this.legacyApi) {
114110
headers["X-Dgraph-Vars"] = JSON.stringify(req.vars);
@@ -217,9 +213,9 @@ var DgraphClientStub = (function () {
217213
else {
218214
return Promise.reject("Mutation has no data");
219215
}
220-
var headers = {
216+
var headers = Object.assign({}, this.options.headers !== undefined ? this.options.headers : {}, {
221217
"Content-Type": "application/" + (usingJSON ? "json" : "rdf"),
222-
};
218+
});
223219
if (usingJSON && this.legacyApi) {
224220
headers["X-Dgraph-MutationType"] = "json";
225221
}

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": "20.7.0",
3+
"version": "20.7.1-rc1",
44
"description": "A javascript HTTP client for Dgraph",
55
"license": "Apache-2.0",
66
"repository": {

src/clientStub.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class DgraphClientStub {
4141
// tslint:disable-next-line no-any
4242
jsonParser?(text: string): any;
4343
} = {},
44-
options?: Options,
44+
options: Options = {},
4545
) {
4646
if (addr === undefined) {
4747
// tslint:disable-next-line no-http-string
@@ -50,11 +50,7 @@ export class DgraphClientStub {
5050
this.addr = addr;
5151
}
5252

53-
if (options === undefined) {
54-
this.options = {};
55-
} else {
56-
this.options = options;
57-
}
53+
this.options = options;
5854

5955
this.legacyApi = !!stubConfig.legacyApi;
6056
this.jsonParser =
@@ -95,7 +91,10 @@ export class DgraphClientStub {
9591
}
9692

9793
public query(req: Request): Promise<Response> {
98-
const headers: { [k: string]: string } = {};
94+
const headers = Object.assign(
95+
{},
96+
this.options.headers !== undefined ? this.options.headers : {},
97+
);
9998
if (req.vars !== undefined) {
10099
if (this.legacyApi) {
101100
headers["X-Dgraph-Vars"] = JSON.stringify(req.vars);
@@ -228,9 +227,14 @@ export class DgraphClientStub {
228227
return Promise.reject("Mutation has no data");
229228
}
230229

231-
const headers: { [k: string]: string } = {
232-
"Content-Type": `application/${usingJSON ? "json" : "rdf"}`,
233-
};
230+
const headers = Object.assign(
231+
{},
232+
this.options.headers !== undefined ? this.options.headers : {},
233+
{
234+
"Content-Type": `application/${usingJSON ? "json" : "rdf"}`,
235+
},
236+
);
237+
234238
if (usingJSON && this.legacyApi) {
235239
headers["X-Dgraph-MutationType"] = "json";
236240
}
@@ -314,6 +318,7 @@ export class DgraphClientStub {
314318
}
315319

316320
const res: LoginResponse = await this.callAPI("login", {
321+
...this.options,
317322
method: "POST",
318323
body: JSON.stringify(body),
319324
});

0 commit comments

Comments
 (0)