7.0.34 • Published 10 months ago

@takinola/handy-shopify v7.0.34

Weekly downloads
1
License
AGPL-3.0
Repository
-
Last release
10 months ago

Handy-Shopify.js Documentation

Introduction

Features

Dependencies

Getting Started

Globals

Globals are set using handy.system.systemGlobal.set(global_name, global_value)

shopify_custom_app

If true, indicates the app is a custom app. Used to bypass functionality that is only applicable to public apps eg billing selection

handy_shopify_default_webhooks

Array of webhooks always set upon installation in the shop (typically, shop/update and shop/uninstall)

API Documentation

Module: handy-shopfy

addPostAppInstallFunctions

Adds new functions that are executed whenever the app is installed on a shop. The functions must return promises and are executed in the context of the shop “this” object

arguments

  • @param {array} functionArray - array of functions to be executed

example

addPostAppInstallFunctions([function_a, function_b,....])

authenticateJWT

Middleware to authenticate calls from front end using JWT Used to authenticate calls from interfaces embedded inside the Shopify admin

arguments

  • @param {object} req - express request object
  • @param {object} res - express response object
  • @param {function} next - express next object

example

let shopifyAdminRouter = express.router

shopifyAdminRouter.use(authenticateJWT)
shopifyAdminRouter.post('/random_path', (req, res)=>{})

Shop

This is the Shop class. It is based on the handy baseObject

Shop objects have the following properties (in addition to those inherited from the handy baseObject) insert property list

Shop objects have the following methods

getAccessScopes

Get list of all access scopes for currently installed app

arguments None

example

let shop = new Shop(...)
const scopes = await shop.getAccessScopes();
// scopes = ['read_theme', 'write_themes', ...]
validateAccessScopes

Middleware to validate shop has all the current access scopes

arguments

  • @param {object} req - Express request object
  • @param {object} res - Express response object
  • @param {object} next - Express next object

example

app.get('/settings', validateAccessScopes, (req, res)=>{
  const myshopify_domain = req.query.shop;  // it is important that the myshopify domain is included in the req.query.shop value
})
getThemes

Gets all themes available in the shop

arguments

  • @param {object} {current} - if true, returns only the published theme otherwise returns all themes

example

let shop = new Shop(...)
let current = true
shop.getThemes({current})
.then((currentTheme)=> /.../)
createAsset

Creates an (or updates an existing) asset in the shop theme

arguments

  • @param {object} asset - asset to be created. format {key: 'eg snippet/my_file.liquid', value: 'eg content of file'}
  • @param {string} themeId - id of the theme where the asset will be created

example

let shop = new Shop(...)
const asset = {
  key: 'snippet/my_file.liquid',
  value: '{{ this is my file }}'
}
const themeId = '124567'
shop.createAsset(asset, themeId)
.then((createdAsset)=> /.../)
getAsset

Get asset from the shop theme

arguments

  • @param {string} key - key of the asset eg "snippet/my_file.liquid"
  • @param {string} themeId - id of the theme where the asset is located

example

let shop = new Shop(...)
const key = 'snippet/my_file.liquid'
const themeId = '1234567'
shop.getAsset(key, themeId)
.then((asset)=> /../)
createScriptTags

Adds script tags to designated shop. This function needs to be bound to the shop that is creating script tags. If using REST API, createShopifyAPI needs to have been run on the shop before calling this function

arguments

  • @param {array} scriptTags - script tags to be created
  • @param {bool} gql - if true, use the GraphQL API

example

let shop = new Shop(...)
const gql = true;
createScriptTags.bind(shop)([
    {
      script_tag: {
        name: 'my tag',
        display_scope: 'ONLINE_STORE',
        src: 'https://path/to/script.js'
      }
    },
    {
      script_tag: {
        name: 'my other tag',
        display_scope: 'ALL',
        src: 'https://path/to/other/script.js'
      }
    }
  ],
  gql)

  // shop.app_settings.scripttags will contain the array of all active script tags
createWebhooks

Adds webhooks to the designated shop. This function needs to be bound to the shop that it is creating webhooks on. Also, createShopifyAPI needs to have been run on the shop before calling this function

arguments

  • @param {object} webhooks - webhooks to be created
  • @param {bool} gql - if yes, use GraphQL API

example

let shop = new Shop(...) 

// GraphQL API example
const gql = true;
createWebhooks.bind(shop)({
  webhook: {
    topic: 'ORDER_CREATE',
    address: 'https://path/webhook/should/call',
    format: 'JSON'
  }
}, gql)


// REST API example
createWebhooks.bind(shop)({
  webhook: {
    topic: ‘orders/create’,
    address: ‘https://path/webhook/should/call’,
    format: ‘json’
  }
})


