-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathConnection.js
More file actions
69 lines (58 loc) · 2.17 KB
/
Connection.js
File metadata and controls
69 lines (58 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const base64 = require('isomorphic-base64');
class Connection {
constructor(options = {}, meta) {
this.config(options, meta);
}
// The (optional) `meta` argument is useful if the user wants to override the
// ordinary creation of fetch requests or headers for a connection. Fetch
// request creation can be overriden by providing a `createRequest` method on
// `meta`, and header creation can be overriden with `createHeaders`.
config(options, meta) {
const config = Object.assign({}, this, options, { meta });
// If it ends with / slice it off
if (
config.endpoint &&
config.endpoint.lastIndexOf('/') === config.endpoint.length - 1
) {
config.endpoint = config.endpoint.slice(0, -1);
}
this.endpoint = config.endpoint;
this.username = config.username;
this.password = config.password;
this.token = config.token;
this.meta = config.meta;
}
headers() {
const headers = new Headers();
const authorization = this.token
? `bearer ${this.token}`
: `Basic ${base64.btoa(`${this.username}:${this.password || ''}`)}`;
headers.set('Authorization', authorization);
headers.set('Accept', '*/*');
if (this.meta && this.meta.createHeaders) {
return this.meta.createHeaders({ headers });
}
return headers;
}
uri(...resource) {
return `${this.endpoint}/${resource.join('/')}`;
}
request(...resource) {
if (!this.meta || !this.meta.createRequest) {
// We *could* just return a new Request from this method at all times (in
// this case, just `new Request(this.uri(...resource))`), but,
// unfortunately, `new Request` throws an error in Firefox if the URI
// string includes credentials, which would plausibly count as a breaking
// change to stardog.js. Something to consider for later, though.
return this.uri(...resource);
}
return this.meta.createRequest({
uri: this.uri(...resource),
// The Request constructor is passed here as a convenience, since it will
// vary based on whether this library is being used in Node-like or
// browser-like environments.
Request,
});
}
}
module.exports = Connection;