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

@heseya/store-core

v7.1.0-beta.4

Published

[![NPM version](https://img.shields.io/npm/v/@heseya/store-core)](https://www.npmjs.com/package/@heseya/store-core) [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fheseya%2Fsdk-core.svg?type=shield)](https://app.fossa.com/projects/g

Downloads

1,016

Readme

@heseya/store-core

NPM version FOSSA Status   Code Coverage   Downloads  

Type definitions

Features

  • All Heseya API endpoints are exposed as methods on the SDK object.
  • Full typing of all API responses and requests, as well as for the all Heseya models and enums.
  • Automatic conversion between JSON and native types.
  • Automatic handling request query parameters.
  • Support for custom Axios instance.
  • Helper functions and class for handling common tasks when creating e-commerce applications.

Instalation

$ yarn add @heseya/store-core
// or
$ npm i @heseya/store-core

Usage

Type definitions

SDK package contains a set of type definitions for the Heseya API, you can import them directly into your project.

Full list of exported types corresponds to the SDK modules and it is available here.

Each of the Heseya's Models has a corresponding type definition, with different type for the Model list, details, create and update methods. The convention is following:

  • {ModelName}Listed - type for the list of models
  • {ModelName} - type for the details of a model
  • {ModelName}CreateDto - type for the create method of a model
  • {ModelName}UpdateDto - type for the update method of a model

Example for product types:

import { Product, ProductListed, ProductCreateDto, ProductUpdateDto } from '@heseya/store-core'

const productCreateDto: ProductCreateDto = {
  ...
}

await heseya.Products.create(productCreateDto)

Heseya Api Service

Initialization

You can initialize the API Service by calling the createHeseyaApiService function. It is important to pass the axios instance to the function.

Axios instance needs to be configured to use the URL of the Heseya API. Without it the SDK will not be able to make requests to the API.

import axios from 'axios'
import { createHeseyaApiService, HeseyaApiService } from '@heseya/store-core'

const axiosInstance = axios.create({ baseURL: 'https://api.example.com' })

const heseya: HeseyaApiService = createHeseyaApiService(axiosInstance)
Initialization in Nuxt.js

In nuxt, you can inject the service into the context. This allows you to use the service in your components. To do this you need to create the following plugin:

import { Plugin } from '@nuxt/types'
import { createHeseyaApiService } from '@heseya/store-core'

const heseyaPlugin: Plugin = ({ $axios }, inject) => {
  inject('heseya', createHeseyaApiService($axios))
}

export default heseyaPlugin

Usage

Now, you can use the HeseyaApiService object to call the API for any of the endpoints.

For example, you can fetch all the products:

const products = await heseya.Products.get()

All the methods on the HeseyaApiService object return promises. If the request fails, the promise will be rejected with an default AxiosError object. This package provides a helper function to handle that errors formatApiError.

Authorization

The SDK does not provide any authorization. You need to implement your own authorization mechanism. To do this, you should use the axios instance that you injected into the createHeseyaApiService function. That instance needs to have interceptors configured to add the authorization header, as well as to handle the token refreshing.

To handle auth requests you can use methods from the Auth module.

Auth Axios Enhancer

Package provides a helper function to handle everything related to the authorization. Thanks to it, you can modify the axios instance to add the authorization header, and to handle the token refreshing.

The instance modified in this way may be used in the createHeseyaApiService function.

import axios from 'axios'
import { enhanceAxiosWithAuthTokenRefreshing } from '@heseya/store-core'

const axiosInstance = enhanceAxiosWithAuthTokenRefreshing(axios.create(), {
  heseyaUrl: 'https://api.example.com',
  getAccessToken: () => localStorage.get('accessToken'),
  getRefreshToken: () => localStorage.get('refreshToken'),
  setAccessToken: (token: string) => localStorage.set('accessToken', token),
  setRefreshToken: (token: string) => localStorage.set('refreshToken', token),
  setIdentityToken: (token: string) => localStorage.set('identityToken', token),
  onTokenRefreshError: (error) => handleError(error),
  shouldIncludeAuthorizationHeader: (config) => config.url?.startsWith('/auth'),
})

Modified axios will try to refresh the access token every time the request fails with the 401 response code. If token refreshing will succeed, the request will be retried, otherwise axios will throw original error.

When token refreshing fails, not only the original error will be thrown, but also the config.onTokenRefreshError function will be called. You should use it to logout the user.

Event bus

You can create an event bus to handle some events in your store. The main purpose of this feature is to create an abstract way to react to different actions that your client is performing in the store. For example, you can emit events to Google Analytics or to the Facebook Pixel.

Creating Event Bus

import { createHeseyaEventBusService } from '@heseya/store-core'

const eventBus = createHeseyaEventBusService()

Listening and emitting events

import { HeseyaEvent } from '@heseya/store-core'

// somewhere in your events config file
eventBus.on(HeseyaEvent.AddToCart, (product) => {
  gtm.emit('add_to_cart', { product_id: product.id })
})

// somewhere in your store code
eventBus.emit(HeseyaEvent.AddToCart, { id: '123' })

License

FOSSA Status