HardWare
HardWare
NNuxt
Created by HardWare on 1/29/2025 in #❓・help
Append data on useFetch on page change instead reload
I had consider it but I think thats to much powerhouse just for gallery infinite scroll
7 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
i will add types so it wont bother me 😆
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
i guess pnpm dont really care xd
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
whole file looks fine for me, any suggestions whould be grate and why
import type { H3Event } from 'h3'
import type { IncomingMessage } from 'node:http'
import multer from 'multer'
import fs from 'fs'

const middleware = multer({ dest: 'tmp/', limits: { fileSize: 5 * 1024 * 1024 } }).single('image')

/**
* Middleware function to handle file uploads using Multer.
*
* @param {H3Event} event - The event object from H3.
* @returns {Promise<{ file?: Express.Multer.File, body?: Record<string, any> }>} - A promise that resolves with the uploaded file and request body.
*/

const useMulter = async (event: H3Event): Promise<{
file?: Express.Multer.File | undefined,
body?: Record<string, unknown>,
}> => {
return new Promise((resolve, reject) => {
// eslint-disable-next-line
const req = event.node.req as IncomingMessage & any
// eslint-disable-next-line
const res = event.node.res as any

// Run multer middleware
// eslint-disable-next-line
middleware(req, res, (er: any) => {
if (er) {
reject(er)
return
}
resolve({
body: req.body,
file: req.file,
})
})
})
}

/**
* Delete a tmp from file system.
*
* @param filePath - Path to the file to be deleted.
*/
const deleteMulterFile = (filePath: string): void => {
fs.unlink(filePath, (err) => {
if (err) {
console.error('Error deleting the file:', err)
}
})
}

export { useMulter, deleteMulterFile }
import type { H3Event } from 'h3'
import type { IncomingMessage } from 'node:http'
import multer from 'multer'
import fs from 'fs'

const middleware = multer({ dest: 'tmp/', limits: { fileSize: 5 * 1024 * 1024 } }).single('image')

/**
* Middleware function to handle file uploads using Multer.
*
* @param {H3Event} event - The event object from H3.
* @returns {Promise<{ file?: Express.Multer.File, body?: Record<string, any> }>} - A promise that resolves with the uploaded file and request body.
*/

const useMulter = async (event: H3Event): Promise<{
file?: Express.Multer.File | undefined,
body?: Record<string, unknown>,
}> => {
return new Promise((resolve, reject) => {
// eslint-disable-next-line
const req = event.node.req as IncomingMessage & any
// eslint-disable-next-line
const res = event.node.res as any

// Run multer middleware
// eslint-disable-next-line
middleware(req, res, (er: any) => {
if (er) {
reject(er)
return
}
resolve({
body: req.body,
file: req.file,
})
})
})
}

/**
* Delete a tmp from file system.
*
* @param filePath - Path to the file to be deleted.
*/
const deleteMulterFile = (filePath: string): void => {
fs.unlink(filePath, (err) => {
if (err) {
console.error('Error deleting the file:', err)
}
})
}

export { useMulter, deleteMulterFile }
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
:usefetchwrong:
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
Well, I'am not using express so i have to keep it as is.
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
Thank you, gonna try it ❤️
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
I've resolve my problem by making multer as server utils, only problem are types in multer. I will be glad if you help me get rid of as any. Any way thank you for sources.
const { file, body } = await useMulter(event).catch(() => {
throw createError({ statusCode: 500, message: 'File upload failed' })
})
const { file, body } = await useMulter(event).catch(() => {
throw createError({ statusCode: 500, message: 'File upload failed' })
})
const useMulter = (event: H3Event): Promise<{ file?: Express.Multer.File; body?: Record<string, unknown> }> => {
return new Promise((resolve) => {
// Run multer middleware
// eslint-disable-next-line
middleware(event.node.req as any , event.node.res as any, (er) => {
if (er) {
throw er
}
resolve({
// eslint-disable-next-line
body: (event.node.req as any).body,
// eslint-disable-next-line
file: (event.node.req as any).file,
})
})
})
}
const useMulter = (event: H3Event): Promise<{ file?: Express.Multer.File; body?: Record<string, unknown> }> => {
return new Promise((resolve) => {
// Run multer middleware
// eslint-disable-next-line
middleware(event.node.req as any , event.node.res as any, (er) => {
if (er) {
throw er
}
resolve({
// eslint-disable-next-line
body: (event.node.req as any).body,
// eslint-disable-next-line
file: (event.node.req as any).file,
})
})
})
}
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
Can you gave me some example how to use multer? I'm kinda confused how it work. I have make something but idk if thats fine?
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
Gonna check it out, thank you
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
@kapa.ai provide me example code of that event
31 replies
NNuxt
Created by HardWare on 1/20/2025 in #❓・help
Handling multipart/form-data images on backend api
@kapa.ai I do know how to send image to Cloudinary. How to handle image from api event
31 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
On postman they works fine, same as curl request from nuxt container to api
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
I’ve tested other entities of my api, and that’s kinda strange. Some have that issue, some aren’t. I don’t know what to think about it
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
Is there possibility that response is not decompressed correctly?
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
No description
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
I'm also not sure, but it looks like parsing error thats why it looks like ss above
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
Yes, simple $fetch works in Nitro on container name request http://php
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
On server, I haven’t tried. Give a sec
22 replies
NNuxt
Created by HardWare on 11/27/2024 in #❓・help
$fetch and useFetch proxy request receive strange binary instead proper json+ld object
Simple $fetch works fine. What do you mean „in Nitro”, am not sure?
22 replies