import {z} from 'zod' export type GetApiWebstoreArgs = {'x-store-domain': string}; export type GetApiWebstoreOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWebstoreResponse = z.object({store: z.object({name: z.string().optional(), type: z.enum(["STANDALONE", "MINECRAFT", "FIVEM", "REDM", "GTA-SA", "HYTALE"]).optional(), currency: z.enum(["BRL", "USD", "EUR"]).optional(), auth: z.boolean().optional(), require_document: z.boolean().optional(), require_phone: z.boolean().optional(), phone_mode: z.enum(["DISABLED", "OPTIONAL", "REQUIRED"]).optional(), require_address: z.boolean().optional(), maintenance: z.boolean().optional(), business: z.object({}).optional(), active_domain: z.string().optional()}).optional(), theme: z.object({primary_color: z.string().optional(), secondary_color: z.string().optional(), logo: z.string().nullable().optional(), favicon: z.string().nullable().optional(), description: z.string().nullable().optional()}).optional(), socials: z.object({instagram: z.string().nullable().optional(), twitter: z.string().nullable().optional(), discord: z.string().nullable().optional(), youtube: z.string().nullable().optional(), facebook: z.string().nullable().optional(), tiktok: z.string().nullable().optional(), whatsapp: z.string().nullable().optional(), telegram: z.string().nullable().optional()}).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 }; /** * Store Info * * GET /webstore */ export const getApiWebstore = async (args: GetApiWebstoreArgs, options?: GetApiWebstoreOptions) => { const {baseUrl = "https://api.centralcart.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-store-domain'] != null) headers.set("x-store-domain", String(args['x-store-domain'])) const url = `${baseUrl.replace(/\/+$/, '')}/webstore${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(getApiWebstoreResponse, await res.json()) };