-
Notifications
You must be signed in to change notification settings - Fork 28
/
api-client-helpers.ts
116 lines (102 loc) · 3.18 KB
/
api-client-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { aws4Interceptor } from 'aws4-axios'
import globalAxios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios'
import { USER_AGENT } from '../constants'
import {
APIConfigurationParameters,
DEFAULT_API_BASE_PATH,
ModelErrorContainer,
SellingPartnerMismatchRegionError,
SellingPartnerNotFoundRegionError,
} from '../types'
import { apiErrorFactory } from './api-error-factory'
const regions = new Map<string, string[]>(
Object.entries({
'us-east-1': [
'https://sellingpartnerapi-na.amazon.com',
'https://sandbox.sellingpartnerapi-na.amazon.com',
],
'eu-west-1': [
'https://sellingpartnerapi-eu.amazon.com',
'https://sandbox.sellingpartnerapi-eu.amazon.com',
],
'us-west-2': [
'https://sellingpartnerapi-fe.amazon.com',
'https://sandbox.sellingpartnerapi-fe.amazon.com',
],
}),
)
export class ApiClientHelpers {
static isAPIModelError(
axiosError: AxiosError<unknown | ModelErrorContainer>,
): axiosError is AxiosError<ModelErrorContainer> {
const isError = (axiosError as AxiosError<ModelErrorContainer>).response?.data.errors.every(
(element) => 'code' in element && 'message' in element,
)
return !!isError
}
static getAxiosInstance(parameters: APIConfigurationParameters): AxiosInstance {
let axiosInstance: AxiosInstance
const { axios } = parameters
if (axios) {
axiosInstance = axios
} else {
const { accessToken, credentials, region, roleArn } =
ApiClientHelpers.validateRegion(parameters)
axiosInstance = globalAxios.create({
headers: {
'user-agent': USER_AGENT,
'x-amz-access-token': accessToken ?? '',
},
})
axiosInstance.interceptors.request.use(
aws4Interceptor(
{
region,
service: 'execute-api',
assumeRoleArn: roleArn,
},
credentials,
),
)
}
axiosInstance.interceptors.response.use(
(response: AxiosResponse) => response,
(error: AxiosError) => {
if (ApiClientHelpers.isAPIModelError(error)) {
throw apiErrorFactory(error)
}
throw error
},
)
return axiosInstance
}
static extractRegion(basePath: string): string {
for (const [region, basePaths] of regions) {
if (basePaths.includes(basePath)) {
return region
}
}
throw new SellingPartnerNotFoundRegionError(basePath)
}
static validateRegion(parameters: APIConfigurationParameters): APIConfigurationParameters {
if (parameters.basePath && !parameters.region) {
return {
...parameters,
region: ApiClientHelpers.extractRegion(parameters.basePath),
}
}
if (!parameters.basePath && !parameters.region) {
return {
...parameters,
region: ApiClientHelpers.extractRegion(DEFAULT_API_BASE_PATH),
}
}
if (!parameters.basePath && parameters.region) {
const defaultRegion = ApiClientHelpers.extractRegion(DEFAULT_API_BASE_PATH)
if (defaultRegion !== parameters.region) {
throw new SellingPartnerMismatchRegionError(defaultRegion, parameters.region)
}
}
return parameters
}
}