All Projects → saleor → saleor-sdk

saleor / saleor-sdk

Licence: MIT license
JavaScript/TypeScript SDK for building e-commerce experiences and checkouts with Saleor API.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to saleor-sdk

Spree
Open Source headless multi-language/multi-currency/multi-store eCommerce platform
Stars: ✭ 11,571 (+9156.8%)
Mutual labels:  ecommerce, e-commerce
Aimeos Typo3
TYPO3 e-commerce extension for professional, ultra fast online shops, complex B2B applications and #gigacommerce
Stars: ✭ 157 (+25.6%)
Mutual labels:  ecommerce, e-commerce
Reaction
Mailchimp Open Commerce is an API-first, headless commerce platform built using Node.js, React, GraphQL. Deployed via Docker and Kubernetes.
Stars: ✭ 11,588 (+9170.4%)
Mutual labels:  ecommerce, e-commerce
Aimeos
Integrated online shop based on Laravel 8 and the Aimeos e-commerce framework
Stars: ✭ 2,354 (+1783.2%)
Mutual labels:  ecommerce, e-commerce
E Commerce Db
Database schema for e-commerce (webstores) sites.
Stars: ✭ 245 (+96%)
Mutual labels:  ecommerce, e-commerce
Saleor
A modular, high performance, headless e-commerce platform built with Python, GraphQL, Django, and React.
Stars: ✭ 14,720 (+11676%)
Mutual labels:  ecommerce, e-commerce
Ymple Ecommerce
Node js E-commerce Framework powered with Sails.js & Node.js as an Ecommerce Platform Shop Solution
Stars: ✭ 152 (+21.6%)
Mutual labels:  ecommerce, e-commerce
Ecommerce Laravel Bootstrap
Responsive, Multi-Vendor, MultiLanguage Online Store Platform (shopping cart solution)
Stars: ✭ 99 (-20.8%)
Mutual labels:  ecommerce, e-commerce
Smartstorenet
Open Source ASP.NET MVC Enterprise eCommerce Shopping Cart Solution
Stars: ✭ 2,363 (+1790.4%)
Mutual labels:  ecommerce, e-commerce
Shopyo
🎁 Your Open web framework, designed with big in mind. Flask with Django advantages. Build your management systems, ERP products & mobile backend (coming soon). Small business needs apps included by default. First timers friendly. Email: [email protected] | password: pass
Stars: ✭ 172 (+37.6%)
Mutual labels:  ecommerce, e-commerce
Aimeos Core
Aimeos PHP e-commerce framework for ultra fast online shops, complex B2B applications and #gigacommerce
Stars: ✭ 2,152 (+1621.6%)
Mutual labels:  ecommerce, e-commerce
Coreshop
CoreShop - Pimcore eCommerce
Stars: ✭ 193 (+54.4%)
Mutual labels:  ecommerce, e-commerce
Vc Storefront
VirtoCommerce Storefront for ASP.NET Core 3.1 repository
Stars: ✭ 122 (-2.4%)
Mutual labels:  ecommerce, e-commerce
Commerce
Fully integrated ecommerce for Craft CMS
Stars: ✭ 144 (+15.2%)
Mutual labels:  ecommerce, e-commerce
Laracom
Laravel FREE E-Commerce Software
Stars: ✭ 1,570 (+1156%)
Mutual labels:  ecommerce, e-commerce
Orocommerce
Main OroCommerce package with core functionality.
Stars: ✭ 148 (+18.4%)
Mutual labels:  ecommerce, e-commerce
Aimeos Slim
Slim PHP package for professional, ultra fast online shops
Stars: ✭ 98 (-21.6%)
Mutual labels:  ecommerce, e-commerce
Ios Print Sdk
iOS Print SDK. Easily add print on demand functionality to your app within minutes! Print Postcards, Magnets, Photo Prints, Posters, Stickers, T-Shirts, PhotoBooks, etc.
Stars: ✭ 99 (-20.8%)
Mutual labels:  ecommerce, e-commerce
Importexportfree
Improve default Magento 2 Import / Export features - cron jobs, CSV , XML , JSON , Excel , mapping of any format, Google Sheet, data and price modification, improved speed and a lot more!
Stars: ✭ 160 (+28%)
Mutual labels:  ecommerce, e-commerce
Next Ecommerce
⚡️ Quantum Ecommerce. Made with Next.js | GraphQL | Apollo Server | Apollo Client | SSR
Stars: ✭ 186 (+48.8%)
Mutual labels:  ecommerce, e-commerce

Saleor SDK

