npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aftership/shop-now

v0.0.5-beta.2

Published

Returns shop now library

Downloads

2

Readme

@aftership/shop-now

Returns shop now library

🚀 Pre requirement

Exchange for other items SDK requires Premium or above plan. Log in to AfterShip Returns Admin to upgrade your subscription.

✨ Features

  • 🏄🏼‍♂️ Easy to learn and use.
  • ⚛️ Supports React.
  • 🪄 Multiple custom APIs that support exchange for anything on store.
  • 🎪 Support multiple e-commerce platforms, including Shopify, Bigcommerce, and Salesforce Commerce Cloud.
  • 🎯 Written in TypeScript with predictable static types.

📦 Install

npm i @aftership/shop-now

🤹‍♀️ Usage

React

import {useReturnsShopNow} from '@aftership/shop-now/react';

Example

  import {useReturnsShopNow} from '@aftership/shop-now/react';

  const {
    initCart
    updateCart,
    addToCart,
    removeFromCart,
    checkout,
    buyNow,
    shopContext,
    error,
  } = useReturnsShopNow({platform: 'shopify'});

  useQuery(
    'get-cart',
    async () => {
      return fetchCart();
    },
    {
      onSuccess: (data) => {
        updateCart(
          data.map((item) => ({
            quantity: data.quantity,
            external_product_id: data.external_product_id,
            external_variant_id: data.external_variant_id,
          }))
        );
      },
    }
  );

  const handleAddToCart = (lineItem) => {
    addToCart?.({
      quantity: lineItem.quantity,
      external_product_id: lineItem.external_product_id,
      external_variant_id: lineItem.external_variant_id,
    });
  };

  const handleRemoveFromCart = (lineItem) => {
    removeFromCart?.({
      quantity: lineItem.quantity,
      external_product_id: lineItem.external_product_id,
      external_variant_id: lineItem.external_variant_id,
    });
  };

Type

interface LineItem {
  external_product_id: string;
  external_variant_id: string;
  quantity: number;
}

type LineItems = LineItem[];

type PlatformContext = Partial<{
  shopInfo: {
    currency: string;
    language: string;
    rc_origin: string;
  };
  price: {amount: number; currency: string};
  style: {
    background: string;
    font: string;
  };
}>;

export type ReturnsError = {
  baseUrl: string;
  input: string;
  requertUrl: string;
  method?: string;
  message?: string;
  returnsAPIException: boolean;
} & Record<string, any>;

enum EventAction {
  GET_CART_TOTAL = 'getCartTotal',
  ADD_TO_CART = 'addToCart',
  UPDATE_TO_CART = 'updateToCart',
  REMOVE_FROM_CART = 'removeFromCart',
  CHECKOUT = 'checkout',
  BUY_NOW = 'buyNow',
  ON_CONTEXT_CHANGE = 'onContextChange',
  ERROR = 'error',
}
import {LineItem} from '@aftership/shop-now/dist/core/types/cart';
import {EventAction} from '@aftership/shop-now/dist/core/constant';
import {ReturnsError} from '@aftership/shop-now/dist/core/utils/CustomError';
import {PlatformContext} from '@aftership/shop-now/dist/core/types/platform';

API

initCart

initCart is used to initialize the shopping cart. When users first enter the page to perform an "EFA" (exchange for anything), they need to call "initCart" to save the shopping cart data. Similar to updateCart it can overwrite the shopping cart data by passing an array as a parameter.

initCart(lineItems)

updateCart

updateCart allows you to overwrite the shopping cart data by passing an array as a parameter.

updateCart(lineItems)

addToCart

addToCart is used to add an individual item to the shopping cart.

addToCart(lineItem)

removeFromCart

removeFromCart is used to remove a single item from the shopping cart.

removeFromCart(lineItem)

getCart

getCart is used to retrieve the data saved in the shopping cart model.

getCart() -> lineItems

checkout

The checkout action carries the information of the items saved in the shopping cart and redirects you to the store's returns page for the checkout process.

buyNow

For the buyNow action, you would need to pass the information of the current item to overwrite the shopping cart's item information. Then, you will be redirected to the store's returns page for the checkout process.

goBack

The goBack action will take you back to the returns page.

initTranslation

The initTranslation function initializes the multilingual configuration set in the returns page.

getCreditTranslationText

After initializing the multilingual settings using initTranslation, you can use the getCreditTranslationText method. By passing in the parameters amount and currency, it will return the corresponding text based on the configured language settings.

getGoBackTranslationText

The "getGoBackTranslationText" function returns the corresponding translation text for the "goback" action in the returns page.

getError

The getError function retrieves the error message.

getContext

The getContext function retrieves the context information.

setOnEventChangeCallback

The setOnEventChangeCallback function is used to set callbacks for events such as adding items to the shopping cart, context changes, or error events.

const onEventChangeCallback = (eventName: PlatformEventName, context?: PlatformContext) => {
      if (eventName === EventAction.ON_CONTEXT_CHANGE) {
      }
      if (eventName === EventAction.ERROR) {
      }
      if (eventName === EventAction.GET_CART_TOTAL) {
      }
      if (eventName === EventAction.ADD_TO_CART) {
      }
      if (eventName === EventAction.UPDATE_TO_CART) {
      }
      if (eventName === EventAction.BUY_NOW) {
      }
      if (eventName === EventAction.CHECKOUT) {
      }
    },

setOnEventChangeCallback(onEventChangeCallback)

React Component

import {ReturnsShopNowBanner} from '@aftership/shop-now/react';

// Here is an example of the default returns banner component:

const {updateCart, shopContext, initTranslation, getCreditTranslationText} =
  useReturnsShopNow({platform: 'shopify'});

useEffect(() => {
  initTranslation?.();
}, [initTranslation]);

const creditText = useMemo(() => {
  return (
    getCreditTranslationText?.(
      shopContext?.price?.amount,
      shopContext?.price?.currency
    ) ?? '--'
  );
}, [shopContext]);

<ReturnsShopNowBanner
  styleConfig={shopContext?.style}
  creditText={creditText}
/>;

note

1、If you use useReturnsShopNow will also provide a shopContext with its corresponding state and error. You don't need to worry about how to fetch the data or handle errors; it's ready to use out of the box. 2、Please refer to the PlatformContext mentioned above for the type of shopContext. Please refer to the ReturnsError mentioned above for the type of error.

shopContext

shopInfo

currency: The returns page carries the currency of the store. language: The returns page carries the language of the store. rc_origin: The returns page origin url

price amount & currency The calculated amount and currency for returns.

style The style of the returns admin banner.

error

returnsAPIException indicates whether it is an error related to the core returns API business.

3、If you want to use multilingual support for returns, you can utilize the initialization method for multilingual within the useEffect hook.

useEffect(() => {
  initTranslation?.();
}, [initTranslation]);

const creditText = useMemo(() => {
  return (
    getCreditTranslationText?.(
      shopContext?.price?.amount,
      shopContext?.price?.currency
    ) ?? '--'
  );
}, [shopContext]);