-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
Add support for React Native in @TypedFormData.Body() #1018
Comments
I have a question. Is it really okay just by writinng the string path? It actually converted to the Can you show me an example creating the I want to know about below type exactly. Is it really okay with primitive object instance? Or needs another class type? {
uri: 'path-to-file-on-the-phone',
name: 'file-name',
type: 'image/png',
} |
yes sure React-Native import { SafeAreaView, Button } from 'react-native';
export default function HomeScreen() {
const handlePress = async () => {
const body = new FormData();
body.append('file', {
name: 'image.png',
type: 'image/png',
uri: 'https://picsum.photos/500/500', // this can be any uri so local phone uri or internet
});
fetch('http://192.168.0.106:3050/upload', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body,
});
};
return (
<SafeAreaView
style={{
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
gap: 100,
}}
>
<Button title="changePP" onPress={handlePress} />
</SafeAreaView>
);
} Fastify Server import fastify from 'fastify';
import multipart from '@fastify/multipart';
const server = fastify();
server.register(multipart);
server.post('/upload', async (req, res) => {
const file = await req.file();
console.log((await file?.toBuffer())?.byteLength);
});
server.listen({
port: 3050,
host: '0.0.0.0',
}); The value of |
In the Therefore, I don't know how to solve this problem especially for the React Native. For a while, how about solving this problem by hard casting to the I will consider the adapt the |
what do you mean by hard casting ? using the If the interface IFileUri {
uri: string;
name: string;
type: string;
} these are the only required properties. |
I'll solve this problem by making below type. By the way, I'm considering below type names. @omaziarz, will you determine the type name?
export type FormDataInput<T extends object> =
T extends Array<any>
? never
: T extends Function
? never
: {
[P in keyof T]: T[P] extends Array<infer U>
? FormDataValue<U>[]
: FormDataValue<T[P]>;
};
type FormDataValue<T> = T extends File ? T | FileProps : T;
interface FileProps {
uri: string;
name: string;
type: string;
} |
Well i'm not really good with names myself haha but |
Fix samchon/nestia#1018: `HttpMigrateRouteFetcher` for React Native.
Fix #1018: add `FormDataInput<T>` type for React Native environment.
Upgrade to latest. Would works properly. |
@samchon just to confirm with you that it works thank you very much !! |
Feature Request
In react native the only way to send Files in a multipart request is by doing the following :
this doesn't work :
As expected when using the first approach I will get the following error when using the custom object:
I think that to support all environments, validation inside
@TypedFormData.Body()
should support the custom object as a valid Blob because, at least, in fastify the custom object works the same way as a Blob without any additional handling neededThe text was updated successfully, but these errors were encountered: