Skip to content
Snippets Groups Projects
Commit a06f6060 authored by Bruno Boiget's avatar Bruno Boiget
Browse files

fix(fetch): use fetch provided by load function when available

parent 23613b88
No related branches found
No related tags found
3 merge requests!76Create new stable for V12,!73Create a new testing for V12,!70Resolve "incohérence dans le code partie 2"
Pipeline #53726 passed
import {fetchData} from '../utils/api/methods';
export async function load({parent}) {
export async function load({parent, fetch}) {
const {env} = await parent();
const {items: articles, response} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit: 4,
order: 'createdAt DESC',
fields: {content: false},
......
import {fetchData, getTags} from '../../utils/api/methods';
export async function load({url, parent}) {
export async function load({url, parent, fetch}) {
const path = url.pathname;
const currentPage = url.searchParams.get('page') || 1;
const search = url.searchParams.get('search');
......@@ -35,6 +35,7 @@ export async function load({url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......@@ -46,7 +47,7 @@ export async function load({url, parent}) {
skip: currentPage === 1 ? 0 : (Number(currentPage) - 1) * limit,
});
const tagsList = await getTags(env.API_HOST);
const tagsList = await getTags(env.API_HOST, fetch);
return {
articles: items,
total,
......
export async function load({params = {}, parent}) {
export async function load({params = {}, parent, fetch}) {
const {env} = await parent();
const responseArticle = await fetch(
`${env.API_HOST}/articles/${params.slug}`,
......
import {fetchData} from '../../utils/api/methods';
export async function load({url, parent}) {
export async function load({url, parent, fetch}) {
const path = url.pathname;
const page = url.searchParams.get('page') || 1;
const search = url.searchParams.get('search');
......@@ -25,6 +25,7 @@ export async function load({url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......
import {fetchData, getTags} from '../../../utils/api/methods';
export async function load({params, url, parent}) {
export async function load({params, url, parent, fetch}) {
const path = url.pathname;
const {env} = await parent();
......@@ -20,6 +20,7 @@ export async function load({params, url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......@@ -35,7 +36,7 @@ export async function load({params, url, parent}) {
`${env.API_HOST}/structures/${author.structure}`,
);
const structure = await responseStructure.json();
const tagsList = await getTags(env.API_HOST);
const tagsList = await getTags(env.API_HOST, fetch);
return {
articles: items,
total,
......
import {fetchData} from '../../utils/api/methods';
export async function load({url, parent}) {
export async function load({url, parent, fetch}) {
const path = url.pathname;
const page = url.searchParams.get('page') || 1;
const search = url.searchParams.get('search');
......@@ -24,6 +24,7 @@ export async function load({url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......
import {fetchData, getTags} from '../../../utils/api/methods';
export async function load({params, url, parent}) {
export async function load({params, url, parent, fetch}) {
const path = url.pathname;
const page = url.searchParams.get('page') || 1;
const search = url.searchParams.get('search');
......@@ -19,6 +19,7 @@ export async function load({params, url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......@@ -28,7 +29,7 @@ export async function load({params, url, parent}) {
where,
skip: page === 1 ? 0 : (Number(page) - 1) * limit,
});
const tagsList = await getTags(env.API_HOST);
const tagsList = await getTags(env.API_HOST, fetch);
return {
articles: items,
total,
......
import {fetchData} from '../../utils/api/methods';
export async function load({parent}) {
export async function load({parent, fetch}) {
const fields = {};
const order = 'name ASC';
const apiurl = 'structures';
......@@ -8,6 +8,7 @@ export async function load({parent}) {
const {items} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit: 100,
order,
fields,
......
import {fetchData} from '../../../utils/api/methods';
export async function load({params, parent}) {
export async function load({params, parent, fetch}) {
const {env} = await parent();
const responseStructure = await fetch(
`${env.API_HOST}/structures/${params._id}`,
......@@ -9,6 +9,7 @@ export async function load({params, parent}) {
const {items: authors, response} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit: 6,
order: 'articlesCount DESC',
fields: {},
......@@ -18,6 +19,7 @@ export async function load({params, parent}) {
});
const {items: articles} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit: 4,
order: 'createdAt DESC',
fields: {content: false},
......
import {fetchData, getTags} from '../../../../utils/api/methods';
export async function load({params, url, parent}) {
export async function load({params, url, parent, fetch}) {
const path = url.pathname;
const page = url.searchParams.get('page') || 1;
const search = url.searchParams.get('search');
......@@ -42,6 +42,7 @@ export async function load({params, url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......@@ -52,7 +53,7 @@ export async function load({params, url, parent}) {
include,
skip: page === 1 ? 0 : (Number(page) - 1) * limit,
});
const tagsList = await getTags(env.API_HOST);
const tagsList = await getTags(env.API_HOST, fetch);
return {
articles: items,
total,
......
import {fetchData} from '../../../../utils/api/methods';
export async function load({params, url, parent}) {
export async function load({params, url, parent, fetch}) {
const path = url.pathname;
const page = url.searchParams.get('page') || 1;
const search = url.searchParams.get('search');
......@@ -29,6 +29,7 @@ export async function load({params, url, parent}) {
const {items, total} = await fetchData({
host: env.API_HOST,
fetcher: fetch,
limit,
order,
fields,
......
......@@ -35,6 +35,7 @@ export async function fetchData({
count = true,
countOnly = false,
include,
fetcher = fetch,
}) {
const whereQuery = {...where};
if (value) {
......@@ -52,12 +53,12 @@ export async function fetchData({
};
const response = countOnly
? null
: await fetch(`${host}/${apiurl}?filter=${JSON.stringify(queryFilters)}`);
: await fetcher(`${host}/${apiurl}?filter=${JSON.stringify(queryFilters)}`);
const items = countOnly ? null : await response.json();
let total;
if (count) {
const newTotal = await fetch(
const newTotal = await fetcher(
`${host}/${apiurl}/count?where=${JSON.stringify(whereQuery)}`,
);
const result = await newTotal.json();
......@@ -66,9 +67,9 @@ export async function fetchData({
return {items, total, response};
}
export async function getTags(host) {
export async function getTags(host, fetcher = fetch) {
const queryFilters = {fields: {_id: false}};
const response = await fetch(
const response = await fetcher(
`${host}/tags?filter=${JSON.stringify(queryFilters)}`,
);
const items = await response.json();
......@@ -76,8 +77,8 @@ export async function getTags(host) {
return tags;
}
export async function getMaintenance(host) {
const response = await fetch(`${host}/appsettings`);
export async function getMaintenance(host, fetcher = fetch) {
const response = await fetcher(`${host}/appsettings`);
if (response.ok) {
const result = await response.json();
return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment