diff --git a/src/data-inclusion/cli/action/data-inclusion.action.ts b/src/data-inclusion/cli/action/data-inclusion.action.ts index 195ffcd0..bf2ef249 100644 --- a/src/data-inclusion/cli/action/data-inclusion.action.ts +++ b/src/data-inclusion/cli/action/data-inclusion.action.ts @@ -32,39 +32,18 @@ const fetchAllPages = async ( return fetchAllPages({ key, url }, totalPages, currentPage + 1, [...allPagesDatas, ...response.data.items]); }; -const fetchDataInclusionStructures = async ({ key, url }: Api): Promise => - axios - /* eslint-disable-next-line @typescript-eslint/naming-convention */ - .get(`${url}&page=1`, bearerTokenHeader(key)) - .then( - async ( - response: AxiosResponse<{ pages: number; items: SchemaStructureDataInclusion[] }> - ): Promise => [ - ...response.data.items, - ...(await fetchAllPages({ key, url }, response.data.pages)) - ] - ); - -const fetchDataInclusionServices = async ({ key, url }: Api): Promise => - axios - /* eslint-disable-next-line @typescript-eslint/naming-convention */ - .get(`${url}&page=1`, bearerTokenHeader(key)) - .then( - async ( - response: AxiosResponse<{ pages: number; items: SchemaServiceDataInclusion[] }> - ): Promise => [ - ...response.data.items, - ...(await fetchAllPages({ key, url }, response.data.pages)) - ] - ); +const fetchFromDataInclusionApi = async ({ key, url }: Api): Promise => { + const { items, pages }: { items: T[]; pages: number } = (await axios.get(`${url}&page=1`, bearerTokenHeader(key))).data; + return [...items, ...(await fetchAllPages({ key, url }, pages))]; +}; export const dataInclusionAction = async (dataInclusionOptions: DataInclusionOptions): Promise => { - const responseStructures: SchemaStructureDataInclusion[] = await fetchDataInclusionStructures({ + const responseStructures: SchemaStructureDataInclusion[] = await fetchFromDataInclusionApi({ key: dataInclusionOptions.dataInclusionApiKey, url: 'https://api.data.inclusion.beta.gouv.fr/api/v0/structures?source=dora' }); - const responseServices: SchemaServiceDataInclusion[] = await fetchDataInclusionServices({ + const responseServices: SchemaServiceDataInclusion[] = await fetchFromDataInclusionApi({ key: dataInclusionOptions.dataInclusionApiKey, url: 'https://api.data.inclusion.beta.gouv.fr/api/v0/services?source=dora' });