Skip to content

Commit

Permalink
fix: websocket and fetch typings
Browse files Browse the repository at this point in the history
Signed-off-by: blu3beri <berend@animo.id>
  • Loading branch information
berendsliedrecht committed May 21, 2021
1 parent bf12c89 commit 5d908de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { isNodeJS, isReactNative } from './environment'

let fetch
type Fetch = {
(input: RequestInfo, init?: RequestInit): Promise<Response>
}

let fetch: Fetch
let Headers
let Request
let Response

// NodeJS doesn't have fetch by default
if (!fetch && isNodeJS()) {
if (isNodeJS()) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodeFetch = require('node-fetch')

fetch = nodeFetch.default
Headers = nodeFetch.Headers
Request = nodeFetch.Request
Response = nodeFetch.Response
} else if (!fetch && isReactNative()) {
} else if (isReactNative()) {
fetch = global.fetch
Headers = global.Headers
Request = global.Request
Expand Down
15 changes: 12 additions & 3 deletions src/utils/ws.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { isNodeJS, isReactNative } from './environment'

let WebSocket
type WS = {
new (url: string, protocols?: string | string[] | undefined): WebSocket
prototype: WebSocket
readonly CLOSED: number
readonly CLOSING: number
readonly CONNECTING: number
readonly OPEN: number
}

let WebSocket: WS

// NodeJS doesn't have WebSocket by default
if (!WebSocket && isNodeJS()) {
if (isNodeJS()) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodeWebSocket = require('ws')

WebSocket = nodeWebSocket
} else if (!WebSocket && isReactNative()) {
} else if (isReactNative()) {
WebSocket = global.WebSocket
} else {
WebSocket = window.WebSocket.bind(window)
Expand Down

0 comments on commit 5d908de

Please sign in to comment.