Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/shopify

src/shop.ts

86 lines · 2.0 KB · TypeScript
import { shopifyGraphql } from './lib/client.ts'
import type { ShopifyClientOptions } from './types.ts'

export type ShopSummary = {
	id: string
	name: string
	email: string | null
	myshopifyDomain: string
	url: string | null
	currencyCode: string | null
	ianaTimezone: string | null
	plan: {
		displayName: string | null
		partnerDevelopment: boolean
		shopifyPlus: boolean
	}
	primaryDomain: { host: string | null; url: string | null }
}

/**
 * Load a slim shop profile (read-only). Good first authenticated call.
 *
 * @example
 * import { getShop } from 'kody:@kody/shopify/shop'
 * const shop = await getShop({ shop: 'acme' })
 * // => { name: 'Acme', myshopifyDomain: 'acme.myshopify.com', ... }
 */
export async function getShop(
	params: ShopifyClientOptions = {},
): Promise<ShopSummary> {
	const data = await shopifyGraphql<{
		shop: {
			id: string
			name: string
			email?: string | null
			myshopifyDomain: string
			url?: string | null
			currencyCode?: string | null
			ianaTimezone?: string | null
			plan?: {
				displayName?: string | null
				partnerDevelopment?: boolean
				shopifyPlus?: boolean
			} | null
			primaryDomain?: { host?: string | null; url?: string | null } | null
		}
	}>({
		shop: params.shop,
		apiVersion: params.apiVersion,
		query: `query ShopSummary {
			shop {
				id
				name
				email
				myshopifyDomain
				url
				currencyCode
				ianaTimezone
				plan { displayName partnerDevelopment shopifyPlus }
				primaryDomain { host url }
			}
		}`,
	})
	const shop = data.shop
	return {
		id: shop.id,
		name: shop.name,
		email: shop.email ?? null,
		myshopifyDomain: shop.myshopifyDomain,
		url: shop.url ?? null,
		currencyCode: shop.currencyCode ?? null,
		ianaTimezone: shop.ianaTimezone ?? null,
		plan: {
			displayName: shop.plan?.displayName ?? null,
			partnerDevelopment: Boolean(shop.plan?.partnerDevelopment),
			shopifyPlus: Boolean(shop.plan?.shopifyPlus),
		},
		primaryDomain: {
			host: shop.primaryDomain?.host ?? null,
			url: shop.primaryDomain?.url ?? null,
		},
	}
}

/** Default export: shop profile. */
export default getShop