// shop.app_settings.webhooks will contain the array of active webhooks
resetWebhooks

This recreates the Shopify webhooks for the shop. This is sometimes necessary if the Shopify webhook creation process fails at installation or if new webhooks are to be added to the shop (by updating the 'shopify_default_webhooks' system variable)

arguments

none

example

let shop = new Shop(...)
shop.resetWebhooks()
getOrders

This fetches orders from Shopify

arguments

  • @param {int} orderId - if provided, will retrieve the particular order with that id (not valid for GraphQL API)
  • @param {string} fulfilment_status - if provided will retrieve only orders with that fulfillment status. defaults to 'unfulfilled'
  • @param {boolean} stream - if true, the data will be provided as a stream in chuncks
  • @param {boolean} gql - if true, use GraphQL API
  • @params {array} fields - fields to be returned if using GraphQL API. child customer and line item fields should be prefixed with "customer" and "line_items" respectively
  • NOTE: field line items will return data in format
  • lineItems { edges { node { ...requested fields } } }

example

let shop = new Shop(...)
shop.getOrders(null, 'partial')

or
const stream = true;
const orderGetter = shop.getOrders(null, null, stream)
const delimiter = '!@$%^&*()';
handy.system.sendData(orderGetter, delimiter, res)
.then(()=> / continue processing /)
.catch((err)=> / error processing /)

or

const gql = true
const fields = ['id', 'name', 'shippingAddress', 'customer__displayName', 'line_items__id']
const orderId = "gid://shopify/order/123456"
shop.getOrders(orderId, null, null, gql, fields)
.then((orders)=> /.../)
updateOrder

Update existing order NOTE: Only editing with the REST API is enabled

arguments

  • @param {string} id - order id
  • @param {object} order - order object with required parameters
  • @param {boolean} gql - if true, use GraphQL

example

let shop = new Shop(...)
const id = '1234567'
const order = {
  id: '1234567',
  note_attributes: {
    name: 'new attribute',
    value: 'i changed'
  }
}

shop.updateOrder({id, order})
.then((updatedOrder)=> / continue processing /)
.catch((err)=> / error processing /)
createProduct

Create a product in the shop

arguments

  • @param {object} productDefinition - object with the product properties
  • @param {array} fields - fields to be returned if using GraphQL API
  • @param {bool} gql - if true, use GraphQL API

example

let shop = new Shop(...)

const productDefinition = {
  title: 'my product',
  bodyHtml: '<p>Please buy this amazing product</p>',
  vendor: 'Seller Co'
}

const fields = ['id', 'handle', 'tags']
const gql = true

shop.createProduct(productDefinition, fields, gql)
.then(product => /.../)
getProducts

Retrieve the list of products in the shop

arguments

  • @params {array} fields - fields to be returned for each product object
  • NOTE: prefixing fields with "variant__" will get the variant fields. Requesting variant fields will slow down the query considerably
  • @params {array} ids - if provided, only the products with these specific ids will be returned
  • @params {string} handle - if provided, only the product with this handle will be returned
  • @params {string} product_type - if provided, only products with this product type will be returned
  • @params {bool} stream - if true, the data will be provided as a stream in chuncks
  • @params {bool} gql - if true, use GraphQL API
  • GraphQL only @params {object} filter - key value parameters to filter results

example

let shop = new Shop(...)

shop.getProducts({fields: ['id', 'handle'], ids: [1234, 5678], handle: 'cool-product', product_type: 'blockbuster', stream: false})
.then((products)=>{ / process products})
.catch((err)=> { / process errors})

or

const productGetter = shop.getProducts({stream: true})
const delimiter = '!@$%^&*()';
handy.system.sendData(productGetter, delimiter, res)
.then(()=> / continue processing)
.catch((err)=> / error processing)

or

const filter = {product_type: 'blockbuster'}
const fields = ['id', 'title', 'description', 'productType', 'variant__id']
const gql = true            // use GraphQL API
shop.getProducts({fields, filter, gql})
.then((products)=> {/ process products})
.catch((err)=> process errors)
getProductVariant

Retrieve a specific product variant

arguments

  • @param {string} id - id of variant to be retrieved
  • @param {array} fields - list of fields to be returned if using GraphQL
  • NOTE: field inventoryItem will return data in format
  • {inventoryItem
  • {
  • id 
  • inventory (first: 100){
  •   edges { node
  •           id
  •           available
  •           location {id name isActive shipsInventory}
  •         }
  • }
  • }
  • }
  • @param {bool} gql - if true, use GraphQL API

example

let shop = new Shop(...)

// REST API
const id = '1234'
shop.getProductVariant(id)
.then(variant => / process variant)

