npm.io
1.0.7 • Published 2 years ago

typed-shopify-storefront

Licence
MIT
Version
1.0.7
Deps
0
Size
36 kB
Vulns
0
Weekly
0
Stars
1

Typed Storefront

License: MIT GitHub issues

A simple, lightweight, explicit library for interacting with the Shopify Storefront API

Example

import shopify, { ProductVariant, SelectedProductOption, LineIem } from 'typed-shopify-storefront'

const client = shopify({
    accessToken: 'somestore_storefront_token',
    domain: 'somestore.myshopify.com'
})

(async () => {
    // Fetch all products within store
    const collection = await client.product.all()

    // Fetch data of a single product
    const product = await client.product.get(collection[0])

    // Find a product variant from selected options
    const options: SelectedProductOptions[] = [
        { name: "Color", value: "Black" },
        { name: "Size", value: "1" }
    ]
    const variant = client.product.findVariant(product.variants, options)

    // Create a checkout
    const cart: LineItem[] = [
        { variantId: variant.id, amount: 1 }
    ]
    const checkout = await client.checkout.create(cart)

    console.log(checkout.webUrl)
})

Documentation

Initialize the client

const  client = shopify({
    accessToken:  'somestore_storefront_token',
    domain:  'somestore.myshopify.com'
})

Once initialized, the client object is created with methods for interacting with the Shopify API.

Product
Method Description Parameters Returns
all() Retrieves all products from the store. None Collection
get(handle) Retrieves a product by its handle. handle: string Product
findVariant(variants, selectedOptions) Finds a variant of a product based on selected options. variants: ProductVariant[], selectedOptions: SelectedProductOption[] ProductVariant | undefined
Checkout
Method Description Parameters Returns
create(lineItems) Creates a new checkout with specified line items. lineItems: LineItem[] Checkout