Fixed some type issues with the api framework

- fixes a bunch of red squiggly lines due to type issues
- this in turn makes it slightly easier to read the API pipeline code
This commit is contained in:
Hannah Wolfe 2022-08-21 16:31:41 +01:00
parent 4cd210c29c
commit 0695f74a65
4 changed files with 20 additions and 11 deletions

View File

@ -19,6 +19,8 @@ class Frame {
* file: Uploaded file * file: Uploaded file
* files: Uploaded files * files: Uploaded files
* apiType: Content or admin api access * apiType: Content or admin api access
* docName: The endpoint name, e.g. "posts"
* method: The method name, e.g. "browse"
*/ */
this.options = {}; this.options = {};
this.data = {}; this.data = {};
@ -26,6 +28,9 @@ class Frame {
this.file = {}; this.file = {};
this.files = []; this.files = [];
this.apiType = null; this.apiType = null;
this.docName = null;
this.method = null;
this.response = null;
} }
/** /**

View File

@ -174,23 +174,21 @@ const STAGES = {
* 4. Controller - Execute the controller implementation & receive model response. * 4. Controller - Execute the controller implementation & receive model response.
* 5. Output Serialisation - Output formatting, Deprecations, Extra attributes etc... * 5. Output Serialisation - Output formatting, Deprecations, Extra attributes etc...
* *
* @param {Function} apiController * @param {Object} apiController
* @param {Object} apiUtils - Local utils (validation & serialisation) from target API version * @param {Object} apiUtils - Local utils (validation & serialisation) from target API version
* @param {String} [apiType] - Content or Admin API access * @param {String} [apiType] - Content or Admin API access
* @return {Function} * @return {Object}
*/ */
const pipeline = (apiController, apiUtils, apiType) => { const pipeline = (apiController, apiUtils, apiType) => {
const keys = Object.keys(apiController); const keys = Object.keys(apiController);
const docName = apiController.docName;
// CASE: api controllers are objects with configuration. // CASE: api controllers are objects with configuration.
// We have to ensure that we expose a functional interface e.g. `api.posts.add` has to be available. // We have to ensure that we expose a functional interface e.g. `api.posts.add` has to be available.
return keys.reduce((obj, key) => { return keys.reduce((obj, method) => {
const docName = apiController.docName; const apiImpl = _.cloneDeep(apiController)[method];
const method = key;
const apiImpl = _.cloneDeep(apiController)[key]; obj[method] = function wrapper() {
obj[key] = function wrapper() {
const apiConfig = {docName, method}; const apiConfig = {docName, method};
let options; let options;
let data; let data;
@ -253,7 +251,7 @@ const pipeline = (apiController, apiUtils, apiType) => {
}); });
}; };
Object.assign(obj[key], apiImpl); Object.assign(obj[method], apiImpl);
return obj; return obj;
}, {}); }, {});
}; };

View File

@ -11,7 +11,10 @@ describe('Frame', function () {
'user', 'user',
'file', 'file',
'files', 'files',
'apiType' 'apiType',
'docName',
'method',
'response'
]); ]);
}); });

View File

@ -45,7 +45,10 @@ describe('HTTP', function () {
'user', 'user',
'file', 'file',
'files', 'files',
'apiType' 'apiType',
'docName',
'method',
'response'
]); ]);
apiImpl.args[0][0].data.should.eql({a: 'a'}); apiImpl.args[0][0].data.should.eql({a: 'a'});