How to use the @vtex/api.UserInputError function in @vtex/api

To help you get started, we’ve selected a few @vtex/api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vtex-apps / store-graphql / node / resolvers / list / util.ts View on Github external
const checkListItemQuantity = (quantity: any) => {
  if (!quantity || quantity < 0) {
    throw new UserInputError('The item quantity should be greater than 0')
  }
}
github vtex-apps / store-graphql / node / resolvers / catalog / index.ts View on Github external
facets: async (
    _: any,
    { facets, query, map, hideUnavailableItems }: FacetsArgs,
    ctx: Context
  ) => {
    const {
      clients: { catalog },
      clients,
    } = ctx

    if (facets && facets.includes('undefined')) {
      throw new UserInputError('Bad facets parameter provided')
    }

    let result
    const translatedQuery = await translateToStoreDefaultLanguage(
      clients,
      query
    )
    const segmentData = ctx.vtex.segment
    const salesChannel = (segmentData && segmentData.channel.toString()) || ''

    const unavailableString = hideUnavailableItems
      ? `&fq=isAvailablePerSalesChannel_${salesChannel}:1`
      : ''
    if (facets) {
      result = await catalog.facets(facets)
    } else {
github vtex-apps / store-graphql / node / clients / utils.ts View on Github external
export function statusToError(e: any) {
  if (!e.response) {
    throw e
  }

  const { response } = e as AxiosError
  const { status } = response!

  if (status === 401) {
    throw new AuthenticationError(e)
  }
  if (status === 403) {
    throw new ForbiddenError(e)
  }
  if (status === 400) {
    throw new UserInputError(e)
  }

  throw e
}
github vtex-apps / store-graphql / node / resolvers / catalog / index.ts View on Github external
product: async (_: any, rawArgs: ProductArgs, ctx: Context) => {
    const {
      vtex: { account },
      clients: { catalog },
    } = ctx

    const args =
      rawArgs &&
      isValidProductIdentifier(rawArgs.identifier) &&
      !Functions.isGoCommerceAcc(account)
        ? rawArgs
        : { identifier: { field: 'slug', value: rawArgs.slug! } }

    if (!args.identifier) {
      throw new UserInputError('No product identifier provided')
    }

    const { field, value } = args.identifier
    let products = [] as Product[]

    switch (field) {
      case 'id':
        products = await catalog.productById(value)
        break
      case 'slug':
        products = await catalog.product(value)
        break
      case 'ean':
        products = await catalog.productByEan(value)
        break
      case 'reference':
github vtex-apps / store-graphql / node / resolvers / auth / index.ts View on Github external
recoveryPassword: async (_: any, args: any, { vtex: ioContext, request: { headers: { cookie } }, response }: any) => {
    const { VtexSessionToken } = parse(cookie)
    if (!VtexSessionToken) {
      throw new UserInputError(E_TOKEN)
    }
    if (!checkPasswordFormat(args.newPassword)) {
      throw new UserInputError(E_PASS)
    }
    const { headers, data: { authStatus } } = await makeRequest(ioContext, paths.recoveryPassword(VtexSessionToken, args.email, args.newPassword, args.code))
    return setVtexIdAuthCookie(ioContext, response, headers, authStatus)
  },
github vtex-apps / store-graphql / node / clients / masterdata.ts View on Github external
function paginationArgsToHeaders({ page, pageSize }: PaginationArgs) {
  if (page < 1) {
    throw new UserInputError('Smallest page value is 1')
  }

  const startIndex = (page - 1) * pageSize
  const endIndex = startIndex + pageSize

  return {
    'REST-Range': `resources=${startIndex}-${endIndex}`,
  }
}
github vtex-apps / store-graphql / node / resolvers / catalog / index.ts View on Github external
productRecommendations: async (
    _: any,
    { identifier, type }: ProductRecommendationArg,
    ctx: Context
  ) => {
    if (identifier == null || type == null) {
      throw new UserInputError('Wrong input provided')
    }
    const catalogType = inputToCatalogCrossSelling[type]
    let productId = identifier.value
    if (identifier.field !== 'id') {
      const product = await queries.product(_, { identifier }, ctx)
      productId = product!.productId
    }
    return ctx.clients.catalog.crossSelling(productId, catalogType)
  },
github vtex-apps / store-graphql / node / resolvers / headers.ts View on Github external
export const withMDPagination = (currentHeaders = {}) => (ioContext: any, cookie = null) => (page = 1, pageSize = DEFAULT_PAGE_SIZE) => {
  if (page < 1) {
    throw new UserInputError('Smallest page value is 1')
  }
  const startIndex = (page - 1) * pageSize
  const endIndex = startIndex + pageSize
  return {
    ...(withAuthToken(currentHeaders)(ioContext, cookie)),
    'REST-Range': `resources=${startIndex}-${endIndex}`
  }
}
github vtex-apps / store-graphql / node / resolvers / auth / index.ts View on Github external
redefinePassword: async (_: any, args: any, { vtex: ioContext, response }: any) => {
    if (!checkPasswordFormat(args.newPassword) || !checkPasswordFormat(args.currentPassword)) {
      throw new UserInputError(E_PASS)
    }

    const VtexSessionToken = await getSessionToken(ioContext)
    const escapedEmail = encodeURIComponent(args.email)
    const escapedPass = encodeURIComponent(args.currentPassword)
    const escapedNewPass = encodeURIComponent(args.newPassword)
    const passPath = paths.redefinePassword(VtexSessionToken, escapedEmail, escapedPass, escapedNewPass)

    const { headers, data: { authStatus } } = await makeRequest(ioContext, passPath, 'POST', args.vtexIdVersion)

    if(authStatus === 'WrongCredentials') {
      throw new UserInputError('Wrong credentials.')
    }
    else if(authStatus === 'BlockedUser') {
      throw new UserInputError('You were blocked by VTEX ID.')
    }
    return setVtexIdAuthCookie(ioContext, response, headers, authStatus)
  },
github vtex-apps / store-graphql / node / resolvers / list / util.ts View on Github external
const checkProduct = async (item: Item, catalog: any) => {
  const response = await catalog.productBySku([path(['skuId'], item)])
  if (!response.length) {
    throw new UserInputError('Cannot add an invalid product')
  }
}