Last active
February 18, 2017 19:35
-
-
Save Hallmanac/caceaec63d7029378cceeb9843a1c073 to your computer and use it in GitHub Desktop.
Creating accept header interception for $http in Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @ngInject */ | |
function _getHeaderInterceptor(utils: Nebbia.IUtils) { | |
const interceptor: ng.IHttpInterceptor = { | |
request: (config: ng.IRequestConfig) => { | |
const isGet = utils.stringContains("GET", config.method, false); | |
const hasJsonAcceptType = utils.stringContains("/json", config.headers["Accept"], false); | |
const isHtmlFileRequest = _isHtmlUrl(); | |
if (isGet && isHtmlFileRequest && hasJsonAcceptType) { | |
config.headers["Accept"] = "application/html, text/html, */*"; | |
} | |
return config; | |
function _isHtmlUrl(): boolean { | |
const index = config.url.lastIndexOf("."); | |
if (index === -1) { | |
return false; | |
} | |
const fileExt = config.url.slice(index); | |
const isHtml = fileExt === "html"; | |
return isHtml; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment