@cooperjs/sdk
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

Cooper SDK

This package contains methods providing Cooper business logic for storefront. It handles Cooper GraphQL queries and mutations, manages Apollo cache and provides internal state to manage popular storefront use cases, like user authentication or checkout process.

Please take a look at sample storefront which already uses Cooper SDK. For specific use cases you may also refer to cooper-sdk/examples.

⚠️ Note: Cooper SDK is still under heavy development and its API may change.

Table of Contents

Setup

There are two ways to use SDK - making custom implementation or using React components and hooks, which already has that implementation ready.

Using React

First install SDK as dependency to your project

npm install @cooperjs/sdk

Use CooperProvider with passed custom config in a prop. Then use React hooks in any component passed as child to CooperProvider.

import { CooperProvider, useAuth } from "@cooperjs/sdk";

const config = { apiUrl: "http://localhost:8000/graphql/" };
const apolloConfig = {
  /* 
    Optional custom Apollo client config.
    Here you may append custom Apollo cache, links or the whole client. 
    You may also use import { createCooperCache, createCooperClient, createCooperLinks } from "@cooperjs/sdk" to create semi-custom implementation of Apollo.
  */
};

const rootElement = document.getElementById("root");
ReactDOM.render(
  <CooperProvider config={config} apolloConfig={apolloConfig}>
    <App />
  </CooperProvider>,
  rootElement
);

const App = () => {
  const { authenticated, user, signIn } = useAuth();

  const handleSignIn = async () => {
    const { data, dataError } = await signIn("admin@example.com", "admin");

    if (dataError) {
      /**
       * Unable to sign in.
       **/
    } else if (data) {
      /**
       * User signed in succesfully.
       **/
    }
  };

  if (authenticated && user) {
    return <span>Signed in as {user.firstName}</span>;
  } else {
    return <button onClick={handleSignIn}>Sign in</button>;
  }
};

Custom implementation

npm install @cooperjs/sdk

Then use CooperManager to get CooperAPI from connect method. This method may also take optional function as an argument, which will be executed every time the CooperAPI state changes.

const config = { apiUrl: "http://localhost:8000/graphql/" };
const apolloConfig = {
  /* 
    Optional custom Apollo client config.
    Here you may append custom Apollo cache, links or the whole client. 
    You may also use import { createCooperCache, createCooperClient, createCooperLinks } from "@cooperjs/sdk" to create semi-custom implementation of Apollo.
  */
};
const manager = new CooperManager(config, apolloConfig);

const { api, apolloClient } = await manager.connect(cooperAPI => {
  /* Optional listener to API state change. You may use it to update your app state reactively - e.g. trigger the React context update. */
});

Finally, methods from api might be used:

const { data, dataError } = await api.auth.signIn("admin@example.com", "admin");

if (dataError) {
  /**
   * Unable to sign in.
   **/
} else if (data) {
  /**
   * User signed in succesfully. Read user object from data or from api.auth.
   **/
  const userData = api.auth.user;
}

Features

We provide an API with methods and fields, performing one, scoped type of work. You may access them straight from CooperAPI or use React hooks, depending on which setup do you select.

API object React hook Description
CooperAPI.auth useAuth() Handles user authentication and stores data about the currently signed in user.
CooperAPI.cart useCart() Collects products to cart and calculates their prices.
CooperAPI.checkout useCheckout() Uses cart and handles the whole checkout process.
CooperAPI.products useProductDetails(), useProductList() Obtains products.
CooperAPI.collections useCollectionDetails(), useCollectionList() Obtains collections.
CooperAPI.categories useCategoryDetails(), useCategoryList(), useCategoryAncestorsList(), useCategoryChildrenList() Obtains categories.

Local development

Our aim it to build SDK, highly configurable, as a separate package, which you will not require modifications. Although if you want to alter the project, escecially if you want to contribute, it is possible to develop storefront and SDK simultaneously. To do this, you need to link it to the storefront's project.

$ cd lib
$ npm link
$ cd <your storefront path>
$ npm link @cooperjs/sdk

Notice that in our example storefront webpack is configured to always resolve react to ./node_modules/react. It may seem redundant for the most use cases, but helps in sdk's local development, because it overcomes npm's limitations regarding peer dependencies hoisting, explicitely telling webpack to always have one and only copy of react.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.1.70latest

Version History

VersionDownloads (Last 7 Days)Published
0.1.70
0.1.60
0.1.20
0.1.10
0.1.00
0.0.20
0.0.10

Package Sidebar

Install

npm i @cooperjs/sdk

Homepage

itsync.ng/

Weekly Downloads

0

Version

0.1.7

License

BSD-3-Clause

Unpacked Size

1.38 MB

Total Files

637

Last publish

Collaborators

  • sammyiyke001
  • sammy_iyke001