Skip to content

Commit

Permalink
fix: import axios in using require() to avoid `XMLHttpRequest is no…
Browse files Browse the repository at this point in the history
…t defined` (#225)

See: vitejs/vite#6397
  • Loading branch information
plmercereau committed Feb 22, 2022
1 parent 764b881 commit 2e1c055
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changeset/metal-months-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@nhost/hasura-auth-js': patch
'@nhost/hasura-storage-js': patch
'@nhost/nhost-js': patch
---

Axios causes some trouble when used NodeJS / CommonJS. Any code importing `axios` now does so in using the `require()` syntax
4 changes: 3 additions & 1 deletion packages/hasura-auth-js/src/hasura-auth-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosError, AxiosInstance } from 'axios'
import type { AxiosError, AxiosInstance } from 'axios'

import {
ApiChangeEmailResponse,
Expand All @@ -24,6 +24,8 @@ import {
SignUpEmailPasswordParams
} from './utils/types'

const axios = require('axios')

const SERVER_ERROR_CODE = 500
export class HasuraAuthApi {
private url: string
Expand Down
3 changes: 2 additions & 1 deletion packages/hasura-storage-js/src/hasura-storage-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import axios, { AxiosInstance } from 'axios'
import type { AxiosInstance } from 'axios'

import {
ApiDeleteParams,
Expand All @@ -11,6 +11,7 @@ import {
UploadHeaders
} from './utils/types'

const axios = require('axios')
export class HasuraStorageApi {
private url: string
private httpClient: AxiosInstance
Expand Down
3 changes: 2 additions & 1 deletion packages/nhost-js/src/clients/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'

import { FunctionCallResponse } from '../types'

const axios = require('axios')
export interface NhostFunctionsConstructorParams {
url: string
}
Expand Down
4 changes: 3 additions & 1 deletion packages/nhost-js/src/clients/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'

import { GraphqlRequestResponse, GraphqlResponse } from '../types'

const axios = require('axios')

export interface NhostGraphqlConstructorParams {
url: string
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nhost-js/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosResponse } from 'axios'
import type { AxiosResponse } from 'axios'

export type GraphqlRequestResponse =
| {
Expand Down

2 comments on commit 2e1c055

@vercel
Copy link

@vercel vercel bot commented on 2e1c055 Feb 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ataraxy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seemingly broke vite production builds that end up spitting out 'require is not defined' from the vendor bundle. I had to revert back to 0.3.8 to fix.

Please sign in to comment.