You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs.md
+73-78Lines changed: 73 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,28 +2,28 @@
2
2
3
3
### Table of Contents
4
4
5
-
-[api][1]
6
-
-[Examples][2]
7
-
-[configureApi][3]
8
-
-[Parameters][4]
9
-
-[Examples][5]
10
-
-[configureHttp][6]
11
-
-[Parameters][7]
12
-
-[Examples][8]
13
-
-[composeMiddleware][9]
14
-
-[Parameters][10]
15
-
-[Examples][11]
16
-
-[http][12]
17
-
-[Parameters][13]
18
-
-[Examples][14]
19
-
-[HttpError][15]
20
-
-[Parameters][16]
21
-
-[Examples][17]
22
-
-[isAuthenticated][18]
23
-
-[Parameters][19]
24
-
-[Examples][20]
25
-
-[getAuthenticationContext][21]
26
-
-[Examples][22]
5
+
*[api][1]
6
+
*[Examples][2]
7
+
*[configureApi][3]
8
+
*[Parameters][4]
9
+
*[Examples][5]
10
+
*[configureHttp][6]
11
+
*[Parameters][7]
12
+
*[Examples][8]
13
+
*[composeMiddleware][9]
14
+
*[Parameters][10]
15
+
*[Examples][11]
16
+
*[http][12]
17
+
*[Parameters][13]
18
+
*[Examples][14]
19
+
*[HttpError][15]
20
+
*[Parameters][16]
21
+
*[Examples][17]
22
+
*[isAuthenticated][18]
23
+
*[Parameters][19]
24
+
*[Examples][20]
25
+
*[getAuthenticationContext][21]
26
+
*[Examples][22]
27
27
28
28
## api
29
29
@@ -32,12 +32,12 @@ Provides functions to make API requests with specified HTTP methods.
32
32
33
33
The functions are as follows:
34
34
35
-
-`get(url, options)` sends a `'GET'` request
36
-
-`patch(url, body, options)` sends a `'PATCH'` request
37
-
-`post(url, body, options)` sends a `'POST'` request
38
-
-`put(url, body, options)` sends a `'PUT'` request
39
-
-`destroy(url, body, options)` sends a `'DELETE'` request
40
-
-`call(url, method, body, options)` sends a request with specified method
35
+
*`get(url, options)` sends a `'GET'` request
36
+
*`patch(url, body, options)` sends a `'PATCH'` request
37
+
*`post(url, body, options)` sends a `'POST'` request
38
+
*`put(url, body, options)` sends a `'PUT'` request
39
+
*`destroy(url, body, options)` sends a `'DELETE'` request
40
+
*`call(url, method, body, options)` sends a request with specified method
41
41
42
42
Each function can be passed an `options` object, which will eventually be forwarded
43
43
to the [Fetch API][24].
@@ -66,8 +66,8 @@ Note: This configuration can always be overridden by passing in options manually
66
66
67
67
### Parameters
68
68
69
-
-`config`**[Object][26]** An api configuration object
70
-
-`baseApi`**[Object][26]?** An existing api instance to extend with the configuration
69
+
*`config`**[Object][26]** An api configuration object
70
+
*`baseApi`**[Object][26]?** An existing api instance to extend with the configuration
71
71
72
72
### Examples
73
73
@@ -88,8 +88,8 @@ Note: This configuration can always be overridden by passing in options manually
88
88
89
89
### Parameters
90
90
91
-
-`config`**[Object][26]** An http configuration object
92
-
-`baseHttp`**[Object][26]?** An existing http instance to extend with the configuration
91
+
*`config`**[Object][26]** An http configuration object
92
+
*`baseHttp`**[Object][26]?** An existing http instance to extend with the configuration
93
93
94
94
### Examples
95
95
@@ -108,7 +108,7 @@ This can be used to create more complex `before` hooks for [http][23].
108
108
109
109
### Parameters
110
110
111
-
-`middlewares`**...[Function][27]** Functions that receive and return request options
111
+
*`middlewares`**...[Function][27]** Functions that receive and return request options
112
112
113
113
### Examples
114
114
@@ -156,27 +156,29 @@ Any one of these settings can be overriden using the passed-in config object.
156
156
157
157
In addition to the normal Fetch API settings, the config object may also contain these special settings just for `http`:
158
158
159
-
-`url`: The url for the request. This can also be passed in directly as the first argument (see shorthand example).
160
-
-`root`: A path to be appended to the given url (default=`''`).
161
-
-`crsf`: The name of the `meta` tag containing the CSRF token (default=`'csrf-token'`). This can be set to `false` to prevent a token from being sent.
162
-
-`before`: A function that's called before the request executes. This function is passed the request options and its return value will be added to those options.
163
-
It can also return a promise that resolves to a new options object.
164
-
-`bearerToken`: A token to use for bearer auth. If provided, `http` will add the header `"Authorization": "Bearer <bearerToken>"` to the request.
165
-
-`onSuccess`: A function that will be called if the request succeeds. It will be passed the successful response. If it returns a value, `http` will resolve with this value instead of the response.
166
-
-`onFailure`: A function that will be called if the request fails. It will be passed the error that was thrown during the request. If it returns a value, `http` will reject with this value instead of the default error.
167
-
-`successDataPath`: A path to response data that the promise will resolve with.
168
-
-`failureDataPath`: A path to the errors that will be included in the HttpError object (default=`'errors'`)
169
-
-`query`: An object that will be transformed into a query string and appended to the request URL.
170
-
-`overrideHeaders`: A boolean flag indicating whether or not default headers should be included in the request (default=`false`).
171
-
-`camelizeResponse`: A boolean flag indicating whether or not to camelize the response keys (default=`true`). The helper function that does this is also exported from this library as `camelizeKeys`.
172
-
-`decamelizeBody`: A boolean flag indicating whether or not to decamelize the body keys (default=`true`). The helper function that does this is also exported from this library as `decamelizeKeys`.
173
-
-`decamelizeQuery`: A boolean flag indicating whether or not to decamelize the query string keys (default=`true`).
174
-
-`parseJsonStrictly`: A boolean flag indicating whether or not to return the text of the response body if JSON parsing fails (default=`true`). If set to `true` and invalid JSON is received in the response, then `null` will be returned instead.
175
-
-`auth`: An object with the following keys `{ username, password }`. If present, `http` will use [basic auth][28], adding the header `"Authorization": "Basic <authToken>"` to the request, where `<authToken>` is a base64 encoded string of `username:password`.
159
+
*`url`: The url for the request. This can also be passed in directly as the first argument (see shorthand example).
160
+
*`root`: A path to be appended to the given url (default=`''`).
161
+
*`crsf`: The name of the `meta` tag containing the CSRF token (default=`'csrf-token'`). This can be set to `false` to prevent a token from being sent.
162
+
*`before`: A function that's called before the request executes. This function is passed the request options and its return value will be added to those options.
163
+
It can also return a promise that resolves to a new options object.
164
+
*`bearerToken`: A token to use for bearer auth. If provided, `http` will add the header `"Authorization": "Bearer <bearerToken>"` to the request.
165
+
*`onSuccess`: A function that will be called if the request succeeds. It will be passed the successful response. If it returns a value, `http` will resolve with this value instead of the response.
166
+
*`onFailure`: A function that will be called if the request fails. It will be passed the error that was thrown during the request. If it returns a value, `http` will reject with this value instead of the default error.
167
+
*`successDataPath`: A path to response data that the promise will resolve with.
168
+
*`failureDataPath`: A path to the errors that will be included in the HttpError object (default=`'errors'`)
169
+
*`query`: An object that will be transformed into a query string and appended to the request URL.
170
+
*`overrideHeaders`: A boolean flag indicating whether or not default headers should be included in the request (default=`false`).
171
+
*`camelizeResponse`: A boolean flag indicating whether or not to camelize the response keys (default=`true`). The helper function that does this is also exported from this library as `camelizeKeys`.
172
+
*`decamelizeBody`: A boolean flag indicating whether or not to decamelize the body keys (default=`true`). The helper function that does this is also exported from this library as `decamelizeKeys`.
173
+
*`decamelizeQuery`: A boolean flag indicating whether or not to decamelize the query string keys (default=`true`).
174
+
*`parseJsonStrictly`: A boolean flag indicating whether or not to return the text of the response body if JSON parsing fails (default=`true`). If set to `true` and invalid JSON is received in the response, then `null` will be returned instead.
175
+
*`auth`: An object with the following keys `{ username, password }`. If present, `http` will use [basic auth][28], adding the header `"Authorization": "Basic <authToken>"` to the request, where `<authToken>` is a base64 encoded string of `username:password`.
176
+
177
+
Type: [Function][27]
176
178
177
179
### Parameters
178
180
179
-
-`config`**[Object][26]** An object containing config information for the `Fetch` request, as well as the extra keys noted above.
181
+
*`config`**[Object][26]** An object containing config information for the `Fetch` request, as well as the extra keys noted above.
180
182
181
183
### Examples
182
184
@@ -209,17 +211,17 @@ An error class that is thrown by the [http][23] module when a request fails.
209
211
210
212
In addition to the standard [Error][30] attributes, instances of `HttpError` include the following:
211
213
212
-
-`status`: the status code of the response
213
-
-`statusText`: the status text of the response
214
-
-`response`: the full response object
215
-
-`message`: A readable error message with format `<status> - <statusText>`
214
+
*`status`: the status code of the response
215
+
*`statusText`: the status text of the response
216
+
*`response`: the full response object
217
+
*`message`: A readable error message with format `<status> - <statusText>`
216
218
217
219
### Parameters
218
220
219
-
-`status`**[Number][31]** the status code of the response
220
-
-`statusText`**[String][32]** the status text of the response
221
-
-`response`**[Object][26]** the full response object
222
-
-`errors`**[Object][26]** an object containing error messages associated with the response (optional, default `{}`)
221
+
*`status`**[Number][31]** the status code of the response
222
+
*`statusText`**[String][32]** the status text of the response
223
+
*`response`**[Object][26]** the full response object
224
+
*`errors`**[Object][26]** an object containing error messages associated with the response (optional, default `{}`)
Returns **[Boolean][33]** True when the LP Auth Api cookie exists and contains a token.
276
+
If the `context` key is present, this function returns true if the user is
277
+
both authenticated and the specified context is present.
277
278
278
279
## getAuthenticationContext
279
280
280
-
A helper function to retrieve the authentication context for the
281
+
A helper function to retrieve the authentication context for the
281
282
authenticated user.
282
283
283
-
This function returns the context string when the LP Auth Api cookie exists,
284
-
contains a valid token, and contains a context.
285
-
286
-
This function returns `undefined` when there is no context present,
287
-
or if the LP Auth API cookie does not exist.
288
-
289
284
### Examples
290
285
291
286
```javascript
292
287
// After an 'admin' signs in
293
288
getAuthenticationContext() // 'admin'
294
289
295
290
// After a user with no context signs in
296
-
getAuthenticationContext() // undefined
291
+
getAuthenticationContext() // undefined
297
292
298
293
// After sign out
299
294
getAuthenticationContext() // undefined
300
-
301
-
*
302
295
```
303
296
304
-
Returns **[String][32]**
297
+
Returns **([String][32] | [Undefined][34])**The context string when the LP Auth Api cookie exists, contains a valid token, and contains a context. Returns `undefined` when there is no context present, or if the LP Auth API cookie does not exist.
Copy file name to clipboardExpand all lines: src/get-authentication-context.js
+10-13Lines changed: 10 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,25 @@
1
1
import{getLpAuthCookie,parseObject}from'./utils'
2
2
3
3
/**
4
-
* A helper function to retrieve the authentication context for the
4
+
* A helper function to retrieve the authentication context for the
5
5
* authenticated user.
6
-
*
7
-
* This function returns the context string when the LP Auth Api cookie exists,
8
-
* contains a valid token, and contains a context.
9
-
*
10
-
* This function returns `undefined` when there is no context present,
11
-
* or if the LP Auth API cookie does not exist.
12
6
*
13
-
* @returns {String}
7
+
* @name getAuthenticationContext
8
+
* @type Function
9
+
* @returns {String|Undefined} The context string when the LP Auth Api cookie exists, contains a valid token, and contains a context. Returns `undefined` when there is no context present, or if the LP Auth API cookie does not exist.
0 commit comments