feat: Introduce renderer helper

This commit is contained in:
Faruk AYDIN
2024-02-12 23:25:09 +01:00
parent c81531cb7a
commit 4f03f2ab51
3 changed files with 35 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import appConfig from '../../../../config/app';
import appConfig from '../../../../config/app.js';
import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => {
response.json({ version: appConfig.version });
renderObject(response, { version: appConfig.version });
};

View File

@@ -1,7 +1,6 @@
import { describe, it, expect } from 'vitest';
import request from 'supertest';
import app from '../../../../app.js';
import appConfig from '../../../../config/app.js';
describe('GET /api/v1/automatisch/version', () => {
it('should return Automatisch version', async () => {
@@ -9,6 +8,19 @@ describe('GET /api/v1/automatisch/version', () => {
.get('/api/v1/automatisch/version')
.expect(200);
expect(response.body).toEqual({ version: appConfig.version });
const expectedPayload = {
data: {
version: '0.10.0',
},
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'Object',
},
};
expect(response.body).toEqual(expectedPayload);
});
});

View File

@@ -0,0 +1,18 @@
const renderObject = (response, object) => {
const isArray = Array.isArray(object);
const computedPayload = {
data: object,
meta: {
type: object.constructor.name,
count: isArray ? object.length : 1,
isArray,
currentPage: null,
totalPages: null,
},
};
return response.json(computedPayload);
};
export { renderObject };