Skip to content

Commit

Permalink
fix: use destructured object on some functions (NangoHQ#1941)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes NAN-675

In the process of migration to logs v2, I need to pass the new ctx to a
lot of functions and because we use classic parameters everywhere it's a
bit messy especially when there are some optional parameters too.
I didn't modified everything to avoid a too large PR.

- Use destructured object parameters where it would be painful to add a
new param
  • Loading branch information
bodinsamuel authored Apr 3, 2024
1 parent e289e81 commit 9a16171
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 228 deletions.
16 changes: 2 additions & 14 deletions packages/cli/lib/services/local-integration.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IntegrationServiceInterface, NangoIntegrationData, NangoProps, RunnerOutput } from '@nangohq/shared';
import type { IntegrationServiceInterface, RunScriptOptions, RunnerOutput } from '@nangohq/shared';
import { ActionError, NangoError, formatScriptError, NangoSync, localFileService } from '@nangohq/shared';
import * as vm from 'vm';
import * as url from 'url';
Expand All @@ -11,19 +11,7 @@ class IntegrationService implements IntegrationServiceInterface {
return;
}

async runScript(
syncName: string,
_syncId: string,
_activityLogId: number | undefined,
nangoProps: NangoProps,
_integrationData: NangoIntegrationData,
_environmentId: number,
_writeToDb: boolean,
isInvokedImmediately: boolean,
isWebhook: boolean,
optionalLoadLocation?: string,
input?: object
): Promise<RunnerOutput> {
async runScript({ syncName, nangoProps, isInvokedImmediately, isWebhook, optionalLoadLocation, input }: RunScriptOptions): Promise<RunnerOutput> {
try {
const nango = new NangoSync(nangoProps);
const script: string | null = localFileService.getIntegrationFile(syncName, optionalLoadLocation);
Expand Down
20 changes: 10 additions & 10 deletions packages/jobs/lib/crons/autoIdleDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export async function exec(): Promise<void> {

logger.info(`[autoidle] pausing ${sync.id}`);

const resTemporal = await syncClient.runSyncCommand(
sync.schedule_id,
sync.id,
SyncCommand.PAUSE,
activityLogId,
sync.environment_id,
sync.unique_key,
sync.connection_id,
sync.name
);
const resTemporal = await syncClient.runSyncCommand({
scheduleId: sync.schedule_id,
syncId: sync.id,
command: SyncCommand.PAUSE,
activityLogId: activityLogId,
environmentId: sync.environment_id,
providerConfigKey: sync.unique_key,
connectionId: sync.connection_id,
syncName: sync.name
});
if (isErr(resTemporal)) {
continue;
}
Expand Down
30 changes: 15 additions & 15 deletions packages/jobs/lib/integration.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from '@temporalio/activity';
import type { IntegrationServiceInterface, NangoIntegrationData, NangoProps, ServiceResponse } from '@nangohq/shared';
import type { IntegrationServiceInterface, RunScriptOptions, ServiceResponse } from '@nangohq/shared';
import { integrationFilesAreRemote, isCloud, isProd } from '@nangohq/utils/dist/environment/detection.js';
import { createActivityLogMessage, localFileService, remoteFileService, NangoError, formatScriptError, isOk } from '@nangohq/shared';
import type { Runner } from './runner/runner.js';
Expand Down Expand Up @@ -49,20 +49,20 @@ class IntegrationService implements IntegrationServiceInterface {
}
}

async runScript(
syncName: string,
syncId: string,
activityLogId: number | undefined,
nangoProps: NangoProps,
integrationData: NangoIntegrationData,
environmentId: number,
writeToDb: boolean,
isInvokedImmediately: boolean,
isWebhook: boolean,
optionalLoadLocation?: string,
input?: object,
temporalContext?: Context
): Promise<ServiceResponse<any>> {
async runScript({
syncName,
syncId,
activityLogId,
nangoProps,
integrationData,
environmentId,
writeToDb,
isInvokedImmediately,
isWebhook,
optionalLoadLocation,
input,
temporalContext
}: RunScriptOptions): Promise<ServiceResponse<any>> {
const span = tracer
.startSpan('runScript')
.setTag('accountId', nangoProps.accountId)
Expand Down
14 changes: 7 additions & 7 deletions packages/server/lib/controllers/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ class ConnectionController {
success,
error,
response: credentials
} = await connectionService.refreshCredentialsIfNeeded(
} = await connectionService.refreshCredentialsIfNeeded({
connection,
config,
template as ProviderTemplateOAuth2,
null,
environment.id,
providerConfig: config,
template: template as ProviderTemplateOAuth2,
activityLogId: null,
environment_id: environment.id,
instantRefresh,
LogActionEnum.TOKEN
);
logAction: LogActionEnum.TOKEN
});

if (!success) {
errorManager.errResFromNangoErr(res, error);
Expand Down
47 changes: 29 additions & 18 deletions packages/server/lib/controllers/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,18 @@ class OAuthController {
}

if (template.auth_mode === ProviderAuthModes.OAuth2) {
return this.oauth2Request(
template as ProviderTemplateOAuth2,
config,
return this.oauth2Request({
template: template as ProviderTemplateOAuth2,
providerConfig: config,
session,
res,
connectionConfig,
authorizationParams,
callbackUrl,
activityLogId as number,
environmentId,
activityLogId: activityLogId as number,
environment_id: environmentId,
userScope
);
});
} else if (template.auth_mode === ProviderAuthModes.App || template.auth_mode === ProviderAuthModes.Custom) {
const appCallBackUrl = getGlobalAppCallbackUrl();
return this.appRequest(template, config, session, res, authorizationParams, appCallBackUrl, activityLogId as number, environmentId);
Expand Down Expand Up @@ -517,18 +517,29 @@ class OAuthController {
}
}

private async oauth2Request(
template: ProviderTemplateOAuth2,
providerConfig: ProviderConfig,
session: OAuthSession,
res: Response,
connectionConfig: Record<string, string>,
authorizationParams: Record<string, string | undefined>,
callbackUrl: string,
activityLogId: number,
environment_id: number,
userScope?: string
) {
private async oauth2Request({
template,
providerConfig,
session,
res,
connectionConfig,
authorizationParams,
callbackUrl,
activityLogId,
environment_id,
userScope
}: {
template: ProviderTemplateOAuth2;
providerConfig: ProviderConfig;
session: OAuthSession;
res: Response;
connectionConfig: Record<string, string>;
authorizationParams: Record<string, string | undefined>;
callbackUrl: string;
activityLogId: number;
environment_id: number;
userScope?: string | undefined;
}) {
const oauth2Template = template;
const channel = session.webSocketClientId;
const providerConfigKey = session.providerConfigKey;
Expand Down
9 changes: 8 additions & 1 deletion packages/server/lib/controllers/onboarding.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,17 @@ class OnboardingController {
environment_id: environment.id,
operation_name: DEMO_ACTION_NAME
});

if (!activityLogId) {
throw new NangoError('failed_to_create_activity_log');
}
const actionResponse = await syncClient.triggerAction(connection, DEMO_ACTION_NAME, { title: req.body.title }, activityLogId, environment.id);
const actionResponse = await syncClient.triggerAction({
connection,
actionName: DEMO_ACTION_NAME,
input: { title: req.body.title },
activityLogId,
environment_id: environment.id
});

if (isErr(actionResponse)) {
void analytics.track(AnalyticsTypes.DEMO_5_ERR, account.id, { user_id: user.id });
Expand Down
117 changes: 81 additions & 36 deletions packages/server/lib/controllers/proxy.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ class ProxyController {
return;
}

await this.sendToHttpMethod(res, next, method as HTTP_VERB, proxyConfig, activityLogId as number, environment_id, isSync, isDryRun);
await this.sendToHttpMethod({
res,
method: method as HTTP_VERB,
configBody: proxyConfig,
activityLogId: activityLogId as number,
environment_id,
isSync,
isDryRun
});
} catch (error) {
const environmentId = getEnvironmentId(res);
const connectionId = req.get('Connection-Id') as string;
Expand Down Expand Up @@ -180,36 +188,63 @@ class ProxyController {
* @param {HTTP_VERB} method
* @param {ApplicationConstructedProxyConfiguration} configBody
*/
private sendToHttpMethod(
res: Response,
next: NextFunction,
method: HTTP_VERB,
configBody: ApplicationConstructedProxyConfiguration,
activityLogId: number,
environment_id: number,
isSync?: boolean,
isDryRun?: boolean
) {
private sendToHttpMethod({
res,
method,
configBody,
activityLogId,
environment_id,
isSync,
isDryRun
}: {
res: Response;
method: HTTP_VERB;
configBody: ApplicationConstructedProxyConfiguration;
activityLogId: number;
environment_id: number;
isSync?: boolean | undefined;
isDryRun?: boolean | undefined;
}) {
const url = proxyService.constructUrl(configBody);
let decompress = false;

if (configBody.decompress === true || configBody.template?.proxy?.decompress === true) {
decompress = true;
}

return this.request(res, next, method, url, configBody, activityLogId, environment_id, decompress, isSync, isDryRun, configBody.data);
return this.request({
res,
method,
url,
config: configBody,
activityLogId,
environment_id,
decompress,
isSync,
isDryRun,
data: configBody.data
});
}

private async handleResponse(
res: Response,
responseStream: AxiosResponse,
config: ApplicationConstructedProxyConfiguration,
activityLogId: number,
environment_id: number,
url: string,
isSync?: boolean,
isDryRun?: boolean
) {
private async handleResponse({
res,
responseStream,
config,
activityLogId,
environment_id,
url,
isSync = false,
isDryRun = false
}: {
res: Response;
responseStream: AxiosResponse;
config: ApplicationConstructedProxyConfiguration;
activityLogId: number;
environment_id: number;
url: string;
isSync?: boolean | undefined;
isDryRun?: boolean | undefined;
}) {
if (!isSync) {
await updateSuccessActivityLog(activityLogId, true);
}
Expand Down Expand Up @@ -308,19 +343,29 @@ class ProxyController {
* @param {ApplicationConstructedProxyConfiguration} config
*/

private async request(
res: Response,
_next: NextFunction,
method: HTTP_VERB,
url: string,
config: ApplicationConstructedProxyConfiguration,
activityLogId: number,
environment_id: number,
decompress: boolean,
isSync?: boolean,
isDryRun?: boolean,
data?: unknown
) {
private async request({
res,
method,
url,
config,
activityLogId,
environment_id,
decompress,
isSync,
isDryRun,
data
}: {
res: Response;
method: HTTP_VERB;
url: string;
config: ApplicationConstructedProxyConfiguration;
activityLogId: number;
environment_id: number;
decompress: boolean;
isSync?: boolean | undefined;
isDryRun?: boolean | undefined;
data?: unknown;
}) {
try {
const activityLogs: ActivityLogMessage[] = [];
const headers = proxyService.constructHeaders(config);
Expand All @@ -344,7 +389,7 @@ class ProxyController {
createActivityLogMessage(activityLogMessage);
});

this.handleResponse(res, responseStream, config, activityLogId, environment_id, url, isSync, isDryRun);
this.handleResponse({ res, responseStream, config, activityLogId, environment_id, url, isSync, isDryRun });
} catch (error) {
this.handleErrorResponse(res, error, url, config, activityLogId, environment_id);
}
Expand Down
Loading

0 comments on commit 9a16171

Please sign in to comment.