Skip to content

Commit a95e548

Browse files
committed
#306 : refactor and typing for more genericity
1 parent 63201fd commit a95e548

18 files changed

+433
-367
lines changed

Diff for: api/src/core/adapters/hal/HalAPI/getCodemetaSoftware.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { HAL } from "../types/HAL";
2-
import { HalFetchError } from "./type";
1+
import { HAL } from "./types/HAL";
32

43
export async function fetchCodeMetaSoftwareByURL(url: string): Promise<HAL.SoftwareApplication | undefined> {
54
const res = await fetch(`${url}/codemeta`, {
@@ -9,7 +8,7 @@ export async function fetchCodeMetaSoftwareByURL(url: string): Promise<HAL.Softw
98
});
109

1110
if (res === undefined) {
12-
throw new HalFetchError(undefined);
11+
throw new HAL.API.FetchError(undefined);
1312
}
1413

1514
if (res.status === 429) {
@@ -18,7 +17,7 @@ export async function fetchCodeMetaSoftwareByURL(url: string): Promise<HAL.Softw
1817
}
1918

2019
if (res.status === 404) {
21-
throw new HalFetchError(res.status);
20+
throw new HAL.API.FetchError(res.status);
2221
}
2322

2423
const json = await res.json();

Diff for: api/src/core/adapters/hal/HalAPI/getDomains.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import fetch from "node-fetch";
2-
import { HalAPIDomain, HalFetchError } from "./type";
2+
import { HAL } from "./types/HAL";
33

4-
export async function getAllDomains(): Promise<HalAPIDomain[]> {
4+
export async function getAllDomains(): Promise<HAL.API.Domain[]> {
55
// Get all domains
66
const url = "http://api.archives-ouvertes.fr/ref/domain/?fl=*";
77

88
const res = await fetch(url).catch(err => {
99
console.error(err);
10-
throw new HalFetchError(undefined);
10+
throw new HAL.API.FetchError(undefined);
1111
});
1212

1313
if (res.status === 429) {
@@ -16,21 +16,21 @@ export async function getAllDomains(): Promise<HalAPIDomain[]> {
1616
}
1717

1818
if (res.status === 404) {
19-
throw new HalFetchError(res.status);
19+
throw new HAL.API.FetchError(res.status);
2020
}
2121

2222
const json = await res.json();
2323

2424
return json.response.docs;
2525
}
2626

27-
export async function getDomainByCode(code: string): Promise<HalAPIDomain> {
27+
export async function getDomainByCode(code: string): Promise<HAL.API.Domain> {
2828
// Get domain using code
2929
const url = `http://api.archives-ouvertes.fr/ref/domain/?q=code_s:${code}&fl=*`;
3030

3131
const res = await fetch(url).catch(err => {
3232
console.error(err);
33-
throw new HalFetchError(undefined);
33+
throw new HAL.API.FetchError(undefined);
3434
});
3535

3636
if (res.status === 429) {
@@ -39,7 +39,7 @@ export async function getDomainByCode(code: string): Promise<HalAPIDomain> {
3939
}
4040

4141
if (res.status === 404) {
42-
throw new HalFetchError(res.status);
42+
throw new HAL.API.FetchError(res.status);
4343
}
4444

4545
const json = await res.json();

Diff for: api/src/core/adapters/hal/HalAPI/getHalSoftware.ts

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fetch from "node-fetch";
2-
import { HalFetchError, HalRawSoftware } from "./type";
2+
import { HAL } from "./types/HAL";
33

44
// HAL documentation is here : https://api.archives-ouvertes.fr/docs/search
55

66
// Move to get data from API
7-
const halSoftwareFieldsToReturn: (keyof HalRawSoftware)[] = [
7+
const halSoftwareFieldsToReturn: (keyof HAL.API.Software)[] = [
88
"en_abstract_s",
99
"en_title_s",
1010
"fr_abstract_s",
@@ -31,13 +31,13 @@ const halSoftwareFieldsToReturn: (keyof HalRawSoftware)[] = [
3131

3232
export const halSoftwareFieldsToReturnAsString = halSoftwareFieldsToReturn.join(",");
3333

34-
export async function fetchHalSoftwareById(halDocid: string): Promise<HalRawSoftware | undefined> {
34+
export async function fetchHalSoftwareById(halDocid: string): Promise<HAL.API.Software | undefined> {
3535
const res = await fetch(
3636
`https://api.archives-ouvertes.fr/search/?q=docid:${halDocid}&wt=json&fl=${halSoftwareFieldsToReturnAsString}&sort=docid%20asc`
3737
).catch(() => undefined);
3838

3939
if (res === undefined) {
40-
throw new HalFetchError(undefined);
40+
throw new HAL.API.FetchError(undefined);
4141
}
4242

4343
if (res.status === 429) {
@@ -46,30 +46,44 @@ export async function fetchHalSoftwareById(halDocid: string): Promise<HalRawSoft
4646
}
4747

4848
if (res.status === 404) {
49-
throw new HalFetchError(res.status);
49+
throw new HAL.API.FetchError(res.status);
5050
}
5151

5252
const json = await res.json();
5353

5454
return json.response.docs[0];
5555
}
56+
type Props = {
57+
queryString?: string;
58+
SWHFilter?: boolean;
59+
};
60+
61+
export async function fetchHalSoftwares(params: Props): Promise<Array<HAL.API.Software>> {
62+
const { queryString, SWHFilter } = params;
63+
64+
let url = `https://api.archives-ouvertes.fr/search/?fq=docType_s:SOFTWARE&rows=10000&fl=${halSoftwareFieldsToReturnAsString}`;
65+
66+
if (queryString) {
67+
url = url + `&q=${encodeURIComponent(queryString)}`;
68+
}
5669

57-
export async function fetchHalSoftwares(): Promise<Array<HalRawSoftware>> {
5870
// Filter only software who have an swhidId to filter clean data on https://hal.science, TODO remove and set it as an option to be generic
59-
const url = `https://api.archives-ouvertes.fr/search/?q=docType_s:SOFTWARE&rows=10000&fl=${halSoftwareFieldsToReturnAsString}&fq=swhidId_s:["" TO *]`;
71+
if (SWHFilter) {
72+
url = url + `&fq=swhidId_s:["" TO *]`;
73+
}
6074

6175
const res = await fetch(url).catch(err => {
6276
console.error(err);
63-
throw new HalFetchError(undefined);
77+
throw new HAL.API.FetchError(undefined);
6478
});
6579

6680
if (res.status === 429) {
6781
await new Promise(resolve => setTimeout(resolve, 100));
68-
return fetchHalSoftwares();
82+
return fetchHalSoftwares(params);
6983
}
7084

7185
if (res.status === 404) {
72-
throw new HalFetchError(res.status);
86+
throw new HAL.API.FetchError(res.status);
7387
}
7488

7589
const json = await res.json();

Diff for: api/src/core/adapters/hal/HalAPI/getStructure.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { HalFetchError, HalStructure } from "./type";
1+
import { HAL } from "./types/HAL";
22

3-
export const getHalStructureByAcronym = async (structureAcronym: string): Promise<HalStructure | undefined> => {
3+
export const getHalStructureByAcronym = async (structureAcronym: string): Promise<HAL.API.Structure | undefined> => {
44
const url = `http://api.archives-ouvertes.fr/ref/structure/?fl=*&q=acronym_s:"${encodeURIComponent(
55
structureAcronym
66
)}"`;
@@ -9,7 +9,7 @@ export const getHalStructureByAcronym = async (structureAcronym: string): Promis
99
signal: AbortSignal.timeout(10000)
1010
}).catch(err => {
1111
console.error(err);
12-
throw new HalFetchError(undefined);
12+
throw new HAL.API.FetchError(undefined);
1313
});
1414

1515
if (res.status === 429) {
@@ -18,27 +18,27 @@ export const getHalStructureByAcronym = async (structureAcronym: string): Promis
1818
}
1919

2020
if (res.status === 404) {
21-
throw new HalFetchError(res.status);
21+
throw new HAL.API.FetchError(res.status);
2222
}
2323

2424
const json = await res.json();
2525

2626
if (json.error) {
27-
throw new HalFetchError(json.error);
27+
throw new HAL.API.FetchError(json.error);
2828
}
2929

3030
// What do to when multiple for one acronym while in code meta only reference to acronym => LIDILEM, EPFL
3131
return json.response.docs?.[0]; // json.response.numFound === 1 ? : undefined;
3232
};
3333

34-
export const getHalStructureById = async (docid: number): Promise<HalStructure | undefined> => {
34+
export const getHalStructureById = async (docid: number): Promise<HAL.API.Structure | undefined> => {
3535
const url = `http://api.archives-ouvertes.fr/ref/structure/?fl=*&q=docid:${docid}`;
3636

3737
const res = await fetch(url, {
3838
signal: AbortSignal.timeout(10000)
3939
}).catch(err => {
4040
console.error(err);
41-
throw new HalFetchError(undefined);
41+
throw new HAL.API.FetchError(undefined);
4242
});
4343

4444
if (res.status === 429) {
@@ -47,13 +47,13 @@ export const getHalStructureById = async (docid: number): Promise<HalStructure |
4747
}
4848

4949
if (res.status === 404) {
50-
throw new HalFetchError(res.status);
50+
throw new HAL.API.FetchError(res.status);
5151
}
5252

5353
const json = await res.json();
5454

5555
if (json.error) {
56-
throw new HalFetchError(json.error);
56+
throw new HAL.API.FetchError(json.error);
5757
}
5858

5959
return json.response.numFound === 1 ? json.response.docs?.[0] : undefined;

0 commit comments

Comments
 (0)