2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
const { createAgent } = require('./agent');
|
|
|
|
const { superAdmin } = require('./strapi');
|
2018-06-13 15:31:33 +02:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const CONTENT_API_URL_PREFIX = '/api';
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
const createRequest = ({ strapi } = {}) => createAgent(strapi);
|
2021-09-07 21:03:30 +02:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const createContentAPIRequest = ({ strapi, auth = {} } = {}) => {
|
|
|
|
const { token } = auth;
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
return createAgent(strapi, { urlPrefix: CONTENT_API_URL_PREFIX, token });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default content api agent
|
|
|
|
return createAgent(strapi, { urlPrefix: CONTENT_API_URL_PREFIX, token: 'test-token' });
|
2021-09-07 21:03:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const createAuthRequest = ({ strapi, userInfo = superAdmin.credentials }) => {
|
|
|
|
return createAgent(strapi).login(userInfo);
|
|
|
|
};
|
2019-05-06 17:27:24 +02:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const transformToRESTResource = (input) => {
|
2021-08-10 08:42:42 +02:00
|
|
|
if (Array.isArray(input)) {
|
2022-08-08 23:33:39 +02:00
|
|
|
return input.map((value) => transformToRESTResource(value));
|
2021-08-10 08:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const { id, ...attributes } = input;
|
|
|
|
return { id, attributes };
|
|
|
|
};
|
|
|
|
|
2019-05-06 17:27:24 +02:00
|
|
|
module.exports = {
|
|
|
|
createRequest,
|
2021-09-07 21:03:30 +02:00
|
|
|
createContentAPIRequest,
|
2019-05-06 17:27:24 +02:00
|
|
|
createAuthRequest,
|
2021-08-10 08:42:42 +02:00
|
|
|
transformToRESTResource,
|
2019-05-06 17:27:24 +02:00
|
|
|
};
|