From eea235f97d5cc0af132ce9ccab34cc610d41c5c9 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 31 May 2020 09:44:24 +0300 Subject: [PATCH] Slightly change API providers behavior, import providers from IconifyProviders global --- packages/core/src/api/config.ts | 17 +++++-- packages/core/src/api/modules.ts | 26 ++-------- packages/core/tests/30-api/10-modules-test.ts | 4 +- packages/core/tests/30-api/20-loading-test.ts | 12 ++--- packages/iconify/src/iconify.ts | 50 ++++++++++++++++--- 5 files changed, 67 insertions(+), 42 deletions(-) diff --git a/packages/core/src/api/config.ts b/packages/core/src/api/config.ts index a7a39e0..8e80e1a 100644 --- a/packages/core/src/api/config.ts +++ b/packages/core/src/api/config.ts @@ -19,13 +19,19 @@ export type PartialIconifyAPIConfig = Partial; function createConfig( source: PartialIconifyAPIConfig ): IconifyAPIConfig | null { - if (!source.resources) { - return null; + let resources; + if (typeof source.resources === 'string') { + resources = [source.resources]; + } else { + resources = source.resources; + if (!(resources instanceof Array) || !resources.length) { + return null; + } } const result: IconifyAPIConfig = { // API hosts - resources: source.resources, + resources: resources, // Root path path: source.path === void 0 ? '/' : source.path, @@ -103,12 +109,13 @@ configStorage[''] = createConfig({ export function setAPIConfig( provider: string, customConfig: PartialIconifyAPIConfig -): void { +): boolean { const config = createConfig(customConfig); if (config === null) { - return; + return false; } configStorage[provider] = config; + return true; } /** diff --git a/packages/core/src/api/modules.ts b/packages/core/src/api/modules.ts index 5e0385e..2937ea7 100644 --- a/packages/core/src/api/modules.ts +++ b/packages/core/src/api/modules.ts @@ -35,36 +35,18 @@ export interface IconifyAPIModule { /** * Local storate types and entries */ -interface ModulesStorage { - default?: IconifyAPIModule; - providers: Record; -} -const storage: ModulesStorage = { - providers: Object.create(null), -}; - -/** - * Set default API module - */ -export function setDefaultAPIModule(item: IconifyAPIModule): void { - storage.default = item; -} +const storage: Record = Object.create(null); /** * Set API module */ -export function setProviderAPIModule( - provider: string, - item: IconifyAPIModule -): void { - storage.providers[provider] = item; +export function setAPIModule(provider: string, item: IconifyAPIModule): void { + storage[provider] = item; } /** * Get API module */ export function getAPIModule(provider: string): IconifyAPIModule | undefined { - return storage.providers[provider] === void 0 - ? storage.default - : storage.providers[provider]; + return storage[provider] === void 0 ? storage[''] : storage[provider]; } diff --git a/packages/core/tests/30-api/10-modules-test.ts b/packages/core/tests/30-api/10-modules-test.ts index c62239e..876986c 100644 --- a/packages/core/tests/30-api/10-modules-test.ts +++ b/packages/core/tests/30-api/10-modules-test.ts @@ -9,7 +9,7 @@ import { IconifyAPIConfig, } from '../../lib/api/config'; import { - setProviderAPIModule, + setAPIModule, APIQueryParams, getAPIModule, IconifyAPIModule, @@ -55,7 +55,7 @@ describe('Testing API modules', () => { }); // Set fake module - setProviderAPIModule(provider, { + setAPIModule(provider, { prepare: prepareQuery, send: sendQuery, }); diff --git a/packages/core/tests/30-api/20-loading-test.ts b/packages/core/tests/30-api/20-loading-test.ts index 3473683..29c9167 100644 --- a/packages/core/tests/30-api/20-loading-test.ts +++ b/packages/core/tests/30-api/20-loading-test.ts @@ -4,7 +4,7 @@ import 'mocha'; import { expect } from 'chai'; import { RedundancyPendingItem } from '@cyberalien/redundancy'; import { setAPIConfig, IconifyAPIConfig } from '../../lib/api/config'; -import { setProviderAPIModule, APIQueryParams } from '../../lib/api/modules'; +import { setAPIModule, APIQueryParams } from '../../lib/api/modules'; import { API } from '../../lib/api/'; describe('Testing API loadIcons', () => { @@ -88,7 +88,7 @@ describe('Testing API loadIcons', () => { expect(asyncCounter).to.be.equal(3); }; - setProviderAPIModule(provider, { + setAPIModule(provider, { prepare: prepareQuery, send: sendQuery, }); @@ -215,7 +215,7 @@ describe('Testing API loadIcons', () => { }); }; - setProviderAPIModule(provider, { + setAPIModule(provider, { prepare: prepareQuery, send: sendQuery, }); @@ -316,7 +316,7 @@ describe('Testing API loadIcons', () => { } }; - setProviderAPIModule(provider, { + setAPIModule(provider, { prepare: prepareQuery, send: sendQuery, }); @@ -439,7 +439,7 @@ describe('Testing API loadIcons', () => { } }; - setProviderAPIModule(provider, { + setAPIModule(provider, { prepare: prepareQuery, send: sendQuery, }); @@ -598,7 +598,7 @@ describe('Testing API loadIcons', () => { } }; - setProviderAPIModule(provider, { + setAPIModule(provider, { prepare: prepareQuery, send: sendQuery, }); diff --git a/packages/iconify/src/iconify.ts b/packages/iconify/src/iconify.ts index a571189..4b7a2ee 100644 --- a/packages/iconify/src/iconify.ts +++ b/packages/iconify/src/iconify.ts @@ -40,8 +40,12 @@ import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage'; // API import { API } from '@iconify/core/lib/api/'; -import { setDefaultAPIModule } from '@iconify/core/lib/api/modules'; -import { setAPIConfig, IconifyAPIConfig } from '@iconify/core/lib/api/config'; +import { setAPIModule } from '@iconify/core/lib/api/modules'; +import { + setAPIConfig, + PartialIconifyAPIConfig, + IconifyAPIConfig, +} from '@iconify/core/lib/api/config'; import { prepareQuery, sendQuery } from './modules/api-jsonp'; import { IconifyIconLoaderCallback, @@ -172,9 +176,9 @@ export interface IconifyGlobal { resumeObserver: () => void; /** - * Set API configuration + * Add API provider */ - setAPIConfig: ( + addAPIProvider: ( provider: string, customConfig: Partial ) => void; @@ -381,8 +385,8 @@ const Iconify: IconifyGlobal = { // Resume observer resumeObserver: observer.resume, - // API configuration - setAPIConfig: setAPIConfig, + // Add API provider + addAPIProvider: setAPIConfig, // Scan DOM scanDOM: scanDOM, @@ -428,11 +432,43 @@ loadCache(); // Set API coreModules.api = API; -setDefaultAPIModule({ +setAPIModule('', { send: sendQuery, prepare: prepareQuery, }); +// Set API from global "IconifyProviders" +interface WindowWithIconifyProviders { + IconifyProviders: Record; +} +if ( + ((window as unknown) as WindowWithIconifyProviders).IconifyProviders !== + void 0 +) { + const providers = ((window as unknown) as WindowWithIconifyProviders) + .IconifyProviders; + if (typeof providers === 'object' && providers !== null) { + for (let key in providers) { + const err = 'IconifyProviders[' + key + '] is invalid.'; + try { + const value = providers[key]; + if ( + typeof value !== 'object' || + !value || + value.resources === void 0 + ) { + continue; + } + if (!setAPIConfig(key, value)) { + console.error(err); + } + } catch (err) { + console.error(err); + } + } + } +} + // Load observer browserModules.observer = observer; setTimeout(() => {