// GraphQL API
const id = "gid://shopify/productVariant/123456"
const fields = ['id', 'price', 'title']
const gql = true
shop.getProductVariant(id, fields, gql)
.then(variant => / process variant)
getAllProductVariants

Retrieve the list of product variants in the shop

arguments

  • @param {array} fields - fields to be returned for each variant object. child product fields should be prefixed with "product" eg "productid"
  • @param {object} filter - key value parameters to filter results
  • @param {bool} gql - if true, use GraphQL API

example

let shop = new Shop(...)

const fields = ['id', 'price', 'title']
const filter = {product_type: 'blockbuster'}
const gql = true;
shop.getAllProductVariants({fields, filter, gql})
.then((variants)=> / process variants)
.catch((err)=> / process errors)
createProductVariant

Create a product variant

arguments

  • @params {object} variantDefinition - variant properties
  • @params {array} fields - variant fields to be returned
  • @params {bool} gql - if true, use GraphQL API

example

const fields = ['id', 'price', 'title']
const variantDefinition = {
  price: 10000,
  sku: 'my_variant',
  taxable: true,
  options: 'variant_title'
}

const gql = true;

shop.createProductVariant(variantDefinition, fields, gql)
.then(variant => / process variant)
updateProductVariant

Update an existing product variant

arguments

  • @params {object} variantDefinition - new variant properties. must include id of variant to be updated
  • @params {array} fields - variant fields to be returned
  • @params {bool} gql - if true, use GraphQL API

examples

const fields = ['id', 'price', 'title']
const variantDefinition = {
  price: 20000,
  sku: 'my_variant_updated',
  taxable: true,
  id: "gid://shopify/Variant/123456"
}

const gql = true;

shop.updateProductVariant(variantDefinition, fields, gql)
.then(variant => / process variant)
adjustVariantInventory

Add or remove inventory for specified variant

arguments

  • @params {string} variant_id - GraphQL id of variant
  • @params {int} available_adjustment - quantity to add (or remove, if negative) from inventory
  • NOTE: This method modifies the first active location

examples

const variant_id = 'gid://shopify/ProductVariant/123456'
const available_adjustment = 10

const shop = new Shop(...)
shop.adjustVariantInventory(variant_id, available_adjustment)
.then((inventoryLevelId)=> /.../)
getCustomers

Get the customer details for a shop

arguments

  • @params {array} fields - customer fields to be returned
  • NOTE including the field "metafields" will return all metafields attached to the customer. Requesting metafields will slow the query down considerably
  • @params {array} ids - ids of customers to be returned (when provided for GraphQL, only the customer with the first Id is returned)
  • @params {bool} gql - if true, use GraphQL API
  • @params {bool} stream - if true, the data will be provided as a stream in chuncks (only available with the GraphQL API)

examples

const fields = ['id', 'displayName', 'email']
const gql = true;

const shop = new Shop(...)
shop.getCustomers({fields, gql})
.then((customers)=> / process customers)
updateCustomer

Update a customer record in Shopify

arguments

  • @params {object} customer - customer details to be updated
  • @params {bool} gql - if true, use GraphQL API

examples

const shop = new Shop(...)
const customer = {
  id: 'gid://shopify/Customer/123456789',
  note: 'This is a VIP customer',
  metafields: {
    namespace: 'my app',
    key: 'vip',
    value: 'true',
    valueType: 'STRING',
    id: 'gid://shopify/Metafield/987654321'
  }
}

const gql = true

shop.updateCustomer(customer, gql)
.then((customerRecord)=> / customerRecord === {id} )

How handy-shopifyjs works

License

Credits

7.0.34

10 months ago

7.0.33

11 months ago

7.0.32

11 months ago

7.0.31

11 months ago

7.0.30

1 year ago

7.0.29

1 year ago

7.0.28

1 year ago

7.0.27

1 year ago

7.0.26

1 year ago

7.0.25

1 year ago

7.0.23

1 year ago

7.0.22

1 year ago

7.0.21

1 year ago

7.0.20

1 year ago

7.0.18

1 year ago

7.0.19

1 year ago

7.0.16

1 year ago

7.0.17

1 year ago

7.0.15

1 year ago

7.0.14

1 year ago

7.0.13

1 year ago

7.0.12

1 year ago

7.0.10

1 year ago

7.0.11

1 year ago

7.0.9

1 year ago

7.0.8

1 year ago

7.0.7

1 year ago

7.0.6

1 year ago

7.0.5

1 year ago

7.0.4

1 year ago

7.0.3

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

6.6.17

1 year ago

6.6.16

1 year ago

6.6.15

1 year ago

6.6.14

2 years ago

6.6.13

2 years ago

6.6.12

2 years ago

