Skip to content

Commit

Permalink
fix: crash on window access in node environments
Browse files Browse the repository at this point in the history
Fix retrieve not working in Node environments caused by accessing `window` without first checking if it exists.
  • Loading branch information
kleinfreund committed Dec 5, 2023
1 parent 089edd6 commit bf014f3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Tests passing](https://github.com/kleinfreund/retrieve/workflows/Tests/badge.svg)](https://github.com/kleinfreund/retrieve/actions)

A convenience wrapper around fetch for the browser (well, anything that has `fetch` really).
A convenience wrapper around fetch for the browser (and anything that has `fetch`).

This package’s files are distributed in the ES module format and have not been transpiled.

Expand Down Expand Up @@ -124,11 +124,11 @@ The request URL.
- `URL`: Will be used as-is.
- `string`:
- Absolute URL string: Will be used as-is.
- Relative URL path string: Will be turned into an absolute URL (using `config.baseUrl` if set; otherwise `window.location.origin`).
- Relative URL path string: Will be turned into an absolute URL (using `config.baseUrl`).

##### `baseUrl` (optional)

**Default**: `window.location.origin`
**Default**: `window.location.origin` in browser environments; otherwise, `undefined`

Base for request URL. Ignored if `config.url` is a `URL` object or an absolute URL `string`.

Expand Down
4 changes: 2 additions & 2 deletions dist/retrieve.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ interface RetrieveConfig {
* - `URL`: Will be used as-is.
* - `string`:
* - Absolute URL string: Will be used as-is.
* - Relative URL path string: Will be turned into an absolute URL (using `config.baseUrl` if set; otherwise `window.location.origin`).
* - Relative URL path string: Will be turned into an absolute URL (using `config.baseUrl`).
*/
url: string | URL;
/**
* Base for request URL. Ignored if `url` is a URL object or an absolute URL string.
*
* **Default**: `window.location.origin`
* **Default**: `window.location.origin` in browser environments; otherwise, `undefined`
*/
baseUrl?: string | URL;
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/retrieve.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export interface RetrieveConfig {
* - `URL`: Will be used as-is.
* - `string`:
* - Absolute URL string: Will be used as-is.
* - Relative URL path string: Will be turned into an absolute URL (using `config.baseUrl` if set; otherwise `window.location.origin`).
* - Relative URL path string: Will be turned into an absolute URL (using `config.baseUrl`).
*/
url: string | URL

/**
* Base for request URL. Ignored if `url` is a URL object or an absolute URL string.
*
* **Default**: `window.location.origin`
* **Default**: `window.location.origin` in browser environments; otherwise, `undefined`
*/
baseUrl?: string | URL

Expand Down Expand Up @@ -346,7 +346,8 @@ export async function retrieve(config: RetrieveConfig): Promise<RetrieveResponse
*/
function createUrl(config: RetrieveConfig): URL {
// Process request URL
const url = new URL(config.url, config.baseUrl ?? window.location.origin)
const baseUrl = config.baseUrl ?? (typeof window !== 'undefined' ? window.location.origin : undefined)
const url = new URL(config.url, baseUrl)

// Turns `params` into query parameters for GET requests
if (config.params) {
Expand Down

0 comments on commit bf014f3

Please sign in to comment.