Is there an easy way to encode/decode a string to/from Base64 in Nuxt.js with support for server-side rendering and preferably TypeScript?
There is no way built-in and I didn't find any plugin, so I created a tiny plugin myself, that can simply be dropped into the /plugins directory.
import { Plugin } from '@nuxt/types'
function encodeBase64 (value: string): string {
if (process.client) {
return window.btoa(unescape(encodeURIComponent(value)))
} else {
return Buffer.from(value, 'ascii').toString('base64')
}
}
function decodeBase64 (value: string): string {
if (process.client) {
return decodeURIComponent(escape(window.atob(value)))
} else {
return Buffer.from(value, 'base64').toString('ascii')
}
}
declare module 'vue/types/vue' {
interface Vue {
$encodeBase64: (value: string) => string,
$decodeBase64: (value: string) => string
}
}
declare module '@nuxt/types' {
// nuxtContext.app.$lines inside asyncData, fetch, plugins, middleware, nuxtServerInit
interface NuxtAppOptions {
$encodeBase64: (value: string) => string,
$decodeBase64: (value: string) => string
}
// nuxtContext.$lines
interface Context {
$encodeBase64: (value: string) => string,
$decodeBase64: (value: string) => string
}
}
declare module 'vuex/types/index' {
// this.$lines inside Vuex stores
// eslint-disable-next-line
interface Store<S> {
$encodeBase64: (value: string) => string,
$decodeBase64: (value: string) => string
}
}
const base64Plugin: Plugin = (_context, inject) => {
inject('encodeBase64', encodeBase64)
inject('decodeBase64', decodeBase64)
}
export default base64Plugin
Don't forget to load it in your nuxt.config.ts:
plugins: [
{ src: '~/plugins/base64.ts' },
],
Now use $encodeBase64() and $decodeBase64() in your Vue.js components both client-side and server-side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With