richard.herbert
02/24/2023, 5:59 PMevent.getResponse().setData()
to allow me to drop the pagination element and possibly other parts?richard.herbert
02/24/2023, 6:01 PMsetDataWithPagination()
method?bdw429s
02/24/2023, 6:06 PMprc.response
in a preProcess
interceptorbdw429s
02/24/2023, 6:06 PMrichard.herbert
02/24/2023, 6:08 PMpreProcess
interceptor of my handler (I assume that's what you meant)?bdw429s
02/24/2023, 6:09 PMmodels
folder šbdw429s
02/24/2023, 6:09 PMTheNo, that doesn't really make sense. ColdBox interceptors are unrelated to handlersinterceptor of my handler (I assume that's what you meant)?preProcess
richard.herbert
02/24/2023, 6:13 PMI usually put CFCs in theIn a HMVC app would that go in afoldermodels
models
folder in the app root or closer to the code, say modules_app/api/modules_app/v1/models
location?
No, that doesn't really make senseOkay, be gentle with me, this is a new area for me. So I need to look into interceptors?
bdw429s
02/24/2023, 6:15 PMIn a HMVC app would that go in aAgain, up to you. If you have your API broken out into a module then it would make good sense to put that CFC inside the API module. If you want to re-use the same response for all versions of your API, you could put it underfolder in the app root or closer to the code, saymodels
location?modules_app/api/modules_app/v1/models
modules_app/models
but if you think every version of your API may have a different response object, then you could stick it in the v1 module.bdw429s
02/24/2023, 6:16 PMprc.response
and so long as you put your custom version there on each request, ColdBox won't care.bdw429s
02/24/2023, 6:17 PMSo I need to look into interceptors?Apparently š The shortest path from A to B is probably just to stick a
preProcess
method in your module's ModuleConfig.cfc
since module configs are automatically registered as interceptors. Just keep in mind, interceptors are global and will run on every request unless you implement an eventPattern
annotation to restrict it only to API requests
https://coldbox.ortusbooks.com/the-basics/interceptors/restricting-executionrichard.herbert
02/24/2023, 6:19 PMit would make good sense to put that CFC inside the API moduleOkay, yes, I can see the logic in that.
just to stick aOkay, I'll look into that too. Thank you for the steer, I'll do some more reading now.method in your module'spreProcess
ModuleConfig.cfc
bdw429s
02/24/2023, 6:19 PMbdw429s
02/24/2023, 6:20 PMbdw429s
02/24/2023, 6:20 PMModuleConfig.cfc
not the coldbox.cfc
file as his docs showrichard.herbert
02/24/2023, 6:24 PMrichard.herbert
02/27/2023, 5:43 PM/modules_app/api/ModuleConfig.cfc
interceptors = [ {
class: 'api.interceptors.RestResponseInterceptor',
name: 'RestResponseInterceptor',
properties: {}
} ];
and this to to the new interceptor /modules_app/api/interceptors/RestResponseInterceptor.cfc
component {
void function configure() {}
function preProcess( event, interceptData, buffer, rc, prc )
eventPattern='^v1:scim\.'
{
prc.response = wirebox.getInstance( 'modules_app.api.models.RestResponseObject' );
}
}
and then I when on to writing my new response object.
I was hoping to be able to add new methods like setDataSimple()
and setDataNoPagination()
that I could use with event.getResponse()
in my handler but that didn't seem to be the way to go. It seemed that I needed to override the setDataPacket()
method with the response I want, so I added this to /modules_app/api/models/RestResponseObject.cfc
component extends=coldbox.system.web.context.Response {
/**
* Returns a custom response formatted data packet using the information in the response
*
* @reset Reset the 'data' element of the original data packet
*/
struct function getDataPacket( boolean reset = false ) {
if( isSimpleValue( getData() ) ) {
var packet = {
'data': getData()
};
} else {
var packet = getData();
}
// Are we reseting the data packet
if ( arguments.reset ) {
packet.data = {};
}
return packet;
}
}
which works for the one case I currently have. My question now is, do I need replicate this approach for every variation of a response object or is there a better, more flexible way to meet my needs?bdw429s
02/27/2023, 7:45 PMbdw429s
02/27/2023, 7:46 PMbdw429s
02/27/2023, 7:46 PMbdw429s
02/27/2023, 7:47 PMrichard.herbert
02/27/2023, 7:54 PMGET
request.
I assume I'd have to build my dynamic-ness into my getDataPacket()
method? I was hoping to have something like setDataNoPagination()
so I could influence the packet when populating it with my data.bdw429s
02/27/2023, 7:58 PMbdw429s
02/27/2023, 7:58 PMbdw429s
02/27/2023, 7:59 PMrichard.herbert
02/27/2023, 8:07 PMevent.getResponse()
.setDataNoPagination( response )
.setStatus( 201 );
I would have to add a setDataNoPagination()
method in my custom response object?
How would I get getDataPacket()
to return the modified packet?
I'm not sure I understand how this is all wired up to allow a dynamic response object?bdw429s
02/27/2023, 8:14 PMHow would I getYou would put that code in your overriddento return the modified packet?getDataPacket()
getDataPacket()
method, no?richard.herbert
02/27/2023, 8:17 PMsetDataNoPagination()
I want the response packet to be in one format and when I populated my data with setDataSimple()
I want the response packet to be in a different format?bdw429s
02/27/2023, 8:17 PMbdw429s
02/27/2023, 8:18 PMbdw429s
02/27/2023, 8:18 PMbdw429s
02/27/2023, 8:18 PMbdw429s
02/27/2023, 8:18 PMgetDataPacket
as you desire using CFML!bdw429s
02/27/2023, 8:20 PMbdw429s
02/27/2023, 8:20 PMrichard.herbert
02/27/2023, 8:25 PMhonestly I'm not understanding the questionAnd there's me thinking English was my first language š I'll leave you be as I can see I'm frustrating you with my lack of insight into how ColdBox wires these things up š
bdw429s
02/27/2023, 8:26 PMbdw429s
02/27/2023, 8:26 PMbdw429s
02/27/2023, 8:26 PMaroundHandler()
methodbdw429s
02/27/2023, 8:27 PMbdw429s
02/27/2023, 8:30 PMprc.response
CFC instance is created (unless you'e already put one there)
⢠Your handler code calls whatever methods it wants to in order to "build up" the information that needs to be returned to the client (status code, content type, the data to return in the HTTP response body)
⢠The second half of the aroundhandler method takes whatever info is in that reponse object to pass into the event.renderData()
call for you, and whatever the response.getDataPacket
method returns is what gets passed in as the data
for render databdw429s
02/27/2023, 8:30 PMrichard.herbert
02/27/2023, 8:34 PM