Need help with conditional logic in Zapier app development
I’m still learning JavaScript and building my first Zapier integration. I have an authentication test that should fail when wrong credentials are provided, but I can’t figure out how to make it work properly.
My test setup:
const should = require('should');
const zapier = require('zapier-platform-core');
const MyApp = require('../../app');
const tester = zapier.createAppTester(MyApp);
describe('Auth Test - Fetch Categories', () => {
zapier.tools.env.inject();
it('should return data array', finished => {
const config = {
authData: { token: process.env.AUTH_TOKEN },
inputData: {}
};
tester(MyApp.triggers['fetchcategories'].operation.perform, config)
.then(data => {
data.includes('categoryId');
finished();
})
.catch(data);
});
});
Success response example:
{"categoryId":2847,"title":"Active Users","total":15}
Error response example:
{"STATUS":"ERROR","MESSAGE":"Authentication failed"}
My trigger function:
const { substituteVars } = require('../helpers');
const fetchCategories = (z, bundle) => {
let endpoint = 'https://myapi.example.com/v1/categories?token={{token}}';
endpoint = substituteVars(endpoint, bundle);
const request = z.request({ url: endpoint });
return request.then(response => {
response.throwForStatus();
return z.JSON.parse(response.content);
});
};
module.exports = {
key: 'fetchcategories',
noun: 'Category',
display: {
label: 'Fetch Categories',
description: 'Loads available categories from API',
hidden: true,
important: false
},
operation: {
inputFields: [
{
key: 'category',
label: 'Category Filter',
type: 'string',
required: false
}
],
outputFields: [
{
key: 'total',
type: 'string'
},
{
key: 'categoryId',
type: 'string',
label: 'Category ID'
},
{
key: 'title',
type: 'string',
label: 'Category Title'
}
],
perform: fetchCategories,
sample: { total: 156, categoryId: 19203, title: 'Active Users' }
}
};
When testing authentication in Zapier’s interface, I want the auth to fail and display the MESSAGE field from the error response. What’s the correct way to handle this conditional behavior?