mirror of
https://github.com/StepanovPlaton/jelly_belly_wiki.git
synced 2026-04-04 04:40:43 +04:00
09-07
This commit is contained in:
19
src/entities/item/combinations/index.ts
Normal file
19
src/entities/item/combinations/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import {
|
||||
combinationSchema,
|
||||
combinationsSchema,
|
||||
pageOfCombinationsSchema,
|
||||
thisItemIsCombination,
|
||||
type CombinationType,
|
||||
type PageOfCombinationsType,
|
||||
} from "./schema";
|
||||
export {
|
||||
combinationSchema,
|
||||
combinationsSchema,
|
||||
pageOfCombinationsSchema,
|
||||
thisItemIsCombination,
|
||||
type CombinationType,
|
||||
type PageOfCombinationsType,
|
||||
};
|
||||
|
||||
import { CombinationsService } from "./service";
|
||||
export { CombinationsService };
|
||||
42
src/entities/item/combinations/schema.ts
Normal file
42
src/entities/item/combinations/schema.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { z } from "zod";
|
||||
import { ItemType, TypesOfItems } from "../types";
|
||||
|
||||
export const combinationSchema = z.object({
|
||||
combinationId: z.number(),
|
||||
name: z.string(),
|
||||
tag: z.array(z.string()),
|
||||
|
||||
// Показывает, что этот item - combination
|
||||
type: z
|
||||
.any()
|
||||
.optional()
|
||||
.transform(() => TypesOfItems.combination),
|
||||
});
|
||||
export type CombinationType = z.infer<typeof combinationSchema>;
|
||||
|
||||
export const isCombination = (a: any): a is CombinationType => {
|
||||
return combinationSchema.safeParse(a).success;
|
||||
};
|
||||
|
||||
export const combinationsSchema = z.array(z.any()).transform((a) => {
|
||||
const combinations: CombinationType[] = [];
|
||||
a.forEach((e) => {
|
||||
if (isCombination(e)) combinations.push(combinationSchema.parse(e));
|
||||
else console.error("Combination parse error - ", e);
|
||||
});
|
||||
return combinations;
|
||||
});
|
||||
|
||||
export const pageOfCombinationsSchema = z.object({
|
||||
totalCount: z.number(),
|
||||
pageSize: z.number(),
|
||||
currentPage: z.number(),
|
||||
totalPages: z.number(),
|
||||
items: combinationsSchema,
|
||||
});
|
||||
|
||||
export type PageOfCombinationsType = z.infer<typeof pageOfCombinationsSchema>;
|
||||
|
||||
export const thisItemIsCombination = (i: ItemType): i is CombinationType => {
|
||||
return (i as CombinationType).type === TypesOfItems.combination;
|
||||
};
|
||||
29
src/entities/item/combinations/service.ts
Normal file
29
src/entities/item/combinations/service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { HTTPService } from "@/shared/utils/http";
|
||||
import { IItemService, staticImplements } from "../types";
|
||||
import { combinationSchema, pageOfCombinationsSchema } from "./schema";
|
||||
|
||||
@staticImplements<IItemService>()
|
||||
export abstract class CombinationsService {
|
||||
public static urlPrefix = "combinations";
|
||||
public static cacheOptions = {
|
||||
next: {
|
||||
revalidate: 60 * 5,
|
||||
},
|
||||
};
|
||||
|
||||
public static async Get(id: number) {
|
||||
return await HTTPService.get(
|
||||
`/${this.urlPrefix}/${id}`,
|
||||
combinationSchema,
|
||||
this.cacheOptions
|
||||
);
|
||||
}
|
||||
|
||||
public static async GetPage(page: number, pageSize?: number) {
|
||||
return await HTTPService.get(
|
||||
`/${this.urlPrefix}?pageIndex=${page}&pageSize=${pageSize ?? 2 * 3 * 3}`,
|
||||
pageOfCombinationsSchema,
|
||||
this.cacheOptions
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user