-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: universal environment #832
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Grigorii K. Shartsev <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Over all very nice approach ✨
// Node.js runtime | ||
if ('_nc_l10n_locale' in globalThis && globalThis._nc_l10n_locale) { | ||
return globalThis._nc_l10n_locale | ||
} | ||
|
||
// Fallback to English | ||
return 'en' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
globalThis
always works in all contexts.
// Node.js runtime | |
if ('_nc_l10n_locale' in globalThis && globalThis._nc_l10n_locale) { | |
return globalThis._nc_l10n_locale | |
} | |
// Fallback to English | |
return 'en' | |
return globalThis._nc_l10n_locale ?? 'en' |
declare global { | ||
// eslint-disable-next-line camelcase, no-var | ||
var _nc_l10n_locale: string | undefined | ||
// eslint-disable-next-line camelcase, no-var | ||
var _nc_l10n_language: string | undefined | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will make it available also in exported typings.
Should probably be moved to a globals.d.ts
file
// Node.js runtime | ||
if ('_nc_l10n_language' in globalThis && globalThis._nc_l10n_language) { | ||
return globalThis._nc_l10n_language | ||
} | ||
|
||
// Fallback to English | ||
return 'en' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
// Node.js runtime | |
if ('_nc_l10n_language' in globalThis && globalThis._nc_l10n_language) { | |
return globalThis._nc_l10n_language | |
} | |
// Fallback to English | |
return 'en' | |
return globalThis._nc_l10n_language ?? 'en' |
} catch { | ||
// Error is probably a SyntaxError due to invalid response, this is handled by the next line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will remove useful information for debugging, instead "duplicate here"
} catch { | |
// Error is probably a SyntaxError due to invalid response, this is handled by the next line | |
} catch (error) { | |
throw new Error('Invalid content of translation bundle', { cause: error }) |
if (typeof bundle.translations === 'object') { | ||
register(appName, bundle.translations) | ||
return Promise.resolve(bundle).then(callback) | ||
} | ||
request.send() | ||
}) | ||
|
||
// load JSON translation bundle per AJAX | ||
return promise | ||
.then((result) => { | ||
register(appName, result.translations) | ||
return result | ||
}) | ||
.then(callback) | ||
throw new Error('Invalid content of translation bundle') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer error handling instead of success handling
if (typeof bundle.translations !== 'object') {
throw new Error('Invalid content of translation bundle')
}
register(appName, bundle.translations)
return callback(bundle)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only wanted to change XHR
with fetch
here, without changing anything else
Resolves
Todo
window
withglobalThis
@nextcloud/router
only when requiredXHR
withfetch
mock-xmlhttprequest
withmsw