OrdisPrime d091af4778
Basic purchasing + custom purchasing API (#20)
Endpoint for custom API: https://localhost:443/custom/addItem
example request:
{
"type": "Weapon",
"internalName": "/Lotus/Weapons/Grineer/Pistols/GrineerMicrowavegun/GrnMicrowavePistol",
"accountId": "6488fd2e7bec200069ca4242"
}
2023-06-14 02:26:19 +02:00

56 lines
1.5 KiB
TypeScript

import { isString, parseString } from "@/src/helpers/general";
import { items } from "@/static/data/items";
export enum ItemType {
Powersuit = "Powersuit",
Weapon = "Weapon"
}
export const isItemType = (itemType: string): itemType is ItemType => {
return Object.keys(ItemType).includes(itemType);
};
const parseItemType = (itemType: unknown): ItemType => {
if (!itemType || !isString(itemType) || !isItemType(itemType)) {
throw new Error("incorrect item type");
}
return itemType;
};
interface IAddItemRequest {
type: ItemType;
InternalName: string;
accountId: string;
}
export const isInternalName = (internalName: string): boolean => {
const item = items.find(i => i.uniqueName === internalName);
return Boolean(item);
};
const parseInternalName = (internalName: unknown): string => {
if (!isString(internalName) || !isInternalName(internalName)) {
throw new Error("incorrect internal name");
}
return internalName;
};
const toAddItemRequest = (body: unknown): IAddItemRequest => {
if (!body || typeof body !== "object") {
throw new Error("incorrect or missing add item request data");
}
if ("type" in body && "internalName" in body && "accountId" in body) {
return {
type: parseItemType(body.type),
InternalName: parseInternalName(body.internalName),
accountId: parseString(body.accountId)
};
}
throw new Error("malformed add item request");
};
export { toAddItemRequest };