@looker/sdk-node 23.16.0 → 23.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +6 -24
- package/lib/esm/index.js +5 -60
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/nodeSdk.js +7 -20
- package/lib/esm/nodeSdk.js.map +1 -1
- package/lib/esm/nodeServices.js +6 -13
- package/lib/esm/nodeServices.js.map +1 -1
- package/lib/esm/nodeSession.js +8 -15
- package/lib/esm/nodeSession.js.map +1 -1
- package/lib/esm/nodeSettings.js +16 -31
- package/lib/esm/nodeSettings.js.map +1 -1
- package/lib/esm/nodeTransport.js +37 -46
- package/lib/esm/nodeTransport.js.map +1 -1
- package/lib/esm/testUtils/index.js +1 -16
- package/lib/esm/testUtils/index.js.map +1 -1
- package/lib/esm/testUtils/testUtils.js +11 -23
- package/lib/esm/testUtils/testUtils.js.map +1 -1
- package/lib/nodeSdk.d.ts +1 -2
- package/lib/nodeSdk.js +0 -6
- package/lib/nodeSdk.js.map +1 -1
- package/lib/nodeServices.js.map +1 -1
- package/lib/nodeSession.js.map +1 -1
- package/lib/nodeSettings.js.map +1 -1
- package/lib/nodeTransport.js.map +1 -1
- package/lib/testUtils/testUtils.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [23.18.0](https://github.com/looker-open-source/sdk-codegen/compare/sdk-node-v23.16.0...sdk-node-v23.18.0) (2023-11-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Miscellaneous Chores
|
|
12
|
+
|
|
13
|
+
* **sdk-node:** Synchronize undefined versions
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Dependencies
|
|
17
|
+
|
|
18
|
+
* The following workspace dependencies were updated
|
|
19
|
+
* dependencies
|
|
20
|
+
* @looker/sdk bumped from ^23.16.0 to ^23.18.0
|
|
21
|
+
|
|
8
22
|
## [23.16.0](https://github.com/looker-open-source/sdk-codegen/compare/sdk-node-v23.14.1...sdk-node-v23.16.0) (2023-09-21)
|
|
9
23
|
|
|
10
24
|
|
package/README.md
CHANGED
|
@@ -76,8 +76,8 @@ Verify authentication works and that API calls will succeed with code similar to
|
|
|
76
76
|
```typescript
|
|
77
77
|
import { LookerNodeSDK } from '@looker/sdk-node'
|
|
78
78
|
(async () => {
|
|
79
|
-
// create a Node SDK object for API
|
|
80
|
-
const sdk = LookerNodeSDK.
|
|
79
|
+
// create a Node SDK object for API 4.0
|
|
80
|
+
const sdk = LookerNodeSDK.init40()
|
|
81
81
|
// retrieve your user account to verify correct credentials
|
|
82
82
|
const me = await sdk.ok(sdk.me(
|
|
83
83
|
"id, first_name, last_name, display_name, email, personal_space_id, home_space_id, group_ids, role_ids"))
|
|
@@ -101,37 +101,19 @@ import { LookerNodeSDK } from '@looker/sdk-node'
|
|
|
101
101
|
})()
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
**NOTE**: By default, `LookerNodeSDK.
|
|
104
|
+
**NOTE**: By default, `LookerNodeSDK.init40()` will check for environment variables. Environment variables can be ignored by passing an empty string to the NodeSettings constructor.
|
|
105
105
|
|
|
106
106
|
```typescript
|
|
107
107
|
// Ignore any SDK environment variables for the node runtime
|
|
108
108
|
const settings = new NodeSettingsIniFile('')
|
|
109
109
|
const sdk = LookerNodeSDK.init40(settings)
|
|
110
|
-
const sdk31 = LookerNodeSDK.init31(settings)
|
|
111
110
|
```
|
|
112
111
|
|
|
113
112
|
### Developing with multiple API versions
|
|
114
113
|
|
|
115
|
-
Starting with Looker release
|
|
114
|
+
Starting with Looker release 23.18, API 3.1 and 3.0 have been removed. Please use the stable and current version API 4.0 as shown below.
|
|
116
115
|
|
|
117
|
-
`LookerNodeSDK.
|
|
118
|
-
|
|
119
|
-
`LookerNodeSDK.init40()` `LookerBrowserSDK.init40()` and `Looker40SDK()` all initialize the API 4.1 implementation of the SDK.
|
|
120
|
-
|
|
121
|
-
Code similar to the following can be used to develop with both the 3.1 and 4.0 SDKs in the same source file:
|
|
122
|
-
|
|
123
|
-
```typescript
|
|
124
|
-
import { Looker40SDK, Looker31SDK } from '@looker/sdk'
|
|
125
|
-
import { NodeSession, NodeSettingsIniFile } from '@looker/sdk-node'
|
|
126
|
-
|
|
127
|
-
const settings = new NodeSettingsIniFile()
|
|
128
|
-
const session = new NodeSession(settings)
|
|
129
|
-
const sdk = new Looker40SDK(session)
|
|
130
|
-
const sdk31 = new Looker31SDK(session)
|
|
131
|
-
|
|
132
|
-
const me40 = await sdk.ok(sdk.me())
|
|
133
|
-
const me31 = await sdk.ok(sdk31.me()) // or sdk31.ok(sdk31.me())
|
|
134
|
-
```
|
|
116
|
+
`LookerNodeSDK.init40()` `LookerBrowserSDK.init40()` and `Looker40SDK()` all initialize the API 4.0 implementation of the SDK.
|
|
135
117
|
|
|
136
118
|
## Using NodeSession for automatic authentication
|
|
137
119
|
|
|
@@ -213,7 +195,7 @@ describe('sudo', () => {
|
|
|
213
195
|
Once the desired environment variables are set, the following code is all that's required to initialize the Looker SDK and retrieve the API credential's `User` information.
|
|
214
196
|
|
|
215
197
|
```typescript
|
|
216
|
-
const sdk = LookerNodeSDK.
|
|
198
|
+
const sdk = LookerNodeSDK.init40(new NodeSettings())
|
|
217
199
|
const me = await sdk.ok(sdk.me())
|
|
218
200
|
```
|
|
219
201
|
|
package/lib/esm/index.js
CHANGED
|
@@ -1,61 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var _nodeServices = require("./nodeServices");
|
|
7
|
-
Object.keys(_nodeServices).forEach(function (key) {
|
|
8
|
-
if (key === "default" || key === "__esModule") return;
|
|
9
|
-
if (key in exports && exports[key] === _nodeServices[key]) return;
|
|
10
|
-
Object.defineProperty(exports, key, {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: function get() {
|
|
13
|
-
return _nodeServices[key];
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
var _nodeSession = require("./nodeSession");
|
|
18
|
-
Object.keys(_nodeSession).forEach(function (key) {
|
|
19
|
-
if (key === "default" || key === "__esModule") return;
|
|
20
|
-
if (key in exports && exports[key] === _nodeSession[key]) return;
|
|
21
|
-
Object.defineProperty(exports, key, {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function get() {
|
|
24
|
-
return _nodeSession[key];
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
var _nodeSettings = require("./nodeSettings");
|
|
29
|
-
Object.keys(_nodeSettings).forEach(function (key) {
|
|
30
|
-
if (key === "default" || key === "__esModule") return;
|
|
31
|
-
if (key in exports && exports[key] === _nodeSettings[key]) return;
|
|
32
|
-
Object.defineProperty(exports, key, {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function get() {
|
|
35
|
-
return _nodeSettings[key];
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
var _nodeTransport = require("./nodeTransport");
|
|
40
|
-
Object.keys(_nodeTransport).forEach(function (key) {
|
|
41
|
-
if (key === "default" || key === "__esModule") return;
|
|
42
|
-
if (key in exports && exports[key] === _nodeTransport[key]) return;
|
|
43
|
-
Object.defineProperty(exports, key, {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
get: function get() {
|
|
46
|
-
return _nodeTransport[key];
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
var _nodeSdk = require("./nodeSdk");
|
|
51
|
-
Object.keys(_nodeSdk).forEach(function (key) {
|
|
52
|
-
if (key === "default" || key === "__esModule") return;
|
|
53
|
-
if (key in exports && exports[key] === _nodeSdk[key]) return;
|
|
54
|
-
Object.defineProperty(exports, key, {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
get: function get() {
|
|
57
|
-
return _nodeSdk[key];
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
});
|
|
1
|
+
export * from './nodeServices';
|
|
2
|
+
export * from './nodeSession';
|
|
3
|
+
export * from './nodeSettings';
|
|
4
|
+
export * from './nodeTransport';
|
|
5
|
+
export * from './nodeSdk';
|
|
61
6
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\n// Version 21.0.4\nexport * from './nodeServices'\nexport * from './nodeSession'\nexport * from './nodeSettings'\nexport * from './nodeTransport'\nexport * from './nodeSdk'\n"],"mappings":"AA2BA,cAAc,gBAAgB;AAC9B,cAAc,eAAe;AAC7B,cAAc,gBAAgB;AAC9B,cAAc,iBAAiB;AAC/B,cAAc,WAAW"}
|
package/lib/esm/nodeSdk.js
CHANGED
|
@@ -1,25 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.LookerNodeSDK = void 0;
|
|
7
|
-
var _sdk = require("@looker/sdk");
|
|
8
|
-
var _ = require(".");
|
|
9
|
-
class LookerNodeSDK {
|
|
10
|
-
static init31(settings, transport, session) {
|
|
11
|
-
settings = settings || new _.NodeSettingsIniFile(_sdk.environmentPrefix);
|
|
12
|
-
transport = transport || new _.NodeTransport(settings);
|
|
13
|
-
session = session || new _.NodeSession(settings, transport);
|
|
14
|
-
return new _sdk.Looker31SDK(session);
|
|
15
|
-
}
|
|
1
|
+
import { Looker40SDK, environmentPrefix } from '@looker/sdk';
|
|
2
|
+
import { NodeSettingsIniFile, NodeSession, NodeTransport } from '.';
|
|
3
|
+
export class LookerNodeSDK {
|
|
16
4
|
static init40(settings, transport, session) {
|
|
17
5
|
var _session;
|
|
18
|
-
settings = settings || new
|
|
19
|
-
transport = transport || new
|
|
20
|
-
session = (_session = session) !== null && _session !== void 0 ? _session : new
|
|
21
|
-
return new
|
|
6
|
+
settings = settings || new NodeSettingsIniFile(environmentPrefix);
|
|
7
|
+
transport = transport || new NodeTransport(settings);
|
|
8
|
+
session = (_session = session) !== null && _session !== void 0 ? _session : new NodeSession(settings, transport);
|
|
9
|
+
return new Looker40SDK(session);
|
|
22
10
|
}
|
|
23
11
|
}
|
|
24
|
-
exports.LookerNodeSDK = LookerNodeSDK;
|
|
25
12
|
//# sourceMappingURL=nodeSdk.js.map
|
package/lib/esm/nodeSdk.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeSdk.js","names":["
|
|
1
|
+
{"version":3,"file":"nodeSdk.js","names":["Looker40SDK","environmentPrefix","NodeSettingsIniFile","NodeSession","NodeTransport","LookerNodeSDK","init40","settings","transport","session","_session"],"sources":["../../src/nodeSdk.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\nimport type { ITransport, IApiSettings, IAuthSession } from '@looker/sdk-rtl'\nimport { Looker40SDK, environmentPrefix } from '@looker/sdk'\nimport { NodeSettingsIniFile, NodeSession, NodeTransport } from '.'\n\n/**\n * @class LookerNodeSDK\n *\n * Simple factory for the Node version of the Looker SDK. Provides default connectivity for SDK methods\n *\n */\nexport class LookerNodeSDK {\n /**\n * Creates a [[Looker40SDK]] object.\n *\n * @param settings Defaults to the settings from LookerIni\n *\n * @param transport Defaults to a `NodeTransport` object\n *\n * @param session Defaults to `NodeSession` which logs in the user\n */\n static init40(\n settings?: IApiSettings,\n transport?: ITransport,\n session?: IAuthSession\n ) {\n settings = settings || new NodeSettingsIniFile(environmentPrefix)\n transport = transport || new NodeTransport(settings)\n session = session ?? new NodeSession(settings, transport)\n return new Looker40SDK(session)\n }\n}\n"],"mappings":"AA2BA,SAASA,WAAW,EAAEC,iBAAiB,QAAQ,aAAa;AAC5D,SAASC,mBAAmB,EAAEC,WAAW,EAAEC,aAAa,QAAQ,GAAG;AAQnE,OAAO,MAAMC,aAAa,CAAC;EAUzB,OAAOC,MAAMA,CACXC,QAAuB,EACvBC,SAAsB,EACtBC,OAAsB,EACtB;IAAA,IAAAC,QAAA;IACAH,QAAQ,GAAGA,QAAQ,IAAI,IAAIL,mBAAmB,CAACD,iBAAiB,CAAC;IACjEO,SAAS,GAAGA,SAAS,IAAI,IAAIJ,aAAa,CAACG,QAAQ,CAAC;IACpDE,OAAO,IAAAC,QAAA,GAAGD,OAAO,cAAAC,QAAA,cAAAA,QAAA,GAAI,IAAIP,WAAW,CAACI,QAAQ,EAAEC,SAAS,CAAC;IACzD,OAAO,IAAIR,WAAW,CAACS,OAAO,CAAC;EACjC;AACF"}
|
package/lib/esm/nodeServices.js
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.NodeServices = void 0;
|
|
7
|
-
var _sdkRtl = require("@looker/sdk-rtl");
|
|
8
|
-
var _nodeTransport = require("./nodeTransport");
|
|
9
1
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
10
2
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
11
3
|
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
12
|
-
|
|
4
|
+
import { sdkError } from '@looker/sdk-rtl';
|
|
5
|
+
import { NodeCryptoHash, NodeTransport } from './nodeTransport';
|
|
6
|
+
export class NodeServices {
|
|
13
7
|
constructor(services) {
|
|
14
8
|
_defineProperty(this, "crypto", void 0);
|
|
15
9
|
_defineProperty(this, "settings", void 0);
|
|
16
10
|
_defineProperty(this, "transport", void 0);
|
|
17
11
|
if (!services.settings) {
|
|
18
|
-
throw
|
|
12
|
+
throw sdkError({
|
|
19
13
|
message: 'Missing required IApiSettings'
|
|
20
14
|
});
|
|
21
15
|
}
|
|
22
16
|
this.settings = services.settings;
|
|
23
|
-
this.transport = services.transport || new
|
|
24
|
-
this.crypto = services.crypto || new
|
|
17
|
+
this.transport = services.transport || new NodeTransport(this.settings);
|
|
18
|
+
this.crypto = services.crypto || new NodeCryptoHash();
|
|
25
19
|
}
|
|
26
20
|
}
|
|
27
|
-
exports.NodeServices = NodeServices;
|
|
28
21
|
//# sourceMappingURL=nodeServices.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeServices.js","names":["
|
|
1
|
+
{"version":3,"file":"nodeServices.js","names":["sdkError","NodeCryptoHash","NodeTransport","NodeServices","constructor","services","_defineProperty","settings","message","transport","crypto"],"sources":["../../src/nodeServices.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\nimport type {\n ICryptoHash,\n IApiSettings,\n ITransport,\n IPlatformServices,\n} from '@looker/sdk-rtl'\nimport { sdkError } from '@looker/sdk-rtl'\nimport { NodeCryptoHash, NodeTransport } from './nodeTransport'\n\nexport class NodeServices implements IPlatformServices {\n /** Cryptography service interface */\n crypto: ICryptoHash\n /** SDK configuration interface */\n settings: IApiSettings\n transport: ITransport\n\n constructor(services: Partial<IPlatformServices>) {\n if (!services.settings) {\n throw sdkError({ message: 'Missing required IApiSettings' })\n }\n this.settings = services.settings\n this.transport = services.transport || new NodeTransport(this.settings)\n this.crypto = services.crypto || new NodeCryptoHash()\n }\n}\n"],"mappings":";;;AAgCA,SAASA,QAAQ,QAAQ,iBAAiB;AAC1C,SAASC,cAAc,EAAEC,aAAa,QAAQ,iBAAiB;AAE/D,OAAO,MAAMC,YAAY,CAA8B;EAOrDC,WAAWA,CAACC,QAAoC,EAAE;IAAAC,eAAA;IAAAA,eAAA;IAAAA,eAAA;IAChD,IAAI,CAACD,QAAQ,CAACE,QAAQ,EAAE;MACtB,MAAMP,QAAQ,CAAC;QAAEQ,OAAO,EAAE;MAAgC,CAAC,CAAC;IAC9D;IACA,IAAI,CAACD,QAAQ,GAAGF,QAAQ,CAACE,QAAQ;IACjC,IAAI,CAACE,SAAS,GAAGJ,QAAQ,CAACI,SAAS,IAAI,IAAIP,aAAa,CAAC,IAAI,CAACK,QAAQ,CAAC;IACvE,IAAI,CAACG,MAAM,GAAGL,QAAQ,CAACK,MAAM,IAAI,IAAIT,cAAc,CAAC,CAAC;EACvD;AACF"}
|
package/lib/esm/nodeSession.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.NodeSession = void 0;
|
|
7
|
-
var _sdkRtl = require("@looker/sdk-rtl");
|
|
8
|
-
var _nodeTransport = require("./nodeTransport");
|
|
9
1
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
2
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
3
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
12
4
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
13
5
|
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
6
|
+
import { sdkError, encodeParams, AuthToken, AuthSession } from '@looker/sdk-rtl';
|
|
7
|
+
import { NodeTransport } from './nodeTransport';
|
|
14
8
|
var strPost = 'POST';
|
|
15
9
|
var strDelete = 'DELETE';
|
|
16
|
-
class NodeSession extends
|
|
10
|
+
export class NodeSession extends AuthSession {
|
|
17
11
|
constructor(settings, transport) {
|
|
18
|
-
super(settings, transport || new
|
|
12
|
+
super(settings, transport || new NodeTransport(settings));
|
|
19
13
|
this.settings = settings;
|
|
20
14
|
_defineProperty(this, "apiPath", '/api/4.0');
|
|
21
|
-
_defineProperty(this, "_authToken", new
|
|
22
|
-
_defineProperty(this, "_sudoToken", new
|
|
15
|
+
_defineProperty(this, "_authToken", new AuthToken());
|
|
16
|
+
_defineProperty(this, "_sudoToken", new AuthToken());
|
|
23
17
|
}
|
|
24
18
|
get activeToken() {
|
|
25
19
|
if (this._sudoToken.access_token) {
|
|
@@ -106,11 +100,11 @@ class NodeSession extends _sdkRtl.AuthSession {
|
|
|
106
100
|
var clientId = section.client_id;
|
|
107
101
|
var clientSecret = section.client_secret;
|
|
108
102
|
if (!clientId || !clientSecret) {
|
|
109
|
-
throw
|
|
103
|
+
throw sdkError({
|
|
110
104
|
message: 'API credentials client_id and/or client_secret are not set'
|
|
111
105
|
});
|
|
112
106
|
}
|
|
113
|
-
var body =
|
|
107
|
+
var body = encodeParams({
|
|
114
108
|
client_id: clientId,
|
|
115
109
|
client_secret: clientSecret
|
|
116
110
|
});
|
|
@@ -155,5 +149,4 @@ class NodeSession extends _sdkRtl.AuthSession {
|
|
|
155
149
|
})();
|
|
156
150
|
}
|
|
157
151
|
}
|
|
158
|
-
exports.NodeSession = NodeSession;
|
|
159
152
|
//# sourceMappingURL=nodeSession.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeSession.js","names":["_sdkRtl","require","_nodeTransport","asyncGeneratorStep","gen","resolve","reject","_next","_throw","key","arg","info","value","error","done","Promise","then","_asyncToGenerator","fn","self","args","arguments","apply","err","undefined","_defineProperty","obj","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","_toPrimitive","String","input","hint","prim","Symbol","toPrimitive","res","call","TypeError","Number","strPost","strDelete","NodeSession","AuthSession","constructor","settings","transport","NodeTransport","AuthToken","activeToken","_sudoToken","access_token","_authToken","isAuthenticated","token","isActive","authenticate","props","_this","getToken","headers","Authorization","concat","isSudo","sudoId","_this2","login","reset","_this3","_login","toString","logout","_this4","result","_logout","sudoLogout","_this5","newId","_this6","section","readConfig","clientId","client_id","clientSecret","client_secret","sdkError","message","body","encodeParams","ok","request","apiPath","setToken","promise","encodeURI","init","accessToken","_this7","exports"],"sources":["../../src/nodeSession.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\nimport type {\n IRequestProps,\n ITransport,\n HttpMethod,\n IApiSettings,\n IAccessToken,\n IError,\n} from '@looker/sdk-rtl'\nimport { sdkError, encodeParams, AuthToken, AuthSession } from '@looker/sdk-rtl'\nimport { NodeTransport } from './nodeTransport'\n\nconst strPost: HttpMethod = 'POST'\nconst strDelete: HttpMethod = 'DELETE'\n\nexport class NodeSession extends AuthSession {\n private readonly apiPath: string = '/api/4.0'\n _authToken: AuthToken = new AuthToken()\n _sudoToken: AuthToken = new AuthToken()\n\n constructor(public settings: IApiSettings, transport?: ITransport) {\n super(settings, transport || new NodeTransport(settings))\n }\n\n /**\n * Abstraction of AuthToken retrieval to support sudo mode\n */\n get activeToken() {\n if (this._sudoToken.access_token) {\n return this._sudoToken\n }\n return this._authToken\n }\n\n /**\n * Is there an active authentication token?\n */\n isAuthenticated() {\n // TODO I think this can be simplified\n const token = this.activeToken\n if (!(token && token.access_token)) return false\n return token.isActive()\n }\n\n /**\n * Add authentication data to the pending API request\n * @param props initialized API request properties\n *\n * @returns the updated request properties\n */\n async authenticate(props: IRequestProps) {\n const token = await this.getToken()\n if (token && token.access_token) {\n props.headers.Authorization = `Bearer ${token.access_token}`\n }\n return props\n }\n\n isSudo() {\n return !!this.sudoId && this._sudoToken.isActive()\n }\n\n /**\n * retrieve the current authentication token. If there is no active token, performs default\n * login to retrieve the token\n */\n async getToken() {\n if (!this.isAuthenticated()) {\n await this.login()\n }\n return this.activeToken\n }\n\n /**\n * Reset the authentication session\n */\n reset() {\n this.sudoId = ''\n this._authToken.reset()\n this._sudoToken.reset()\n }\n\n /**\n * Activate the authentication token for the API3 or sudo user\n * @param sudoId {string | number}: optional. If provided, impersonates the user specified\n *\n */\n async login(sudoId?: string | number) {\n if (sudoId || sudoId !== this.sudoId || !this.isAuthenticated()) {\n if (sudoId) {\n await this._login(sudoId.toString())\n } else {\n await this._login()\n }\n }\n return this.activeToken\n }\n\n /**\n * Logout the active user. If the active user is sudo, the session reverts to the API3 user\n */\n async logout() {\n let result = false\n if (this.isAuthenticated()) {\n result = await this._logout()\n }\n return result\n }\n\n private async sudoLogout() {\n let result = false\n if (this.isSudo()) {\n result = await this.logout() // Logout the current sudo\n this._sudoToken.reset()\n }\n return result\n }\n\n // internal login method that manages default auth token and sudo workflow\n private async _login(newId?: string) {\n // for linty freshness, always logout sudo if set\n await this.sudoLogout()\n\n if (newId !== this.sudoId) {\n // Assign new requested sudo id\n this.sudoId = newId || ''\n }\n\n if (!this._authToken.isActive()) {\n this.reset()\n // only retain client API3 credentials for the lifetime of the login request\n const section = this.settings.readConfig()\n const clientId = section.client_id\n const clientSecret = section.client_secret\n if (!clientId || !clientSecret) {\n throw sdkError({\n message: 'API credentials client_id and/or client_secret are not set',\n })\n }\n const body = encodeParams({\n client_id: clientId,\n client_secret: clientSecret,\n })\n // authenticate client\n const token = await this.ok(\n this.transport.request<IAccessToken, IError>(\n strPost,\n `${this.apiPath}/login`,\n undefined,\n body\n )\n )\n this._authToken.setToken(token)\n }\n\n if (this.sudoId) {\n // Use the API user auth to sudo\n const token = this.activeToken\n const promise = this.transport.request<IAccessToken, IError>(\n strPost,\n encodeURI(`${this.apiPath}/login/${newId}`),\n null,\n null,\n // ensure the auth token is included in the sudo request\n (init: IRequestProps) => {\n if (token.access_token) {\n init.headers.Authorization = `Bearer ${token.access_token}`\n }\n return init\n },\n this.settings // TODO this may not be needed here\n )\n\n const accessToken = await this.ok(promise)\n\n this._sudoToken.setToken(accessToken)\n }\n\n return this.activeToken\n }\n\n private async _logout() {\n const token = this.activeToken\n const promise = this.transport.request<string, IError>(\n strDelete,\n `${this.apiPath}/logout`,\n null,\n null,\n // ensure the auth token is included in the logout promise\n (init: IRequestProps) => {\n if (token.access_token) {\n init.headers.Authorization = `Bearer ${token.access_token}`\n }\n return init\n },\n this.settings\n )\n\n await this.ok(promise)\n\n // If no error was thrown, logout was successful\n if (this.sudoId) {\n // User was logged out, so set auth back to default\n this.sudoId = ''\n this._sudoToken.reset()\n if (!this._authToken.isActive()) {\n await this.login()\n }\n } else {\n // completely logged out\n this.reset()\n }\n return true\n }\n}\n"],"mappings":";;;;;;AAkCA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,cAAA,GAAAD,OAAA;AAA+C,SAAAE,mBAAAC,GAAA,EAAAC,OAAA,EAAAC,MAAA,EAAAC,KAAA,EAAAC,MAAA,EAAAC,GAAA,EAAAC,GAAA,cAAAC,IAAA,GAAAP,GAAA,CAAAK,GAAA,EAAAC,GAAA,OAAAE,KAAA,GAAAD,IAAA,CAAAC,KAAA,WAAAC,KAAA,IAAAP,MAAA,CAAA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;MACA,OAAO,IAAI;IAAA;EACb;AACF;AAAC+B,OAAA,CAAAxD,WAAA,GAAAA,WAAA"}
|
|
1
|
+
{"version":3,"file":"nodeSession.js","names":["sdkError","encodeParams","AuthToken","AuthSession","NodeTransport","strPost","strDelete","NodeSession","constructor","settings","transport","_defineProperty","activeToken","_sudoToken","access_token","_authToken","isAuthenticated","token","isActive","authenticate","props","_this","_asyncToGenerator","getToken","headers","Authorization","concat","isSudo","sudoId","_this2","login","reset","_this3","_login","toString","logout","_this4","result","_logout","sudoLogout","_this5","newId","_this6","section","readConfig","clientId","client_id","clientSecret","client_secret","message","body","ok","request","apiPath","undefined","setToken","promise","encodeURI","init","accessToken","_this7"],"sources":["../../src/nodeSession.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\nimport type {\n IRequestProps,\n ITransport,\n HttpMethod,\n IApiSettings,\n IAccessToken,\n IError,\n} from '@looker/sdk-rtl'\nimport { sdkError, encodeParams, AuthToken, AuthSession } from '@looker/sdk-rtl'\nimport { NodeTransport } from './nodeTransport'\n\nconst strPost: HttpMethod = 'POST'\nconst strDelete: HttpMethod = 'DELETE'\n\nexport class NodeSession extends AuthSession {\n private readonly apiPath: string = '/api/4.0'\n _authToken: AuthToken = new AuthToken()\n _sudoToken: AuthToken = new AuthToken()\n\n constructor(public settings: IApiSettings, transport?: ITransport) {\n super(settings, transport || new NodeTransport(settings))\n }\n\n /**\n * Abstraction of AuthToken retrieval to support sudo mode\n */\n get activeToken() {\n if (this._sudoToken.access_token) {\n return this._sudoToken\n }\n return this._authToken\n }\n\n /**\n * Is there an active authentication token?\n */\n isAuthenticated() {\n // TODO I think this can be simplified\n const token = this.activeToken\n if (!(token && token.access_token)) return false\n return token.isActive()\n }\n\n /**\n * Add authentication data to the pending API request\n * @param props initialized API request properties\n *\n * @returns the updated request properties\n */\n async authenticate(props: IRequestProps) {\n const token = await this.getToken()\n if (token && token.access_token) {\n props.headers.Authorization = `Bearer ${token.access_token}`\n }\n return props\n }\n\n isSudo() {\n return !!this.sudoId && this._sudoToken.isActive()\n }\n\n /**\n * retrieve the current authentication token. If there is no active token, performs default\n * login to retrieve the token\n */\n async getToken() {\n if (!this.isAuthenticated()) {\n await this.login()\n }\n return this.activeToken\n }\n\n /**\n * Reset the authentication session\n */\n reset() {\n this.sudoId = ''\n this._authToken.reset()\n this._sudoToken.reset()\n }\n\n /**\n * Activate the authentication token for the API3 or sudo user\n * @param sudoId {string | number}: optional. If provided, impersonates the user specified\n *\n */\n async login(sudoId?: string | number) {\n if (sudoId || sudoId !== this.sudoId || !this.isAuthenticated()) {\n if (sudoId) {\n await this._login(sudoId.toString())\n } else {\n await this._login()\n }\n }\n return this.activeToken\n }\n\n /**\n * Logout the active user. If the active user is sudo, the session reverts to the API3 user\n */\n async logout() {\n let result = false\n if (this.isAuthenticated()) {\n result = await this._logout()\n }\n return result\n }\n\n private async sudoLogout() {\n let result = false\n if (this.isSudo()) {\n result = await this.logout() // Logout the current sudo\n this._sudoToken.reset()\n }\n return result\n }\n\n // internal login method that manages default auth token and sudo workflow\n private async _login(newId?: string) {\n // for linty freshness, always logout sudo if set\n await this.sudoLogout()\n\n if (newId !== this.sudoId) {\n // Assign new requested sudo id\n this.sudoId = newId || ''\n }\n\n if (!this._authToken.isActive()) {\n this.reset()\n // only retain client API3 credentials for the lifetime of the login request\n const section = this.settings.readConfig()\n const clientId = section.client_id\n const clientSecret = section.client_secret\n if (!clientId || !clientSecret) {\n throw sdkError({\n message: 'API credentials client_id and/or client_secret are not set',\n })\n }\n const body = encodeParams({\n client_id: clientId,\n client_secret: clientSecret,\n })\n // authenticate client\n const token = await this.ok(\n this.transport.request<IAccessToken, IError>(\n strPost,\n `${this.apiPath}/login`,\n undefined,\n body\n )\n )\n this._authToken.setToken(token)\n }\n\n if (this.sudoId) {\n // Use the API user auth to sudo\n const token = this.activeToken\n const promise = this.transport.request<IAccessToken, IError>(\n strPost,\n encodeURI(`${this.apiPath}/login/${newId}`),\n null,\n null,\n // ensure the auth token is included in the sudo request\n (init: IRequestProps) => {\n if (token.access_token) {\n init.headers.Authorization = `Bearer ${token.access_token}`\n }\n return init\n },\n this.settings // TODO this may not be needed here\n )\n\n const accessToken = await this.ok(promise)\n\n this._sudoToken.setToken(accessToken)\n }\n\n return this.activeToken\n }\n\n private async _logout() {\n const token = this.activeToken\n const promise = this.transport.request<string, IError>(\n strDelete,\n `${this.apiPath}/logout`,\n null,\n null,\n // ensure the auth token is included in the logout promise\n (init: IRequestProps) => {\n if (token.access_token) {\n init.headers.Authorization = `Bearer ${token.access_token}`\n }\n return init\n },\n this.settings\n )\n\n await this.ok(promise)\n\n // If no error was thrown, logout was successful\n if (this.sudoId) {\n // User was logged out, so set auth back to default\n this.sudoId = ''\n this._sudoToken.reset()\n if (!this._authToken.isActive()) {\n await this.login()\n }\n } else {\n // completely logged out\n this.reset()\n }\n return true\n }\n}\n"],"mappings":";;;;;AAkCA,SAASA,QAAQ,EAAEC,Y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B,MAAM0C,MAAI,CAAC9B,KAAK,CAAC,CAAC;QACpB;MACF,CAAC,MAAM;QAEL8B,MAAI,CAAC7B,KAAK,CAAC,CAAC;MACd;MACA,OAAO,IAAI;IAAA;EACb;AACF"}
|
package/lib/esm/nodeSettings.js
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.readIniConfig = exports.readEnvConfig = exports.getenv = exports.NodeSettingsIniFile = exports.NodeSettings = exports.ApiConfigSection = exports.ApiConfig = void 0;
|
|
7
|
-
var fs = _interopRequireWildcard(require("fs"));
|
|
8
|
-
var ini = _interopRequireWildcard(require("ini"));
|
|
9
|
-
var _sdkRtl = require("@looker/sdk-rtl");
|
|
10
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
12
1
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
2
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
14
3
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
4
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
16
5
|
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
17
|
-
|
|
6
|
+
import * as fs from 'fs';
|
|
7
|
+
import * as ini from 'ini';
|
|
8
|
+
import { ApiConfigMap, ApiSettings, DefaultSettings, ValueSettings, sdkError, unquote } from '@looker/sdk-rtl';
|
|
9
|
+
export var getenv = function getenv(name) {
|
|
18
10
|
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
|
|
19
11
|
var val = process.env[name];
|
|
20
12
|
return val === undefined ? defaultValue : val;
|
|
21
13
|
};
|
|
22
|
-
|
|
23
|
-
var
|
|
24
|
-
exports.ApiConfig = ApiConfig;
|
|
25
|
-
var ApiConfigSection = (contents, section) => {
|
|
14
|
+
export var ApiConfig = contents => ini.parse(contents);
|
|
15
|
+
export var ApiConfigSection = (contents, section) => {
|
|
26
16
|
var config = ApiConfig(contents);
|
|
27
17
|
if (!section) {
|
|
28
18
|
section = Object.keys(config)[0];
|
|
@@ -36,21 +26,19 @@ var ApiConfigSection = (contents, section) => {
|
|
|
36
26
|
}
|
|
37
27
|
return settings;
|
|
38
28
|
};
|
|
39
|
-
|
|
40
|
-
var readEnvConfig = envPrefix => {
|
|
29
|
+
export var readEnvConfig = envPrefix => {
|
|
41
30
|
var values = {};
|
|
42
|
-
var configMap =
|
|
31
|
+
var configMap = ApiConfigMap(envPrefix);
|
|
43
32
|
Object.keys(configMap).forEach(key => {
|
|
44
33
|
var envKey = configMap[key];
|
|
45
34
|
if (process.env[envKey] !== undefined) {
|
|
46
|
-
var val =
|
|
35
|
+
var val = unquote(process.env[envKey]);
|
|
47
36
|
values[key] = val;
|
|
48
37
|
}
|
|
49
38
|
});
|
|
50
39
|
return values;
|
|
51
40
|
};
|
|
52
|
-
|
|
53
|
-
var readIniConfig = (fileName, envPrefix, section) => {
|
|
41
|
+
export var readIniConfig = (fileName, envPrefix, section) => {
|
|
54
42
|
var config = readEnvConfig(envPrefix);
|
|
55
43
|
if (fileName && fs.existsSync(fileName)) {
|
|
56
44
|
config = _objectSpread(_objectSpread({}, ApiConfigSection(fs.readFileSync(fileName, 'utf8'), section)), config);
|
|
@@ -58,13 +46,12 @@ var readIniConfig = (fileName, envPrefix, section) => {
|
|
|
58
46
|
Object.keys(config).forEach(key => {
|
|
59
47
|
var val = config[key];
|
|
60
48
|
if (typeof val === 'string') {
|
|
61
|
-
config[key] =
|
|
49
|
+
config[key] = unquote(val);
|
|
62
50
|
}
|
|
63
51
|
});
|
|
64
52
|
return config;
|
|
65
53
|
};
|
|
66
|
-
|
|
67
|
-
class NodeSettings extends _sdkRtl.ApiSettings {
|
|
54
|
+
export class NodeSettings extends ApiSettings {
|
|
68
55
|
constructor(envPrefix, contents, section) {
|
|
69
56
|
var settings;
|
|
70
57
|
if (contents) {
|
|
@@ -77,7 +64,7 @@ class NodeSettings extends _sdkRtl.ApiSettings {
|
|
|
77
64
|
} else {
|
|
78
65
|
settings = readEnvConfig(envPrefix);
|
|
79
66
|
}
|
|
80
|
-
super(_objectSpread(_objectSpread({},
|
|
67
|
+
super(_objectSpread(_objectSpread({}, DefaultSettings()), settings));
|
|
81
68
|
_defineProperty(this, "envPrefix", void 0);
|
|
82
69
|
_defineProperty(this, "section", void 0);
|
|
83
70
|
this.section = section !== null && section !== void 0 ? section : '';
|
|
@@ -87,19 +74,18 @@ class NodeSettings extends _sdkRtl.ApiSettings {
|
|
|
87
74
|
return readEnvConfig(this.envPrefix);
|
|
88
75
|
}
|
|
89
76
|
}
|
|
90
|
-
|
|
91
|
-
class NodeSettingsIniFile extends NodeSettings {
|
|
77
|
+
export class NodeSettingsIniFile extends NodeSettings {
|
|
92
78
|
constructor(envPrefix) {
|
|
93
79
|
var fileName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
94
80
|
var section = arguments.length > 2 ? arguments[2] : undefined;
|
|
95
81
|
if (fileName && !fs.existsSync(fileName)) {
|
|
96
|
-
throw
|
|
82
|
+
throw sdkError({
|
|
97
83
|
message: "File ".concat(fileName, " was not found")
|
|
98
84
|
});
|
|
99
85
|
}
|
|
100
86
|
fileName = fileName || './looker.ini';
|
|
101
87
|
var config = readIniConfig(fileName, envPrefix, section);
|
|
102
|
-
var settings =
|
|
88
|
+
var settings = ValueSettings(config, envPrefix);
|
|
103
89
|
super(envPrefix, settings, section);
|
|
104
90
|
_defineProperty(this, "fileName", void 0);
|
|
105
91
|
this.fileName = fileName;
|
|
@@ -109,5 +95,4 @@ class NodeSettingsIniFile extends NodeSettings {
|
|
|
109
95
|
return readIniConfig(this.fileName, this.envPrefix, section);
|
|
110
96
|
}
|
|
111
97
|
}
|
|
112
|
-
exports.NodeSettingsIniFile = NodeSettingsIniFile;
|
|
113
98
|
//# sourceMappingURL=nodeSettings.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeSettings.js","names":["fs","_interopRequireWildcard","require","ini","_sdkRtl","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","ownKeys","object","enumerableOnly","keys","getOwnPropertySymbols","symbols","filter","sym","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","_defineProperty","getOwnPropertyDescriptors","defineProperties","value","_toPropertyKey","configurable","writable","arg","_toPrimitive","String","input","hint","prim","Symbol","toPrimitive","undefined","res","TypeError","Number","getenv","name","defaultValue","val","process","env","exports","ApiConfig","contents","parse","ApiConfigSection","section","config","settings","Error","concat","api_version","console","warn","readEnvConfig","envPrefix","values","configMap","ApiConfigMap","envKey","unquote","readIniConfig","fileName","existsSync","readFileSync","NodeSettings","ApiSettings","constructor","DefaultSettings","readConfig","_section","NodeSettingsIniFile","sdkError","message","ValueSettings"],"sources":["../../src/nodeSettings.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\nimport * as fs from 'fs'\nimport * as ini from 'ini'\nimport type { IApiSettings, IApiSection } from '@looker/sdk-rtl'\nimport {\n ApiConfigMap,\n ApiSettings,\n DefaultSettings,\n ValueSettings,\n sdkError,\n unquote,\n} from '@looker/sdk-rtl'\n\n/**\n * Read an environment key. Use defaultValue if it doesn't exist\n * @param {string} name Environment variable name\n * @param {string | undefined} defaultValue\n * @returns {string | undefined} The value of the environment variable if it exists, or defaultValue\n */\nexport const getenv = (\n name: string,\n defaultValue: string | undefined = undefined\n) => {\n const val = process.env[name]\n return val === undefined ? defaultValue : val\n}\n\n/**\n * Complete .INI file parse results\n */\nexport interface IApiConfig {\n [key: string]: any\n}\n\n/**\n * Parses `.ini` formatted content\n * @param contents formatted as an `.ini` file\n * @constructor\n */\nexport const ApiConfig = (contents: string): IApiConfig => ini.parse(contents)\n\n/**\n * Extract named or (default) first section from INI file\n * @param contents {string} Parameters formatted as an INI file\n * @param section {[key: string]: any;} Contents of INI section\n * @constructor\n */\nexport const ApiConfigSection = (\n contents: string,\n section?: string\n): IApiSection => {\n const config = ApiConfig(contents)\n if (!section) {\n // default to the first section if not specified\n section = Object.keys(config)[0]\n }\n const settings = config[section]\n if (!settings) {\n throw new Error(`No section named \"${section}\" was found`)\n }\n if (settings.api_version) {\n console.warn(\n 'api_version is no longer read from a configuration file by the SDK'\n )\n }\n return settings\n}\n\n/**\n * A utility function that loads environment variables and maps them to the standard configuration values\n *\n * @returns the populated `IApiSection`, which may be empty\n */\nexport const readEnvConfig = (envPrefix: string) => {\n const values: IApiSection = {}\n const configMap = ApiConfigMap(envPrefix)\n Object.keys(configMap).forEach((key) => {\n const envKey = configMap[key]\n if (process.env[envKey] !== undefined) {\n // Value exists. Map environment variable keys to config variable keys\n const val = unquote(process.env[envKey])\n values[key] = val\n }\n })\n return values\n}\n\n/**\n * A utility function that loads the configuration values from the specified file name and overrides them\n * with environment variable values, if the environment variables exist\n *\n * @param {string} fileName Name of configuration file to read\n * @param envPrefix environment variable prefix. Pass an empty string to skip environment reading.\n * @param {string} section Optional. Name of section of configuration file to read\n * @returns {IApiSection} containing the configuration values\n */\nexport const readIniConfig = (\n fileName: string,\n envPrefix: string,\n section?: string\n) => {\n // get environment variables\n let config = readEnvConfig(envPrefix)\n if (fileName && fs.existsSync(fileName)) {\n // override any config file settings with environment values if the environment value is set\n config = {\n ...ApiConfigSection(fs.readFileSync(fileName, 'utf8'), section),\n ...config,\n }\n }\n // Unquote any quoted configuration values\n Object.keys(config).forEach((key) => {\n const val = config[key]\n if (typeof val === 'string') {\n config[key] = unquote(val)\n }\n })\n return config\n}\n\n/**\n * Read configuration settings from Node environment variables\n *\n * This class initializes SDK settings **only** from the values passed in to its constructor and\n * (potentially) configured environment variables, and does not read a configuration file at all\n *\n * Any environment variables that **are** set, will override the values passed in to the constructor\n * with the same key\n *\n */\nexport class NodeSettings extends ApiSettings {\n protected readonly envPrefix!: string\n public section: string\n\n /**\n * Initialize config settings for the node SDK runtime\n * @param envPrefix Environment variable name prefix. Use empty string to not read the environment\n * @param contents contents of the read config\n * @param section name of ini section to process\n */\n constructor(\n envPrefix: string,\n contents?: string | IApiSettings,\n section?: string\n ) {\n let settings: IApiSettings\n if (contents) {\n if (typeof contents === 'string') {\n settings = ApiConfigSection(contents, section) as IApiSettings\n } else {\n settings = contents\n }\n settings = { ...readEnvConfig(envPrefix), ...settings }\n } else {\n settings = readEnvConfig(envPrefix) as IApiSettings\n }\n super({ ...DefaultSettings(), ...settings })\n this.section = section ?? ''\n this.envPrefix = envPrefix\n }\n\n /**\n * **NOTE**: If `envPrefix` is defined in the constructor, environment variables will be read in this call.\n *\n * @param _section section name is ignored here.\n */\n readConfig(_section?: string): IApiSection {\n return readEnvConfig(this.envPrefix)\n }\n}\n\n/**\n * Example class that reads a configuration from a file in node\n *\n * If `fileName` is not specified in the constructor, the default file name is `./looker.ini`\n *\n * **Warning**: `.ini` files storing credentials should be secured in the run-time environment, and\n * ignored by version control systems so credentials never get checked in to source code repositories.\n * A recommended pattern is using Node environment variables to specify confidential API credentials\n * while using an `.ini` file for values like `base_url`.\n *\n * **Note**: If the configuration file is specified but does **not** exist, an error will be thrown.\n * No error is thrown if the fileName defaulted to `./looker.ini` inside the constructor and that\n * file does not exist. In that case, configuration from environment variables will be required.\n *\n */\nexport class NodeSettingsIniFile extends NodeSettings {\n private readonly fileName!: string\n\n constructor(envPrefix: string, fileName = '', section?: string) {\n if (fileName && !fs.existsSync(fileName)) {\n throw sdkError({ message: `File ${fileName} was not found` })\n }\n // default fileName to looker.ini\n fileName = fileName || './looker.ini'\n const config = readIniConfig(fileName, envPrefix, section)\n const settings = ValueSettings(config, envPrefix)\n super(envPrefix, settings, section)\n this.fileName = fileName\n }\n\n /**\n * Read a configuration section and return it as a generic keyed collection\n * If the configuration file doesn't exist, environment variables will be used for the values\n * Environment variables, if set, also override the configuration file values\n *\n * **NOTE**: If `envPrefix` is defined in the constructor, environment variables will be read in this call.\n *\n * @param section {string} Name of Ini section to read. Optional. Defaults to first section.\n *\n */\n readConfig(section?: string): IApiSection {\n section = section || this.section\n return readIniConfig(this.fileName, this.envPrefix, section)\n }\n}\n"],"mappings":";;;;;;AA0BA,IAAAA,EAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,GAAA,GAAAF,uBAAA,CAAAC,OAAA;AAEA,IAAAE,OAAA,GAAAF,OAAA;AAOwB,SAAAG,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,yBAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAL,wBAAAS,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAA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C/D;IAEAA,QAAQ,GAAGA,QAAQ,IAAI,cAAc;IACrC,IAAMf,MAAM,GAAGc,aAAa,CAACC,QAAQ,EAAEP,SAAS,EAAET,OAAO,CAAC;IAC1D,IAAME,QAAQ,GAAG,IAAA0B,qBAAa,EAAC3B,MAAM,EAAEQ,SAAS,CAAC;IACjD,KAAK,CAACA,SAAS,EAAEP,QAAQ,EAAEF,OAAO,CAAC;IAAA9B,eAAA;IACnC,IAAI,CAAC8C,QAAQ,GAAGA,QAAQ;EAC1B;EAYAO,UAAUA,CAACvB,OAAgB,EAAe;IACxCA,OAAO,GAAGA,OAAO,IAAI,IAAI,CAACA,OAAO;IACjC,OAAOe,aAAa,CAAC,IAAI,CAACC,QAAQ,EAAE,IAAI,CAACP,SAAS,EAAET,OAAO,CAAC;EAC9D;AACF;AAACL,OAAA,CAAA8B,mBAAA,GAAAA,mBAAA"}
|
|
1
|
+
{"version":3,"file":"nodeSettings.js","names":["fs","ini","ApiConfigMap","ApiSettings","DefaultSettings","ValueSettings","sdkError","unquote","getenv","name","defaultValue","arguments","length","undefined","val","process","env","ApiConfig","contents","parse","ApiConfigSection","section","config","Object","keys","settings","Error","concat","api_version","console","warn","readEnvConfig","envPrefix","values","configMap","forEach","key","envKey","readIniConfig","fileName","existsSync","_objectSpread","readFileSync","NodeSettings","constructor","_defineProperty","readConfig","_section","NodeSettingsIniFile","message"],"sources":["../../src/nodeSettings.ts"],"sourcesContent":["/*\n\n MIT License\n\n Copyright (c) 2021 Looker Data Sciences, Inc.\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n\n */\n\nimport * as fs from 'fs'\nimport * as ini from 'ini'\nimport type { IApiSettings, IApiSection } from '@looker/sdk-rtl'\nimport {\n ApiConfigMap,\n ApiSettings,\n DefaultSettings,\n ValueSettings,\n sdkError,\n unquote,\n} from '@looker/sdk-rtl'\n\n/**\n * Read an environment key. Use defaultValue if it doesn't exist\n * @param {string} name Environment variable name\n * @param {string | undefined} defaultValue\n * @returns {string | undefined} The value of the environment variable if it exists, or defaultValue\n */\nexport const getenv = (\n name: string,\n defaultValue: string | undefined = undefined\n) => {\n const val = process.env[name]\n return val === undefined ? defaultValue : val\n}\n\n/**\n * Complete .INI file parse results\n */\nexport interface IApiConfig {\n [key: string]: any\n}\n\n/**\n * Parses `.ini` formatted content\n * @param contents formatted as an `.ini` file\n * @constructor\n */\nexport const ApiConfig = (contents: string): IApiConfig => ini.parse(contents)\n\n/**\n * Extract named or (default) first section from INI file\n * @param contents {string} Parameters formatted as an INI file\n * @param section {[key: string]: any;} Contents of INI section\n * @constructor\n */\nexport const ApiConfigSection = (\n contents: string,\n section?: string\n): IApiSection => {\n const config = ApiConfig(contents)\n if (!section) {\n // default to the first section if not specified\n section = Object.keys(config)[0]\n }\n const settings = config[section]\n if (!settings) {\n throw new Error(`No section named \"${section}\" was found`)\n }\n if (settings.api_version) {\n console.warn(\n 'api_version is no longer read from a configuration file by the SDK'\n )\n }\n return settings\n}\n\n/**\n * A utility function that loads environment variables and maps them to the standard configuration values\n *\n * @returns the populated `IApiSection`, which may be empty\n */\nexport const readEnvConfig = (envPrefix: string) => {\n const values: IApiSection = {}\n const configMap = ApiConfigMap(envPrefix)\n Object.keys(configMap).forEach((key) => {\n const envKey = configMap[key]\n if (process.env[envKey] !== undefined) {\n // Value exists. Map environment variable keys to config variable keys\n const val = unquote(process.env[envKey])\n values[key] = val\n }\n })\n return values\n}\n\n/**\n * A utility function that loads the configuration values from the specified file name and overrides them\n * with environment variable values, if the environment variables exist\n *\n * @param {string} fileName Name of configuration file to read\n * @param envPrefix environment variable prefix. Pass an empty string to skip environment reading.\n * @param {string} section Optional. Name of section of configuration file to read\n * @returns {IApiSection} containing the configuration values\n */\nexport const readIniConfig = (\n fileName: string,\n envPrefix: string,\n section?: string\n) => {\n // get environment variables\n let config = readEnvConfig(envPrefix)\n if (fileName && fs.existsSync(fileName)) {\n // override any config file settings with environment values if the environment value is set\n config = {\n ...ApiConfigSection(fs.readFileSync(fileName, 'utf8'), section),\n ...config,\n }\n }\n // Unquote any quoted configuration values\n Object.keys(config).forEach((key) => {\n const val = config[key]\n if (typeof val === 'string') {\n config[key] = unquote(val)\n }\n })\n return config\n}\n\n/**\n * Read configuration settings from Node environment variables\n *\n * This class initializes SDK settings **only** from the values passed in to its constructor and\n * (potentially) configured environment variables, and does not read a configuration file at all\n *\n * Any environment variables that **are** set, will override the values passed in to the constructor\n * with the same key\n *\n */\nexport class NodeSettings extends ApiSettings {\n protected readonly envPrefix!: string\n public section: string\n\n /**\n * Initialize config settings for the node SDK runtime\n * @param envPrefix Environment variable name prefix. Use empty string to not read the environment\n * @param contents contents of the read config\n * @param section name of ini section to process\n */\n constructor(\n envPrefix: string,\n contents?: string | IApiSettings,\n section?: string\n ) {\n let settings: IApiSettings\n if (contents) {\n if (typeof contents === 'string') {\n settings = ApiConfigSection(contents, section) as IApiSettings\n } else {\n settings = contents\n }\n settings = { ...readEnvConfig(envPrefix), ...settings }\n } else {\n settings = readEnvConfig(envPrefix) as IApiSettings\n }\n super({ ...DefaultSettings(), ...settings })\n this.section = section ?? ''\n this.envPrefix = envPrefix\n }\n\n /**\n * **NOTE**: If `envPrefix` is defined in the constructor, environment variables will be read in this call.\n *\n * @param _section section name is ignored here.\n */\n readConfig(_section?: string): IApiSection {\n return readEnvConfig(this.envPrefix)\n }\n}\n\n/**\n * Example class that reads a configuration from a file in node\n *\n * If `fileName` is not specified in the constructor, the default file name is `./looker.ini`\n *\n * **Warning**: `.ini` files storing credentials should be secured in the run-time environment, and\n * ignored by version control systems so credentials never get checked in to source code repositories.\n * A recommended pattern is using Node environment variables to specify confidential API credentials\n * while using an `.ini` file for values like `base_url`.\n *\n * **Note**: If the configuration file is specified but does **not** exist, an error will be thrown.\n * No error is thrown if the fileName defaulted to `./looker.ini` inside the constructor and that\n * file does not exist. In that case, configuration from environment variables will be required.\n *\n */\nexport class NodeSettingsIniFile extends NodeSettings {\n private readonly fileName!: string\n\n constructor(envPrefix: string, fileName = '', section?: string) {\n if (fileName && !fs.existsSync(fileName)) {\n throw sdkError({ message: `File ${fileName} was not found` })\n }\n // default fileName to looker.ini\n fileName = fileName || './looker.ini'\n const config = readIniConfig(fileName, envPrefix, section)\n const settings = ValueSettings(config, envPrefix)\n super(envPrefix, settings, section)\n this.fileName = fileName\n }\n\n /**\n * Read a configuration section and return it as a generic keyed collection\n * If the configuration file doesn't exist, environment variables will be used for the values\n * Environment variables, if set, also override the configuration file values\n *\n * **NOTE**: If `envPrefix` is defined in the constructor, environment variables will be read in this call.\n *\n * @param section {string} Name of Ini section to read. Optional. Defaults to first section.\n *\n */\n readConfig(section?: string): IApiSection {\n section = section || this.section\n return readIniConfig(this.fileName, this.envPrefix, section)\n }\n}\n"],"mappings":";;;;;AA0BA,OAAO,KAAKA,EAAE,MAAM,IAAI;AACxB,OAAO,KAAKC,GAAG,MAAM,KAAK;AAE1B,SACEC,YAAY,EACZC,WAAW,EACXC,eAAe,EACfC,aAAa,EACbC,QAAQ,EACRC,OAAO,QACF,iBAAiB;AAQxB,OAAO,IAAMC,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