Disallow any types
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* eslint-disable complexity, @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable complexity */
|
||||
import { Router } from 'express';
|
||||
import { getKlipyApiKey, hasKlipyApiKey } from '../config/variables';
|
||||
|
||||
@@ -25,6 +25,28 @@ interface NormalizedKlipyGif {
|
||||
height: number;
|
||||
}
|
||||
|
||||
interface KlipyGifVariants {
|
||||
md?: unknown;
|
||||
sm?: unknown;
|
||||
xs?: unknown;
|
||||
hd?: unknown;
|
||||
}
|
||||
|
||||
interface KlipyGifItem {
|
||||
type?: unknown;
|
||||
slug?: unknown;
|
||||
id?: unknown;
|
||||
title?: unknown;
|
||||
file?: KlipyGifVariants;
|
||||
}
|
||||
|
||||
interface KlipyApiResponse {
|
||||
data?: {
|
||||
data?: unknown;
|
||||
has_next?: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
function pickFirst<T>(...values: (T | null | undefined)[]): T | undefined {
|
||||
for (const value of values) {
|
||||
if (value != null)
|
||||
@@ -91,16 +113,38 @@ function pickGifMeta(sizeVariant: unknown): NormalizedMediaMeta | null {
|
||||
return normalizeMediaMeta(candidate?.gif) ?? normalizeMediaMeta(candidate?.webp);
|
||||
}
|
||||
|
||||
function normalizeGifItem(item: any): NormalizedKlipyGif | null {
|
||||
if (!item || typeof item !== 'object' || item.type === 'ad')
|
||||
function extractKlipyResponseData(payload: unknown): { items: unknown[]; hasNext: boolean } {
|
||||
if (typeof payload !== 'object' || payload === null) {
|
||||
return {
|
||||
items: [],
|
||||
hasNext: false
|
||||
};
|
||||
}
|
||||
|
||||
const response = payload as KlipyApiResponse;
|
||||
const items = Array.isArray(response.data?.data) ? response.data.data : [];
|
||||
|
||||
return {
|
||||
items,
|
||||
hasNext: response.data?.has_next === true
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeGifItem(item: unknown): NormalizedKlipyGif | null {
|
||||
if (!item || typeof item !== 'object')
|
||||
return null;
|
||||
|
||||
const lowVariant = pickFirst(item.file?.md, item.file?.sm, item.file?.xs, item.file?.hd);
|
||||
const highVariant = pickFirst(item.file?.hd, item.file?.md, item.file?.sm, item.file?.xs);
|
||||
const gifItem = item as KlipyGifItem;
|
||||
|
||||
if (gifItem.type === 'ad')
|
||||
return null;
|
||||
|
||||
const lowVariant = pickFirst(gifItem.file?.md, gifItem.file?.sm, gifItem.file?.xs, gifItem.file?.hd);
|
||||
const highVariant = pickFirst(gifItem.file?.hd, gifItem.file?.md, gifItem.file?.sm, gifItem.file?.xs);
|
||||
const lowMeta = pickGifMeta(lowVariant);
|
||||
const highMeta = pickGifMeta(highVariant);
|
||||
const selectedMeta = highMeta ?? lowMeta;
|
||||
const slug = sanitizeString(item.slug) ?? sanitizeString(item.id);
|
||||
const slug = sanitizeString(gifItem.slug) ?? sanitizeString(gifItem.id);
|
||||
|
||||
if (!slug || !selectedMeta?.url)
|
||||
return null;
|
||||
@@ -108,7 +152,7 @@ function normalizeGifItem(item: any): NormalizedKlipyGif | null {
|
||||
return {
|
||||
id: slug,
|
||||
slug,
|
||||
title: sanitizeString(item.title),
|
||||
title: sanitizeString(gifItem.title),
|
||||
url: selectedMeta.url,
|
||||
previewUrl: lowMeta?.url ?? selectedMeta.url,
|
||||
width: selectedMeta.width ?? lowMeta?.width ?? 0,
|
||||
@@ -196,9 +240,7 @@ router.get('/klipy/gifs', async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
const rawItems = Array.isArray((payload as any)?.data?.data)
|
||||
? (payload as any).data.data
|
||||
: [];
|
||||
const { items: rawItems, hasNext } = extractKlipyResponseData(payload);
|
||||
const results = rawItems
|
||||
.map((item: unknown) => normalizeGifItem(item))
|
||||
.filter((item: NormalizedKlipyGif | null): item is NormalizedKlipyGif => !!item);
|
||||
@@ -206,7 +248,7 @@ router.get('/klipy/gifs', async (req, res) => {
|
||||
res.json({
|
||||
enabled: true,
|
||||
results,
|
||||
hasNext: (payload as any)?.data?.has_next === true
|
||||
hasNext
|
||||
});
|
||||
} catch (error) {
|
||||
if ((error as { name?: string })?.name === 'AbortError') {
|
||||
|
||||
6
server/src/types/sqljs.d.ts
vendored
6
server/src/types/sqljs.d.ts
vendored
@@ -1,9 +1,9 @@
|
||||
declare module 'sql.js';
|
||||
declare module 'sql.js' {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
// eslint-disable-next-line
|
||||
export default function initSqlJs(config?: { locateFile?: (file: string) => string }): Promise<any>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
// eslint-disable-next-line
|
||||
export type Database = any;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
// eslint-disable-next-line
|
||||
export type Statement = any;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user