-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.ts
147 lines (129 loc) · 3.26 KB
/
base.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* tslint:disable */
/* eslint-disable */
/**
* Iam
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
* Contact: info@affinidi.com
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
import type { Configuration } from './configuration'
// Some imports not used depending on template conditions
// @ts-ignore
import type { AxiosPromise, AxiosInstance, RawAxiosRequestConfig } from 'axios'
import globalAxios, { AxiosError } from 'axios'
import axiosRetry, { IAxiosRetryConfig } from 'axios-retry'
export class SdkError extends Error {
private readonly details: undefined
private readonly traceId: string
private readonly httpStatusCode: number | undefined
constructor(originalError: unknown = {}) {
const isAxiosError = originalError instanceof AxiosError
if (!isAxiosError) {
throw originalError
}
super(originalError.response?.data?.message)
this.name = originalError.response?.data?.name
this.details = originalError.response?.data?.details
this.message = originalError.response?.data?.message
this.traceId = originalError.response?.data?.traceId
this.httpStatusCode = originalError.response?.status
}
}
export const BASE_PATH = 'https://apse1.api.affinidi.io/iam'.replace(/\/+$/, '')
const DEFAULT_REQUEST_RETRIES = 3
/**
*
* @export
*/
export const COLLECTION_FORMATS = {
csv: ',',
ssv: ' ',
tsv: '\t',
pipes: '|',
}
/**
*
* @export
* @interface RequestArgs
*/
export interface RequestArgs {
url: string
options: RawAxiosRequestConfig
}
type RetryConfig = {
retries?: number
isExponentialDelay?: boolean
}
/**
*
* @export
* @class BaseAPI
*/
export class BaseAPI {
protected configuration: Configuration | undefined
protected retryConfig: IAxiosRetryConfig
constructor(
configuration?: Configuration,
retryConfig?: RetryConfig,
protected basePath: string = BASE_PATH,
protected axios: AxiosInstance = globalAxios,
) {
if (configuration) {
this.configuration = configuration
this.basePath = configuration.basePath ?? basePath
}
this.retryConfig = {
retries:
retryConfig?.retries >= 0 &&
retryConfig?.retries <= DEFAULT_REQUEST_RETRIES
? retryConfig.retries
: DEFAULT_REQUEST_RETRIES,
retryDelay: retryConfig?.isExponentialDelay
? axiosRetry.exponentialDelay
: () => {
return 0
},
retryCondition: (error: AxiosError) => {
if (
axiosRetry.isNetworkOrIdempotentRequestError(error) ||
axiosRetry.isNetworkError(error)
) {
return true
}
throw new SdkError(error)
},
}
axiosRetry(globalAxios, this.retryConfig)
}
}
/**
*
* @export
* @class RequiredError
* @extends {Error}
*/
export class RequiredError extends Error {
constructor(
public field: string,
msg?: string,
) {
super(msg)
this.name = 'RequiredError'
}
}
interface ServerMap {
[key: string]: {
url: string
description: string
}[]
}
/**
*
* @export
*/
export const operationServerMap: ServerMap = {}