@shopify/gate-context-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@shopify/gate-context-client

npm version CI PRs Welcome

The gate-context-client package provides an abstract interface to read and write gate context information. The client writes gate context information for use by Shopify functions. For example, the client could write a signed message to the context. That message could be validated and processed by a Shopify function in order to apply discounts or block checkout.

Installation

Install the shopify/gate-context-client package.

yarn add @shopify/gate-context-client

Documentation

See the Tokengating example app tutorial for further documentation. Specifically, the end of the Show gates on the storefront using a theme app extension part about writing an HMAC.

Code example

interface Input {
  walletAddress: string;
  walletVerificationMessage?: string;
  walletVerificationSignature?: string;
}

const client = getGateContextClient(
  backingStore: 'ajaxApi',
);

async function run(data: Input) {
  try {
    await client.write(data)
  } catch(error) {
    console.error('Error writing to GateContext', error);
  }
}

Backends

The client is intended to abstract backend details and automatically choose the correct backend where possible. For example, for the Online store channel, the Cart Ajax API will be used. The Cart Ajax API is the only supported backend right now.

Cart Ajax API

For the Cart Ajax API, the data will be written to a special cart attribute called _shopify_gate_context as a JSON string. The client implementation for this backend will automatically serialize and deserialize the value as needed.

After writing via the client and inspecting the cart, via /cart.js, you'd see something like the following:

{
  "token": "111f",
  "attributes": {
    "_shopify_gate_context": "{\"attribute1\": 123}"
  },
  ...
}

shopifyGateContextGenerator

This is an async transformation function that will get called before the write to the backend occurs, and may be used to override the written value.

interface Input {
  walletAddress: string;
  walletVerificationMessage?: string;
  walletVerificationSignature?: string;
}

const client = getGateContextClient({
  backingStore: 'ajaxApi',
  // adds an additional key before writing
  shopifyGateContextGenerator: (data: Input) => {
    const {walletAddress} = data;
    // async call using walletAddress
    return Promise.resolve({...data, extraKey: ''});
  },
});

async function run(data: Input) {
  try {
    await client.write(data);
  } catch (error) {
    console.error('Error writing to GateContext', error);
  }
}

Contributing

Pull requests are welcome. See the contribution guidelines for more information.

License

MIT © Shopify, see LICENSE.md for details.

Shopify Logo

Readme

Keywords

none

Package Sidebar

Install

npm i @shopify/gate-context-client

Weekly Downloads

48

Version

1.0.1

License

MIT

Unpacked Size

10 kB

Total Files

4

Last publish

Collaborators

  • jaimie.rockburn
  • blittle
  • shopify-admin
  • maryharte
  • crisfmb
  • pmoloney89
  • netlohan
  • st999999
  • justin-irl
  • megswim
  • wcandillon
  • nathanpjf
  • shopify-dep
  • goodforonefare
  • lemonmade
  • vsumner
  • wizardlyhel
  • antoine.grant
  • tsov
  • andyw8-shopify
  • henrytao
  • hannachen
  • vividviolet
  • bpscott