mirror of
https://github.com/iconify/iconify.git
synced 2024-11-09 23:00:56 +00:00
Support hosts in mock API module, allow host specific API modules
This commit is contained in:
parent
6c2eb2392c
commit
7143e4abbd
@ -20,8 +20,6 @@ export type IconifyMockAPIDelayCallback = (
|
||||
* Fake API result
|
||||
*/
|
||||
interface IconifyMockAPIBase {
|
||||
provider: string;
|
||||
|
||||
// Response
|
||||
// Number if error should be sent, JSON on success
|
||||
response: number | IconifyJSON | Record<string, unknown>;
|
||||
@ -32,6 +30,7 @@ interface IconifyMockAPIBase {
|
||||
|
||||
export interface IconifyMockIconsAPI extends IconifyMockAPIBase {
|
||||
type: 'icons';
|
||||
provider: string;
|
||||
|
||||
// Request parameters
|
||||
prefix: string;
|
||||
@ -47,6 +46,7 @@ export interface IconifyMockIconsAPI extends IconifyMockAPIBase {
|
||||
|
||||
export interface IconifyMockCustomAPI extends IconifyMockAPIBase {
|
||||
type: 'custom';
|
||||
provider: string;
|
||||
|
||||
// Request parameters
|
||||
uri: string;
|
||||
@ -56,7 +56,22 @@ export interface IconifyMockCustomAPI extends IconifyMockAPIBase {
|
||||
response: number | Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type IconifyMockAPI = IconifyMockIconsAPI | IconifyMockCustomAPI;
|
||||
export interface IconifyMockCustomHostAPI extends IconifyMockAPIBase {
|
||||
type: 'host';
|
||||
host: string;
|
||||
|
||||
// Request parameters
|
||||
uri: string;
|
||||
|
||||
// Response
|
||||
// Number if error should be sent, JSON on success
|
||||
response: number | Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type IconifyMockAPI =
|
||||
| IconifyMockIconsAPI
|
||||
| IconifyMockCustomAPI
|
||||
| IconifyMockCustomHostAPI;
|
||||
|
||||
/**
|
||||
* Fake API storage for icons
|
||||
@ -73,18 +88,28 @@ export const iconsStorage: Record<
|
||||
*
|
||||
* [provider][uri] = response
|
||||
*/
|
||||
export const customStorage: Record<
|
||||
export const customProviderStorage: Record<
|
||||
string,
|
||||
Record<string, IconifyMockCustomAPI>
|
||||
> = Object.create(null);
|
||||
|
||||
/**
|
||||
* Fake API storage for custom queries
|
||||
*
|
||||
* [host][uri] = response
|
||||
*/
|
||||
export const customHostStorage: Record<
|
||||
string,
|
||||
Record<string, IconifyMockCustomHostAPI>
|
||||
> = Object.create(null);
|
||||
|
||||
/**
|
||||
* Set data for mocking API
|
||||
*/
|
||||
export function mockAPIData(data: IconifyMockAPI): void {
|
||||
const provider = data.provider;
|
||||
switch (data.type) {
|
||||
case 'icons': {
|
||||
const provider = data.provider;
|
||||
if (iconsStorage[provider] === void 0) {
|
||||
iconsStorage[provider] = Object.create(null);
|
||||
}
|
||||
@ -100,10 +125,20 @@ export function mockAPIData(data: IconifyMockAPI): void {
|
||||
}
|
||||
|
||||
case 'custom': {
|
||||
if (customStorage[provider] === void 0) {
|
||||
customStorage[provider] = Object.create(null);
|
||||
const provider = data.provider;
|
||||
if (customProviderStorage[provider] === void 0) {
|
||||
customProviderStorage[provider] = Object.create(null);
|
||||
}
|
||||
customStorage[provider][data.uri] = data;
|
||||
customProviderStorage[provider][data.uri] = data;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'host': {
|
||||
const host = data.host;
|
||||
if (customHostStorage[host] === void 0) {
|
||||
customHostStorage[host] = Object.create(null);
|
||||
}
|
||||
customHostStorage[host][data.uri] = data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -220,23 +255,26 @@ export const mockAPIModule: IconifyAPIModule = {
|
||||
status: PendingQueryItem
|
||||
) => {
|
||||
const provider = params.provider;
|
||||
if (provider === void 0) {
|
||||
// Fail: return 400 Bad Request
|
||||
status.done(void 0, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
let data: IconifyMockAPI;
|
||||
|
||||
switch (params.type) {
|
||||
case 'icons': {
|
||||
if (provider === void 0) {
|
||||
// Fail: return 400 Bad Request
|
||||
status.done(void 0, 400);
|
||||
return;
|
||||
}
|
||||
const index = (params as MockAPIIconsQueryParams).index;
|
||||
data = iconsStorage[provider]?.[params.prefix]?.[index];
|
||||
break;
|
||||
}
|
||||
|
||||
case 'custom': {
|
||||
data = customStorage[provider]?.[params.uri];
|
||||
data = (
|
||||
provider === void 0
|
||||
? customHostStorage[host]
|
||||
: customProviderStorage[provider]
|
||||
)?.[params.uri];
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -86,8 +86,11 @@ export function sendAPIQuery(
|
||||
if (config) {
|
||||
redundancy = initRedundancy(config);
|
||||
|
||||
// Use default API provider
|
||||
const api = getAPIModule('');
|
||||
// Use host instead of API provider (defaults to '')
|
||||
const moduleKey = target.resources
|
||||
? (target.resources[0] as string)
|
||||
: '';
|
||||
const api = getAPIModule(moduleKey);
|
||||
if (api) {
|
||||
send = api.send;
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import { loadIcons } from '../../lib/api/icons';
|
||||
import type { IconifyMockAPIDelayDoneCallback } from '../../lib/api/modules/mock';
|
||||
import { mockAPIModule, mockAPIData } from '../../lib/api/modules/mock';
|
||||
import { getStorage, iconExists } from '../../lib/storage/storage';
|
||||
import { sendAPIQuery } from '../../lib/api/query';
|
||||
|
||||
describe('Testing mock API module', () => {
|
||||
let prefixCounter = 0;
|
||||
@ -18,10 +19,13 @@ describe('Testing mock API module', () => {
|
||||
|
||||
// Set API module for provider
|
||||
const provider = nextPrefix();
|
||||
setAPIConfig(provider, {
|
||||
resources: ['https://api1.local'],
|
||||
|
||||
before(() => {
|
||||
setAPIConfig(provider, {
|
||||
resources: ['https://api1.local'],
|
||||
});
|
||||
setAPIModule(provider, mockAPIModule);
|
||||
});
|
||||
setAPIModule(provider, mockAPIModule);
|
||||
|
||||
// Tests
|
||||
it('404 response', (done) => {
|
||||
@ -286,4 +290,71 @@ describe('Testing mock API module', () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('Custom query', (done) => {
|
||||
mockAPIData({
|
||||
type: 'custom',
|
||||
provider,
|
||||
uri: '/test',
|
||||
response: {
|
||||
foo: true,
|
||||
},
|
||||
});
|
||||
|
||||
let isSync = true;
|
||||
|
||||
sendAPIQuery(
|
||||
provider,
|
||||
{
|
||||
type: 'custom',
|
||||
provider,
|
||||
uri: '/test',
|
||||
},
|
||||
(data, error) => {
|
||||
expect(error).to.be.equal(void 0);
|
||||
expect(data).to.be.eql({
|
||||
foo: true,
|
||||
});
|
||||
expect(isSync).to.be.equal(false);
|
||||
done();
|
||||
}
|
||||
);
|
||||
|
||||
isSync = false;
|
||||
});
|
||||
|
||||
it('Custom query with host', (done) => {
|
||||
const host = 'http://' + nextPrefix();
|
||||
setAPIModule(host, mockAPIModule);
|
||||
mockAPIData({
|
||||
type: 'host',
|
||||
host,
|
||||
uri: '/test',
|
||||
response: {
|
||||
foo: 2,
|
||||
},
|
||||
});
|
||||
|
||||
let isSync = true;
|
||||
|
||||
sendAPIQuery(
|
||||
{
|
||||
resources: [host],
|
||||
},
|
||||
{
|
||||
type: 'custom',
|
||||
uri: '/test',
|
||||
},
|
||||
(data, error) => {
|
||||
expect(error).to.be.equal(void 0);
|
||||
expect(data).to.be.eql({
|
||||
foo: 2,
|
||||
});
|
||||
expect(isSync).to.be.equal(false);
|
||||
done();
|
||||
}
|
||||
);
|
||||
|
||||
isSync = false;
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user