2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-06 07:20:40 +00:00

Slightly change API providers behavior, import providers from IconifyProviders global

This commit is contained in:
Vjacheslav Trushkin 2020-05-31 09:44:24 +03:00
parent 22d832a7e2
commit eea235f97d
5 changed files with 67 additions and 42 deletions

View File

@ -19,13 +19,19 @@ export type PartialIconifyAPIConfig = Partial<IconifyAPIConfig>;
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;
}
/**

View File

@ -35,36 +35,18 @@ export interface IconifyAPIModule {
/**
* Local storate types and entries
*/
interface ModulesStorage {
default?: IconifyAPIModule;
providers: Record<string, IconifyAPIModule>;
}
const storage: ModulesStorage = {
providers: Object.create(null),
};
/**
* Set default API module
*/
export function setDefaultAPIModule(item: IconifyAPIModule): void {
storage.default = item;
}
const storage: Record<string, IconifyAPIModule> = 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];
}

View File

@ -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,
});

View File

@ -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,
});

View File

@ -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<IconifyAPIConfig>
) => 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<string, PartialIconifyAPIConfig>;
}
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(() => {