@microsoft/rayfin-functions 1.20.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) Microsoft Corporation.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @microsoft/rayfin-functions
2
+
3
+ ## Security
4
+
5
+ Microsoft takes the security of our software products and services seriously, which
6
+ includes all source code repositories in our GitHub organizations.
7
+
8
+ **Please do not report security vulnerabilities through public GitHub issues.**
9
+
10
+ For security reporting information, locations, contact information, and policies,
11
+ please review the latest guidance for Microsoft repositories at
12
+ [https://aka.ms/SECURITY.md](https://aka.ms/SECURITY.md).
13
+
14
+ ## Trademarks
15
+
16
+ This project may contain trademarks or logos for projects, products, or services.
17
+ Authorized use of Microsoft trademarks or logos must follow the [Microsoft Trademark and Brand Guidelines](https://www.microsoft.com/legal/intellectualproperty/trademarks/usage/general).
18
+ Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
19
+ Any use of third-party trademarks or logos is subject to those third parties' policies.
20
+
21
+ ## License
22
+
23
+ Copyright (c) Microsoft Corporation.
24
+
25
+ MIT License
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy
28
+ of this software and associated documentation files (the "Software"), to deal
29
+ in the Software without restriction, including without limitation the rights
30
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ copies of the Software, and to permit persons to whom the Software is
32
+ furnished to do so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in all
35
+ copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
+ SOFTWARE.
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @fileoverview Functions API client for invoking serverless functions.
3
+ * This file provides an API for invoking serverless functions through the Rayfin platform.
4
+ */
5
+ import { ApiClient, SdkError, NetworkError } from '@microsoft/rayfin-lib';
6
+ /**
7
+ * Functions error specific to the Rayfin SDK.
8
+ */
9
+ export declare class FunctionsError extends SdkError {
10
+ constructor(message: string, code?: string);
11
+ }
12
+ /**
13
+ * Parameters for a function invocation request.
14
+ */
15
+ export interface FunctionInvocationParams {
16
+ /**
17
+ * The name of the function to invoke.
18
+ */
19
+ functionName: string;
20
+ /**
21
+ * Optional parameters to pass to the function.
22
+ */
23
+ parameters?: Record<string, any>;
24
+ /**
25
+ * Optional headers to include with the invocation request.
26
+ */
27
+ headers?: Record<string, string>;
28
+ }
29
+ /**
30
+ * Response from a function invocation.
31
+ */
32
+ export interface FunctionInvocationResponse<T = any> {
33
+ /**
34
+ * The name of the function that was invoked.
35
+ */
36
+ functionName: string;
37
+ /**
38
+ * A unique identifier for this invocation.
39
+ */
40
+ invocationId: string;
41
+ /**
42
+ * Status of the function invocation (Success, Failed, etc.)
43
+ */
44
+ status: string;
45
+ /**
46
+ * The output from the function. This can be a string containing JSON or any other data.
47
+ * When it's a JSON string, it will typically have its own nested structure with functionName,
48
+ * invocationId, status, output, and errors fields.
49
+ */
50
+ output: string | T;
51
+ /**
52
+ * Any errors that occurred during the function invocation.
53
+ */
54
+ errors: Array<string | Record<string, any>>;
55
+ }
56
+ /**
57
+ * Main Functions class for Rayfin.
58
+ * This class provides the high-level API for invoking serverless functions.
59
+ */
60
+ export declare class FunctionsApi {
61
+ private apiClient;
62
+ static readonly errors: {
63
+ FunctionsError: typeof FunctionsError;
64
+ NetworkError: typeof NetworkError;
65
+ SdkError: typeof SdkError;
66
+ };
67
+ /**
68
+ * @param apiClient An instance of ApiClient to be used for API requests.
69
+ */
70
+ constructor(apiClient: ApiClient);
71
+ /**
72
+ * Helper method to parse the output field of a function response when it contains a JSON string.
73
+ *
74
+ * @param response The function invocation response
75
+ * @returns The parsed output object, or the original output if it's not valid JSON
76
+ */
77
+ parseOutput<T = any, R = any>(response: FunctionInvocationResponse<T>): R | string | T;
78
+ /**
79
+ * Invokes a serverless function with the given parameters.
80
+ *
81
+ * @param params The function invocation parameters
82
+ * @returns A promise that resolves with the function invocation response
83
+ * @throws {FunctionsError} If the function invocation fails
84
+ * @throws {NetworkError} For network-related issues
85
+ * @throws {SdkError} For any other unexpected SDK errors
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const response = await functions.invoke({
90
+ * functionName: 'processOrder',
91
+ * parameters: {
92
+ * orderId: '12345',
93
+ * amount: 99.99
94
+ * }
95
+ * });
96
+ *
97
+ * // Direct access to the response
98
+ * console.log(response.status);
99
+ * console.log(response.invocationId);
100
+ *
101
+ * // Parse the output field if it contains JSON
102
+ * const parsedOutput = functions.parseOutput(response);
103
+ * console.log(parsedOutput); // Will contain the actual result
104
+ * ```
105
+ */
106
+ invoke<T = any>(params: FunctionInvocationParams): Promise<FunctionInvocationResponse<T>>;
107
+ }
108
+ //# sourceMappingURL=Functions.d.ts.map
@@ -0,0 +1,129 @@
1
+ /**
2
+ * @fileoverview Functions API client for invoking serverless functions.
3
+ * This file provides an API for invoking serverless functions through the Rayfin platform.
4
+ */
5
+ import { SdkError, NetworkError } from '@microsoft/rayfin-lib';
6
+ import { FUNCTIONS_INVOKE_PATH } from '@microsoft/rayfin-lib';
7
+ /**
8
+ * Functions error specific to the Rayfin SDK.
9
+ */
10
+ export class FunctionsError extends SdkError {
11
+ constructor(message, code) {
12
+ super(message, code || 'FUNCTIONS_ERROR');
13
+ }
14
+ }
15
+ /**
16
+ * Main Functions class for Rayfin.
17
+ * This class provides the high-level API for invoking serverless functions.
18
+ */
19
+ export class FunctionsApi {
20
+ apiClient;
21
+ // Static access to custom errors for easy import by consumers
22
+ static errors = {
23
+ FunctionsError,
24
+ NetworkError,
25
+ SdkError,
26
+ };
27
+ /**
28
+ * @param apiClient An instance of ApiClient to be used for API requests.
29
+ */
30
+ constructor(apiClient) {
31
+ this.apiClient = apiClient;
32
+ }
33
+ /**
34
+ * Helper method to parse the output field of a function response when it contains a JSON string.
35
+ *
36
+ * @param response The function invocation response
37
+ * @returns The parsed output object, or the original output if it's not valid JSON
38
+ */
39
+ parseOutput(response) {
40
+ if (typeof response.output === 'string') {
41
+ try {
42
+ // Try to parse the output as JSON
43
+ const parsedOutput = JSON.parse(response.output);
44
+ // If the parsed output has its own 'output' field, return that
45
+ if (parsedOutput &&
46
+ typeof parsedOutput === 'object' &&
47
+ 'output' in parsedOutput) {
48
+ return parsedOutput.output;
49
+ }
50
+ // Otherwise return the entire parsed object
51
+ return parsedOutput;
52
+ }
53
+ catch (e) {
54
+ // If parsing fails, return the original string
55
+ return response.output;
56
+ }
57
+ }
58
+ // If output is not a string, return it as is
59
+ return response.output;
60
+ }
61
+ /**
62
+ * Invokes a serverless function with the given parameters.
63
+ *
64
+ * @param params The function invocation parameters
65
+ * @returns A promise that resolves with the function invocation response
66
+ * @throws {FunctionsError} If the function invocation fails
67
+ * @throws {NetworkError} For network-related issues
68
+ * @throws {SdkError} For any other unexpected SDK errors
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const response = await functions.invoke({
73
+ * functionName: 'processOrder',
74
+ * parameters: {
75
+ * orderId: '12345',
76
+ * amount: 99.99
77
+ * }
78
+ * });
79
+ *
80
+ * // Direct access to the response
81
+ * console.log(response.status);
82
+ * console.log(response.invocationId);
83
+ *
84
+ * // Parse the output field if it contains JSON
85
+ * const parsedOutput = functions.parseOutput(response);
86
+ * console.log(parsedOutput); // Will contain the actual result
87
+ * ```
88
+ */
89
+ async invoke(params) {
90
+ try {
91
+ // Basic validation
92
+ if (!params.functionName) {
93
+ throw new FunctionsError('Function name is required for invocation', 'MISSING_FUNCTION_NAME');
94
+ }
95
+ const { functionName, parameters, headers } = params;
96
+ // Prepare the request payload
97
+ const payload = {
98
+ functionName,
99
+ parameters: parameters || {},
100
+ };
101
+ // Make the POST request to the functions/invoke endpoint
102
+ const response = await this.apiClient.post(FUNCTIONS_INVOKE_PATH, payload, { headers });
103
+ // Check for function errors in the response
104
+ if (response.errors && response.errors.length > 0) {
105
+ const errorMessage = typeof response.errors[0] === 'string'
106
+ ? response.errors[0]
107
+ : JSON.stringify(response.errors[0]);
108
+ throw new FunctionsError(`Function invocation failed: ${errorMessage}`, 'FUNCTION_EXECUTION_ERROR');
109
+ }
110
+ // Check for failed status
111
+ if (response.status.toLowerCase() !== 'success' &&
112
+ response.status.toLowerCase() !== 'succeeded') {
113
+ throw new FunctionsError(`Function invocation failed with status: ${response.status}`, 'FUNCTION_EXECUTION_ERROR');
114
+ }
115
+ return response;
116
+ }
117
+ catch (error) {
118
+ // Re-throw specific SDK errors
119
+ if (error instanceof FunctionsError ||
120
+ error instanceof NetworkError ||
121
+ error instanceof SdkError) {
122
+ throw error;
123
+ }
124
+ // Wrap other errors in a FunctionsError
125
+ throw new FunctionsError(`An unexpected error occurred during function invocation: ${error.message || error}`, 'UNKNOWN_FUNCTION_ERROR');
126
+ }
127
+ }
128
+ }
129
+ //# sourceMappingURL=Functions.js.map
@@ -0,0 +1,2 @@
1
+ export { FunctionsApi } from './Functions';
2
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { FunctionsApi } from './Functions';
2
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@microsoft/rayfin-functions",
3
+ "version": "1.20.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**/*.js",
9
+ "dist/**/*.d.ts",
10
+ "!dist/**/__tests__/**",
11
+ "LICENSE"
12
+ ],
13
+ "devDependencies": {
14
+ "eslint": "^9.28.0",
15
+ "prettier": "^3.5.3",
16
+ "typescript": "^5.8.3",
17
+ "vitest": "^3.2.3",
18
+ "@vitest/coverage-v8": "~3.2.4",
19
+ "rimraf": "~6.0.1"
20
+ },
21
+ "dependencies": {
22
+ "@microsoft/rayfin-lib": "1.20.0"
23
+ },
24
+ "publishConfig": {
25
+ "registry": "https://npm.pkg.github.com",
26
+ "access": "restricted"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/microsoft/project-rayfin.git",
31
+ "directory": "packages/typescript-sdk/functions"
32
+ },
33
+ "keywords": [],
34
+ "author": "",
35
+ "license": "MIT",
36
+ "type": "module",
37
+ "scripts": {
38
+ "build": "tsc",
39
+ "build:watch": "tsc --watch",
40
+ "clean": "rimraf dist && rimraf .tsbuildinfo",
41
+ "test": "vitest run"
42
+ }
43
+ }