UNPKG

@spree/storefront-api-v2-sdk

Version:

Node module to easily integrate your JavaScript or TypeScript application with Spree API V2. You can create an entirely custom Storefront in JS/TS with this package including one page checkout, Single Page Apps, PWAs and so on

35 lines (31 loc) 956 B
import { AxiosResponse } from 'axios' import get from 'lodash/get' import BasicSpreeError from './BasicSpreeError' export default class ExpandedSpreeError extends BasicSpreeError { public errors: any constructor(serverResponse: AxiosResponse, errorsSummary: string, errors: any) { super(serverResponse, errorsSummary) Object.setPrototypeOf(this, ExpandedSpreeError.prototype) this.name = 'ExpandedSpreeError' this.errors = Object.keys(errors).reduce((acc, field) => { const keys = field.split('.') let key = keys.shift() let node = acc while (key) { if (!node[key]) { if (keys.length === 0) { node[key] = errors[field] } else { node[key] = {} } } node = node[key] key = keys.shift() } return acc }, {}) } public getErrors(path: string[]): string[] | null { return get(this.errors, path, null) } }