@intuned/runtime-dev 1.0.6-cli-auth.0.0.6-test → 1.0.6-cli-auth.0.0.8-test

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.
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require("../dist/commands/cli-auth-sessions/check.js");
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -1 +1,45 @@
1
- "use strict";
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ var _commander = require("commander");
5
+ var fs = _interopRequireWildcard(require("fs-extra"));
6
+ var _dotenv = _interopRequireDefault(require("dotenv"));
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _utils = require("./utils");
9
+ var _chalk = _interopRequireDefault(require("chalk"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
12
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
13
+ _dotenv.default.config({
14
+ path: `.env`
15
+ });
16
+ _commander.program.description("Check an auth session, if it is still valid or not").option("-a, --auth-session <session>", "Name of the auth session instance to use").action(async options => {
17
+ try {
18
+ if (!options.authSession) {
19
+ throw new Error("Auth session path is required");
20
+ }
21
+ const authSessionInstancePath = _path.default.resolve(process.cwd(), options.authSession);
22
+ if (!(await fs.exists(authSessionInstancePath))) {
23
+ throw new Error(`Auth session file not found at ${authSessionInstancePath}, please provide a valid path`);
24
+ }
25
+ const _isAuthEnabled = await (0, _utils.isAuthEnabled)();
26
+ if (!_isAuthEnabled) {
27
+ throw new Error("Auth session is not enabled, enable it in Intuned.json to be able to use it");
28
+ }
29
+ const checkApiExists = await (0, _utils.ensureAuthApi)("check");
30
+ if (!checkApiExists) {
31
+ throw new Error("Auth check creation API not implemented, please create it in the auth sessions specified directory");
32
+ }
33
+ const checkResult = await (0, _utils.runCheckApi)(authSessionInstancePath);
34
+ if (checkResult) {
35
+ console.log(_chalk.default.green("✓ Auth session checked successfully"));
36
+ } else {
37
+ console.log(_chalk.default.red("✗ Auth session is not valid, check failed"));
38
+ }
39
+ } catch (error) {
40
+ console.error(_chalk.default.red(`Failed to create auth session: ${error.message}`));
41
+ } finally {
42
+ process.exit(0);
43
+ }
44
+ });
45
+ _commander.program.parse(process.argv);
@@ -14,22 +14,39 @@ _dotenv.default.config({
14
14
  path: `.env`
15
15
  });
16
16
  _commander.program.description("Create an auth session").option("-i, --input <input>", "Auth session input parameters file").option("-n, --auth-session-name <output>", "Custom name for the auth session instance stored").action(async options => {
17
- let authSessionInput = {};
18
- if (options.input) {
19
- const authSessionInputFile = options.input;
20
- const authSessionInputPath = _path.default.join(process.cwd(), authSessionInputFile);
21
- authSessionInput = await fs.readJSON(authSessionInputPath);
17
+ try {
18
+ const _isAuthEnabled = await (0, _utils.isAuthEnabled)();
19
+ if (!_isAuthEnabled) {
20
+ throw new Error("Auth session is not enabled, enable it in Intuned.json to be able to use it");
21
+ }
22
+ const createApiExists = await (0, _utils.ensureAuthApi)("create");
23
+ if (!createApiExists) {
24
+ throw new Error("Auth session creation API not implemented, please create it in the auth sessions specified directory");
25
+ }
26
+ let authSessionInput = {};
27
+ if (options.input) {
28
+ const inputFilePath = _path.default.resolve(process.cwd(), options.input);
29
+ if (!(await fs.exists(inputFilePath))) {
30
+ throw new Error(`Auth session input file not found at ${inputFilePath}`);
31
+ }
32
+ const authSessionInputFile = options.input;
33
+ const authSessionInputPath = _path.default.join(process.cwd(), authSessionInputFile);
34
+ authSessionInput = await fs.readJSON(authSessionInputPath);
35
+ }
36
+ const session = await (0, _utils.runCreateApi)(authSessionInput);
37
+ if (!session) {
38
+ console.error(_chalk.default.red("Failed to create auth session."));
39
+ process.exit(1);
40
+ }
41
+ const authSessionInstancePath = await (0, _utils.storeAuthSessionInstance)(session, options.authSessionName, authSessionInput);
42
+ console.log(_chalk.default.green("✓ Auth session created successfully!"));
43
+ if (authSessionInstancePath) {
44
+ console.log(_chalk.default.underline.green.white(`🔒 Auth session instance and metadata stored at ${authSessionInstancePath}`));
45
+ }
46
+ } catch (error) {
47
+ console.error(_chalk.default.red(`Failed to create auth session: ${error.message}`));
48
+ } finally {
49
+ process.exit(0);
22
50
  }
23
- const session = await (0, _utils.runCreateApi)(authSessionInput);
24
- if (!session) {
25
- console.error(_chalk.default.red("Failed to create auth session."));
26
- process.exit(1);
27
- }
28
- const authSessionInstancePath = await (0, _utils.storeAuthSessionInstace)(session, options.authSessionName);
29
- console.log(_chalk.default.green("✓ Auth session created successfully!"));
30
- if (authSessionInstancePath) {
31
- console.log(_chalk.default.underline.green.white(`🔒 Auth session instance stored at ${authSessionInstancePath}`));
32
- }
33
- process.exit(0);
34
51
  });
35
52
  _commander.program.parse(process.argv);
@@ -1,5 +1,9 @@
1
1
  import { StorageState } from "../../common/contextStorageStateHelpers";
2
2
  export declare function isAuthEnabled(): Promise<boolean>;
3
- export declare function ensureCreateApi(): Promise<boolean>;
4
- export declare function runCreateApi(authSessionInput: Record<string, any>): Promise<any>;
5
- export declare function storeAuthSessionInstace(authSessionInstance: StorageState, customName?: string): Promise<string>;
3
+ export declare function ensureAuthApi(operation: "create" | "check"): Promise<boolean>;
4
+ export declare function runCreateApi(authSessionInput: Record<string, any>): Promise<StorageState>;
5
+ export declare function runCheckApi(authSessionPath: string): Promise<boolean>;
6
+ export declare function runCreateApiViaCLI(authSessionInput: Record<string, any>): Promise<StorageState>;
7
+ export declare function runCheckApiViaCLI(authSessionPath: string): Promise<boolean>;
8
+ export declare function storeAuthSessionInstance(authSessionInstance: StorageState, customName?: string, authSessionInput?: Record<string, any>): Promise<string>;
9
+ export declare function retrieveAuthSessionInstance(): Promise<void>;
@@ -3,10 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.ensureCreateApi = ensureCreateApi;
6
+ exports.ensureAuthApi = ensureAuthApi;
7
7
  exports.isAuthEnabled = isAuthEnabled;
8
+ exports.retrieveAuthSessionInstance = retrieveAuthSessionInstance;
9
+ exports.runCheckApi = runCheckApi;
10
+ exports.runCheckApiViaCLI = runCheckApiViaCLI;
8
11
  exports.runCreateApi = runCreateApi;
9
- exports.storeAuthSessionInstace = storeAuthSessionInstace;
12
+ exports.runCreateApiViaCLI = runCreateApiViaCLI;
13
+ exports.storeAuthSessionInstance = storeAuthSessionInstance;
10
14
  var _path = _interopRequireDefault(require("path"));
11
15
  var _constants = require("../../common/constants");
12
16
  var fs = _interopRequireWildcard(require("fs-extra"));
@@ -16,6 +20,7 @@ var _nanoid = require("nanoid");
16
20
  var _runApi = require("../../common/runApi");
17
21
  var _tsNodeImport = require("../common/tsNodeImport");
18
22
  var _promptly = require("promptly");
23
+ var _utils = require("../../common/cli/utils");
19
24
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
20
25
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
21
26
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -28,24 +33,37 @@ async function isAuthEnabled() {
28
33
  return false;
29
34
  }
30
35
  }
31
- async function ensureCreateApi() {
32
- const createApiFile = `${_constants.AUTH_SESSIONS_FOLDER_NAME}/create.ts`;
36
+ async function ensureAuthApi(operation) {
37
+ const createApiFile = `${_constants.AUTH_SESSIONS_FOLDER_NAME}/${operation}.ts`;
33
38
  const createApiFilePath = _path.default.join(process.cwd(), createApiFile);
34
39
  return await fs.exists(createApiFilePath);
35
40
  }
36
41
  async function runCreateApi(authSessionInput) {
37
42
  try {
38
- const createApiRunId = (0, _nanoid.nanoid)();
43
+ const createApiOperationId = (0, _nanoid.nanoid)();
39
44
  const authSessionInstance = await (0, _asyncLocalStorage.runWithContext)({
40
45
  runEnvironment: _enums.RunEnvironment.IDE,
41
46
  extendedPayloads: [],
42
- runId: createApiRunId
47
+ runId: createApiOperationId
43
48
  }, () => runCreateApiViaCLI(authSessionInput));
44
49
  return authSessionInstance;
45
50
  } catch (error) {
46
51
  throw new Error(`Error running the create API: ${error.message}`);
47
52
  }
48
53
  }
54
+ async function runCheckApi(authSessionPath) {
55
+ try {
56
+ const checkApiOperationId = (0, _nanoid.nanoid)();
57
+ const checkResult = await (0, _asyncLocalStorage.runWithContext)({
58
+ runEnvironment: _enums.RunEnvironment.IDE,
59
+ extendedPayloads: [],
60
+ runId: checkApiOperationId
61
+ }, () => runCheckApiViaCLI(authSessionPath));
62
+ return checkResult;
63
+ } catch (error) {
64
+ throw new Error(`Error running the create API: ${error.message}`);
65
+ }
66
+ }
49
67
  async function runCreateApiViaCLI(authSessionInput) {
50
68
  const createApiName = `${_constants.AUTH_SESSIONS_FOLDER_NAME}/create`;
51
69
  const generator = (0, _runApi.runApiGenerator)({
@@ -93,17 +111,68 @@ async function runCreateApiViaCLI(authSessionInput) {
93
111
  }
94
112
  return session;
95
113
  }
96
- async function storeAuthSessionInstace(authSessionInstance, customName) {
114
+ async function runCheckApiViaCLI(authSessionPath) {
115
+ const runApiResult = await (0, _runApi.runApi)({
116
+ automationFunction: {
117
+ name: `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`
118
+ },
119
+ runOptions: {
120
+ headless: false,
121
+ environment: "standalone"
122
+ },
123
+ auth: {
124
+ session: {
125
+ type: "file",
126
+ path: authSessionPath
127
+ },
128
+ runCheck: false
129
+ },
130
+ importFunction: _tsNodeImport.tsNodeImport
131
+ });
132
+ if (runApiResult.isErr()) {
133
+ if (runApiResult.error instanceof _runApi.AutomationError) {
134
+ throw runApiResult.error.error;
135
+ }
136
+ console.error(runApiResult.error);
137
+ throw new Error("Error running auth session check");
138
+ }
139
+ const result = runApiResult.value.result;
140
+ if (!result) {
141
+ throw new Error("Auth session check failed");
142
+ }
143
+ return result;
144
+ }
145
+ async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput) {
97
146
  try {
98
147
  const authSessionsDirectoryPath = _path.default.join(process.cwd(), _constants.AUTH_SESSIONS_INSTANCES_FOLDER_NAME);
99
148
  await fs.ensureDir(authSessionsDirectoryPath);
100
- const authSessionInstanceFileName = customName ?? `auth-session-${Date.now()}`;
101
- const authSessionInstanceFilePath = _path.default.join(authSessionsDirectoryPath, `${authSessionInstanceFileName}.json`);
102
- await fs.writeJSON(authSessionInstanceFilePath, authSessionInstance, {
149
+ const authSessionInstanceId = customName ?? `auth-session-${Date.now()}`;
150
+ const authSessionInstancePath = _path.default.join(authSessionsDirectoryPath, authSessionInstanceId);
151
+ const authSessionInstanceStoragePath = _path.default.join(authSessionInstancePath, `auth-session.json`);
152
+ await fs.ensureDir(authSessionInstanceStoragePath);
153
+ await fs.writeJSON(authSessionInstanceStoragePath, authSessionInstance, {
103
154
  spaces: 2
104
155
  });
105
- return authSessionInstanceFilePath;
156
+ const projectAuthConfig = await (0, _utils.getSettingIntunedJSON)("authSessions");
157
+ const authSessionMetadata = {
158
+ createdAt: new Date().toISOString(),
159
+ updatedAt: new Date().toISOString(),
160
+ authSessionInput: authSessionInput ?? {},
161
+ authSessionId: authSessionInstanceId,
162
+ authSessionType: projectAuthConfig.type ?? "API",
163
+ ...(projectAuthConfig.type === "MANUAL" && {
164
+ recorderStartUrl: projectAuthConfig.startUrl,
165
+ recorderEndUrl: projectAuthConfig.endUrl
166
+ })
167
+ };
168
+ const authSessionInstanceMetadataPath = _path.default.join(authSessionInstancePath, `metadata.json`);
169
+ await fs.ensureDir(authSessionInstanceMetadataPath);
170
+ await fs.writeJSON(authSessionInstanceMetadataPath, authSessionMetadata, {
171
+ spaces: 2
172
+ });
173
+ return authSessionInstancePath;
106
174
  } catch (error) {
107
175
  throw new Error(`Error storing auth session instance: ${error.message}`);
108
176
  }
109
- }
177
+ }
178
+ async function retrieveAuthSessionInstance() {}
@@ -12,7 +12,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
12
12
  _dotenv.default.config({
13
13
  path: `.env`
14
14
  });
15
- _commander.program.name("intuned-run").description("Run an Intuned API with parameters").argument("<api-name>", "Name of the API to run").option("-i, --parameters-file <file>", "JSON file containing API parameters").option("-a, --auth-session <session>", "Auth session path").option("-s, --store-results", "Store the results of the running API in a dedicated folder").action(async (apiName, options) => {
15
+ _commander.program.name("intuned-run").description("Run an Intuned API with parameters").argument("<api-name>", "Name of the API to run").option("-i, --parameters-file <file>", "JSON file containing API parameters").option("-a, --auth-session <session>", "Name of the auth session instance to use").option("-s, --store-results", "Store the results of the running API in a dedicated folder").action(async (apiName, options) => {
16
16
  try {
17
17
  if (!apiName) {
18
18
  throw new Error("API name is required, please provide it");
@@ -1,8 +1,10 @@
1
1
  import { Payload } from "../../runtime/export";
2
+ import { ApiAuthSessionBehavior } from "../../common/cli/types";
2
3
  export declare function loadParameters(parametersFile: string): Promise<object | null>;
3
4
  export declare function writeResultToFile(runId: string, result: any, payloadToAppend?: Payload[]): Promise<void>;
4
5
  export declare function runApiViaCLI(apiName: string, inputData: object | null | undefined, options?: {
5
6
  authSessionPath?: string;
7
+ authSessionsMode?: ApiAuthSessionBehavior;
6
8
  }): Promise<{
7
9
  result: any;
8
10
  payloadToAppend: Payload[] | undefined;
@@ -13,6 +13,8 @@ var _chalk = _interopRequireDefault(require("chalk"));
13
13
  var _runApi = require("../../common/runApi");
14
14
  var _tsNodeImport = require("../common/tsNodeImport");
15
15
  var _utils = require("../../common/cli/utils");
16
+ var _types = require("../../common/cli/types");
17
+ var _utils2 = require("../cli-auth-sessions/utils");
16
18
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
17
19
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
18
20
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -62,6 +64,7 @@ async function writeResultToFile(runId, result, payloadToAppend) {
62
64
  }
63
65
  async function runApiViaCLI(apiName, inputData, options) {
64
66
  let authSessionPathToUse = null;
67
+ const authSessionsMode = (options === null || options === void 0 ? void 0 : options.authSessionsMode) ?? "PERFORM_CHECK_AND_REFRESH";
65
68
  if (options !== null && options !== void 0 && options.authSessionPath) {
66
69
  const authSessions = await (0, _utils.getSettingIntunedJSON)("authSessions");
67
70
  if (authSessions.enabled) {
@@ -75,6 +78,20 @@ async function runApiViaCLI(apiName, inputData, options) {
75
78
  }
76
79
  }
77
80
  }
81
+ if (authSessionsMode === _types.ApiAuthSessionBehavior.PERFORM_CHECK_AND_REFRESH && authSessionPathToUse) {
82
+ const checkApiExists = await (0, _utils2.ensureAuthApi)("check");
83
+ if (!checkApiExists) {
84
+ throw new Error("Auth session check API not implemented, please create it in the auth sessions specified directory");
85
+ }
86
+ const checkResult = await (0, _utils2.runCheckApiViaCLI)(authSessionPathToUse);
87
+ if (!checkResult) {
88
+ console.log(_chalk.default.yellow("Auth session check failed, trying to refresh it..."));
89
+ const createApiExists = await (0, _utils2.ensureAuthApi)("create");
90
+ if (!createApiExists) {
91
+ throw new Error("Auth session creation API not implemented, please create it in the auth sessions specified directory");
92
+ }
93
+ }
94
+ }
78
95
  const runApiResult = await (0, _runApi.runApi)({
79
96
  automationFunction: {
80
97
  name: `api/${apiName}`,
@@ -89,7 +106,7 @@ async function runApiViaCLI(apiName, inputData, options) {
89
106
  type: "file",
90
107
  path: authSessionPathToUse
91
108
  },
92
- runCheck: true
109
+ runCheck: false
93
110
  } : undefined,
94
111
  importFunction: _tsNodeImport.tsNodeImport
95
112
  });
@@ -50,3 +50,7 @@ export interface AuthCredentials {
50
50
  workspaceId: string;
51
51
  apiKey: string;
52
52
  }
53
+ export declare enum ApiAuthSessionBehavior {
54
+ PERFORM_CHECK_AND_REFRESH = "PERFORM_CHECK_AND_REFRESH",
55
+ SKIP_CHECK_AND_REFRESH = "SKIP_CHECK_AND_REFRESH"
56
+ }
@@ -3,5 +3,10 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.templateIds = void 0;
7
- const templateIds = exports.templateIds = ["default", "empty", "linkedin-recorder", "api-auth-sessions", "nested-scheduling", "ai-extractors", "npm-auth-sessions"];
6
+ exports.templateIds = exports.ApiAuthSessionBehavior = void 0;
7
+ const templateIds = exports.templateIds = ["default", "empty", "linkedin-recorder", "api-auth-sessions", "nested-scheduling", "ai-extractors", "npm-auth-sessions"];
8
+ let ApiAuthSessionBehavior = exports.ApiAuthSessionBehavior = function (ApiAuthSessionBehavior) {
9
+ ApiAuthSessionBehavior["PERFORM_CHECK_AND_REFRESH"] = "PERFORM_CHECK_AND_REFRESH";
10
+ ApiAuthSessionBehavior["SKIP_CHECK_AND_REFRESH"] = "SKIP_CHECK_AND_REFRESH";
11
+ return ApiAuthSessionBehavior;
12
+ }({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.0.6-cli-auth.0.0.6-test",
3
+ "version": "1.0.6-cli-auth.0.0.8-test",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -54,7 +54,8 @@
54
54
  "run-api": "./bin/run-api",
55
55
  "deploy": "./bin/deploy",
56
56
  "cli-build": "./bin/cli-build",
57
- "create-auth-session": "./bin/create-auth-session"
57
+ "create-auth-session": "./bin/create-auth-session",
58
+ "check-auth-session": "./bin/check-auth-session"
58
59
  },
59
60
  "dependencies": {
60
61
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",