import {z} from 'zod' export type GetApiV1ProductsProductIdArgs = {productId: number, showPurchasableOpt: boolean}; export type GetApiV1ProductsProductIdOptions = {baseUrl?: string | undefined, robloxLegacyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const robloxEconomyApiPremiumPricingModel = z.object({premiumDiscountPercentage: z.number().int().optional(), premiumPriceInRobux: z.number().int().optional()}); export const robloxEconomyApiModelsPurchasableDetailResponse = z.object({purchasable: z.boolean().optional(), reason: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(17), z.literal(18), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(25), z.literal(26)]).optional(), productId: z.number().int().optional(), statusCode: z.number().int().optional(), title: z.string().optional(), errorMsg: z.string().optional(), showDivId: z.string().optional(), shortfallPrice: z.number().int().optional(), balanceAfterSale: z.number().int().optional(), expectedPrice: z.number().int().optional(), currency: z.number().int().optional(), price: z.number().int().optional(), assetId: z.number().int().optional(), assetName: z.string().optional(), assetType: z.string().optional(), assetTypeDisplayName: z.string().optional(), assetIsWearable: z.boolean().optional(), sellerName: z.string().optional(), transactionVerb: z.string().optional(), isMultiPrivateSale: z.boolean().optional(), premiumPricing: robloxEconomyApiPremiumPricingModel.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Retrieves the product purchasable details given its id. * * GET /v1/products/{productId} */ export const getApiV1ProductsProductId = async (args: GetApiV1ProductsProductIdArgs, options?: GetApiV1ProductsProductIdOptions) => { const {baseUrl = "https://economy.roblox.com", fetch: fetchFn = fetch, robloxLegacyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.showPurchasableOpt != null) query.append("showPurchasableOpt", String(args.showPurchasableOpt)) if (robloxLegacyCookie !== undefined) cookies.push(".ROBLOSECURITY=" + encodeURIComponent(robloxLegacyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/products/${encodeURIComponent(String(args.productId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(robloxEconomyApiModelsPurchasableDetailResponse, await res.json()) };