-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add check to
@aws-amplify/backend
to throw early in a browser conte…
…xt (#1810) * add check to secret() to throw early in a browser context * "client-side applications" -> "the browser" * lift "is browser" check to a utility, execute at package entrypoint * remove stale check from secret() * update changeset description * Update packages/backend/src/is_browser.ts Co-authored-by: Kamil Sobol <[email protected]> --------- Co-authored-by: Kamil Sobol <[email protected]>
- Loading branch information
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@aws-amplify/backend': patch | ||
--- | ||
|
||
add check to `@aws-amplify/backend` entrypoint to throw early in a browser context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { equal } from 'node:assert/strict'; | ||
import { afterEach, describe, it } from 'node:test'; | ||
import { isBrowser } from './is_browser.js'; | ||
|
||
void describe('is browser', () => { | ||
void afterEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (global as any).window; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (global as any).document; | ||
}); | ||
|
||
void it('should be true in a browser context', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(global as any).window = {}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(global as any).document = {}; | ||
|
||
equal(isBrowser(), true); | ||
}); | ||
|
||
void it('should be false in a node context', () => { | ||
equal(isBrowser(), false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Checks whether we are in a browser | ||
*/ | ||
export const isBrowser = () => { | ||
// @ts-expect-error we're checking for browser context and globalThis does not have an index signature | ||
return typeof globalThis.window !== 'undefined'; | ||
}; |