base44-etoro-backend-functions-infra 1.0.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.
Files changed (3) hide show
  1. package/README.md +58 -0
  2. package/index.js +52 -0
  3. package/package.json +23 -0
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # base44-etoro-backend-functions-infra
2
+
3
+ eToro backend functions infrastructure for Base44, providing utilities for generating eToro API headers.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install base44-etoro-backend-functions-infra
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### In Deno
14
+
15
+ ```javascript
16
+ import { getEtoroHeaders } from 'npm:base44-etoro-backend-functions-infra';
17
+
18
+ // In your Deno.serve handler
19
+ const headers = await getEtoroHeaders(req, user);
20
+ ```
21
+
22
+ ### Options
23
+
24
+ The function accepts an optional third parameter for configuration:
25
+
26
+ ```javascript
27
+ const headers = await getEtoroHeaders(req, user, {
28
+ apiKey: 'your-api-key',
29
+ userKey: 'your-user-key'
30
+ });
31
+ ```
32
+
33
+ If options are not provided, the function will attempt to read from `Deno.env`:
34
+ - `ETORO_API_KEY`
35
+ - `ETORO_USER_KEY`
36
+
37
+ ## Function Details
38
+
39
+ ### `getEtoroHeaders(req, user, options?)`
40
+
41
+ Generates appropriate headers for eToro API requests.
42
+
43
+ **Parameters:**
44
+ - `req` (Request): The incoming request object
45
+ - `user` (Object): The authenticated user object with a `role` property
46
+ - `options` (Object, optional): Configuration options
47
+ - `apiKey` (string): eToro API key
48
+ - `userKey` (string): eToro user key
49
+
50
+ **Returns:**
51
+ - `Promise<Object>`: Headers object containing:
52
+ - `x-request-id`: UUID v4
53
+ - `Content-Type`: 'application/json'
54
+ - `Authorization`: Bearer token (if SSO token provided)
55
+ - `x-api-key` and `x-user-key`: API credentials (if admin role and no SSO token)
56
+
57
+ **Throws:**
58
+ - Error if user is not admin and no SSO token is provided
package/index.js ADDED
@@ -0,0 +1,52 @@
1
+ import { createClient } from '@base44/sdk';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+
4
+ /**
5
+ * Creates an eToro API client instance
6
+ * @param {string} appId - Base44 application ID from environment
7
+ * @returns {Object} Base44 client instance
8
+ */
9
+ function createBase44Client(appId) {
10
+ return createClient({
11
+ appId: appId,
12
+ });
13
+ }
14
+
15
+ /**
16
+ * Generates eToro API headers for requests
17
+ * @param {Request} req - The incoming request object
18
+ * @param {Object} user - The authenticated user object
19
+ * @param {Object} options - Configuration options
20
+ * @param {string} options.appId - Base44 application ID (defaults to Deno.env.get('BASE44_APP_ID'))
21
+ * @param {string} options.apiKey - eToro API key (defaults to Deno.env.get('ETORO_API_KEY'))
22
+ * @param {string} options.userKey - eToro user key (defaults to Deno.env.get('ETORO_USER_KEY'))
23
+ * @returns {Promise<Object>} Headers object for eToro API requests
24
+ */
25
+ export async function getEtoroHeaders(req, user, options = {}) {
26
+ const headers = {
27
+ 'x-request-id': uuidv4(),
28
+ 'Content-Type': 'application/json',
29
+ };
30
+
31
+ const ssoToken = req.headers.get('sso_authorization');
32
+ if (ssoToken) {
33
+ const arr = ssoToken.split(' ');
34
+ const token = arr.length > 1 ? arr[1] : arr[0];
35
+ headers['Authorization'] = `Bearer ${token}`;
36
+ } else {
37
+ if (user.role == 'admin') {
38
+ // Use provided options or fall back to environment variables
39
+ const apiKey = options.apiKey ?? (typeof Deno !== 'undefined' ? Deno.env.get('ETORO_API_KEY') : '');
40
+ const userKey = options.userKey ?? (typeof Deno !== 'undefined' ? Deno.env.get('ETORO_USER_KEY') : '');
41
+
42
+ headers['x-api-key'] = apiKey ?? '';
43
+ headers['x-user-key'] = userKey ?? '';
44
+ } else {
45
+ throw new Error('Unauthorized: Admin role required when SSO token is not provided');
46
+ }
47
+ }
48
+ return headers;
49
+ }
50
+
51
+ // Export the client creation function as well for convenience
52
+ export { createBase44Client };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "base44-etoro-backend-functions-infra",
3
+ "version": "1.0.0",
4
+ "description": "eToro backend functions infrastructure for Base44, including header generation utilities",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./index.js"
9
+ },
10
+ "keywords": [
11
+ "etoro",
12
+ "base44",
13
+ "backend",
14
+ "functions",
15
+ "infrastructure"
16
+ ],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "dependencies": {
20
+ "@base44/sdk": "^0.1.0",
21
+ "uuid": "^9.0.0"
22
+ }
23
+ }