@@ -7,8 +7,8 @@ below you can find the properties and their usage.
7
7
8
8
``` typescript
9
9
export const OpenAPI: OpenAPIConfig = {
10
- BASE: ' http://localhost:3000/my- api' ,
11
- VERSION: ' 1 .0' ,
10
+ BASE: ' http://localhost:3000/api' ,
11
+ VERSION: ' 2 .0' ,
12
12
WITH_CREDENTIALS: false ,
13
13
CREDENTIALS: ' include' ,
14
14
TOKEN: undefined ,
@@ -19,31 +19,126 @@ export const OpenAPI: OpenAPIConfig = {
19
19
};
20
20
```
21
21
22
- ## Properties
22
+ Properties
23
+ ===
23
24
24
- ` OpenAPI.BASE `
25
- Test
25
+ ### ` OpenAPI.BASE `
26
26
27
- ` OpenAPI.VERSION `
28
- Test
27
+ The base path of the OpenAPI server, this is generated from the spec,
28
+ but can be overwritten to switch servers.
29
29
30
- ` OpenAPI.WITH_CREDENTIALS `
31
- Test
30
+ ``` typescript
31
+ if (process .env === ' development' ) {
32
+ OpenAPI .BASE = ' http://staging.company.com:3000/api' ;
33
+ }
34
+ if (process .env === ' production' ) {
35
+ OpenAPI .BASE = ' /api' ;
36
+ }
37
+ ```
38
+
39
+ ### ` OpenAPI.VERSION `
40
+
41
+ The version param in the OpenAPI paths ` {api-version} ` . The version is taken from the spec,
42
+ but can be updated to call multiple versions of the same OpenAPI backend.
43
+
44
+ ### ` OpenAPI.WITH_CREDENTIALS `
45
+
46
+ Similar to the [ withCredentials] ( https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials )
47
+ property of the XHR specification. When set to true, cross-site requests should be made
48
+ using credentials such as cookies, authorization headers, etc.
49
+
50
+ ### ` OpenAPI.CREDENTIALS `
51
+
52
+ Similar to the [ credentials] ( https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#sending_a_request_with_credentials_included )
53
+ property of the Fetch specification. When ` OpenAPI.WITH_CREDENTIALS ` is set to true,
54
+ this property controls the specific implementation for Fetch and Node-Fetch clients.
55
+ Valid values are: ` include ` , ` omit ` and ` same-origin ` .
56
+
57
+ ### ` OpenAPI.TOKEN `
58
+
59
+ Set the Bearer authentication token to use for the requests. This needs to be a valid
60
+ (non-expired) token, otherwise the request will fail. The property can be updated as often
61
+ as you want, this is useful for scenario's where the token would automatically refresh
62
+ after x minutes. This property also allows you to use an ` async ` method that will be resolved
63
+ before requests are made.
64
+
65
+ ``` typescript
66
+ OpenAPI .TOKEN = ' MY_TOKEN' ;
67
+
68
+ OpenAPI .TOKEN = async () => {
69
+ // Note: loading this from a JSON file is not recommended ;-)
70
+ const response = await fetch (' configuration.json' );
71
+ const { token } = response .json ();
72
+ return token ;
73
+ };
74
+ ```
75
+
76
+ ### ` OpenAPI.USERNAME `
77
+
78
+ Set the basic authentication username, although not recommended, the basic authentication
79
+ header is still supported. The username and password hash will be calculated by the client
80
+ before sending the request. This property also allows you to use an ` async ` method that
81
+ will be resolved before requests are made.
82
+
83
+ ``` typescript
84
+ OpenAPI .USERNAME = ' john' ;
32
85
33
- ` OpenAPI.CREDENTIALS `
34
- Test
86
+ OpenAPI .USERNAME = async () => {
87
+ // Note: loading this from a JSON file is not recommended ;-)
88
+ const response = await fetch (' configuration.json' );
89
+ const { username } = response .json ();
90
+ return username ;
91
+ };
92
+ ```
93
+
94
+ ### ` OpenAPI.PASSWORD `
95
+
96
+ Set the basic authentication password. See ` OpenAPI.USERNAME ` for more info.
97
+
98
+ ``` typescript
99
+ OpenAPI .PASSWORD = ' welcome123' ;
35
100
36
- ` OpenAPI.TOKEN `
37
- Test
101
+ OpenAPI .PASSWORD = async () => {
102
+ // Note: loading this from a JSON file is not recommended ;-)
103
+ const response = await fetch (' configuration.json' );
104
+ const { password } = response .json ();
105
+ return password ;
106
+ };
107
+ ```
38
108
39
- ` OpenAPI.USERNAME `
40
- Test
109
+ ### ` OpenAPI.HEADERS `
41
110
42
- ` OpenAPI.PASSWORD `
43
- Test
111
+ This property allows you to specify additional headers to send for each request. This can be useful
112
+ for adding headers that are not generated through the spec. Or adding headers for tracking purposes.
113
+ This property also allows you to use an ` async ` method that will be resolved before requests are made.
44
114
45
- ` OpenAPI.HEADERS `
46
- Test
115
+ ``` typescript
116
+ OpenAPI .HEADERS = {
117
+ ' x-navigator' : window .navigator .appVersion ,
118
+ ' x-environment' : process .env ,
119
+ ' last-modified' : ' Wed, 21 Oct 2015 07:28:00 GMT' ,
120
+ };
47
121
48
- ` OpenAPI.ENCODE_PATH `
49
- Test
122
+ OpenAPI .HEADERS = async () => {
123
+ // Note: loading this from a JSON file is not recommended ;-)
124
+ const response = await fetch (' configuration.json' );
125
+ const { headers } = response .json ();
126
+ return headers ;
127
+ };
128
+ ```
129
+
130
+ ### ` OpenAPI.ENCODE_PATH `
131
+
132
+ By default, all path parameters are encoded using the ` encodeURI ` method. This will convert invalid
133
+ URL characters, for example spaces, backslashes, etc. However, you might want to make the encoding
134
+ more strict due to security restrictions. So you can set this to ` encodeURIComponent ` to encode
135
+ most non-alphanumerical characters to percentage encoding. Or set a customer encoder that just
136
+ replaces some special characters.
137
+
138
+ ``` typescript
139
+ OpenAPI .ENCODE_PATH = encodeURIComponent ;
140
+
141
+ OpenAPI .ENCODE_PATH = (value : string ) => {
142
+ return value .replace (' :' , ' _' );
143
+ };
144
+ ```
0 commit comments