0.1.58 • Published 28 days ago

@avapardaz/base-api v0.1.58

Weekly downloads
-
License
-
Repository
-
Last release
28 days ago

Nuxt fetch with automatic error handling

Features

  • integration with vee-validate version 4
  • handle api errors simply
  • handle refresh_token on 401 (with support parallel requests)

install

First install it:

npm i @avapardaz/base-api

config your store

your store should implement this functionality:

interface AuthStore{
 clearStore:()=>void;
 isLoggedIn:boolean;
 isRefreshing:boolean;
 isRefreshSuccess:boolean;
 doRefreshToken:()=>void;
 addTokenToConfig:(config:Record<string,any>)=>Record<string,any>;
}

we recommend Pinia

here it's an example:

import { defineStore } from 'pinia'
import { AuthState, AuthTokens } from '../../composables/auth/Auth.interface';
import { useRefreshTokenService } from './useRefreshToken.service';
import { useStorage } from '@vueuse/core';
const defaultState = (): AuthState => ({
  accessToken: null,
  refreshToken: null,
  expiresIn: null,
  isRefreshing: false,
  isRefreshSuccess: false,
})
export const useAuthStore = defineStore('auth', {
  state: defaultState,
  getters: {
    isLoggedIn(state) {
      // return false
      return state.accessToken !== null
    },
    getToken(state) {
      return state.accessToken
    },
    getRefreshToken(state) {
      return state.refreshToken
    },
    getExpiresIn(state) {
      return state.expiresIn
    },
  },
  actions: {
    initialStateFromLocalStore() {
      const token = localStorage.getItem(this.getLocalStorageKey)
      if (token) {
        this.setToken(JSON.parse(token), false)
      }
    },
    
    setToken({ accessToken, refreshToken, expiresIn }: AuthTokens, setLocalStorage = true) {
      this.accessToken = accessToken
      this.refreshToken = refreshToken
      this.expiresIn = expiresIn||0
      setLocalStorage && localStorage.setItem(this.getLocalStorageKey, JSON.stringify({ accessToken, refreshToken, expiresIn }))
    },
    clearStore() {
      Object.assign(this, defaultState())
      localStorage.removeItem(this.getLocalStorageKey);
      this.decrementActiveAccountIndexIfShould();
    },
   
    doRefreshToken() {
      this.isRefreshing = true;
      const service = useRefreshTokenService();
      return service(this.refreshToken!).then((response) => {
        if (response != undefined) {
          this.setToken(response.tokens, true);
          this.isRefreshSuccess = true;
          return response;
        }
      }).finally(() => {
        this.isRefreshing = false;
      })
    },
    addTokenToConfig(config: Record<string, any>) {
      if (!config.headers) {
        config.headers = {}
      }
      config.headers['Authorization'] = `Bearer ${this.getToken}`
      return config
    }
  },
})

load token to user store

we strongly recommend avoid load token server side, you don't need it in the server, the reason for this is you can cache any page you want without concerning about caching specific user token for all users! create a client plugin:

//auth.client.ts
import { useAuthStore } from "../composables/auth/Auth.store"
export default defineNuxtPlugin(({ pinia }) => {
    const authStore = useAuthStore(pinia)
    authStore.initialStateFromLocalStore()
})

wrap main composable

we already add class-transformer if you don't need it just remove this part of code

import {ClassConstructor, plainToInstance, instanceToPlain} from "class-transformer";
import {FetchOptions} from "ofetch"
import qs from "qs";
import {useAuthStore} from "../auth/Auth.store";

export const useFetchApi = <R, T = {}>(classTransformer: ClassConstructor<T> = null as unknown as ClassConstructor<T>) => {
  const authStore = useAuthStore();
  const {showToast: toastMessage} = useToast();//custom toast
  const showToast = (message: string) => {
    toastMessage({message, type: ToastEnum.error})
  }
  const getValidationErrors = (e: Record<string, any>) => {//in order to integrate with vee-validate should return server validation errors on key vlaue format, you should pass setErrors in customConfig to automatically setErros when statusCode is 422
    const errors = {} as Record<string, string>
    if (e?.response?._data && Array.isArray(e.response._data)) {
      //@ts-ignore
      e.response._data.forEach(item => {
        errors[item.field] = item.message;
      });
    }
    return errors;
  }
  const fetchData = baseUseFetchApi<R>(authStore, {
    showToast,
    getValidationErrors,
    authRoute: "/auth",
    baseURL: 'your base url'
  });
  return (url: string, config: FetchOptions = {}, customConfig: FetchCustomConfig = {}) => {
    if (config.params) {//format query params with qs package optioanl
      url = url + '?' + qs.stringify(config.params)
      delete config.params;
    }
    return fetchData(url, config, customConfig).then((response) => {
      if (classTransformer != null) {
        const instance = plainToInstance(classTransformer, response, {excludeExtraneousValues: true})
        return instanceToPlain(instance, {excludeExtraneousValues: true}) as unknown as R
      }
      return response
    });
  }

}

