import {z} from 'zod' export type GetApiAdminShopJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const shopDto = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), country: z.string().optional(), country_code: z.string().optional(), country_name: z.string().optional(), county_taxes: z.boolean().optional(), created_at: z.number().int().optional(), currency: z.string().optional(), current_active_storefront: z.number().int().optional(), customer_email: z.string().optional(), domain: z.string().optional(), email: z.string().optional(), enable: z.boolean().optional(), enable_multi_storefront: z.boolean().optional(), enable_onboarding_multi_sf: z.boolean().optional(), end_free_trial_at: z.number().int().optional(), iana_timezone: z.string().optional(), id: z.number().int().optional(), latitude: z.number().optional(), longitude: z.number().optional(), max_free_storefront: z.number().int().optional(), money_format: z.string().optional(), money_in_emails_format: z.string().optional(), money_with_currency_format: z.string().optional(), money_with_currency_in_emails_format: z.string().optional(), multi_storefront: z.number().int().optional(), name: z.string().optional(), online_store_enable: z.number().int().optional(), onshopbase_domain: z.string().optional(), parent_id: z.number().int().optional(), password_enabled: z.boolean().optional(), phone: z.string().optional(), primary_domain: z.string().optional(), primary_locale: z.string().optional(), province: z.string().optional(), province_code: z.string().optional(), shop_owner: z.string().optional(), source: z.string().optional(), storefront_fee: z.number().optional(), storefront_free: z.boolean().optional(), subscription_expired_at: z.number().int().optional(), tax_shipping: z.boolean().optional(), taxes_included: z.boolean().optional(), timezone: z.string().optional(), updated_at: z.number().int().optional(), used_free_storefront: z.number().int().optional(), weight_unit: z.string().optional(), zip: z.string().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 shop. * * GET /admin/shop.json */ export const getApiAdminShopJson = async (options?: GetApiAdminShopJsonOptions) => { const {baseUrl = "https://shop-name.onshopbase.com", fetch: fetchFn = fetch, APPACCESSTOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APPACCESSTOKEN !== undefined) headers.set("APP_ACCESS_TOKEN", APPACCESSTOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/admin/shop.json${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(shopDto, await res.json()) };