Default options for dojo/request

We would like to setup requests to the server with the smallest boilerplate code as possible. Our endpoints accept json and return json.

Historically we have been doing the following for GET requests:

    request.get("/api/Maintenance/States", {
        query: { activeOnly: this.activeOnly },
        handleAs: "json"
    }).then(
        function (response) {
            // do something with the parsed json 
        }
    );

And the following for POST requests:

    request.post("/api/Maintenance/States", {
        headers: { "Content-Type": "application/json" },
        data: JSON.stringify(state),
        handleAs: "json"
    }).then(
        function (response) {
            // do something with the parsed json
        }
    );

I stumbled upon the requestProvider option of dojoConfig and dojo/request/registry the other day and have the following code:

dojoConfig = {
	...
	requestProvider: "dojo/request/registry",
	...
};
require(["dojo/request/registry", "dojo/request/xhr"], function(request, xhr) {
    request.register(/^\/api/, function(url, options) {
        options = options || {};
        options.handleAs = options.handleAs || "json";
        var data = options.data;
        if (data != null) {
            var headers = options.headers = options.headers || {};
            headers["Content-Type"] = headers["Content-Type"] || "application/json";
            if (headers["Content-Type"] === "application/json" && typeof data === "object") {
                options.data = JSON.stringify(data);
            }
        }
        return xhr(url, options);
    }, true);
});

Which will reduce code above to the following while still allowing me to specify if I need to override:

    request.get("/api/Maintenance/States", {
        query: { activeOnly: this.activeOnly }
    }).then(
        function (response) {
            // do something with the parsed json 
        }
    );
    request.post("/api/Maintenance/States", {
        data: state
    }).then(
        function (response) {
            // do something with the parsed json
        }
    );

It does seem to work correctly. Is this the correct/optimal usage of dojo/request/registry?

Yes, this is reasonable, and very similar to what we suggest in https://www.sitepen.com/blog/2012/08/21/introducing-dojorequest/ .