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) {
|
get(args) {
|
||||||
args.method = 'GET';
|
args.method = 'GET';
|
||||||
return this._fetch(args);
|
return this._fetch(args);
|
||||||
@ -28,12 +49,17 @@ export class HttpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_fetch(options) {
|
_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 = {
|
const requestOptions = {
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
...options,
|
...options,
|
||||||
};
|
};
|
||||||
|
|
||||||
return fetch(options.url, requestOptions)
|
return fetch(options.url || window.location.href, requestOptions)
|
||||||
.then(
|
.then(
|
||||||
(response) => {
|
(response) => {
|
||||||
this._responseInterceptors.forEach((interceptor) => interceptor(response, options));
|
this._responseInterceptors.forEach((interceptor) => interceptor(response, options));
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import moment from 'moment';
|
|||||||
import './exam-correct.sass';
|
import './exam-correct.sass';
|
||||||
|
|
||||||
|
|
||||||
const EXAM_CORRECT_URL_POST = 'correct';
|
|
||||||
const EXAM_CORRECT_HEADERS = {
|
const EXAM_CORRECT_HEADERS = {
|
||||||
'Content-Type': HttpClient.ACCEPT.JSON,
|
'Content-Type': HttpClient.ACCEPT.JSON,
|
||||||
'Accept': HttpClient.ACCEPT.JSON,
|
'Accept': HttpClient.ACCEPT.JSON,
|
||||||
@ -198,7 +197,6 @@ export class ExamCorrect {
|
|||||||
const body = this._toRequestBody(this._userInput.value);
|
const body = this._toRequestBody(this._userInput.value);
|
||||||
|
|
||||||
this._app.httpClient.post({
|
this._app.httpClient.post({
|
||||||
url: EXAM_CORRECT_URL_POST,
|
|
||||||
headers: EXAM_CORRECT_HEADERS,
|
headers: EXAM_CORRECT_HEADERS,
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
}).then(
|
}).then(
|
||||||
@ -290,7 +288,6 @@ export class ExamCorrect {
|
|||||||
const body = this._toRequestBody(userId || user, results, result);
|
const body = this._toRequestBody(userId || user, results, result);
|
||||||
|
|
||||||
this._app.httpClient.post({
|
this._app.httpClient.post({
|
||||||
url: EXAM_CORRECT_URL_POST,
|
|
||||||
headers: EXAM_CORRECT_HEADERS,
|
headers: EXAM_CORRECT_HEADERS,
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
}).then(
|
}).then(
|
||||||
@ -522,7 +519,6 @@ export class ExamCorrect {
|
|||||||
const body = this._toRequestBody(listItem.getAttribute(EXAM_CORRECT_USER_ATTR), results.partResults, results.result);
|
const body = this._toRequestBody(listItem.getAttribute(EXAM_CORRECT_USER_ATTR), results.partResults, results.result);
|
||||||
|
|
||||||
this._app.httpClient.post({
|
this._app.httpClient.post({
|
||||||
url: EXAM_CORRECT_URL_POST,
|
|
||||||
headers: EXAM_CORRECT_HEADERS,
|
headers: EXAM_CORRECT_HEADERS,
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
}).then(
|
}).then(
|
||||||
|
|||||||
@ -1830,6 +1830,8 @@ siteLayout' headingOverride widget = do
|
|||||||
mcurrentRoute <- getCurrentRoute
|
mcurrentRoute <- getCurrentRoute
|
||||||
let currentHandler = classifyHandler <$> mcurrentRoute
|
let currentHandler = classifyHandler <$> mcurrentRoute
|
||||||
|
|
||||||
|
currentApproot' <- siteApproot <$> getYesod <*> (reqWaiRequest <$> getRequest)
|
||||||
|
|
||||||
-- Get the breadcrumbs, as defined in the YesodBreadcrumbs instance.
|
-- Get the breadcrumbs, as defined in the YesodBreadcrumbs instance.
|
||||||
let
|
let
|
||||||
breadcrumbs' mcRoute = do
|
breadcrumbs' mcRoute = do
|
||||||
@ -2095,6 +2097,11 @@ siteLayout' headingOverride widget = do
|
|||||||
pc <- widgetToPageContent $ do
|
pc <- widgetToPageContent $ do
|
||||||
webpackLinks_main StaticR
|
webpackLinks_main StaticR
|
||||||
toWidget $(juliusFile "templates/i18n.julius")
|
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
|
wellKnownHtmlLinks
|
||||||
|
|
||||||
$(widgetFile "default-layout")
|
$(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.addMany(#{frontendI18n});
|
||||||
window.App.i18n.setDatetimeLocale(#{frontendDatetimeLocale});
|
window.App.i18n.setDatetimeLocale(#{frontendDatetimeLocale});
|
||||||
} else {
|
} else {
|
||||||
throw new Error('I18n JavaScript service is missing!');
|
throw new Error('I18n service is missing!');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user