Repository: gatsbyjs/gatsby
File: packages/gatsby-source-drupal/src/gatsby-node.js (JavaScript)

Code before:
          // in the JSON API response.
          // See https://www.drupal.org/docs/8/modules/jsonapi/includes
          if (d.data.included) {
            data = data.concat(d.data.included)
          }
          if (d.data.links && d.data.links.next) {
            data = await getNext(d.data.links.next, data)
          }

          return data
        }

        let data = []
        if (isTranslatable === false) {
          data = await getNext(url)
        } else {
          for (let i = 0; i < languageConfig.enabledLanguages.length; i++) {
            let currentLanguage = languageConfig.enabledLanguages[i]
            let dataForLanguage

            if (currentLanguage === languageConfig.defaultLanguage) {
              dataForLanguage = await getNext(url)
            } else {
              const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/, ``)
              const urlPath = url.href.replace(`${baseUrlWithoutTrailingSlash}/${apiBase}/`, ``)
              dataForLanguage = await getNext(`${baseUrlWithoutTrailingSlash}/${currentLanguage}/${apiBase}/${urlPath}`)
            }
            
            data = data.concat(dataForLanguage)
          }
        }

        const result = {
          type,
          data,
        }

        // eslint-disable-next-line consistent-return
        return result
      })
    )
  } catch (e) {
    gracefullyRethrow(drupalFetchActivity, e)
    return
  }

  drupalFetchActivity.end()

  const nodes = new Map()

  // first pass - create basic nodes

Review comment (suggestion):
I'm open to handling this in a different way or using a library for the URL parsing. I don't like having to manually edit the URL and it's error prone.

Code after:
            }
          }

          let d
          try {
            d = await axios.get(url, {
              auth: basicAuth,
              headers,
              params,
            })
          } catch (error) {
            if (error.response && error.response.status == 405) {
              // The endpoint doesn't support the GET method, so just skip it.
              return []
            } else {
              console.error(`Failed to fetch ${url}`, error.message)
              console.log(error.data)
              throw error
            }
          }
          data = data.concat(d.data.data)
          // Add support for includes. Includes allow entity data to be expanded
          // based on relationships. The expanded data is exposed as `included`
          // in the JSON API response.
          // See https://www.drupal.org/docs/8/modules/jsonapi/includes
          if (d.data.included) {
            data = data.concat(d.data.included)
          }
          if (d.data.links && d.data.links.next) {
            data = await getNext(d.data.links.next, data)
          }

          return data
        }

        let data = []
        if (isTranslatable === false) {
          data = await getNext(url)
        } else {
          for (let i = 0; i < languageConfig.enabledLanguages.length; i++) {
            let currentLanguage = languageConfig.enabledLanguages[i]
            const urlPath = url.href.split(`${apiBase}/`).pop()
            const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/, ``)
            // The default language's JSON API is at the root.
            if (
              currentLanguage === getOptions().languageConfig.defaultLanguage ||
              baseUrlWithoutTrailingSlash.slice(-currentLanguage.length) ==
                currentLanguage
            ) {
              currentLanguage = ``
            }