mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 11:46:19 +01:00
Merge branch 'MDL-82778-main' of https://github.com/andrewnicols/moodle
This commit is contained in:
commit
45faf93d87
9
.upgradenotes/MDL-82778-2024081502451922.yml
Normal file
9
.upgradenotes/MDL-82778-2024081502451922.yml
Normal file
@ -0,0 +1,9 @@
|
||||
issueNumber: MDL-82778
|
||||
notes:
|
||||
core:
|
||||
- message: >
|
||||
A new JS module for interacting with the Routed REST API has been
|
||||
introduced.
|
||||
|
||||
For more information see the documentation in the `core/fetch` module.
|
||||
type: improved
|
9
lib/amd/build/fetch.min.js
vendored
9
lib/amd/build/fetch.min.js
vendored
@ -1,10 +1,3 @@
|
||||
define("core/fetch",["exports","core/config","./pending"],(function(_exports,_config,_pending){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}
|
||||
/**
|
||||
* The core/fetch module allows you to make web service requests to the Moodle API.
|
||||
*
|
||||
* @module core/fetch
|
||||
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.request=_exports.performPut=_exports.performPost=_exports.performHead=_exports.performGet=_exports.performDelete=void 0,_config=_interopRequireDefault(_config),_pending=_interopRequireDefault(_pending);const normaliseComponent=component=>component.replace(/^core_/,""),getRequest=(component,endpoint,_ref)=>{let{params:params={},body:body=null,method:method="GET"}=_ref;const url=new URL("".concat(_config.default.apibase,"rest/v2/").concat(component,"/").concat(endpoint)),options={method:method,headers:{Accept:"application/json","Content-Type":"application/json"}};return Object.entries(params).forEach((_ref2=>{let[key,value]=_ref2;url.searchParams.append(key,value)})),body&&(body instanceof FormData?options.body=body:options.body=body instanceof Object?JSON.stringify(body):body),new Request(url,options)},request=async function(component,action){let{params:params={},body:body=null,method:method="GET"}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const pending=new _pending.default("Requesting ".concat(component,"/").concat(action," with ").concat(method)),result=await fetch(getRequest(normaliseComponent(component),action,{params:params,method:method,body:body}));if(pending.resolve(),result.ok)return result.json();throw new Error(result.statusText)};_exports.request=request;_exports.performGet=function(component,action){let{params:params={}}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return request(component,action,{params:params,method:"GET"})};_exports.performHead=function(component,action){let{params:params={}}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return request(component,action,{params:params,method:"HEAD"})};_exports.performPost=function(component,action){let{body:body}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return request(component,action,{body:body,method:"POST"})};_exports.performPut=function(component,action){let{body:body}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return request(component,action,{body:body,method:"POST"})};_exports.performDelete=function(component,action){let{params:params={},body:body=null}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return request(component,action,{body:body,params:params,method:"DELETE"})}}));
|
||||
define("core/fetch",["exports","core/config","./pending"],(function(_exports,_config,_pending){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _classStaticPrivateMethodGet(receiver,classConstructor,method){return function(receiver,classConstructor){if(receiver!==classConstructor)throw new TypeError("Private static access of wrong provenance")}(receiver,classConstructor),method}function _classPrivateFieldInitSpec(obj,privateMap,value){!function(obj,privateCollection){if(privateCollection.has(obj))throw new TypeError("Cannot initialize the same private elements twice on an object")}(obj,privateMap),privateMap.set(obj,value)}function _classPrivateFieldGet(receiver,privateMap){return function(receiver,descriptor){if(descriptor.get)return descriptor.get.call(receiver);return descriptor.value}(receiver,_classExtractFieldDescriptor(receiver,privateMap,"get"))}function _classPrivateFieldSet(receiver,privateMap,value){return function(receiver,descriptor,value){if(descriptor.set)descriptor.set.call(receiver,value);else{if(!descriptor.writable)throw new TypeError("attempted to set read only private field");descriptor.value=value}}(receiver,_classExtractFieldDescriptor(receiver,privateMap,"set"),value),value}function _classExtractFieldDescriptor(receiver,privateMap,action){if(!privateMap.has(receiver))throw new TypeError("attempted to "+action+" private field on non-instance");return privateMap.get(receiver)}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_config=_interopRequireDefault(_config),_pending=_interopRequireDefault(_pending);var _request=new WeakMap,_promise=new WeakMap,_resolve=new WeakMap,_reject=new WeakMap;class RequestWrapper{constructor(request){_classPrivateFieldInitSpec(this,_request,{writable:!0,value:null}),_classPrivateFieldInitSpec(this,_promise,{writable:!0,value:null}),_classPrivateFieldInitSpec(this,_resolve,{writable:!0,value:null}),_classPrivateFieldInitSpec(this,_reject,{writable:!0,value:null}),_classPrivateFieldSet(this,_request,request),_classPrivateFieldSet(this,_promise,new Promise(((resolve,reject)=>{_classPrivateFieldSet(this,_resolve,resolve),_classPrivateFieldSet(this,_reject,reject)})))}get request(){return _classPrivateFieldGet(this,_request)}get promise(){return _classPrivateFieldGet(this,_promise)}handleResponse(response){response.ok?_classPrivateFieldGet(this,_resolve).call(this,response):_classPrivateFieldGet(this,_reject).call(this,response.statusText)}}class Fetch{static async request(component,action){let{params:params={},body:body=null,method:method="GET"}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const pending=new _pending.default("Requesting ".concat(component,"/").concat(action," with ").concat(method)),requestWrapper=_classStaticPrivateMethodGet(Fetch,Fetch,_getRequest).call(Fetch,_classStaticPrivateMethodGet(Fetch,Fetch,_normaliseComponent).call(Fetch,component),action,{params:params,method:method,body:body}),result=await fetch(requestWrapper.request);return pending.resolve(),requestWrapper.handleResponse(result),requestWrapper.promise}static performGet(component,action){let{params:params={}}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.request(component,action,{params:params,method:"GET"})}static performHead(component,action){let{params:params={}}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.request(component,action,{params:params,method:"HEAD"})}static performPost(component,action){let{body:body}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.request(component,action,{body:body,method:"POST"})}static performPut(component,action){let{body:body}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.request(component,action,{body:body,method:"PUT"})}static performPatch(component,action){let{body:body}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.request(component,action,{body:body,method:"PATCH"})}static performDelete(component,action){let{params:params={},body:body=null}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return this.request(component,action,{body:body,params:params,method:"DELETE"})}}function _normaliseComponent(component){return component.replace(/^core_/,"")}function _getRequest(component,endpoint,_ref){let{params:params={},body:body=null,method:method="GET"}=_ref;const url=new URL("".concat(_config.default.apibase,"/rest/v2/").concat(component,"/").concat(endpoint)),options={method:method,headers:{Accept:"application/json","Content-Type":"application/json"}};return Object.entries(params).forEach((_ref2=>{let[key,value]=_ref2;url.searchParams.append(key,value)})),body&&(body instanceof FormData?options.body=body:options.body=body instanceof Object?JSON.stringify(body):body),new RequestWrapper(new Request(url,options))}return _exports.default=Fetch,_exports.default}));
|
||||
|
||||
//# sourceMappingURL=fetch.min.js.map
|
File diff suppressed because one or more lines are too long
@ -17,221 +17,329 @@
|
||||
* The core/fetch module allows you to make web service requests to the Moodle API.
|
||||
*
|
||||
* @module core/fetch
|
||||
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
||||
* @copyright Andrew Lyons <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @example <caption>Perform a single GET request</caption>
|
||||
* import Fetch from 'core/fetch';
|
||||
*
|
||||
* const result = Fetch.performGet('mod_example', 'animals', { params: { type: 'mammal' } });
|
||||
*
|
||||
* result.then((response) => {
|
||||
* // Do something with the Response object.
|
||||
* })
|
||||
* .catch((error) => {
|
||||
* // Handle the error
|
||||
* });
|
||||
*/
|
||||
|
||||
import Cfg from 'core/config';
|
||||
import PendingPromise from './pending';
|
||||
|
||||
/**
|
||||
* Normalise the component name to remove the core_ prefix.
|
||||
* A wrapper around the Request, including a Promise that is resolved when the request is complete.
|
||||
*
|
||||
* @param {string} component
|
||||
* @returns {string}
|
||||
* @class RequestWrapper
|
||||
* @private
|
||||
*/
|
||||
const normaliseComponent = (component) => component.replace(/^core_/, '');
|
||||
class RequestWrapper {
|
||||
/** @var {Request} */
|
||||
#request = null;
|
||||
|
||||
/**
|
||||
* Get the Request object for a given API request.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} endpoint The endpoint within the componet to call
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @param {string|Object|FormData} [params.body = null] The HTTP method to use
|
||||
* @param {string} [params.method = "GET"] The HTTP method to use
|
||||
* @returns {Request}
|
||||
*/
|
||||
const getRequest = (
|
||||
component,
|
||||
endpoint,
|
||||
{
|
||||
params = {},
|
||||
body = null,
|
||||
method = 'GET',
|
||||
/** @var {Promise} */
|
||||
#promise = null;
|
||||
|
||||
/** @var {Function} */
|
||||
#resolve = null;
|
||||
|
||||
/** @var {Function} */
|
||||
#reject = null;
|
||||
|
||||
/**
|
||||
* Create a new RequestWrapper.
|
||||
*
|
||||
* @param {Request} request The request object that is wrapped
|
||||
*/
|
||||
constructor(request) {
|
||||
this.#request = request;
|
||||
this.#promise = new Promise((resolve, reject) => {
|
||||
this.#resolve = resolve;
|
||||
this.#reject = reject;
|
||||
});
|
||||
}
|
||||
) => {
|
||||
const url = new URL(`${Cfg.apibase}rest/v2/${component}/${endpoint}`);
|
||||
const options = {
|
||||
method,
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
};
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
url.searchParams.append(key, value);
|
||||
});
|
||||
/**
|
||||
* Get the wrapped Request.
|
||||
*
|
||||
* @returns {Request}
|
||||
* @private
|
||||
*/
|
||||
get request() {
|
||||
return this.#request;
|
||||
}
|
||||
|
||||
if (body) {
|
||||
if (body instanceof FormData) {
|
||||
options.body = body;
|
||||
} else if (body instanceof Object) {
|
||||
options.body = JSON.stringify(body);
|
||||
/**
|
||||
* Get the Promise link to this request.
|
||||
*
|
||||
* @return {Promise}
|
||||
* @private
|
||||
*/
|
||||
get promise() {
|
||||
return this.#promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the response from the request.
|
||||
*
|
||||
* @param {Response} response
|
||||
* @private
|
||||
*/
|
||||
handleResponse(response) {
|
||||
if (response.ok) {
|
||||
this.#resolve(response);
|
||||
} else {
|
||||
options.body = body;
|
||||
this.#reject(response.statusText);
|
||||
}
|
||||
}
|
||||
|
||||
return new Request(url, options);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
* A class to handle requests to the Moodle REST API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @param {string|Object|FormData} [params.body = null] The HTTP method to use
|
||||
* @param {string} [params.method = "GET"] The HTTP method to use
|
||||
* @returns {Promise<object>}
|
||||
* @class Fetch
|
||||
*/
|
||||
const request = async(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
body = null,
|
||||
method = 'GET',
|
||||
} = {},
|
||||
) => {
|
||||
const pending = new PendingPromise(`Requesting ${component}/${action} with ${method}`);
|
||||
const result = await fetch(
|
||||
getRequest(
|
||||
normaliseComponent(component),
|
||||
export default class Fetch {
|
||||
/**
|
||||
* Make a single request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @param {string|Object|FormData} [params.body = null] The HTTP method to use
|
||||
* @param {string} [params.method = "GET"] The HTTP method to use
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static async request(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
body = null,
|
||||
method = 'GET',
|
||||
} = {},
|
||||
) {
|
||||
const pending = new PendingPromise(`Requesting ${component}/${action} with ${method}`);
|
||||
const requestWrapper = Fetch.#getRequest(
|
||||
Fetch.#normaliseComponent(component),
|
||||
action,
|
||||
{params, method, body},
|
||||
),
|
||||
);
|
||||
{ params, method, body },
|
||||
);
|
||||
const result = await fetch(requestWrapper.request);
|
||||
|
||||
pending.resolve();
|
||||
pending.resolve();
|
||||
|
||||
if (result.ok) {
|
||||
return result.json();
|
||||
requestWrapper.handleResponse(result);
|
||||
|
||||
return requestWrapper.promise;
|
||||
}
|
||||
|
||||
throw new Error(result.statusText);
|
||||
};
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static performGet(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
} = {},
|
||||
) {
|
||||
return this.request(
|
||||
component,
|
||||
action,
|
||||
{ params, method: 'GET' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
const performGet = (
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
} = {},
|
||||
) => request(
|
||||
component,
|
||||
action,
|
||||
{params, method: 'GET'},
|
||||
);
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static performHead(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
} = {},
|
||||
) {
|
||||
return this.request(
|
||||
component,
|
||||
action,
|
||||
{ params, method: 'HEAD' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
const performHead = (
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
} = {},
|
||||
) => request(
|
||||
component,
|
||||
action,
|
||||
{params, method: 'HEAD'},
|
||||
);
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {string|Object|FormData} params.body The HTTP method to use
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static performPost(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
} = {},
|
||||
) {
|
||||
return this.request(
|
||||
component,
|
||||
action,
|
||||
{ body, method: 'POST' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {string|Object|FormData} params.body The HTTP method to use
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
const performPost = (
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
} = {},
|
||||
) => request(
|
||||
component,
|
||||
action,
|
||||
{body, method: 'POST'},
|
||||
);
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {string|Object|FormData} params.body The HTTP method to use
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static performPut(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
} = {},
|
||||
) {
|
||||
return this.request(
|
||||
component,
|
||||
action,
|
||||
{ body, method: 'PUT' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {string|Object|FormData} params.body The HTTP method to use
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
const performPut = (
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
} = {},
|
||||
) => request(
|
||||
component,
|
||||
action,
|
||||
{body, method: 'POST'},
|
||||
);
|
||||
/**
|
||||
* Make a PATCH request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {string|Object|FormData} params.body The HTTP method to use
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static performPatch(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
} = {},
|
||||
) {
|
||||
return this.request(
|
||||
component,
|
||||
action,
|
||||
{ body, method: 'PATCH' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @param {string|Object|FormData} [params.body = null] The HTTP method to use
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
const performDelete = (
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
body = null,
|
||||
} = {},
|
||||
) => request(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
params,
|
||||
method: 'DELETE',
|
||||
},
|
||||
);
|
||||
/**
|
||||
* Make a request to the Moodle API.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} action The component action to perform
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @param {string|Object|FormData} [params.body = null] The HTTP method to use
|
||||
* @returns {Promise<Response>} A promise that resolves to the Response object for the request
|
||||
*/
|
||||
static performDelete(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
params = {},
|
||||
body = null,
|
||||
} = {},
|
||||
) {
|
||||
return this.request(
|
||||
component,
|
||||
action,
|
||||
{
|
||||
body,
|
||||
params,
|
||||
method: 'DELETE',
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export {
|
||||
request,
|
||||
performGet,
|
||||
performHead,
|
||||
performPost,
|
||||
performPut,
|
||||
performDelete,
|
||||
};
|
||||
/**
|
||||
* Normalise the component name to remove the core_ prefix.
|
||||
*
|
||||
* @param {string} component
|
||||
* @returns {string}
|
||||
*/
|
||||
static #normaliseComponent(component) {
|
||||
return component.replace(/^core_/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Request for a given API request.
|
||||
*
|
||||
* @param {string} component The frankenstyle component name
|
||||
* @param {string} endpoint The endpoint within the componet to call
|
||||
* @param {object} params
|
||||
* @param {object} [params.params = {}] The parameters to pass to the API
|
||||
* @param {string|Object|FormData} [params.body = null] The HTTP method to use
|
||||
* @param {string} [params.method = "GET"] The HTTP method to use
|
||||
* @returns {RequestWrapper}
|
||||
*/
|
||||
static #getRequest(
|
||||
component,
|
||||
endpoint,
|
||||
{
|
||||
params = {},
|
||||
body = null,
|
||||
method = 'GET',
|
||||
}
|
||||
) {
|
||||
const url = new URL(`${Cfg.apibase}/rest/v2/${component}/${endpoint}`);
|
||||
const options = {
|
||||
method,
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
};
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
url.searchParams.append(key, value);
|
||||
});
|
||||
|
||||
if (body) {
|
||||
if (body instanceof FormData) {
|
||||
options.body = body;
|
||||
} else if (body instanceof Object) {
|
||||
options.body = JSON.stringify(body);
|
||||
} else {
|
||||
options.body = body;
|
||||
}
|
||||
}
|
||||
|
||||
return new RequestWrapper(new Request(url, options));
|
||||
}
|
||||
}
|
||||
|
@ -360,13 +360,13 @@ class page_requirements_manager {
|
||||
|
||||
if (!empty($CFG->router_configured)) {
|
||||
return sprintf(
|
||||
"%s/api/",
|
||||
"%s/api",
|
||||
$CFG->wwwroot,
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
"%s/r.php/api/",
|
||||
"%s/r.php/api",
|
||||
$CFG->wwwroot,
|
||||
);
|
||||
}
|
||||
|
4
user/amd/build/repository.min.js
vendored
4
user/amd/build/repository.min.js
vendored
@ -1,10 +1,10 @@
|
||||
define("core_user/repository",["exports","core/config","core/ajax","core/fetch"],(function(_exports,_config,_ajax,_fetch){var obj;
|
||||
define("core_user/repository",["exports","core/config","core/ajax","core/fetch"],(function(_exports,_config,_ajax,_fetch){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}
|
||||
/**
|
||||
* Module to handle AJAX interactions.
|
||||
*
|
||||
* @module core_user/repository
|
||||
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.unenrolUser=_exports.submitUserEnrolmentForm=_exports.setUserPreferences=_exports.setUserPreference=_exports.sendMessagesToUsers=_exports.getUserPreferences=_exports.getUserPreference=_exports.createNotesForUsers=void 0,_config=(obj=_config)&&obj.__esModule?obj:{default:obj};const checkUserId=userid=>{if(0!==Number(userid)&&Number(userid)!==_config.default.userId)throw new Error("Invalid user ID: ".concat(userid,". It is only possible to manage preferences for the current user."))},addLegacySavedProperty=(response,preferences)=>{const debugLogger={get:(target,prop,receiver)=>"then"===prop?null:"saved"===prop?(window.console.warn("The saved property is deprecated. Please use the response object directly."),preferences.filter((preference=>target.hasOwnProperty(preference.name))).map((preference=>({name:preference.name,userid:_config.default.userid})))):Reflect.get(target,prop,receiver)};return Promise.resolve(new Proxy(response,debugLogger))};_exports.getUserPreference=function(name){let userid=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return getUserPreferences(name,userid).then((response=>response[name]))};const getUserPreferences=function(){let name=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,userid=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;checkUserId(userid);const endpoint=["current","preferences"];return name&&endpoint.push(name),(0,_fetch.performGet)("core_user",endpoint.join("/"))};_exports.getUserPreferences=getUserPreferences;_exports.setUserPreference=function(name){let value=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,userid=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return checkUserId(userid),(0,_fetch.performPost)("core_user","current/preferences/".concat(name),{body:{value:value}}).then((response=>addLegacySavedProperty(response,[{name:name}])))};_exports.setUserPreferences=preferences=>(preferences.forEach((preference=>checkUserId(preference.userid))),(0,_fetch.performPost)("core_user","current/preferences",{body:{preferences:Object.fromEntries(preferences.map((preference=>[preference.name,preference.value])))}}).then((response=>addLegacySavedProperty(response,preferences))));_exports.unenrolUser=userEnrolmentId=>(0,_ajax.call)([{methodname:"core_enrol_unenrol_user_enrolment",args:{ueid:userEnrolmentId}}])[0];_exports.submitUserEnrolmentForm=formdata=>(0,_ajax.call)([{methodname:"core_enrol_submit_user_enrolment_form",args:{formdata:formdata}}])[0];_exports.createNotesForUsers=notes=>(0,_ajax.call)([{methodname:"core_notes_create_notes",args:{notes:notes}}])[0];_exports.sendMessagesToUsers=messages=>(0,_ajax.call)([{methodname:"core_message_send_instant_messages",args:{messages:messages}}])[0]}));
|
||||
*/Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.unenrolUser=_exports.submitUserEnrolmentForm=_exports.setUserPreferences=_exports.setUserPreference=_exports.sendMessagesToUsers=_exports.getUserPreferences=_exports.getUserPreference=_exports.createNotesForUsers=void 0,_config=_interopRequireDefault(_config),_fetch=_interopRequireDefault(_fetch);const checkUserId=userid=>{if(0!==Number(userid)&&Number(userid)!==_config.default.userId)throw new Error("Invalid user ID: ".concat(userid,". It is only possible to manage preferences for the current user."))},addLegacySavedProperty=(response,preferences)=>{const debugLogger={get:(target,prop,receiver)=>"then"===prop?null:"saved"===prop?(window.console.warn("The saved property is deprecated. Please use the response object directly."),preferences.filter((preference=>target.hasOwnProperty(preference.name))).map((preference=>({name:preference.name,userid:_config.default.userid})))):Reflect.get(target,prop,receiver)};return Promise.resolve(new Proxy(response,debugLogger))};_exports.getUserPreference=function(name){let userid=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return getUserPreferences(name,userid).then((response=>response[name]))};const getUserPreferences=function(){let name=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,userid=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;checkUserId(userid);const endpoint=["current","preferences"];return name&&endpoint.push(name),_fetch.default.performGet("core_user",endpoint.join("/")).then((response=>response.json()))};_exports.getUserPreferences=getUserPreferences;_exports.setUserPreference=function(name){let value=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,userid=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return checkUserId(userid),_fetch.default.performPost("core_user","current/preferences/".concat(name),{body:{value:value}}).then((response=>response.json())).then((response=>addLegacySavedProperty(response,[{name:name}])))};_exports.setUserPreferences=preferences=>(preferences.forEach((preference=>checkUserId(preference.userid))),_fetch.default.performPost("core_user","current/preferences",{body:{preferences:Object.fromEntries(preferences.map((preference=>[preference.name,preference.value])))}}).then((response=>response.json())).then((response=>addLegacySavedProperty(response,preferences))));_exports.unenrolUser=userEnrolmentId=>(0,_ajax.call)([{methodname:"core_enrol_unenrol_user_enrolment",args:{ueid:userEnrolmentId}}])[0];_exports.submitUserEnrolmentForm=formdata=>(0,_ajax.call)([{methodname:"core_enrol_submit_user_enrolment_form",args:{formdata:formdata}}])[0];_exports.createNotesForUsers=notes=>(0,_ajax.call)([{methodname:"core_notes_create_notes",args:{notes:notes}}])[0];_exports.sendMessagesToUsers=messages=>(0,_ajax.call)([{methodname:"core_message_send_instant_messages",args:{messages:messages}}])[0]}));
|
||||
|
||||
//# sourceMappingURL=repository.min.js.map
|
File diff suppressed because one or more lines are too long
@ -23,7 +23,7 @@
|
||||
|
||||
import Config from 'core/config';
|
||||
import {call as fetchMany} from 'core/ajax';
|
||||
import {performGet, performPost} from 'core/fetch';
|
||||
import Fetch from 'core/fetch';
|
||||
|
||||
const checkUserId = (userid) => {
|
||||
if (Number(userid) === 0) {
|
||||
@ -95,7 +95,7 @@ export const getUserPreferences = (name = null, userid = 0) => {
|
||||
endpoint.push(name);
|
||||
}
|
||||
|
||||
return performGet('core_user', endpoint.join('/'));
|
||||
return Fetch.performGet('core_user', endpoint.join('/')).then((response) => response.json());
|
||||
};
|
||||
|
||||
/**
|
||||
@ -108,7 +108,7 @@ export const getUserPreferences = (name = null, userid = 0) => {
|
||||
*/
|
||||
export const setUserPreference = (name, value = null, userid = 0) => {
|
||||
checkUserId(userid);
|
||||
return performPost(
|
||||
return Fetch.performPost(
|
||||
'core_user',
|
||||
`current/preferences/${name}`,
|
||||
{
|
||||
@ -116,6 +116,7 @@ export const setUserPreference = (name, value = null, userid = 0) => {
|
||||
},
|
||||
)
|
||||
// Return the result of the fetch call, and also add in the legacy saved property.
|
||||
.then((response) => response.json())
|
||||
.then((response) => addLegacySavedProperty(response, [{name}]));
|
||||
};
|
||||
|
||||
@ -127,7 +128,7 @@ export const setUserPreference = (name, value = null, userid = 0) => {
|
||||
*/
|
||||
export const setUserPreferences = (preferences) => {
|
||||
preferences.forEach((preference) => checkUserId(preference.userid));
|
||||
return performPost(
|
||||
return Fetch.performPost(
|
||||
'core_user',
|
||||
'current/preferences',
|
||||
{
|
||||
@ -137,6 +138,7 @@ export const setUserPreferences = (preferences) => {
|
||||
},
|
||||
)
|
||||
// Return the result of the fetch call, and also add in the legacy saved property.
|
||||
.then((response) => response.json())
|
||||
.then((response) => addLegacySavedProperty(response, preferences));
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user