2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

Update core package for new redundancy structure

This commit is contained in:
Vjacheslav Trushkin 2022-01-25 22:18:52 +02:00
parent db883f9593
commit 2e838b5ff4
6 changed files with 63 additions and 55 deletions

View File

@ -46,7 +46,7 @@ describe('Testing API loadIcons', () => {
return [item];
};
const sendQuery = (host, params, item) => {
const sendQuery = (host, params, callback) => {
// This callback should be called after prepareQuery
expect(asyncCounter).toBe(2);
asyncCounter++;
@ -64,7 +64,7 @@ describe('Testing API loadIcons', () => {
expect(params).toEqual(expected);
// Send data
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -170,7 +170,7 @@ describe('Testing API loadIcons', () => {
};
let queryCounter = 0;
const sendQuery = (host, params, item) => {
const sendQuery = (host, params, callback) => {
// Test input
expect(host).toBe('https://api1.local');
@ -196,7 +196,7 @@ describe('Testing API loadIcons', () => {
body: '<path d="" />',
};
});
item.done({
callback('success', {
prefix,
icons,
// Test mismatched provider: should be ignored because provider name is not affected by actual API response
@ -263,7 +263,7 @@ describe('Testing API loadIcons', () => {
};
let queryCounter = 0;
const sendQuery = (host, params, item) => {
const sendQuery = (host, params, callback) => {
queryCounter++;
params;
switch (queryCounter) {
@ -279,7 +279,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -359,7 +359,7 @@ describe('Testing API loadIcons', () => {
};
let queryCounter = 0;
const sendQuery = (host, params, item) => {
const sendQuery = (host, params, callback) => {
queryCounter++;
expect(params.type).toBe('icons');
@ -382,7 +382,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -401,7 +401,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix,
icons: {
icon3: {
@ -514,7 +514,7 @@ describe('Testing API loadIcons', () => {
};
let queryCounter = 0;
const sendQuery = (host, params, item) => {
const sendQuery = (host, params, callback) => {
queryCounter++;
expect(params.type).toBe('icons');
@ -539,7 +539,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix: params.prefix,
icons: {
icon1: {
@ -559,7 +559,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix: params.prefix,
icons: {
icon2: {

View File

@ -1,4 +1,4 @@
import type { PendingQueryItem } from '@iconify/api-redundancy';
import type { QueryModuleResponse } from '@iconify/api-redundancy';
/**
* Params for sendQuery()
@ -31,7 +31,7 @@ export type IconifyAPIPrepareIconsQuery = (
export type IconifyAPISendQuery = (
host: string,
params: IconifyAPIQueryParams,
status: PendingQueryItem
callback: QueryModuleResponse
) => void;
/**

View File

@ -1,4 +1,4 @@
import type { PendingQueryItem } from '@iconify/api-redundancy';
import type { QueryModuleResponse } from '@iconify/api-redundancy';
import type {
IconifyAPIQueryParams,
IconifyAPIPrepareIconsQuery,
@ -106,6 +106,13 @@ function calculateMaxLength(provider: string, prefix: string): number {
return result;
}
/**
* Should query be aborted, based on last HTTP status
*/
function shouldAbort(status: number): boolean {
return status === 404;
}
/**
* Prepare params
*/
@ -178,11 +185,11 @@ function getPath(provider?: string): string {
const send: IconifyAPISendQuery = (
host: string,
params: IconifyAPIQueryParams,
status: PendingQueryItem
callback: QueryModuleResponse
): void => {
if (!fetchModule) {
// Fail: return "424 Failed Dependency" (its not meant to be used like that, but it is the closest match)
status.done(void 0, 424);
callback('abort', 424);
return;
}
@ -208,7 +215,7 @@ const send: IconifyAPISendQuery = (
default:
// Fail: return 400 Bad Request
status.done(void 0, 400);
callback('abort', 400);
return;
}
@ -218,10 +225,11 @@ const send: IconifyAPISendQuery = (
// console.log('API query:', host + path);
fetchModule(host + path)
.then((response) => {
if (response.status !== 200) {
const status = response.status;
if (status !== 200) {
setTimeout(() => {
// Complete on next tick to get out of try...catch
status.done(void 0, response.status);
callback(shouldAbort(status) ? 'abort' : 'next', status);
});
return;
}
@ -234,7 +242,7 @@ const send: IconifyAPISendQuery = (
if (typeof data !== 'object' || data === null) {
setTimeout(() => {
// Complete on next tick to get out of try...catch
status.done(void 0, defaultError);
callback('next', defaultError);
});
return;
}
@ -242,11 +250,11 @@ const send: IconifyAPISendQuery = (
// Store cache and complete on next tick
setTimeout(() => {
// Complete on next tick to get out of try...catch
status.done(data);
callback('success', data);
});
})
.catch(() => {
status.done(void 0, defaultError);
callback('next', defaultError);
});
};

View File

@ -1,4 +1,4 @@
import type { PendingQueryItem } from '@iconify/api-redundancy';
import type { QueryModuleResponse } from '@iconify/api-redundancy';
import type {
IconifyAPIQueryParams,
IconifyAPIPrepareIconsQuery,
@ -104,6 +104,7 @@ function calculateMaxLength(provider: string, prefix: string): number {
// Get available length
const url = mergeParams(prefix + '.js', {
icons: '',
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callback: rootVarName!,
});
result =
@ -170,11 +171,11 @@ const prepare: IconifyAPIPrepareIconsQuery = (
const send: IconifyAPISendQuery = (
host: string,
params: IconifyAPIQueryParams,
status: PendingQueryItem
callback: QueryModuleResponse
): void => {
if (params.type !== 'icons') {
// JSONP supports only icons
status.done(void 0, 400);
callback('abort', 400);
return;
}
@ -201,6 +202,7 @@ const send: IconifyAPISendQuery = (
const url = mergeParams(prefix + '.js', {
icons: iconsList,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callback: rootVarName!.replace('{cb}', callbackName),
});
const path = pathCache[cacheKey] + url;
@ -208,7 +210,7 @@ const send: IconifyAPISendQuery = (
global[callbackName] = (data: unknown): void => {
// Remove callback and complete query
delete global[callbackName];
status.done(data);
callback('success', data);
};
// Create URI

View File

@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars-experimental */
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { PendingQueryItem } from '@iconify/api-redundancy';
import type { QueryModuleResponse } from '@iconify/api-redundancy';
import type {
IconifyAPIIconsQueryParams,
IconifyAPIQueryParams,
@ -252,7 +251,7 @@ export const mockAPIModule: IconifyAPIModule = {
send: (
host: string,
params: IconifyAPIQueryParams,
status: PendingQueryItem
queryCallback: QueryModuleResponse
) => {
const provider = params.provider;
let data: IconifyMockAPI;
@ -261,7 +260,7 @@ export const mockAPIModule: IconifyAPIModule = {
case 'icons': {
if (provider === void 0) {
// Fail: return 400 Bad Request
status.done(void 0, 400);
queryCallback('abort', 400);
return;
}
const index = (params as MockAPIIconsQueryParams).index;
@ -280,12 +279,12 @@ export const mockAPIModule: IconifyAPIModule = {
default:
// Fail: return 400 Bad Request
status.done(void 0, 400);
queryCallback('abort', 400);
return;
}
if (data === void 0) {
status.done(void 0, 404);
queryCallback('abort', 404);
return;
}
@ -308,11 +307,10 @@ export const mockAPIModule: IconifyAPIModule = {
// Run after delay
callback(() => {
if (typeof data.response === 'number') {
status.done(void 0, data.response);
} else {
status.done(data.response);
}
queryCallback(
typeof data.response === 'number' ? 'next' : 'success',
data.response
);
});
},
};

View File

@ -1,4 +1,4 @@
import type { PendingQueryItem } from '@iconify/api-redundancy';
import type { QueryModuleResponse } from '@iconify/api-redundancy';
import { addAPIProvider } from '../../lib/api/config';
import type {
IconifyAPIIconsQueryParams,
@ -58,7 +58,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
// This callback should be called after prepareQuery
expect(asyncCounter).toBe(2);
@ -77,7 +77,7 @@ describe('Testing API loadIcons', () => {
expect(params).toEqual(expected);
// Send data
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -191,7 +191,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
expect(params.type).toBe('icons');
@ -206,7 +206,7 @@ describe('Testing API loadIcons', () => {
expect(params).toEqual(expected);
// Send data
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -265,7 +265,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
expect(params.type).toBe('icons');
@ -280,7 +280,7 @@ describe('Testing API loadIcons', () => {
expect(params).toEqual(expected);
// Send data
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -351,7 +351,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
// Test input
expect(host).toBe('https://api1.local');
@ -378,7 +378,7 @@ describe('Testing API loadIcons', () => {
body: '<path d="" />',
};
});
item.done({
callback('success', {
prefix,
icons,
// Test mismatched provider: should be ignored because provider name is not affected by actual API response
@ -452,7 +452,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
queryCounter++;
params;
@ -469,7 +469,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -556,7 +556,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
queryCounter++;
@ -580,7 +580,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix,
icons: {
icon1: {
@ -599,7 +599,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix,
icons: {
icon3: {
@ -719,7 +719,7 @@ describe('Testing API loadIcons', () => {
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
item: PendingQueryItem
callback: QueryModuleResponse
): void => {
queryCounter++;
@ -745,7 +745,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix: params.prefix,
icons: {
icon1: {
@ -765,7 +765,7 @@ describe('Testing API loadIcons', () => {
expect(host).toBe('https://api2.local');
// Return result
item.done({
callback('success', {
prefix: params.prefix,
icons: {
icon2: {