Skip to content

Commit 10e4bfb

Browse files
committed
feat: properly allow using custom API backend
This is mainly a QoL feature for Apify core devs. Some stubs of support was there for login, this PR makes it actually work
1 parent 5ea47ca commit 10e4bfb

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/commands/login.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ import { updateUserId } from '../lib/hooks/telemetry/useTelemetryState.js';
1616
import { useMaskedInput } from '../lib/hooks/user-confirmations/useMaskedInput.js';
1717
import { useSelectFromList } from '../lib/hooks/user-confirmations/useSelectFromList.js';
1818
import { error, info, success } from '../lib/outputs.js';
19-
import { getLocalUserInfo, getLoggedClient, tildify } from '../lib/utils.js';
19+
import { getApifyAPIBaseUrl, getLocalUserInfo, getLoggedClient, tildify } from '../lib/utils.js';
2020

21-
const CONSOLE_BASE_URL = 'https://console.apify.com/settings/integrations';
22-
// const CONSOLE_BASE_URL = 'http://localhost:3000/settings/integrations';
21+
const CONSOLE_BASE_URL = getApifyAPIBaseUrl()?.includes('localhost')
22+
? 'http://localhost:3000/settings/integrations'
23+
: 'https://console.apify.com/settings/integrations';
2324
const CONSOLE_URL_ORIGIN = new URL(CONSOLE_BASE_URL).origin;
2425

25-
const API_BASE_URL = CONSOLE_BASE_URL.includes('localhost') ? 'http://localhost:3333' : undefined;
26-
2726
// Not really checked right now, but it might come useful if we ever need to do some breaking changes
2827
const API_VERSION = 'v1';
2928

3029
const tryToLogin = async (token: string) => {
31-
const isUserLogged = await getLoggedClient(token, API_BASE_URL);
30+
const isUserLogged = await getLoggedClient(token);
3231
const userInfo = await getLocalUserInfo();
3332

3433
if (isUserLogged) {

src/lib/utils.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
SUPPORTED_NODEJS_VERSION,
4545
} from './consts.js';
4646
import { deleteFile, ensureFolderExistsSync, rimrafPromised } from './files.js';
47+
import { warning } from './outputs.js';
4748
import type { AuthJSON } from './types.js';
4849

4950
// Export AJV properly: https://github.com/ajv-validator/ajv/issues/2132
@@ -92,6 +93,22 @@ export const getLocalRequestQueuePath = (storeId?: string) => {
9293
return join(getLocalStorageDir(), LOCAL_STORAGE_SUBDIRS.requestQueues, storeDir);
9394
};
9495

96+
let hasLoggedAPIBaseUrlDeprecation = false;
97+
export const getApifyAPIBaseUrl = () => {
98+
const envVar = APIFY_ENV_VARS.API_BASE_URL;
99+
100+
const legacyVar = 'APIFY_CLIENT_BASE_URL';
101+
if (process.env[legacyVar]) {
102+
if (!hasLoggedAPIBaseUrlDeprecation) {
103+
warning({ message: `Environment variable '${legacyVar}' is deprecated. Please use '${envVar}' instead.` });
104+
hasLoggedAPIBaseUrlDeprecation = true;
105+
}
106+
return process.env[legacyVar];
107+
}
108+
109+
return process.env[envVar];
110+
};
111+
95112
/**
96113
* Returns object from auth file or empty object.
97114
*/
@@ -144,7 +161,7 @@ export const getApifyClientOptions = (token?: string, apiBaseUrl?: string): Apif
144161

145162
return {
146163
token,
147-
baseUrl: apiBaseUrl || process.env.APIFY_CLIENT_BASE_URL,
164+
baseUrl: apiBaseUrl || getApifyAPIBaseUrl(),
148165
requestInterceptors: [
149166
(config) => {
150167
config.headers ??= new AxiosHeaders() as CJSAxiosHeaders;
@@ -376,7 +393,7 @@ export const outputJobLog = async ({
376393
apifyClient?: ApifyClient;
377394
}) => {
378395
const { id: logId, status } = job;
379-
const client = apifyClient || new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
396+
const client = apifyClient || new ApifyClient({ baseUrl: getApifyAPIBaseUrl() });
380397

381398
// In case job was already done just output log
382399
if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) {

0 commit comments

Comments
 (0)