Skip to content

Commit

Permalink
chore: remove stub geo
Browse files Browse the repository at this point in the history
  • Loading branch information
rmonnier9 committed Dec 17, 2024
1 parent 22513b8 commit aaa2a6d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 46 deletions.
20 changes: 8 additions & 12 deletions clients/geo/communes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import routes from '#clients/routes';
import { stubClient } from '#clients/stub-client-with-snaphots';
import constants from '#models/constants';
import { httpGet } from '#utils/network';
import { IGeoElement } from '.';
Expand All @@ -11,9 +10,12 @@ type IGeoCommuneResponse = {
departement?: { code: string; nom: string };
};

const clientCommunesByName = async (slug: string): Promise<IGeoElement[]> => {
export const clientCommunesByName = async (
slug: string
): Promise<IGeoElement[]> => {
const query = `fields=codesPostaux&format=json&nom=${slug}&fields=departement`;
const response = await httpGet<IGeoCommuneResponse[]>(
`${routes.geo.communes}&nom=${slug}&fields=departement`,
`${routes.geo.communes}?${query}`,
{
timeout: constants.timeout.L,
}
Expand All @@ -22,9 +24,10 @@ const clientCommunesByName = async (slug: string): Promise<IGeoElement[]> => {
return mapToDomainObject(response || []);
};

const clientCommuneByCp = async (cp: string): Promise<IGeoElement[]> => {
export const clientCommuneByCp = async (cp: string): Promise<IGeoElement[]> => {
const query = `fields=codesPostaux&format=json&codePostal=${cp}`;
const response = await httpGet<IGeoCommuneResponse[]>(
`${routes.geo.communes}&codePostal=${cp}`,
`${routes.geo.communes}?${query}`,
{
timeout: constants.timeout.L,
}
Expand Down Expand Up @@ -65,10 +68,3 @@ const mapToDomainObject = (response: IGeoCommuneResponse[]): IGeoElement[] => {
[]
);
};

// We need to stub this API because it can timeout
const stubbedClientCommunesByName = stubClient({ clientCommunesByName });
export {
clientCommuneByCp,
stubbedClientCommunesByName as clientCommunesByName,
};
16 changes: 4 additions & 12 deletions clients/geo/departements.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import routes from '#clients/routes';
import { stubClient } from '#clients/stub-client-with-snaphots';
import constants from '#models/constants';
import { httpGet } from '#utils/network';
import { IGeoElement } from '.';
Expand All @@ -9,23 +8,23 @@ type IGeoDepartementResponse = {
code: string;
};

const clientDepartementsByName = async (
export const clientDepartementsByName = async (
slug: string
): Promise<IGeoElement[]> => {
const response = await httpGet<IGeoDepartementResponse[]>(
`${routes.geo.departements}&nom=${slug}`,
`${routes.geo.departements}?fields=code&format=json&zone=metro,drom,com&nom=${slug}`,
{
timeout: constants.timeout.L,
}
);
return mapToDomainObject(response || []);
};

const clientDepartementByCode = async (
export const clientDepartementByCode = async (
code: string
): Promise<IGeoElement[]> => {
const response = await httpGet<IGeoDepartementResponse[]>(
`${routes.geo.departements}&code=${code}`,
`${routes.geo.departements}?fields=code&format=json&zone=metro,drom,com&code=${code}`,
{
timeout: constants.timeout.L,
}
Expand All @@ -44,10 +43,3 @@ const mapToDomainObject = (
};
});
};

// We need to stub this API because it can timeout
const stubbedClientDepartementByName = stubClient({ clientDepartementsByName });
export {
clientDepartementByCode,
stubbedClientDepartementByName as clientDepartementsByName,
};
17 changes: 8 additions & 9 deletions clients/geo/epcis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import routes from '#clients/routes';
import { stubClient } from '#clients/stub-client-with-snaphots';
import constants from '#models/constants';
import { httpGet } from '#utils/network';
import { IGeoElement } from '.';
Expand All @@ -9,9 +8,11 @@ type IGeoEpciResponse = {
code: string;
};

const clientEpcisBySiren = async (siren: string): Promise<IGeoElement[]> => {
export const clientEpcisBySiren = async (
siren: string
): Promise<IGeoElement[]> => {
const response = await httpGet<IGeoEpciResponse[]>(
`${routes.geo.epcis}&code=${siren}`,
`${routes.geo.epcis}?fields=nom,code&code=${siren}`,
{
timeout: constants.timeout.L,
}
Expand All @@ -20,9 +21,11 @@ const clientEpcisBySiren = async (siren: string): Promise<IGeoElement[]> => {
return mapToDomainObject(response || []);
};

const clientEpcisByName = async (term: string): Promise<IGeoElement[]> => {
export const clientEpcisByName = async (
term: string
): Promise<IGeoElement[]> => {
const response = await httpGet<IGeoEpciResponse[]>(
`${routes.geo.epcis}&nom=${term}`,
`${routes.geo.epcis}?fields=nom,code&nom=${term}`,
{
timeout: constants.timeout.L,
}
Expand All @@ -40,7 +43,3 @@ const mapToDomainObject = (response: IGeoEpciResponse[]): IGeoElement[] => {
};
});
};

// This API can timeout, so we need to stub it
const stubbedClientEpcisByName = stubClient({ clientEpcisByName });
export { stubbedClientEpcisByName as clientEpcisByName, clientEpcisBySiren };
11 changes: 4 additions & 7 deletions clients/geo/regions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import routes from '#clients/routes';
import { stubClient } from '#clients/stub-client-with-snaphots';
import constants from '#models/constants';
import { httpGet } from '#utils/network';
import { IGeoElement } from '.';
Expand All @@ -9,9 +8,11 @@ type IGeoRegionResponse = {
code: string;
};

const clientRegionsByName = async (term: string): Promise<IGeoElement[]> => {
export const clientRegionsByName = async (
term: string
): Promise<IGeoElement[]> => {
const response = await httpGet<IGeoRegionResponse[]>(
`${routes.geo.regions}&nom=${term}`,
`${routes.geo.regions}?fields=nom,code&nom=${term}`,
{
timeout: constants.timeout.L,
}
Expand All @@ -29,7 +30,3 @@ const mapToDomainObject = (response: IGeoRegionResponse[]): IGeoElement[] => {
};
});
};

// This API can timeout, so we need to stub it
const stubbedClientRegionsByName = stubClient({ clientRegionsByName });
export { stubbedClientRegionsByName as clientRegionsByName };
10 changes: 4 additions & 6 deletions clients/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ const routes = {
site: 'https://www.education.gouv.fr/annuaire',
},
geo: {
communes:
'https://geo.api.gouv.fr/communes?fields=codesPostaux&format=json',
departements:
'https://geo.api.gouv.fr/departements?fields=code&format=json&zone=metro,drom,com',
regions: 'https://geo.api.gouv.fr/regions?fields=nom,code',
epcis: 'https://geo.api.gouv.fr/epcis?fields=nom,code',
communes: 'https://geo.api.gouv.fr/communes',
departements: 'https://geo.api.gouv.fr/departements',
regions: 'https://geo.api.gouv.fr/regions',
epcis: 'https://geo.api.gouv.fr/epcis',
},
journalOfficielAssociations: {
ods: {
Expand Down
19 changes: 19 additions & 0 deletions cypress/mocks/handlers/api-geo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HttpResponse, HttpResponseResolver } from 'msw';

export const apiGeoCommunesHandler: HttpResponseResolver = ({ request }) => {
return HttpResponse.json({});
};

export const apiGeoDepartementsHandler: HttpResponseResolver = ({
request,
}) => {
return HttpResponse.json({});
};

export const apiGeoRegionsHandler: HttpResponseResolver = ({ request }) => {
return HttpResponse.json({});
};

export const apiGeoEpcisHandler: HttpResponseResolver = ({ request }) => {
return HttpResponse.json({});
};
10 changes: 10 additions & 0 deletions cypress/mocks/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { http } from 'msw';
import { apiBioHandler } from './handlers/api-bio';
import { apiDataGouvEssHandler } from './handlers/api-data-gouv-ess';
import { apiDataSubventionHandler } from './handlers/api-data-subvention';
import {
apiGeoCommunesHandler,
apiGeoDepartementsHandler,
apiGeoEpcisHandler,
apiGeoRegionsHandler,
} from './handlers/api-geo';
import { apiInclusionHandler } from './handlers/api-inclusion';
import {
apiSireneInseeAuthHandler,
Expand Down Expand Up @@ -53,4 +59,8 @@ export const routesHandlers = [
http.post(routes.sireneInsee.auth, apiSireneInseeAuthHandler),
http.get(routes.sireneInsee.getBySiret('*'), apiSireneInseeSiretHandler),
http.get(routes.sireneInsee.getBySiren('*'), apiSireneInseeSirenHandler),
http.get(routes.geo.communes, apiGeoCommunesHandler),
http.get(routes.geo.departements, apiGeoDepartementsHandler),
http.get(routes.geo.regions, apiGeoRegionsHandler),
http.get(routes.geo.epcis, apiGeoEpcisHandler),
];

0 comments on commit aaa2a6d

Please sign in to comment.