Skip to content

Commit 74de536

Browse files
authored
add optional headers to api calls (metlo-labs#83)
1 parent 55d4d60 commit 74de536

File tree

12 files changed

+170
-57
lines changed

12 files changed

+170
-57
lines changed

frontend/src/api/alerts/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import { GetAlertParams, Alert, UpdateAlertParams } from "@common/types"
33
import { getAPIURL } from "~/constants"
44

55
export const getAlerts = async (
66
params: GetAlertParams,
7+
headers?: AxiosRequestHeaders,
78
): Promise<[Alert[], number]> => {
89
const resp = await axios.get<[Alert[], number]>(`${getAPIURL()}/alerts`, {
910
params,
11+
headers,
1012
})
1113
return resp.data
1214
}
1315

1416
export const resolveAlert = async (
1517
alertId: string,
1618
resolutionMessage: string,
19+
headers?: AxiosRequestHeaders,
1720
): Promise<Alert> => {
1821
const resp = await axios.put<Alert>(
1922
`${getAPIURL()}/alert/resolve/${alertId}`,
2023
{ resolutionMessage },
24+
{ headers },
2125
)
2226
return resp.data
2327
}
2428

2529
export const updateAlert = async (
2630
alertId: string,
2731
updateAlertParams: UpdateAlertParams,
32+
headers?: AxiosRequestHeaders,
2833
): Promise<Alert> => {
2934
const resp = await axios.put<Alert>(
3035
`${getAPIURL()}/alert/${alertId}`,
3136
updateAlertParams,
37+
{ headers },
3238
)
3339
return resp.data
3440
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { GetVulnerabilityAggParams, VulnerabilitySummary } from "@common/types"
2-
import axios from "axios"
2+
import axios, { AxiosRequestHeaders } from "axios"
33
import { getAPIURL } from "~/constants"
44

55
export const getVulnerabilitySummary = async (
66
params: GetVulnerabilityAggParams,
7+
headers?: AxiosRequestHeaders,
78
): Promise<VulnerabilitySummary> => {
89
const resp = await axios.get<VulnerabilitySummary>(
910
`${getAPIURL()}/vulnerability-summary`,
10-
{ params },
11+
{ params, headers },
1112
)
1213
return resp.data
1314
}

frontend/src/api/apiSpecs/index.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import { OpenApiSpec } from "@common/types"
33
import { getAPIURL } from "~/constants"
44

5-
export const getSpecs = async (): Promise<[OpenApiSpec[], number]> => {
5+
export const getSpecs = async (
6+
headers?: AxiosRequestHeaders,
7+
): Promise<[OpenApiSpec[], number]> => {
68
try {
79
const resp = await axios.get<[OpenApiSpec[], number]>(
810
`${getAPIURL()}/specs?listAutoGenerated=false`,
11+
{ headers },
912
)
1013
if (resp.status === 200 && resp.data) {
1114
return resp.data
@@ -17,32 +20,45 @@ export const getSpecs = async (): Promise<[OpenApiSpec[], number]> => {
1720
}
1821
}
1922

20-
export const uploadSpec = async (file: File) => {
23+
export const uploadSpec = async (file: File, headers?: AxiosRequestHeaders) => {
2124
const formData = new FormData()
2225
formData.append("file", file)
2326
return await axios.post(`${getAPIURL()}/spec/new`, formData, {
2427
headers: {
28+
...headers,
2529
"Content-Type": "multipart/form-data",
2630
},
2731
})
2832
}
2933

30-
export const updateSpec = async (name: string, file: File) => {
34+
export const updateSpec = async (
35+
name: string,
36+
file: File,
37+
headers?: AxiosRequestHeaders,
38+
) => {
3139
const formData = new FormData()
3240
formData.append("file", file)
3341
return await axios.put(`${getAPIURL()}/spec/${name}`, formData, {
3442
headers: {
43+
...headers,
3544
"Content-Type": "multipart/form-data",
3645
},
3746
})
3847
}
3948

40-
export const getSpec = async (name: string) => {
41-
const resp = await axios.get<OpenApiSpec>(`${getAPIURL()}/spec/${name}`)
49+
export const getSpec = async (name: string, headers?: AxiosRequestHeaders) => {
50+
const resp = await axios.get<OpenApiSpec>(`${getAPIURL()}/spec/${name}`, {
51+
headers,
52+
})
4253
return resp.data
4354
}
4455

45-
export const deleteSpec = async (name: string) => {
46-
const resp = await axios.delete<OpenApiSpec>(`/api/v1/spec/${name}`)
56+
export const deleteSpec = async (
57+
name: string,
58+
headers?: AxiosRequestHeaders,
59+
) => {
60+
const resp = await axios.delete<OpenApiSpec>(`/api/v1/spec/${name}`, {
61+
headers,
62+
})
4763
return resp.data
4864
}

frontend/src/api/attacks/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import {
33
AttackDetailResponse,
44
AttackResponse,
@@ -8,22 +8,29 @@ import { getAPIURL } from "~/constants"
88

99
export const getAttacks = async (
1010
params: GetAttackParams,
11+
headers?: AxiosRequestHeaders,
1112
): Promise<AttackResponse> => {
1213
const resp = await axios.get<AttackResponse>(`${getAPIURL()}/attacks`, {
1314
params,
15+
headers,
1416
})
1517
return resp.data
1618
}
1719

1820
export const getAttack = async (
1921
attackId: string,
22+
headers?: AxiosRequestHeaders,
2023
): Promise<AttackDetailResponse> => {
2124
const resp = await axios.get<AttackDetailResponse>(
2225
`${getAPIURL()}/attack/${attackId}`,
26+
{ headers },
2327
)
2428
return resp.data
2529
}
2630

27-
export const resolveAttack = async (attackId: string): Promise<void> => {
28-
await axios.put(`${getAPIURL()}/attack/${attackId}/resolve`)
31+
export const resolveAttack = async (
32+
attackId: string,
33+
headers?: AxiosRequestHeaders,
34+
): Promise<void> => {
35+
await axios.put(`${getAPIURL()}/attack/${attackId}/resolve`, { headers })
2936
}

frontend/src/api/dataFields/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import axios from "axios"
1+
import axios, { Axios, AxiosRequestHeaders } from "axios"
22
import { DataField, UpdateDataFieldClassesParams } from "@common/types"
33
import { getAPIURL } from "~/constants"
44

55
export const updateDataFieldClasses = async (
66
fieldId: string,
77
params: UpdateDataFieldClassesParams,
8+
headers?: AxiosRequestHeaders,
89
): Promise<DataField> => {
910
const resp = await axios.post<DataField>(
1011
`${getAPIURL()}/data-field/${fieldId}/update-classes`,
1112
{ ...params },
13+
{ headers },
1214
)
1315
return resp.data
1416
}
1517

16-
export const deleteDataField = async (fieldId: string): Promise<DataField> => {
18+
export const deleteDataField = async (
19+
fieldId: string,
20+
headers?: AxiosRequestHeaders,
21+
): Promise<DataField> => {
1722
const resp = await axios.delete<DataField>(
1823
`${getAPIURL()}/data-field/${fieldId}`,
24+
{ headers },
1925
)
2026
return resp.data
2127
}

frontend/src/api/endpoints/index.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import {
33
ApiEndpoint,
44
ApiEndpointDetailed,
@@ -9,11 +9,12 @@ import { getAPIURL } from "~/constants"
99

1010
export const getEndpoints = async (
1111
params: GetEndpointParams,
12+
headers?: AxiosRequestHeaders,
1213
): Promise<[ApiEndpoint[], number]> => {
1314
try {
1415
const resp = await axios.get<[ApiEndpoint[], number]>(
1516
`${getAPIURL()}/endpoints`,
16-
{ params },
17+
{ params, headers },
1718
)
1819
if (resp.status === 200 && resp.data) {
1920
return resp.data
@@ -27,10 +28,12 @@ export const getEndpoints = async (
2728

2829
export const getEndpoint = async (
2930
endpointId: string,
31+
headers?: AxiosRequestHeaders,
3032
): Promise<ApiEndpointDetailed> => {
3133
try {
3234
const resp = await axios.get<ApiEndpointDetailed>(
3335
`${getAPIURL()}/endpoint/${endpointId}`,
36+
{ headers },
3437
)
3538
if (resp.status === 200 && resp.data) {
3639
return resp.data
@@ -42,9 +45,15 @@ export const getEndpoint = async (
4245
}
4346
}
4447

45-
export const getHosts = async (): Promise<string[]> => {
48+
export const getHosts = async (
49+
headers?: AxiosRequestHeaders,
50+
): Promise<string[]> => {
4651
try {
47-
const resp = await axios.get<string[]>(`${getAPIURL()}/endpoints/hosts`)
52+
const resp = await axios.get<string[]>(
53+
`${getAPIURL()}/endpoints/hosts`,
54+
55+
{ headers },
56+
)
4857
if (resp.status === 200 && resp.data) {
4958
return resp.data
5059
}
@@ -55,10 +64,14 @@ export const getHosts = async (): Promise<string[]> => {
5564
}
5665
}
5766

58-
export const getUsage = async (endpointId: string): Promise<Usage[]> => {
67+
export const getUsage = async (
68+
endpointId: string,
69+
headers?: AxiosRequestHeaders,
70+
): Promise<Usage[]> => {
5971
try {
6072
const resp = await axios.get<Usage[]>(
6173
`${getAPIURL()}/endpoint/${endpointId}/usage`,
74+
{ headers },
6275
)
6376
if (resp.status === 200 && resp.data) {
6477
return resp.data
@@ -73,8 +86,13 @@ export const getUsage = async (endpointId: string): Promise<Usage[]> => {
7386
export const updateEndpointAuthenticated = async (
7487
endpointId: string,
7588
authenticated: boolean,
89+
headers?: AxiosRequestHeaders,
7690
): Promise<void> => {
77-
await axios.put(`${getAPIURL()}/endpoint/${endpointId}/authenticated`, {
78-
authenticated,
79-
})
91+
await axios.put(
92+
`${getAPIURL()}/endpoint/${endpointId}/authenticated`,
93+
{
94+
authenticated,
95+
},
96+
{ headers },
97+
)
8098
}

frontend/src/api/home/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import { Summary, InstanceSettings } from "@common/types"
33
import { getAPIURL } from "~/constants"
44

5-
export const getSummary = async (): Promise<Summary> => {
5+
export const getSummary = async (
6+
headers?: AxiosRequestHeaders,
7+
): Promise<Summary> => {
68
try {
7-
const resp = await axios.get<Summary>(`${getAPIURL()}/summary`)
9+
const resp = await axios.get<Summary>(`${getAPIURL()}/summary`, { headers })
810
if (resp.status === 200 && resp.data) {
911
return resp.data
1012
}
@@ -15,10 +17,13 @@ export const getSummary = async (): Promise<Summary> => {
1517
}
1618
}
1719

18-
export const getInstanceSettings = async (): Promise<InstanceSettings> => {
20+
export const getInstanceSettings = async (
21+
headers?: AxiosRequestHeaders,
22+
): Promise<InstanceSettings> => {
1923
try {
2024
const resp = await axios.get<InstanceSettings>(
2125
`${getAPIURL()}/instance-settings`,
26+
{ headers },
2227
)
2328
return resp.data
2429
} catch (err) {

frontend/src/api/home/updateEmail.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import { getAPIURL } from "~/constants"
33

44
export const updateEmail = async (
55
email: string,
66
skip: boolean,
7+
headers?: AxiosRequestHeaders,
78
): Promise<void> => {
8-
return axios.put(`${getAPIURL()}/instance-settings`, { email, skip })
9+
return axios.put(
10+
`${getAPIURL()}/instance-settings`,
11+
{ email, skip },
12+
{ headers },
13+
)
914
}

frontend/src/api/keys/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import axios from "axios"
1+
import axios, { AxiosRequestHeaders } from "axios"
22
import { ApiKey } from "@common/types"
33
import { getAPIURL } from "~/constants"
44

5-
export const getKeys = async (): Promise<Array<ApiKey>> => {
5+
export const getKeys = async (
6+
headers?: AxiosRequestHeaders,
7+
): Promise<Array<ApiKey>> => {
68
try {
7-
const resp = await axios.get<Array<ApiKey>>(`${getAPIURL()}/keys/list`)
9+
const resp = await axios.get<Array<ApiKey>>(`${getAPIURL()}/keys/list`, {
10+
headers,
11+
})
812
if (resp.status === 200 && resp.data) {
913
return resp.data
1014
}
@@ -13,9 +17,14 @@ export const getKeys = async (): Promise<Array<ApiKey>> => {
1317
throw err
1418
}
1519
}
16-
export const deleteKey = async (key_name: string): Promise<void> => {
20+
export const deleteKey = async (
21+
key_name: string,
22+
headers?: AxiosRequestHeaders,
23+
): Promise<void> => {
1724
try {
18-
const resp = await axios.delete(`${getAPIURL()}/keys/${key_name}/delete`)
25+
const resp = await axios.delete(`${getAPIURL()}/keys/${key_name}/delete`, {
26+
headers,
27+
})
1928
if (resp.status === 200 && resp.data) {
2029
return
2130
} else {
@@ -31,13 +40,15 @@ export const deleteKey = async (key_name: string): Promise<void> => {
3140
}
3241
export const addKey = async (
3342
key_name: string,
43+
headers?: AxiosRequestHeaders,
3444
): Promise<ApiKey & { apiKey: string }> => {
3545
try {
3646
const resp = await axios.post<ApiKey & { apiKey: string }>(
3747
`${getAPIURL()}/keys/create`,
3848
{
3949
name: key_name,
4050
},
51+
{ headers },
4152
)
4253
if (resp.status === 200 && resp.data) {
4354
return resp.data

0 commit comments

Comments
 (0)