Skip to content

Commit bc88751

Browse files
authored
Routine maintenance 2023-07 (#111)
* Backward compatible upgrades * Update dev dependencies * Upgrade husky * Update yarn.lock * Increase test coverage * Upgrade js-cookie to latest * Upgrade query string to latest non-esm version * Upgrade is-promise to latest * Bump minor * Update travis config * Remove invisible character * Remove another character * Improve docs * Add publish action
1 parent 5248bb3 commit bc88751

File tree

14 files changed

+5248
-6318
lines changed

14 files changed

+5248
-6318
lines changed

.github/workflows/publish.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish
2+
run-name: ${{ github.actor }} published a new release
3+
on:
4+
release:
5+
types: [published]
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Check out latest code
11+
uses: actions/checkout@v3
12+
- name: Set up node
13+
uses: actions/setup-node@v3
14+
with:
15+
node-version: '18.x'
16+
registry-url: 'https://registry.npmjs.org'
17+
scope: '@launchpadlab'
18+
- name: Install dependencies
19+
run: HUSKY=0 yarn --frozen-lockfile
20+
- name: Publish package on NPM
21+
run: npm publish
22+
env:
23+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
yarn run docs && git add docs.md

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
2-
node_js:
3-
- '8'
2+
dist: focal
43
cache: yarn
54
before_script:
65
- 'yarn lint'

docs.md

Lines changed: 73 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
### Table of Contents
44

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]
2727

2828
## api
2929

@@ -32,12 +32,12 @@ Provides functions to make API requests with specified HTTP methods.
3232

3333
The functions are as follows:
3434

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
4141

4242
Each function can be passed an `options` object, which will eventually be forwarded
4343
to the [Fetch API][24].
@@ -66,8 +66,8 @@ Note: This configuration can always be overridden by passing in options manually
6666

6767
### Parameters
6868

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
7171

7272
### Examples
7373

@@ -88,8 +88,8 @@ Note: This configuration can always be overridden by passing in options manually
8888

8989
### Parameters
9090

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
9393

9494
### Examples
9595

@@ -108,7 +108,7 @@ This can be used to create more complex `before` hooks for [http][23].
108108

109109
### Parameters
110110

111-
- `middlewares` **...[Function][27]** Functions that receive and return request options
111+
* `middlewares` **...[Function][27]** Functions that receive and return request options
112112

113113
### Examples
114114

@@ -156,27 +156,29 @@ Any one of these settings can be overriden using the passed-in config object.
156156

157157
In addition to the normal Fetch API settings, the config object may also contain these special settings just for `http`:
158158

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]
176178

177179
### Parameters
178180

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.
180182

181183
### Examples
182184

@@ -209,17 +211,17 @@ An error class that is thrown by the [http][23] module when a request fails.
209211

210212
In addition to the standard [Error][30] attributes, instances of `HttpError` include the following:
211213

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>`
216218

217219
### Parameters
218220

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 `{}`)
223225

224226
### Examples
225227

@@ -237,17 +239,14 @@ http('/bad-route').catch(err => console.log(err.name)) // -> "HttpError"
237239
A helper function to determine if the current user is authenticated.
238240
This function accepts an object argument with a `context` key.
239241

240-
This returns true when the LP Auth Api cookie exists and contains a
241-
token.
242-
If the `context` key is present, this function returns true if the user is
243-
both authenticated and the specified context is present.
242+
*Note, this does not **validate** the token, it only checks for
243+
presence, validation must be done on the server.*
244244

245-
Note, this does not **validate** the token, it only checks for
246-
presence, validation must be done on the server.
245+
Type: [Function][27]
247246

248247
### Parameters
249248

250-
- `options` **[Object][26]** config object containing the context (optional, default `{}`)
249+
* `options` **[Object][26]** config object containing the context (optional, default `{}`)
251250

252251
### Examples
253252

@@ -273,35 +272,29 @@ isAuthenticated({ context: 'admin' }) // false
273272
isAuthenticated({ context: 'non-admin' }) // false
274273
```
275274

276-
Returns **[Boolean][33]**
275+
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.
277278

278279
## getAuthenticationContext
279280

280-
A helper function to retrieve the authentication context for the
281+
A helper function to retrieve the authentication context for the
281282
authenticated user.
282283

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-
289284
### Examples
290285

291286
```javascript
292287
// After an 'admin' signs in
293288
getAuthenticationContext() // 'admin'
294289

295290
// After a user with no context signs in
296-
getAuthenticationContext() // undefined
291+
getAuthenticationContext() // undefined
297292

298293
// After sign out
299294
getAuthenticationContext() // undefined
300-
301-
*
302295
```
303296

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.
305298

306299
[1]: #api
307300

@@ -368,3 +361,5 @@ Returns **[String][32]**
368361
[32]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
369362

370363
[33]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
364+
365+
[34]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
testEnvironment: 'jsdom',
23
'setupFiles': [
34
'./test/setup.js',
45
],

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@launchpadlab/lp-requests",
3-
"version": "4.2.2",
3+
"version": "4.3.0",
44
"description": "Request helpers",
55
"main": "lib/index.js",
66
"scripts": {
@@ -9,12 +9,11 @@
99
"build:development": "mkdir -p lib && babel src --watch --out-dir lib",
1010
"clean": "rm -rf lib",
1111
"docs": "documentation build src/index.js -f md -o docs.md",
12+
"prepare": "husky install && yarn run lint && yarn run clean && yarn run build",
1213
"lint": "eslint src",
13-
"precommit": "yarn run docs && git add docs.md",
14-
"prepublish": "yarn run lint && yarn run clean && yarn run build",
14+
"report-coverage": "codeclimate-test-reporter < coverage/lcov.info",
1515
"test": "jest --coverage",
16-
"test:coverage": "jest --coverage",
17-
"report-coverage": "codeclimate-test-reporter < coverage/lcov.info"
16+
"test:coverage": "jest --coverage"
1817
},
1918
"repository": "launchpadlab/lp-requests",
2019
"homepage": "https://github.com/launchpadlab/lp-requests",
@@ -33,21 +32,22 @@
3332
"@babel/core": "^7.5.5",
3433
"@babel/polyfill": "^7.4.4",
3534
"@launchpadlab/babel-preset": "^2.1.0",
36-
"@launchpadlab/eslint-config": "^2.2.3",
35+
"@launchpadlab/eslint-config": "^3.0.1",
3736
"codeclimate-test-reporter": "^0.5.0",
38-
"documentation": "^12.1.1",
39-
"eslint": "^6.1.0",
40-
"husky": "^3.0.3",
41-
"jest": "^24.8.0"
37+
"documentation": "^14.0.2",
38+
"eslint": "^8.46.0",
39+
"husky": "^8.0.0",
40+
"jest": "^29.6.2",
41+
"jest-environment-jsdom": "^29.6.2"
4242
},
4343
"dependencies": {
4444
"Base64": "^1.0.1",
4545
"humps": "^2.0.0",
46-
"is-promise": "^2.1.0",
46+
"is-promise": "^4.0.0",
4747
"isomorphic-fetch": "^3.0.0",
48-
"js-cookie": "^2.1.4",
48+
"js-cookie": "^3.0.5",
4949
"lodash": "^4.17.4",
50-
"query-string": "^5.0.0",
50+
"query-string": "^7.1.3",
5151
"url-join": "^4.0.0"
5252
}
5353
}

src/get-authentication-context.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import { getLpAuthCookie, parseObject } from './utils'
22

33
/**
4-
* A helper function to retrieve the authentication context for the
4+
* A helper function to retrieve the authentication context for the
55
* 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.
126
*
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.
10+
*
1411
* @example
15-
*
12+
*
1613
* // After an 'admin' signs in
1714
* getAuthenticationContext() // 'admin'
18-
*
15+
*
1916
* // After a user with no context signs in
20-
* getAuthenticationContext() // undefined
17+
* getAuthenticationContext() // undefined
2118
*
2219
* // After sign out
2320
* getAuthenticationContext() // undefined
24-
*
25-
**/
21+
*
22+
*/
2623

2724
export default function getAuthenticationContext () {
2825
const lpAuthCookie = getLpAuthCookie()

0 commit comments

Comments
 (0)