-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEndpoint.js
72 lines (70 loc) · 1.9 KB
/
Endpoint.js
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
70
71
72
/*
* Copyright 2022-2025 Digital Bazaar, Inc.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
import {makeHttpsRequest, zcapRequest} from './requests.js';
/**
* Wraps settings from an implementation's json manifest
* in an endpoint for testing with tags.
*
* @param {object} options - Options to use.
* @param {object} [options.oauth2] - Oauth2 options.
* @param {object} options.settings - Settings for the endpoint.
*/
export class Endpoint {
constructor({oauth2, settings}) {
this.settings = {...settings};
if(oauth2) {
// add global oauth2 settings to endpoint settings
this.settings.oauth2 = {...oauth2};
// scopes are endpoint specific so we need to add them
// to the global oauth credentials
if(Array.isArray(this.settings.scopes)) {
this.settings.oauth2.scopes = [...this.settings.scopes];
}
}
}
// ensure tags are unique
get tags() {
return new Set(this.settings.tags);
}
post({headers = {}, url, ...args}) {
const {headers: _headers = {}, endpoint, oauth2, zcap} = this.settings;
if(zcap) {
return zcapRequest({
endpoint: url || endpoint,
zcap,
headers: {..._headers, ...headers},
...args
});
}
return makeHttpsRequest({
url: url || endpoint,
method: 'POST',
oauth2,
headers: {..._headers, ...headers},
...args
});
}
put({headers = {}, url, ...args}) {
const {headers: _headers = {}, endpoint, oauth2} = this.settings;
return makeHttpsRequest({
url: url || endpoint,
method: 'PUT',
oauth2,
headers: {..._headers, ...headers},
...args
});
}
get({headers, url, ...args} = {}) {
const {headers: _headers = {}, endpoint, oauth2} = this.settings;
return makeHttpsRequest({
method: 'GET',
url: url || endpoint,
oauth2,
headers: {..._headers, ...headers},
...args
});
}
}