This package contains methods providing Saleor business logic for a storefront and apps. It handles Saleor GraphQL queries and mutations, manages Apollo cache, and provides an internal state to manage popular storefront use cases, like user authentication.

⚠️ Note: Saleor 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 following dependencies to your project

npm install @saleor/sdk @apollo/client graphql

Use SaleorProvider with passed Saleor's client created by createSaleorClient in a prop. Then use React hooks in any component passed as child to SaleorProvider.

import {
  SaleorProvider,
  createSaleorClient,
  useAuth,
  useAuthState,
} from "@saleor/sdk";

const client = createSaleorClient({
  apiUrl: "<SALEOR_GRAPHQL_URL>",
  channel: "<CHANNEL>",
});

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

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

  const handleSignIn = async () => {
    const { data } = await login({
      email: "[email protected]",
      password: "admin",
    });

    if (data.tokenCreate.errors.length > 0) {
      /**
       * Unable to sign in.
       **/
    } else if (data) {
      /**
       * User signed in successfully.
       **/
    }
  };

  if (authenticated && user) {
    return <span>Signed in as {user.firstName}</span>;
  }

  return <button onClick={handleSignIn}>Sign in</button>;
};

Using with NodeJS and other frameworks

npm install @saleor/sdk @apollo/client graphql

Then use createSaleorClient to get Saleor api methods and internal config variables like channel and Apollo client.

import { createSaleorClient } from "@saleor/sdk";

const client = createSaleorClient({
  apiUrl: "<SALEOR_GRAPHQL_URL>",
  channel: "<CHANNEL>",
});

const { auth, config, _internal } = client;

Finally, API methods can be used:

const { data } = await auth.login({
  email: "[email protected]",
  password: "admin",
});

if (data.tokenCreate.errors.length > 0) {
  /**
   * Unable to sign in.
   **/
} else if (data) {
  /**
   * User signed in successfully.
   **/
  const userData = api.auth.tokenCreate.user;
}

Custom HTTP communication with SDK authorization

SDK provides fetch method with all the necessary authorization headers assigned to HTTP requests and handled authorization token creation, verification and refresh inside, which you may use instead of built-in browser fetch. Using SDK auth login or logout methods will appropriately alter fetch behavior automatically, like including Authorization Bearer header in HTTP request.

import { createFetch } from "@saleor/sdk";

const authorizedFetch = createFetch();

const result = await authorizedFetch("https://example.com");

If you use GraphQL you may pass SDK fetch to the Apollo client:

const authorizationLink = new HttpLink({
  fetch: authorizedFetch,
});
const apolloClient = new ApolloClient({
  link: authorizationLink,
});

SDK fetch method uses cross-fetch under the hood.

Features

We provide an API with methods and fields, performing one, scoped type of work. You may access them straight from createSaleorClient() or use React hooks:

API object React hook Description
getState() useAuthState() Returns current SDK state: user, authenticated and authenticating.
auth useAuth() Handles user authentication methods.
user useUser() Handles user account methods.
config useSaleorConfig() Handles SDK configuration.

SDK supports OpenId Connect methods provided by Saleor API. They are under auth object and useAuth hook. For more usage details, please check https://docs.saleor.io/docs/3.0/developer/available-plugins/openid-connect.

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, especially 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 @saleor/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, explicitly telling webpack to always have one and only copy of react.

Configuration

Set environment variables:

export API_URI=https://your.saleor.instance.com/graphql/
export [email protected]
export TEST_AUTH_PASSWORD=admin

Development

  1. Download repository
  2. Install dependencies: npm i
  3. Now you can start files watcher with: npm run start

Production build

npm run build

Tests

Tests are located at /test directory. To start the test suite:

npm run test

All communication with API is prerecorded using Polly.JS. Unless requests changed or code executes new ones, no requests to API will be made.

Changes in /recordings directory should be reviewed before committing to make sure that changes in communication are intentional.

Code quality

The project has configured Prettier and ESLint. To check your code:

npm run lint

Fetch current GraphQL schema

npm run download-schema

Command will overwrite introspection.json with server schema, based on API_URL variable.

Updating TS types

GraphQL Code Generator is an automatic tool that converts schema to TS types. After changing schema file run:

npm run build-types

Updating recordings

Changes in the core user methods and tests may result in failing tests. Typically you may encounter the following error:

request to http://localhost:8000/graphql/ failed, reason: connect ECONNREFUSED 127.0.0.1:8000

To fix this run npm run test with the following variables:

  • API_URI
  • TEST_AUTH_EMAIL
  • TEST_AUTH_PASSWORD

After the tests run the recordings should be updated. Next time you run tests without variables, tests will use updated recordings.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].