Skip to content
Snippets Groups Projects
Select Git revision
  • 6224ce5a9dc13787425937a13782c66854b07bd5
  • master default protected
  • 1.31
  • 4.25.3
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
  • 4.22.3
  • 4.22.2
  • 4.22.1
  • 4.22.0
  • 4.21.0
23 results

main.mjs

Blame
  • fetch.mjs 3.73 KiB
    /**
     * Copyright schukai GmbH and contributors 2022. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import {internalSymbol} from "../../constants.mjs";
    import {extend} from "../../data/extend.mjs";
    import {Formatter} from "../../text/formatter.mjs";
    import {getGlobalFunction} from "../../types/global.mjs";
    import {isInstance, isString} from "../../types/is.mjs";
    import {validateObject, validateString} from "../../types/validate.mjs";
    import {parseLocale} from "../locale.mjs";
    import {Provider} from "../provider.mjs";
    import {Translations} from "../translations.mjs";
    
    export {Fetch}
    
    /**
     * The fetch provider retrieves a JSON file from the given URL and returns a translation object.
     * 
     * @externalExample ../../example/i18n/providers/fetch.mjs
     * @license AGPLv3
     * @since 1.13.0
     * @copyright schukai GmbH
     * @memberOf Monster.I18n.Providers
     * @see {@link https://datatracker.ietf.org/doc/html/rfc3066}
     * @tutorial i18n-locale-and-formatter
     */
     class Fetch extends Provider {
    
        /**
         * As options the key `fetch` can be passed. This config object is passed to the fetch method as init.
         * 
         * The url may contain placeholders (language, script, region, variants, extlang, privateUse), so you can specify one url for all translations.
         * 
         * ```
         * new Fetch('https://www.example.com/assets/${language}.json')
         * ```
         * 
         * @param {string|URL} url
         * @param {Object} options see {@link Monster.I18n.Providers.Fetch#defaults}
         * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch}
         */
        constructor(url, options) {
            super(options);
    
            if (isInstance(url, URL)) {
                url = url.toString();
            }
    
            if (options === undefined) {
                options = {};
            }
    
            validateString(url);
    
            /**
             * @property {string}
             */
            this.url = url;
    
            /**
             * @private
             * @property {Object} options
             */
            this[internalSymbol] = extend({}, super.defaults, this.defaults, validateObject(options));
    
        }