customConfig gives you this options handled by baseUseFetchApi

import { FetchError } from "ofetch";
export interface FetchCustomConfig {
    setToken?: boolean;
    ignoreErrors?: boolean;
    onError?: (e: FetchError,parsedError?:{statusMessage:string,statusCode:number}) => void;
    onValidationFailed?: (errors: Record<string, string>, e: FetchError) => void;
    toastError?: boolean;
    setErrors?: (errors: { [key: string]: string }) => void;
    // transformer?: (data: any) => any;
    goToLogin?: boolean;
    beforeResponse?: Function;
    debug?: boolean;
    toastValidationFields?: string[];
    messageByStatus?:Record<number,string>
}

Example

export const useCartListService = () => {
  const fetchData = useFetchApi<CartInterface>();
  return () =>
  fetchData(CART_URLS.cartList.url, {
    method: "get",
    params:CART_URLS.cartList.defaultParams
  },{setToken:true});
}

CheckAuthRoutes (optional)

first make sure using pinia for authStore and your composable name is exactly useAuthStore and auto import by nuxt, then inside app.config.ts in your root project:

export default defineAppConfig({
  'base-api': {//use for default error message when statusMessage is not specified
    defaultErrorMessage: 'خطای دریافت اطلاعات از سرور'
  },
  
  authRoute: "/some-landing-page", //first priority for redirect for auth.global.ts
  loginRoute: "/login", //login route considered if authRoute not defined
  authRoutes: [
    // "/shipping",
    { regex: [/^((?!complete-information|page\/*|order-in-person|contact-us|about-us|home|validation|landing|login|oauth\/callback).)*$/.source] },//array of regex this example all routes is auth routes exept mentioned one
  ],
  targetAuthPathKey: "--login-path-useCookie--", //const targetPath=useCookie(appConfig.targetAuthPathKey) if you need redirect user after logged in to his desier route
})

because we check auth routes client side make sure for auth routes your pages are rendering client side in order to avoid server side rendered html show and then redirect:

//app.vue
<template>
  <div>
    <template v-if="isAuthRoute($route.path)">
      <client-only>
        <nuxt-layout>
          <nuxt-page></nuxt-page>
        </nuxt-layout>
      </client-only>
    </template>
    <template v-else>
      <nuxt-layout>
        <nuxt-page></nuxt-page>
      </nuxt-layout>
    </template>
  </div>
</template>
<script setup lang="ts">
const {isAuthRoute}=useIsAuthRoute()
</script>
0.1.58

28 days ago

0.1.57

29 days ago

0.1.56

1 month ago

0.1.54

1 month ago

0.1.55

1 month ago

0.1.53

1 month ago

0.1.52

2 months ago

0.1.51

2 months ago

0.1.50

2 months ago

0.1.49

3 months ago

0.1.48

3 months ago

0.1.47

3 months ago

0.1.46

3 months ago

0.1.45

4 months ago

0.1.43

4 months ago

0.1.44

4 months ago

0.1.42

4 months ago

0.1.41

4 months ago

0.1.40

4 months ago

0.1.38

4 months ago

0.1.39

4 months ago

0.1.37

4 months ago

0.1.36

4 months ago

0.1.35

4 months ago

0.1.34

4 months ago

0.1.32

4 months ago

0.1.33

4 months ago

0.1.30

4 months ago

0.1.31

4 months ago

0.1.29

4 months ago

0.1.27

4 months ago

0.1.28

4 months ago

0.1.25

5 months ago

0.1.26

4 months ago

0.1.21

5 months ago

0.1.22

5 months ago

0.1.23

5 months ago

0.1.24

5 months ago

0.1.20

5 months ago

0.1.17

5 months ago

0.1.18

5 months ago

0.1.19

5 months ago

0.1.16

5 months ago

0.1.14

5 months ago

0.1.15

5 months ago

0.1.12

5 months ago

0.1.13

5 months ago

0.1.10

5 months ago

0.1.11

5 months ago

0.1.2

6 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.9

5 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.1

6 months ago

0.0.9

6 months ago

0.0.2

7 months ago

0.0.1

7 months ago