Skip to content

Commit

Permalink
Merge pull request #907 from zapier/PDE-5488-invoke-debug-info
Browse files Browse the repository at this point in the history
PDE-5488 fix(cli): `zapier invoke --debug` should print non-z.request http logs
  • Loading branch information
eliangcs authored Oct 30, 2024
2 parents 3fdf6ce + 5d30882 commit 88e4701
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
53 changes: 39 additions & 14 deletions packages/cli/src/oclif/commands/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ const testAuth = async (authData, meta, zcacheTestObj) => {
},
zcacheTestObj,
customLogger,
calledFromCliInvoke: true,
});
endSpinner();
return result;
Expand Down Expand Up @@ -367,7 +368,7 @@ class InvokeCommand extends BaseCommand {
]);
}

async startCustomAuth(authFields) {
async startCustomAuth(authFields, zcacheTestObj) {
if (this.nonInteractive) {
throw new Error(
'The `auth start` subcommand for "custom" authentication type only works in interactive mode.'
Expand All @@ -376,7 +377,7 @@ class InvokeCommand extends BaseCommand {
return this.promptForAuthFields(authFields);
}

async startOAuth2(appDefinition) {
async startOAuth2(appDefinition, zcacheTestObj) {
const redirectUri = this.flags['redirect-uri'];
const port = this.flags['local-port'];
const env = {};
Expand Down Expand Up @@ -428,6 +429,9 @@ class InvokeCommand extends BaseCommand {
state: stateParam,
},
},
zcacheTestObj,
customLogger,
calledFromCliInvoke: true,
});
if (!authorizeUrl.includes('&scope=')) {
const scope = appDefinition.authentication.oauth2Config.scope;
Expand Down Expand Up @@ -499,13 +503,16 @@ class InvokeCommand extends BaseCommand {
redirect_uri: redirectUri,
},
},
zcacheTestObj,
customLogger,
calledFromCliInvoke: true,
});

endSpinner();
return authData;
}

async startSessionAuth(appDefinition) {
async startSessionAuth(appDefinition, zcacheTestObj) {
if (this.nonInteractive) {
throw new Error(
'The `auth start` subcommand for "session" authentication type only works in interactive mode.'
Expand All @@ -522,13 +529,16 @@ class InvokeCommand extends BaseCommand {
bundle: {
authData,
},
zcacheTestObj,
customLogger,
calledFromCliInvoke: true,
});
endSpinner();

return { ...authData, ...sessionData };
}

async startAuth(appDefinition) {
async startAuth(appDefinition, zcacheTestObj) {
const authentication = appDefinition.authentication;
if (!authentication) {
console.warn(
Expand All @@ -542,11 +552,11 @@ class InvokeCommand extends BaseCommand {
case 'basic':
return this.startBasicAuth(authentication.fields);
case 'custom':
return this.startCustomAuth(authentication.fields);
return this.startCustomAuth(authentication.fields, zcacheTestObj);
case 'oauth2':
return this.startOAuth2(appDefinition);
return this.startOAuth2(appDefinition, zcacheTestObj);
case 'session':
return this.startSessionAuth(appDefinition);
return this.startSessionAuth(appDefinition, zcacheTestObj);
default:
// TODO: Add support for 'digest' and 'oauth1'
throw new Error(
Expand All @@ -555,7 +565,7 @@ class InvokeCommand extends BaseCommand {
}
}

async refreshOAuth2(appDefinition, authData) {
async refreshOAuth2(appDefinition, authData, zcacheTestObj) {
startSpinner('Invoking authentication.oauth2Config.refreshAccessToken');

const newAuthData = await localAppCommand({
Expand All @@ -564,13 +574,16 @@ class InvokeCommand extends BaseCommand {
bundle: {
authData,
},
zcacheTestObj,
customLogger,
calledFromCliInvoke: true,
});

endSpinner();
return newAuthData;
}

async refreshSessionAuth(appDefinition, authData) {
async refreshSessionAuth(appDefinition, authData, zcacheTestObj) {
startSpinner('Invoking authentication.sessionConfig.perform');

const sessionData = await localAppCommand({
Expand All @@ -579,13 +592,16 @@ class InvokeCommand extends BaseCommand {
bundle: {
authData,
},
zcacheTestObj,
customLogger,
calledFromCliInvoke: true,
});

endSpinner();
return sessionData;
}

async refreshAuth(appDefinition, authData) {
async refreshAuth(appDefinition, authData, zcacheTestObj) {
const authentication = appDefinition.authentication;
if (!authentication) {
console.warn(
Expand All @@ -602,9 +618,9 @@ class InvokeCommand extends BaseCommand {
}
switch (authentication.type) {
case 'oauth2':
return this.refreshOAuth2(appDefinition, authData);
return this.refreshOAuth2(appDefinition, authData, zcacheTestObj);
case 'session':
return this.refreshSessionAuth(appDefinition, authData);
return this.refreshSessionAuth(appDefinition, authData, zcacheTestObj);
default:
throw new Error(
`This command doesn't support refreshing authentication type "${authentication.type}".`
Expand Down Expand Up @@ -840,6 +856,7 @@ class InvokeCommand extends BaseCommand {
zcacheTestObj,
cursorTestObj,
customLogger,
calledFromCliInvoke: true,
});
endSpinner();

Expand Down Expand Up @@ -873,6 +890,7 @@ class InvokeCommand extends BaseCommand {
zcacheTestObj,
cursorTestObj,
customLogger,
calledFromCliInvoke: true,
});
endSpinner();

Expand Down Expand Up @@ -952,7 +970,10 @@ class InvokeCommand extends BaseCommand {
};
switch (actionKey) {
case 'start': {
const newAuthData = await this.startAuth(appDefinition);
const newAuthData = await this.startAuth(
appDefinition,
zcacheTestObj
);
if (_.isEmpty(newAuthData)) {
return;
}
Expand All @@ -963,7 +984,11 @@ class InvokeCommand extends BaseCommand {
return;
}
case 'refresh': {
const newAuthData = await this.refreshAuth(appDefinition, authData);
const newAuthData = await this.refreshAuth(
appDefinition,
authData,
zcacheTestObj
);
if (_.isEmpty(newAuthData)) {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/tools/create-lambda-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const createLambdaHandler = (appRawOrPath) => {
context.callbackWaitsForEmptyEventLoop = true;

// replace native Promise with bluebird (works better with domains)
if (!event.calledFromCli) {
if (!event.calledFromCli || event.calledFromCliInvoke) {
ZapierPromise.patchGlobal();
}

Expand Down Expand Up @@ -253,7 +253,10 @@ const createLambdaHandler = (appRawOrPath) => {

const { skipHttpPatch } = appRaw.flags || {};
// Adds logging for _all_ kinds of http(s) requests, no matter the library
if (!skipHttpPatch && !event.calledFromCli) {
if (
!skipHttpPatch &&
(!event.calledFromCli || event.calledFromCliInvoke)
) {
const httpPatch = createHttpPatch(event);
httpPatch(require('http'), logger);
httpPatch(require('https'), logger); // 'https' needs to be patched separately
Expand Down

0 comments on commit 88e4701

Please sign in to comment.