Skip to content

Conversation

@ShGKme
Copy link
Contributor

@ShGKme ShGKme commented Dec 23, 2024

Resolves

Todo

  • Replace window with globalThis
  • Load @nextcloud/router only when required
    • It is never used in our apps!
  • Replace XHR with fetch
    • + in tests replace mock-xmlhttprequest with msw
    • + Simplify tests

Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
@ShGKme ShGKme added the type: enhancement 🚀 New feature or request label Dec 23, 2024
@ShGKme ShGKme self-assigned this Dec 23, 2024
@susnux susnux added the 2. developing 2️⃣ Work in progress label Feb 1, 2025
Copy link
Contributor

@susnux susnux left a 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 ✨

Comment on lines +23 to +29
// Node.js runtime
if ('_nc_l10n_locale' in globalThis && globalThis._nc_l10n_locale) {
return globalThis._nc_l10n_locale
}

// Fallback to English
return 'en'
Copy link
Contributor

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.

Suggested change
// 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'

Comment on lines +6 to +11
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
}
Copy link
Contributor

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

Comment on lines +61 to +67
// Node.js runtime
if ('_nc_l10n_language' in globalThis && globalThis._nc_l10n_language) {
return globalThis._nc_l10n_language
}

// Fallback to English
return 'en'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Suggested change
// 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'

Comment on lines +175 to +176
} catch {
// Error is probably a SyntaxError due to invalid response, this is handled by the next line
Copy link
Contributor

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"

Suggested change
} 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 })

Comment on lines +170 to +174
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')
Copy link
Contributor

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)

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2. developing 2️⃣ Work in progress type: enhancement 🚀 New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make library independent from server rendering and web-page context

3 participants