feat(http-client): baseUrl and defaultUrl
This commit is contained in:
parent
98e1141e60
commit
693189fe82
@ -15,6 +15,27 @@ export class HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
_baseUrl;
|
||||
|
||||
setBaseUrl(baseUrl) {
|
||||
if (typeof this._baseUrl !== 'undefined') {
|
||||
throw new Error('HttpClient baseUrl is already set');
|
||||
}
|
||||
|
||||
this._baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
_defaultUrl;
|
||||
|
||||
setDefaultUrl(defaultUrl) {
|
||||
if (typeof this._defaultUrl !== 'undefined') {
|
||||
throw new Error('HttpClient defaultUrl is already set');
|
||||
}
|
||||
|
||||
this._defaultUrl = defaultUrl;
|
||||
}
|
||||
|
||||
|
||||
get(args) {
|
||||
args.method = 'GET';
|
||||
return this._fetch(args);
|
||||
@ -28,12 +49,17 @@ export class HttpClient {
|
||||
}
|
||||
|
||||
_fetch(options) {
|
||||
options.url = options.url || this._defaultUrl;
|
||||
|
||||
if (this._baseUrl && options.url && options.url.substring(0,1) === '/' && options.url.substring(0,2) !== '//')
|
||||
options.url = this._baseUrl + (this._baseUrl.substring(this._baseUrl.substring.length - 1) === '/' ? '' : '/') + options.url.substring(1,0);
|
||||
|
||||
const requestOptions = {
|
||||
credentials: 'same-origin',
|
||||
...options,
|
||||
};
|
||||
|
||||
return fetch(options.url, requestOptions)
|
||||
return fetch(options.url || window.location.href, requestOptions)
|
||||
.then(
|
||||
(response) => {
|
||||
this._responseInterceptors.forEach((interceptor) => interceptor(response, options));
|
||||
|
||||
@ -7,7 +7,6 @@ import moment from 'moment';
|
||||
import './exam-correct.sass';
|
||||
|
||||
|
||||
const EXAM_CORRECT_URL_POST = 'correct';
|
||||
const EXAM_CORRECT_HEADERS = {
|
||||
'Content-Type': HttpClient.ACCEPT.JSON,
|
||||
'Accept': HttpClient.ACCEPT.JSON,
|
||||
@ -198,7 +197,6 @@ export class ExamCorrect {
|
||||
const body = this._toRequestBody(this._userInput.value);
|
||||
|
||||
this._app.httpClient.post({
|
||||
url: EXAM_CORRECT_URL_POST,
|
||||
headers: EXAM_CORRECT_HEADERS,
|
||||
body: JSON.stringify(body),
|
||||
}).then(
|
||||
@ -290,7 +288,6 @@ export class ExamCorrect {
|
||||
const body = this._toRequestBody(userId || user, results, result);
|
||||
|
||||
this._app.httpClient.post({
|
||||
url: EXAM_CORRECT_URL_POST,
|
||||
headers: EXAM_CORRECT_HEADERS,
|
||||
body: JSON.stringify(body),
|
||||
}).then(
|
||||
@ -522,7 +519,6 @@ export class ExamCorrect {
|
||||
const body = this._toRequestBody(listItem.getAttribute(EXAM_CORRECT_USER_ATTR), results.partResults, results.result);
|
||||
|
||||
this._app.httpClient.post({
|
||||
url: EXAM_CORRECT_URL_POST,
|
||||
headers: EXAM_CORRECT_HEADERS,
|
||||
body: JSON.stringify(body),
|
||||
}).then(
|
||||
|
||||
@ -1830,6 +1830,8 @@ siteLayout' headingOverride widget = do
|
||||
mcurrentRoute <- getCurrentRoute
|
||||
let currentHandler = classifyHandler <$> mcurrentRoute
|
||||
|
||||
currentApproot' <- siteApproot <$> getYesod <*> (reqWaiRequest <$> getRequest)
|
||||
|
||||
-- Get the breadcrumbs, as defined in the YesodBreadcrumbs instance.
|
||||
let
|
||||
breadcrumbs' mcRoute = do
|
||||
@ -2095,6 +2097,11 @@ siteLayout' headingOverride widget = do
|
||||
pc <- widgetToPageContent $ do
|
||||
webpackLinks_main StaticR
|
||||
toWidget $(juliusFile "templates/i18n.julius")
|
||||
whenIsJust currentApproot' $ \currentApproot ->
|
||||
toWidget $(juliusFile "templates/approot.julius")
|
||||
whenIsJust mcurrentRoute $ \currentRoute' -> do
|
||||
currentRoute <- toTextUrl currentRoute'
|
||||
toWidget $(juliusFile "templates/current-route.julius")
|
||||
wellKnownHtmlLinks
|
||||
|
||||
$(widgetFile "default-layout")
|
||||
|
||||
5
templates/approot.julius
Normal file
5
templates/approot.julius
Normal file
@ -0,0 +1,5 @@
|
||||
if (window.App && window.App.httpClient) {
|
||||
window.App.httpClient.setBaseUrl(#{currentApproot});
|
||||
} else {
|
||||
throw new Error('HttpClient service is missing!');
|
||||
}
|
||||
5
templates/current-route.julius
Normal file
5
templates/current-route.julius
Normal file
@ -0,0 +1,5 @@
|
||||
if (window.App && window.App.httpClient) {
|
||||
window.App.httpClient.setDefaultUrl(#{currentRoute});
|
||||
} else {
|
||||
throw new Error('HttpClient service is missing!');
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
if (window.App) {
|
||||
if (window.App && window.App.i18n) {
|
||||
window.App.i18n.addMany(#{frontendI18n});
|
||||
window.App.i18n.setDatetimeLocale(#{frontendDatetimeLocale});
|
||||
} else {
|
||||
throw new Error('I18n JavaScript service is missing!');
|
||||
throw new Error('I18n service is missing!');
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user