Skip to content

Commit

Permalink
Parameterize auth mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaehring committed Jan 8, 2022
1 parent 9364307 commit 7ebc20f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
19 changes: 19 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ associated metadata and schemata.</p>
<dd><p>The methods for transmitting farmOS data structures, such as assets, logs,
etc, to a farmOS server.</p>
</dd>
<dt><a href="#AuthorizationMixin">AuthorizationMixin</a> ⇒ <code>Object</code></dt>
<dd></dd>
<dt><a href="#FarmClient">FarmClient</a> : <code>Object</code></dt>
<dd><p>A collection of functions for transmitting farmOS data structures to and
from a farmOS Drupal 9 server using JSON:API.</p>
Expand Down Expand Up @@ -257,6 +259,22 @@ etc, to a farmOS server.
| send | <code>sendEntity</code> |
| delete | <code>deleteEntity</code> |

<a name="AuthorizationMixin"></a>

## AuthorizationMixin ⇒ <code>Object</code>
**Kind**: global typedef

| Param | Type |
| --- | --- |
| request | <code>module:axios~AxiosInstance</code> |
| authOptions | <code>Object</code> |

**Properties**

| Name | Type |
| --- | --- |
| authOptions.host | <code>String</code> |

<a name="FarmClient"></a>

## FarmClient : <code>Object</code>
Expand Down Expand Up @@ -297,6 +315,7 @@ Create a farm client for interacting with farmOS servers.

| Name | Type |
| --- | --- |
| options.auth | [<code>AuthorizationMixin</code>](#AuthorizationMixin) |
| options.clientId | <code>String</code> |
| options.getToken | <code>function</code> |
| options.setToken | <code>function</code> |
Expand Down
38 changes: 19 additions & 19 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ import fetchSchema from './schema.js';
* @property {import('./delete.js').deleteEntity} delete
*/

/**
* @typedef {Function} AuthMixin
* @param {import('axios').AxiosInstance} request
* @param {Object} authOptions
* @property {String} authOptions.host
* @returns {Object<string,function>}
*/

/** A collection of functions for transmitting farmOS data structures to and
* from a farmOS Drupal 9 server using JSON:API.
* @typedef {Object} FarmClient
* @property {Function} authorize
* @property {Function} getToken
* @property {import('axios').AxiosInstance} request
* @property {Function} [authorize]
* @property {Function} [getToken]
* @property {Function} info
* @property {Object} schema
* @property {Function} schema.fetch
Expand All @@ -35,17 +43,17 @@ import fetchSchema from './schema.js';
* Create a farm client for interacting with farmOS servers.
* @typedef {Function} client
* @param {String} host
* @param {Object} options
* @property {String} options.clientId
* @property {Function} options.getToken
* @property {Function} options.setToken
* @param {Object} [options]
* @property {AuthMixin=OAuthMixin} [options.auth=oauth]
* @property {String} [options.clientId]
* @property {Function} [options.getToken]
* @property {Function} [options.setToken]
* @returns {FarmClient}
*/
export default function client(host, options) {
const {
clientId,
getToken,
setToken,
auth = oauth,
...authOptions
} = options;

// Instantiate axios client.
Expand All @@ -58,18 +66,10 @@ export default function client(host, options) {
};
const request = axios.create(clientOptions);

// Create oAuth & request helpers.
const oAuthOpts = {
host,
clientId,
getToken,
setToken,
};
const { authorize } = oauth(request, oAuthOpts);
const authMethods = auth(request, { ...authOptions }) || {};

const farm = {
authorize,
getToken,
...authMethods,
request,
info() {
return request('/api');
Expand Down
24 changes: 20 additions & 4 deletions src/client/oauth.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import axios from 'axios';

export default function oAuth(request, opts) {
/**
* @typedef {Object} OAuthMethods
* @property {Function} authorize
* @property {Function} getToken
*/

/**
* @typedef {Function} OAuthMixin
* @param {import('axios').AxiosInstance} request
* @param {Object} authOptions
* @property {String} authOptions.host
* @property {String} authOptions.clientId
* @property {Function} [authOptions.getToken]
* @property {Function} [authOptions.setToken]
* @returns {Object}
*/
export default function oAuth(request, authOptions) {
let memToken = {};
const {
host = '',
clientId = '',
getToken = () => memToken,
setToken = (t) => { memToken = t; },
} = opts;
} = authOptions;
const accessTokenUri = `${host}/oauth/token`;

/*
Expand Down Expand Up @@ -148,7 +164,7 @@ export default function oAuth(request, opts) {
Accept: 'json',
},
data: `grant_type=password&username=${user}&password=${password}&client_id=${clientId}`,
}).then(res => parseToken(res.data))
.catch((error) => { throw error; }),
}).then(res => parseToken(res.data)).catch((error) => { throw error; }),
getToken,
};
}
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export { default as model } from './model/index.js';
* @property {Function} [schema.fetch]
* @property {Object} meta
* @property {Function} meta.isUnsynced
* @property {Object} [remote]
* @property {Object} remote
* @property {import('axios').AxiosInstance} remote.request
* @property {Function} remote.info
* @property {Function} remote.authorize
* @property {Function} remote.getToken
* @property {Function} [remote.info]
* @property {Function} [remote.authorize]
* @property {Function} [remote.getToken]
* @property {FarmEntityMethods} asset
* @property {FarmEntityMethods} log
* @property {FarmEntityMethods} plan
Expand All @@ -46,7 +46,7 @@ export { default as model } from './model/index.js';
* @param {Object} farmConfig
* @property {import('./model/index.js').EntitySchemata} [config.schemata]
* @property {Object} [config.remote]
* @property {import('./client/index.js').FarmClient} [config.remote.adapter]
* @property {import('./client/index.js').client} [config.remote.adapter=d9JsonApiAdapter]
* @property {Array<import('./entities.js').EntityConfig>} [config.entities]
* @returns {FarmObject}
*/
Expand Down

0 comments on commit 7ebc20f

Please sign in to comment.