From 859bd5fe95d6bae74ada7d8cf30ee148d2eba967 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Thu, 8 Aug 2024 00:41:21 +0200 Subject: [PATCH] aa --- .changeset/spotty-bulldogs-confess.md | 7 +++++ .eslintrc.js | 10 +++++++ package.json | 2 +- packages/graphiql-react/src/editor/common.ts | 12 ++++---- .../src/editor/components/image-preview.tsx | 5 +--- packages/graphiql/resources/renderExample.js | 30 +++++++++---------- packages/graphiql/src/components/GraphiQL.tsx | 4 +-- packages/graphiql/test/images/logo.svg | 22 ++++---------- 8 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 .changeset/spotty-bulldogs-confess.md diff --git a/.changeset/spotty-bulldogs-confess.md b/.changeset/spotty-bulldogs-confess.md new file mode 100644 index 00000000000..eed64fbd7f1 --- /dev/null +++ b/.changeset/spotty-bulldogs-confess.md @@ -0,0 +1,7 @@ +--- +'@graphiql/react': patch +'graphiql': patch +--- + +- prefer `location` over `window.location` +- prefer `navigator` over `window.navigator` diff --git a/.eslintrc.js b/.eslintrc.js index 0a9cdf1f0ec..c901e02b64b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -129,6 +129,16 @@ module.exports = { property: 'localStorage', message: 'Use `localStorage` instead', }, + { + object: 'window', + property: 'location', + message: 'Use `location` instead', + }, + { + object: 'window', + property: 'navigator', + message: 'Use `navigator` instead', + }, ], 'no-return-assign': 'error', 'no-return-await': 'error', diff --git a/package.json b/package.json index 6798fa16fc3..68fa2ec4bb4 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "scripts": { "build": "yarn build-clean && yarn tsc && yarn build:nontsc", "build-bundles": "yarn prebuild-bundles && yarn wsrun:noexamples --stages build-bundles", - "build-bundles-clean": "rimraf '{packages,examples,plugins}/**/{bundle,cdn,webpack}'", + "build-bundles-clean": "rimraf '{packages,examples}/**/{bundle,cdn,webpack}'", "build-clean": "yarn tsc --clean", "build-docs": "rimraf packages/graphiql/typedoc && typedoc packages", "build:nontsc": "yarn wsrun:noexamples --exclude-missing --serial build", diff --git a/packages/graphiql-react/src/editor/common.ts b/packages/graphiql-react/src/editor/common.ts index 720f427b101..8db64d0f5b4 100644 --- a/packages/graphiql-react/src/editor/common.ts +++ b/packages/graphiql-react/src/editor/common.ts @@ -3,11 +3,9 @@ import { KeyMap } from './types'; export const DEFAULT_EDITOR_THEME = 'graphiql'; export const DEFAULT_KEY_MAP: KeyMap = 'sublime'; -let isMacOs = false; - -if (typeof window === 'object') { - isMacOs = window.navigator.platform.toLowerCase().indexOf('mac') === 0; -} +const isMacOs = + typeof navigator !== 'undefined' && + navigator.platform.toLowerCase().indexOf('mac') === 0; export const commonKeys = { // Persistent search box in Query Editor @@ -30,10 +28,10 @@ export async function importCodeMirror( addons: Promise[], options?: { useCommonAddons?: boolean }, ) { - const CodeMirror = await import('codemirror').then(c => + const CodeMirror = await import('codemirror').then(mod => // Depending on bundler and settings the dynamic import either returns a // function (e.g. parcel) or an object containing a `default` property - typeof c === 'function' ? c : c.default, + typeof mod === 'function' ? mod : mod.default, ); await Promise.all( options?.useCommonAddons === false diff --git a/packages/graphiql-react/src/editor/components/image-preview.tsx b/packages/graphiql-react/src/editor/components/image-preview.tsx index b330f7bb01a..12eeff732fa 100644 --- a/packages/graphiql-react/src/editor/components/image-preview.tsx +++ b/packages/graphiql-react/src/editor/components/image-preview.tsx @@ -76,11 +76,8 @@ function tokenToURL(token: Token) { const value = token.string.slice(1).slice(0, -1).trim(); try { - const { location } = window; return new URL(value, location.protocol + '//' + location.host); - } catch { - return; - } + } catch {} } function isImageURL(url: URL) { diff --git a/packages/graphiql/resources/renderExample.js b/packages/graphiql/resources/renderExample.js index a6ef4aa8a06..b73a1631638 100644 --- a/packages/graphiql/resources/renderExample.js +++ b/packages/graphiql/resources/renderExample.js @@ -14,7 +14,7 @@ // Parse the search string to get url parameters. const parameters = {}; -for (const entry of window.location.search.slice(1).split('&')) { +for (const entry of location.search.slice(1).split('&')) { const eq = entry.indexOf('='); if (eq >= 0) { parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent( @@ -60,22 +60,22 @@ function updateURL() { } function getSchemaUrl() { - const isDev = window.location.hostname.match(/localhost$/); + const isDev = /localhost$/.test(location.hostname); - if (isDev) { - // This supports an e2e test which ensures that invalid schemas do not load. - if (parameters.bad === 'true') { - return '/bad/graphql'; - } - if (parameters['http-error'] === 'true') { - return '/http-error/graphql'; - } - if (parameters['graphql-error'] === 'true') { - return '/graphql-error/graphql'; - } - return '/graphql'; + if (!isDev) { + return '/.netlify/functions/graphql'; } - return '/.netlify/functions/graphql'; + // This supports an e2e test which ensures that invalid schemas do not load. + if (parameters.bad === 'true') { + return '/bad/graphql'; + } + if (parameters['http-error'] === 'true') { + return '/http-error/graphql'; + } + if (parameters['graphql-error'] === 'true') { + return '/graphql-error/graphql'; + } + return '/graphql'; } // Render into the body. diff --git a/packages/graphiql/src/components/GraphiQL.tsx b/packages/graphiql/src/components/GraphiQL.tsx index c80c261e180..19320b8c3ea 100644 --- a/packages/graphiql/src/components/GraphiQL.tsx +++ b/packages/graphiql/src/components/GraphiQL.tsx @@ -865,8 +865,8 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) { } const modifier = - typeof window !== 'undefined' && - window.navigator.platform.toLowerCase().indexOf('mac') === 0 + typeof navigator !== 'undefined' && + navigator.platform.toLowerCase().indexOf('mac') === 0 ? 'Cmd' : 'Ctrl'; diff --git a/packages/graphiql/test/images/logo.svg b/packages/graphiql/test/images/logo.svg index 43e513e06ca..8b265562b12 100644 --- a/packages/graphiql/test/images/logo.svg +++ b/packages/graphiql/test/images/logo.svg @@ -1,27 +1,15 @@ - + + + - - + - + -