@optimizely-opal/opal-tool-ocp-sdk 0.0.0-OCP-1487.8 → 0.0.0-OCP-1487.10
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/README.md +29 -70
- package/dist/decorator/Decorator.d.ts +0 -6
- package/dist/decorator/Decorator.d.ts.map +1 -1
- package/dist/decorator/Decorator.js +2 -32
- package/dist/decorator/Decorator.js.map +1 -1
- package/dist/decorator/Decorator.test.js +11 -205
- package/dist/decorator/Decorator.test.js.map +1 -1
- package/dist/service/Service.d.ts +2 -20
- package/dist/service/Service.d.ts.map +1 -1
- package/dist/service/Service.js +4 -41
- package/dist/service/Service.js.map +1 -1
- package/dist/service/Service.test.js +1 -92
- package/dist/service/Service.test.js.map +1 -1
- package/package.json +1 -1
- package/src/decorator/Decorator.test.ts +10 -305
- package/src/decorator/Decorator.ts +2 -44
- package/src/service/Service.test.ts +1 -170
- package/src/service/Service.ts +4 -46
- package/dist/utils/ImportUtils.d.ts +0 -15
- package/dist/utils/ImportUtils.d.ts.map +0 -1
- package/dist/utils/ImportUtils.js +0 -77
- package/dist/utils/ImportUtils.js.map +0 -1
- package/src/utils/ImportUtils.ts +0 -45
package/src/service/Service.ts
CHANGED
|
@@ -52,7 +52,6 @@ export class Tool<TAuthData> {
|
|
|
52
52
|
* @param endpoint API endpoint
|
|
53
53
|
* @param handler Function implementing the tool
|
|
54
54
|
* @param authRequirements Authentication requirements (mandatory - OptiID enforced)
|
|
55
|
-
* @param isGlobal Whether the tool is global (accessible irrespective of app installation context)
|
|
56
55
|
*/
|
|
57
56
|
public constructor(
|
|
58
57
|
public name: string,
|
|
@@ -64,8 +63,7 @@ export class Tool<TAuthData> {
|
|
|
64
63
|
params: unknown,
|
|
65
64
|
authData?: TAuthData
|
|
66
65
|
) => Promise<unknown>,
|
|
67
|
-
public authRequirements: AuthRequirement[] = [DEFAULT_OPTIID_AUTH]
|
|
68
|
-
public isGlobal: boolean = false
|
|
66
|
+
public authRequirements: AuthRequirement[] = [DEFAULT_OPTIID_AUTH]
|
|
69
67
|
) {}
|
|
70
68
|
|
|
71
69
|
/**
|
|
@@ -89,21 +87,6 @@ export class ToolsService {
|
|
|
89
87
|
private functions: Map<string, Tool<any>> = new Map();
|
|
90
88
|
private interactions: Map<string, Interaction<any>> = new Map();
|
|
91
89
|
|
|
92
|
-
/**
|
|
93
|
-
* Set the import context for tools that will be imported
|
|
94
|
-
* @param context The context type ('global' | 'regular')
|
|
95
|
-
*/
|
|
96
|
-
public setImportContext(context: 'global' | 'regular'): void {
|
|
97
|
-
globalThis.__IMPORT_CONTEXT = context;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Clear the import context after import is complete
|
|
102
|
-
*/
|
|
103
|
-
public clearImportContext(): void {
|
|
104
|
-
globalThis.__IMPORT_CONTEXT = undefined;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
90
|
/**
|
|
108
91
|
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
109
92
|
* @param authRequirements Original authentication requirements
|
|
@@ -128,7 +111,6 @@ export class ToolsService {
|
|
|
128
111
|
* @param parameters List of parameters for the tool
|
|
129
112
|
* @param endpoint API endpoint for the tool
|
|
130
113
|
* @param authRequirements Authentication requirements (optional)
|
|
131
|
-
* @param isGlobal Whether the tool is global
|
|
132
114
|
*/
|
|
133
115
|
public registerTool<TAuthData>(
|
|
134
116
|
name: string,
|
|
@@ -140,8 +122,7 @@ export class ToolsService {
|
|
|
140
122
|
) => Promise<unknown>,
|
|
141
123
|
parameters: Parameter[],
|
|
142
124
|
endpoint: string,
|
|
143
|
-
authRequirements?: AuthRequirement[]
|
|
144
|
-
isGlobal = false
|
|
125
|
+
authRequirements?: AuthRequirement[]
|
|
145
126
|
): void {
|
|
146
127
|
// Enforce OptiID authentication for all tools
|
|
147
128
|
const enforcedAuthRequirements = this.enforceOptiIdAuth(authRequirements);
|
|
@@ -151,8 +132,7 @@ export class ToolsService {
|
|
|
151
132
|
parameters,
|
|
152
133
|
endpoint,
|
|
153
134
|
handler,
|
|
154
|
-
enforcedAuthRequirements
|
|
155
|
-
isGlobal
|
|
135
|
+
enforcedAuthRequirements
|
|
156
136
|
);
|
|
157
137
|
this.functions.set(endpoint, func);
|
|
158
138
|
}
|
|
@@ -181,7 +161,7 @@ export class ToolsService {
|
|
|
181
161
|
functionContext: ToolFunction | GlobalToolFunction
|
|
182
162
|
): Promise<App.Response> {
|
|
183
163
|
if (req.path === '/discovery') {
|
|
184
|
-
return this.
|
|
164
|
+
return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
|
|
185
165
|
} else {
|
|
186
166
|
const func = this.functions.get(req.path);
|
|
187
167
|
if (func) {
|
|
@@ -228,28 +208,6 @@ export class ToolsService {
|
|
|
228
208
|
return new App.Response(404, 'Function not found');
|
|
229
209
|
}
|
|
230
210
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Handle discovery endpoint requests with context-aware filtering
|
|
235
|
-
* @param functionContext The function context making the request
|
|
236
|
-
* @returns Response with filtered functions based on where tools are defined/imported
|
|
237
|
-
*/
|
|
238
|
-
private handleDiscovery(functionContext: ToolFunction | GlobalToolFunction): App.Response {
|
|
239
|
-
const isGlobalContext = functionContext instanceof GlobalToolFunction;
|
|
240
|
-
|
|
241
|
-
const availableFunctions = Array.from(this.functions.values()).filter((tool) => {
|
|
242
|
-
if (isGlobalContext) {
|
|
243
|
-
// Global context can only see global tools (defined in or imported into GlobalToolFunction)
|
|
244
|
-
return tool.isGlobal;
|
|
245
|
-
} else {
|
|
246
|
-
// Regular context can only see regular tools (defined in or imported into ToolFunction)
|
|
247
|
-
return !tool.isGlobal;
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
return new App.Response(200, { functions: availableFunctions.map((f) => f.toJSON()) });
|
|
252
|
-
}
|
|
253
211
|
}
|
|
254
212
|
|
|
255
213
|
export const toolsService = new ToolsService();
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Import a module as global tools
|
|
3
|
-
* @param modulePath The path to the module to import
|
|
4
|
-
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
5
|
-
* @returns The imported module
|
|
6
|
-
*/
|
|
7
|
-
export declare function importAsGlobal<T = any>(modulePath: string, baseUrl?: string): Promise<T>;
|
|
8
|
-
/**
|
|
9
|
-
* Import a module as regular tools
|
|
10
|
-
* @param modulePath The path to the module to import
|
|
11
|
-
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
12
|
-
* @returns The imported module
|
|
13
|
-
*/
|
|
14
|
-
export declare function importAsRegular<T = any>(modulePath: string, baseUrl?: string): Promise<T>;
|
|
15
|
-
//# sourceMappingURL=ImportUtils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ImportUtils.d.ts","sourceRoot":"","sources":["../../src/utils/ImportUtils.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,CAAC,GAAG,GAAG,EAC1C,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,CAAC,CAAC,CAWZ;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,CAAC,GAAG,GAAG,EAC3C,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,CAAC,CAAC,CAWZ"}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.importAsGlobal = importAsGlobal;
|
|
37
|
-
exports.importAsRegular = importAsRegular;
|
|
38
|
-
const Service_1 = require("../service/Service");
|
|
39
|
-
/**
|
|
40
|
-
* Import a module as global tools
|
|
41
|
-
* @param modulePath The path to the module to import
|
|
42
|
-
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
43
|
-
* @returns The imported module
|
|
44
|
-
*/
|
|
45
|
-
async function importAsGlobal(modulePath, baseUrl) {
|
|
46
|
-
try {
|
|
47
|
-
Service_1.toolsService.setImportContext('global');
|
|
48
|
-
// If baseUrl is provided, resolve the module path relative to it
|
|
49
|
-
// Otherwise, use the modulePath as-is
|
|
50
|
-
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
51
|
-
const module = await Promise.resolve(`${resolvedPath}`).then(s => __importStar(require(s)));
|
|
52
|
-
return module;
|
|
53
|
-
}
|
|
54
|
-
finally {
|
|
55
|
-
Service_1.toolsService.clearImportContext();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Import a module as regular tools
|
|
60
|
-
* @param modulePath The path to the module to import
|
|
61
|
-
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
62
|
-
* @returns The imported module
|
|
63
|
-
*/
|
|
64
|
-
async function importAsRegular(modulePath, baseUrl) {
|
|
65
|
-
try {
|
|
66
|
-
Service_1.toolsService.setImportContext('regular');
|
|
67
|
-
// If baseUrl is provided, resolve the module path relative to it
|
|
68
|
-
// Otherwise, use the modulePath as-is
|
|
69
|
-
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
70
|
-
const module = await Promise.resolve(`${resolvedPath}`).then(s => __importStar(require(s)));
|
|
71
|
-
return module;
|
|
72
|
-
}
|
|
73
|
-
finally {
|
|
74
|
-
Service_1.toolsService.clearImportContext();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
//# sourceMappingURL=ImportUtils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ImportUtils.js","sourceRoot":"","sources":["../../src/utils/ImportUtils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,wCAcC;AAQD,0CAcC;AA5CD,gDAAkD;AAElD;;;;;GAKG;AACI,KAAK,UAAU,cAAc,CAClC,UAAkB,EAClB,OAAgB;IAEhB,IAAI,CAAC;QACH,sBAAY,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QACxC,iEAAiE;QACjE,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC;QAC9E,MAAM,MAAM,GAAG,yBAAa,YAAY,uCAAC,CAAC;QAC1C,OAAO,MAAM,CAAC;IAChB,CAAC;YAAS,CAAC;QACT,sBAAY,CAAC,kBAAkB,EAAE,CAAC;IACpC,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,eAAe,CACnC,UAAkB,EAClB,OAAgB;IAEhB,IAAI,CAAC;QACH,sBAAY,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;QACzC,iEAAiE;QACjE,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC;QAC9E,MAAM,MAAM,GAAG,yBAAa,YAAY,uCAAC,CAAC;QAC1C,OAAO,MAAM,CAAC;IAChB,CAAC;YAAS,CAAC;QACT,sBAAY,CAAC,kBAAkB,EAAE,CAAC;IACpC,CAAC;AACH,CAAC"}
|
package/src/utils/ImportUtils.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { toolsService } from '../service/Service';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Import a module as global tools
|
|
5
|
-
* @param modulePath The path to the module to import
|
|
6
|
-
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
7
|
-
* @returns The imported module
|
|
8
|
-
*/
|
|
9
|
-
export async function importAsGlobal<T = any>(
|
|
10
|
-
modulePath: string,
|
|
11
|
-
baseUrl?: string
|
|
12
|
-
): Promise<T> {
|
|
13
|
-
try {
|
|
14
|
-
toolsService.setImportContext('global');
|
|
15
|
-
// If baseUrl is provided, resolve the module path relative to it
|
|
16
|
-
// Otherwise, use the modulePath as-is
|
|
17
|
-
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
18
|
-
const module = await import(resolvedPath);
|
|
19
|
-
return module;
|
|
20
|
-
} finally {
|
|
21
|
-
toolsService.clearImportContext();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Import a module as regular tools
|
|
27
|
-
* @param modulePath The path to the module to import
|
|
28
|
-
* @param baseUrl The base URL for resolving relative imports (optional)
|
|
29
|
-
* @returns The imported module
|
|
30
|
-
*/
|
|
31
|
-
export async function importAsRegular<T = any>(
|
|
32
|
-
modulePath: string,
|
|
33
|
-
baseUrl?: string
|
|
34
|
-
): Promise<T> {
|
|
35
|
-
try {
|
|
36
|
-
toolsService.setImportContext('regular');
|
|
37
|
-
// If baseUrl is provided, resolve the module path relative to it
|
|
38
|
-
// Otherwise, use the modulePath as-is
|
|
39
|
-
const resolvedPath = baseUrl ? new URL(modulePath, baseUrl).href : modulePath;
|
|
40
|
-
const module = await import(resolvedPath);
|
|
41
|
-
return module;
|
|
42
|
-
} finally {
|
|
43
|
-
toolsService.clearImportContext();
|
|
44
|
-
}
|
|
45
|
-
}
|