Add utils

This commit is contained in:
jeffvli
2022-07-15 17:53:51 -07:00
parent 962095603a
commit 6ff95581f4
5 changed files with 35 additions and 1 deletions

View File

@@ -95,6 +95,7 @@ export type OffsetPagination = {
};
export type PaginationResponse = {
currentPage: number;
nextPage: string;
prevPage: string;
startIndex: number;
@@ -108,6 +109,7 @@ export type SuccessResponse = {
export type PaginationItems = {
limit: number;
page: number;
startIndex: number;
totalEntries: number;
url: string;

View File

@@ -0,0 +1,19 @@
export const getImageUrl = (
serverType: string,
baseUrl: string,
imageId: string,
token?: string
) => {
if (serverType === 'jellyfin') {
return (
`${baseUrl}/Items` +
`/${imageId}` +
`/Images/Primary` +
'?fillHeight=200' +
`&fillWidth=200` +
'&quality=90'
);
}
return '';
};

View File

@@ -24,11 +24,12 @@ export const getSuccessResponse = (options: {
let pagination;
if (paginationItems) {
const { startIndex, totalEntries, limit, url } = paginationItems;
const { startIndex, totalEntries, limit, url, page } = paginationItems;
const hasPrevPage = startIndex - limit >= 0;
const hasNextPage = startIndex + limit <= totalEntries;
pagination = {
currentPage: page,
nextPage: hasNextPage ? getPaginationUrl(url, 'next') : null,
prevPage: hasPrevPage ? getPaginationUrl(url, 'prev') : null,
startIndex,

View File

@@ -10,3 +10,5 @@ export * from './is-json-string';
export * from './validate-request';
export * from './unique-array';
export * from './zod-validation';
export * from './get-image-url';
export * from './random-string';

View File

@@ -0,0 +1,10 @@
export const randomString = () => {
const charSet =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let string = '';
for (let i = 0; i < 12; i += 1) {
const randomPoz = Math.floor(Math.random() * charSet.length);
string += charSet.substring(randomPoz, randomPoz + 1);
}
return string;
};