6.6.11

2 years ago

6.6.10

2 years ago

6.6.9

2 years ago

6.6.8

2 years ago

6.6.7

2 years ago

6.6.6

2 years ago

6.6.5

2 years ago

6.6.4

2 years ago

6.6.3

2 years ago

6.6.2

2 years ago

6.6.1

2 years ago

6.5.1

2 years ago

6.5.0

2 years ago

6.4.1

2 years ago

6.3.16

2 years ago

6.3.14

2 years ago

6.3.15

2 years ago

6.3.12

2 years ago

6.3.13

2 years ago

6.4.0

2 years ago

6.3.11

2 years ago

6.3.10

2 years ago

6.3.4

2 years ago

6.3.3

2 years ago

6.3.6

2 years ago

6.3.5

2 years ago

6.3.8

2 years ago

6.3.7

2 years ago

6.3.9

2 years ago

6.3.2

2 years ago

6.3.1

2 years ago

6.3.0

2 years ago

6.2.0

2 years ago

6.1.1

2 years ago

6.1.0

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.0.8

3 years ago

4.0.7

3 years ago

4.0.6

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.31

3 years ago

3.0.30

3 years ago

3.0.29

3 years ago

3.0.28

3 years ago

3.0.27

3 years ago

3.0.26

3 years ago

3.0.25

3 years ago

3.0.24

3 years ago

3.0.23

3 years ago

3.0.22

3 years ago

3.0.21

3 years ago

3.0.20

3 years ago

3.0.19

3 years ago

3.0.18

3 years ago

3.0.17

3 years ago

3.0.16

3 years ago

3.0.15

3 years ago

3.0.14

3 years ago

3.0.13

3 years ago

3.0.12

3 years ago

3.0.11

3 years ago

3.0.10

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.11

4 years ago

1.1.1

4 years ago

1.0.820

4 years ago

1.0.817

4 years ago

1.0.816

4 years ago

1.0.815

4 years ago

1.0.814

4 years ago

1.0.813

4 years ago

1.0.812

4 years ago

1.0.811

4 years ago

1.0.810

4 years ago

1.0.809

4 years ago

1.0.808

4 years ago

1.0.807

4 years ago

1.0.806

4 years ago

1.0.805

4 years ago

1.0.804

4 years ago

1.0.803

4 years ago

1.0.801

4 years ago

1.0.800

4 years ago

1.0.80

4 years ago

1.0.705

4 years ago

1.0.704

4 years ago

1.0.703

4 years ago

1.0.702

4 years ago

1.0.701

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.425

4 years ago

1.0.424

4 years ago

1.0.423

4 years ago

1.0.422

4 years ago

1.0.421

4 years ago

1.0.420

4 years ago

1.0.419

4 years ago

1.0.418

4 years ago

1.0.417

4 years ago

1.0.416

4 years ago

1.0.415

4 years ago

1.0.414

4 years ago

1.0.413

4 years ago

1.0.412

4 years ago

1.0.411

4 years ago

1.0.410

4 years ago

1.0.408

4 years ago

1.0.407

4 years ago

1.0.406

4 years ago

1.0.405

4 years ago

1.0.404

4 years ago

1.0.403

4 years ago

1.0.402

4 years ago

1.0.401

4 years ago

1.0.4

4 years ago

1.0.331

4 years ago

1.0.330

4 years ago

1.0.329

4 years ago

1.0.328

4 years ago

1.0.327

4 years ago

1.0.326

4 years ago

1.0.325

4 years ago

1.0.324

4 years ago

1.0.323

4 years ago

1.0.322

4 years ago

1.0.321

4 years ago

1.0.320

4 years ago

1.0.319

4 years ago

1.0.318

4 years ago

1.0.316

4 years ago

1.0.315

4 years ago

1.0.314

4 years ago

1.0.313

4 years ago

1.0.312

4 years ago

1.0.311

4 years ago

1.0.310

5 years ago

1.0.309

5 years ago

1.0.308

5 years ago

1.0.307

5 years ago

1.0.306

5 years ago

1.0.305

5 years ago

1.0.304

5 years ago

1.0.303

5 years ago

1.0.302

5 years ago

1.0.301

5 years ago

1.0.3

5 years ago

1.0.205

5 years ago

1.0.204

5 years ago

1.0.203

5 years ago

1.0.202

5 years ago

1.0.201

5 years ago

1.0.2

5 years ago

1.0.199

5 years ago

1.0.198

5 years ago

1.0.197

5 years ago

1.0.196

5 years ago

1.0.195

5 years ago

1.0.194

5 years ago

1.0.193

5 years ago

1.0.192

5 years ago

1.0.191

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.52

5 years ago

0.0.51

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago