Merge pull request #1684 from automatisch/rest-get-triggers
feat: Implement get triggers API endpoint
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import App from '../../../../models/app.js';
|
||||
import { renderObject } from '../../../../helpers/renderer.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const triggers = await App.findTriggersByKey(request.params.appKey);
|
||||
|
||||
renderObject(response, triggers, { serializer: 'Trigger' });
|
||||
};
|
@@ -0,0 +1,35 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import App from '../../../../models/app';
|
||||
import app from '../../../../app.js';
|
||||
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
|
||||
import { createUser } from '../../../../../test/factories/user';
|
||||
import getTriggersMock from '../../../../../test/mocks/rest/api/v1/apps/get-triggers.js';
|
||||
|
||||
describe('GET /api/v1/apps/:appKey/triggers', () => {
|
||||
let currentUser, token;
|
||||
|
||||
beforeEach(async () => {
|
||||
currentUser = await createUser();
|
||||
token = createAuthTokenByUserId(currentUser.id);
|
||||
});
|
||||
|
||||
it('should return the app triggers', async () => {
|
||||
const exampleApp = await App.findOneByKey('github');
|
||||
|
||||
const response = await request(app)
|
||||
.get(`/api/v1/apps/${exampleApp.key}/triggers`)
|
||||
.set('Authorization', token)
|
||||
.expect(200);
|
||||
|
||||
const expectedPayload = getTriggersMock(exampleApp.triggers);
|
||||
expect(response.body).toEqual(expectedPayload);
|
||||
});
|
||||
|
||||
it('should return not found response for invalid app key', async () => {
|
||||
await request(app)
|
||||
.get('/api/v1/apps/invalid-app-key/triggers')
|
||||
.set('Authorization', token)
|
||||
.expect(404);
|
||||
});
|
||||
});
|
@@ -43,7 +43,14 @@ class App {
|
||||
const rawAppData = await getApp(key, stripFuncs);
|
||||
const appData = appInfoConverter(rawAppData);
|
||||
|
||||
return appData.auth;
|
||||
return appData?.auth || {};
|
||||
}
|
||||
|
||||
static async findTriggersByKey(key, stripFuncs = false) {
|
||||
const rawAppData = await getApp(key, stripFuncs);
|
||||
const appData = appInfoConverter(rawAppData);
|
||||
|
||||
return appData?.triggers || [];
|
||||
}
|
||||
|
||||
static async checkAppAndAction(appKey, actionKey) {
|
||||
|
@@ -4,6 +4,7 @@ import { authenticateUser } from '../../../helpers/authentication.js';
|
||||
import getAppAction from '../../../controllers/api/v1/apps/get-app.js';
|
||||
import getAppsAction from '../../../controllers/api/v1/apps/get-apps.js';
|
||||
import getAuthAction from '../../../controllers/api/v1/apps/get-auth.js';
|
||||
import getTriggersAction from '../../../controllers/api/v1/apps/get-triggers.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -11,4 +12,10 @@ router.get('/', authenticateUser, asyncHandler(getAppsAction));
|
||||
router.get('/:appKey', authenticateUser, asyncHandler(getAppAction));
|
||||
router.get('/:appKey/auth', authenticateUser, asyncHandler(getAuthAction));
|
||||
|
||||
router.get(
|
||||
'/:appKey/triggers',
|
||||
authenticateUser,
|
||||
asyncHandler(getTriggersAction)
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
@@ -7,6 +7,7 @@ import flowSerializer from './flow.js';
|
||||
import stepSerializer from './step.js';
|
||||
import appSerializer from './app.js';
|
||||
import authSerializer from './auth.js';
|
||||
import triggerSerializer from './trigger.js';
|
||||
|
||||
const serializers = {
|
||||
User: userSerializer,
|
||||
@@ -18,6 +19,7 @@ const serializers = {
|
||||
Step: stepSerializer,
|
||||
App: appSerializer,
|
||||
Auth: authSerializer,
|
||||
Trigger: triggerSerializer,
|
||||
};
|
||||
|
||||
export default serializers;
|
||||
|
12
packages/backend/src/serializers/trigger.js
Normal file
12
packages/backend/src/serializers/trigger.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const triggerSerializer = (trigger) => {
|
||||
return {
|
||||
description: trigger.description,
|
||||
key: trigger.key,
|
||||
name: trigger.name,
|
||||
pollInterval: trigger.pollInterval,
|
||||
showWebhookUrl: trigger.showWebhookUrl,
|
||||
type: trigger.type,
|
||||
};
|
||||
};
|
||||
|
||||
export default triggerSerializer;
|
21
packages/backend/src/serializers/trigger.test.js
Normal file
21
packages/backend/src/serializers/trigger.test.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import App from '../models/app';
|
||||
import triggerSerializer from './trigger';
|
||||
|
||||
describe('triggerSerializer', () => {
|
||||
it('should return the trigger data', async () => {
|
||||
const triggers = await App.findTriggersByKey('github');
|
||||
const trigger = triggers[0];
|
||||
|
||||
const expectedPayload = {
|
||||
description: trigger.description,
|
||||
key: trigger.key,
|
||||
name: trigger.name,
|
||||
pollInterval: trigger.pollInterval,
|
||||
showWebhookUrl: trigger.showWebhookUrl,
|
||||
type: trigger.type,
|
||||
};
|
||||
|
||||
expect(triggerSerializer(trigger)).toEqual(expectedPayload);
|
||||
});
|
||||
});
|
25
packages/backend/test/mocks/rest/api/v1/apps/get-triggers.js
Normal file
25
packages/backend/test/mocks/rest/api/v1/apps/get-triggers.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const getTriggersMock = (triggers) => {
|
||||
const triggersData = triggers.map((trigger) => {
|
||||
return {
|
||||
description: trigger.description,
|
||||
key: trigger.key,
|
||||
name: trigger.name,
|
||||
pollInterval: trigger.pollInterval,
|
||||
showWebhookUrl: trigger.showWebhookUrl,
|
||||
type: trigger.type,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
data: triggersData,
|
||||
meta: {
|
||||
count: triggers.length,
|
||||
currentPage: null,
|
||||
isArray: true,
|
||||
totalPages: null,
|
||||
type: 'Object',
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default getTriggersMock;
|
Reference in New Issue
Block a user