I am trying to disable the @google-cloud libraries for the client, however, if I try to change the alias, I get an error
Could not resolve "payload-config"
My configuration looks something like this:
const serverOnlyMock = path.resolve(__dirname, 'mock.js')
export default buildConfig({
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
admin: {
user: Users.slug,
bundler: viteBundler({}),
vite: (config) => ({
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
'@google-cloud/text-to-speech': serverOnlyMock,
'@google-cloud/translate': serverOnlyMock,
},
},
}),
},
})
mock.js
export default {}
Payload version 2.0.11
I didn't find any solution... Maybe someone knows how I can use node.js libraries in a custom endpoint ?
My endpoint
import { Endpoint } from 'payload/config'
import { WORDS_AUDIO_UPLOAD_ENDPOINT } from '../../../config/endpoints'
import { textToSpeech } from '../../../utils/textToSpeech'
import payload from 'payload'
export const uploadAudio: Omit<Endpoint, 'root'> = {
method: 'post',
path: WORDS_AUDIO_UPLOAD_ENDPOINT,
handler: async (req, res) => {
try {
const text = req.body.text
const filename = req.body.filename
if (!text) res.status(400).send('text is required')
const { buffer, mimetype } = await textToSpeech(text)
const audio = await payload.create({
collection: 'wordAudio',
file: {
data: buffer,
mimetype,
name: `${filename ?? 'word'}.mp3`,
size: buffer.length,
},
data: {},
})
res.status(201).send({ id: audio.id })
} catch (error) {
payload.logger.error(error)
res.status(400).send(error)
}
},
}
My textToSpeech function:
import { TextToSpeechClient } from '@google-cloud/text-to-speech'
export const textToSpeech = async (text: string, gender: 'NEUTRAL' | 'MALE' | 'FEMALE' = 'NEUTRAL') => {
if (window) return null
try {
const client = new TextToSpeechClient()
const [response] = await client.synthesizeSpeech({
audioConfig: { audioEncoding: 'MP3' },
input: { text },
voice: { languageCode: 'en-US', ssmlGender: gender },
})
const audio = response.audioContent
const buffer = Buffer.from(audio)
return { buffer, mimetype: 'audio/mpeg' }
} catch (e) {
console.error(e)
}
}
Solved it by passing in the overrides in the first argument of
viteBundler()
. i.e.
export default buildConfig({
admin: {
bundler: viteBundler({
resolve: {
alias: {
'module-to-mock': mockModule,
}
}
}),
},
})
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.