mirror of
https://github.com/iconify/iconify.git
synced 2025-01-07 15:44:05 +00:00
Slightly change API providers behavior, import providers from IconifyProviders global
This commit is contained in:
parent
22d832a7e2
commit
eea235f97d
@ -19,13 +19,19 @@ export type PartialIconifyAPIConfig = Partial<IconifyAPIConfig>;
|
|||||||
function createConfig(
|
function createConfig(
|
||||||
source: PartialIconifyAPIConfig
|
source: PartialIconifyAPIConfig
|
||||||
): IconifyAPIConfig | null {
|
): IconifyAPIConfig | null {
|
||||||
if (!source.resources) {
|
let resources;
|
||||||
return null;
|
if (typeof source.resources === 'string') {
|
||||||
|
resources = [source.resources];
|
||||||
|
} else {
|
||||||
|
resources = source.resources;
|
||||||
|
if (!(resources instanceof Array) || !resources.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const result: IconifyAPIConfig = {
|
const result: IconifyAPIConfig = {
|
||||||
// API hosts
|
// API hosts
|
||||||
resources: source.resources,
|
resources: resources,
|
||||||
|
|
||||||
// Root path
|
// Root path
|
||||||
path: source.path === void 0 ? '/' : source.path,
|
path: source.path === void 0 ? '/' : source.path,
|
||||||
@ -103,12 +109,13 @@ configStorage[''] = createConfig({
|
|||||||
export function setAPIConfig(
|
export function setAPIConfig(
|
||||||
provider: string,
|
provider: string,
|
||||||
customConfig: PartialIconifyAPIConfig
|
customConfig: PartialIconifyAPIConfig
|
||||||
): void {
|
): boolean {
|
||||||
const config = createConfig(customConfig);
|
const config = createConfig(customConfig);
|
||||||
if (config === null) {
|
if (config === null) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
configStorage[provider] = config;
|
configStorage[provider] = config;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,36 +35,18 @@ export interface IconifyAPIModule {
|
|||||||
/**
|
/**
|
||||||
* Local storate types and entries
|
* Local storate types and entries
|
||||||
*/
|
*/
|
||||||
interface ModulesStorage {
|
const storage: Record<string, IconifyAPIModule> = Object.create(null);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set API module
|
* Set API module
|
||||||
*/
|
*/
|
||||||
export function setProviderAPIModule(
|
export function setAPIModule(provider: string, item: IconifyAPIModule): void {
|
||||||
provider: string,
|
storage[provider] = item;
|
||||||
item: IconifyAPIModule
|
|
||||||
): void {
|
|
||||||
storage.providers[provider] = item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get API module
|
* Get API module
|
||||||
*/
|
*/
|
||||||
export function getAPIModule(provider: string): IconifyAPIModule | undefined {
|
export function getAPIModule(provider: string): IconifyAPIModule | undefined {
|
||||||
return storage.providers[provider] === void 0
|
return storage[provider] === void 0 ? storage[''] : storage[provider];
|
||||||
? storage.default
|
|
||||||
: storage.providers[provider];
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
IconifyAPIConfig,
|
IconifyAPIConfig,
|
||||||
} from '../../lib/api/config';
|
} from '../../lib/api/config';
|
||||||
import {
|
import {
|
||||||
setProviderAPIModule,
|
setAPIModule,
|
||||||
APIQueryParams,
|
APIQueryParams,
|
||||||
getAPIModule,
|
getAPIModule,
|
||||||
IconifyAPIModule,
|
IconifyAPIModule,
|
||||||
@ -55,7 +55,7 @@ describe('Testing API modules', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Set fake module
|
// Set fake module
|
||||||
setProviderAPIModule(provider, {
|
setAPIModule(provider, {
|
||||||
prepare: prepareQuery,
|
prepare: prepareQuery,
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
});
|
});
|
||||||
|
@ -4,7 +4,7 @@ import 'mocha';
|
|||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { RedundancyPendingItem } from '@cyberalien/redundancy';
|
import { RedundancyPendingItem } from '@cyberalien/redundancy';
|
||||||
import { setAPIConfig, IconifyAPIConfig } from '../../lib/api/config';
|
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/';
|
import { API } from '../../lib/api/';
|
||||||
|
|
||||||
describe('Testing API loadIcons', () => {
|
describe('Testing API loadIcons', () => {
|
||||||
@ -88,7 +88,7 @@ describe('Testing API loadIcons', () => {
|
|||||||
expect(asyncCounter).to.be.equal(3);
|
expect(asyncCounter).to.be.equal(3);
|
||||||
};
|
};
|
||||||
|
|
||||||
setProviderAPIModule(provider, {
|
setAPIModule(provider, {
|
||||||
prepare: prepareQuery,
|
prepare: prepareQuery,
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
});
|
});
|
||||||
@ -215,7 +215,7 @@ describe('Testing API loadIcons', () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
setProviderAPIModule(provider, {
|
setAPIModule(provider, {
|
||||||
prepare: prepareQuery,
|
prepare: prepareQuery,
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
});
|
});
|
||||||
@ -316,7 +316,7 @@ describe('Testing API loadIcons', () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setProviderAPIModule(provider, {
|
setAPIModule(provider, {
|
||||||
prepare: prepareQuery,
|
prepare: prepareQuery,
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
});
|
});
|
||||||
@ -439,7 +439,7 @@ describe('Testing API loadIcons', () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setProviderAPIModule(provider, {
|
setAPIModule(provider, {
|
||||||
prepare: prepareQuery,
|
prepare: prepareQuery,
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
});
|
});
|
||||||
@ -598,7 +598,7 @@ describe('Testing API loadIcons', () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setProviderAPIModule(provider, {
|
setAPIModule(provider, {
|
||||||
prepare: prepareQuery,
|
prepare: prepareQuery,
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
});
|
});
|
||||||
|
@ -40,8 +40,12 @@ import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage';
|
|||||||
|
|
||||||
// API
|
// API
|
||||||
import { API } from '@iconify/core/lib/api/';
|
import { API } from '@iconify/core/lib/api/';
|
||||||
import { setDefaultAPIModule } from '@iconify/core/lib/api/modules';
|
import { setAPIModule } from '@iconify/core/lib/api/modules';
|
||||||
import { setAPIConfig, IconifyAPIConfig } from '@iconify/core/lib/api/config';
|
import {
|
||||||
|
setAPIConfig,
|
||||||
|
PartialIconifyAPIConfig,
|
||||||
|
IconifyAPIConfig,
|
||||||
|
} from '@iconify/core/lib/api/config';
|
||||||
import { prepareQuery, sendQuery } from './modules/api-jsonp';
|
import { prepareQuery, sendQuery } from './modules/api-jsonp';
|
||||||
import {
|
import {
|
||||||
IconifyIconLoaderCallback,
|
IconifyIconLoaderCallback,
|
||||||
@ -172,9 +176,9 @@ export interface IconifyGlobal {
|
|||||||
resumeObserver: () => void;
|
resumeObserver: () => void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set API configuration
|
* Add API provider
|
||||||
*/
|
*/
|
||||||
setAPIConfig: (
|
addAPIProvider: (
|
||||||
provider: string,
|
provider: string,
|
||||||
customConfig: Partial<IconifyAPIConfig>
|
customConfig: Partial<IconifyAPIConfig>
|
||||||
) => void;
|
) => void;
|
||||||
@ -381,8 +385,8 @@ const Iconify: IconifyGlobal = {
|
|||||||
// Resume observer
|
// Resume observer
|
||||||
resumeObserver: observer.resume,
|
resumeObserver: observer.resume,
|
||||||
|
|
||||||
// API configuration
|
// Add API provider
|
||||||
setAPIConfig: setAPIConfig,
|
addAPIProvider: setAPIConfig,
|
||||||
|
|
||||||
// Scan DOM
|
// Scan DOM
|
||||||
scanDOM: scanDOM,
|
scanDOM: scanDOM,
|
||||||
@ -428,11 +432,43 @@ loadCache();
|
|||||||
|
|
||||||
// Set API
|
// Set API
|
||||||
coreModules.api = API;
|
coreModules.api = API;
|
||||||
setDefaultAPIModule({
|
setAPIModule('', {
|
||||||
send: sendQuery,
|
send: sendQuery,
|
||||||
prepare: prepareQuery,
|
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
|
// Load observer
|
||||||
browserModules.observer = observer;
|
browserModules.observer = observer;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user