deligenius / multiparser

multipart/form-data parser for Deno servers
28 stars 7 forks source link

multiparser on Deno 1.2 #6

Closed osorito closed 4 years ago

osorito commented 4 years ago

Hello. I've updated to Deno 1.2 but it gives the following errors.

`error: TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'. Type 'URL' is not assignable to type 'string'. return new URL(url).pathname

    at https://deno.land/std@0.59.0/path/win32.ts:917:18

TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
  Type 'URL' is not assignable to type 'string'.
  return new URL(url).pathname;
at https://deno.land/std@0.59.0/path/posix.ts:438:18

Found 2 errors.`

Can you please help me on this. This way I can keep using your code.

Thanks!

gungunfebrianza commented 4 years ago

this is the problem described in #4 .

gjuoun commented 4 years ago

v2.0.1 is out. Please check it out.

https://github.com/deligenius/multiparser/releases/tag/v2.0.1

osorito commented 4 years ago

Tried a simple example.

export const createPost2 = async (ctx: any) => { try{ const form = await multiParserV2(ctx.request.serverRequest); //console.log(JSON.stringify(form,null,2)); console.log(title ${form?.fields.title}); console.log(content ${form?.fields.content}); console.log(creator ${form?.fields.creator}); console.log(imageUrl ${form?.files.imageUrl.filename}); ctx.response.status = 201; ctx.response.body = { message: 'Post created succesfully', post: {}, creator: {_id: '233', name: ''},imageUrl:form?.files.imageUrl }; }catch(err){ console.log(err); } };

But gave this output.

If I comment this line console.log(imageUrl ${form?.files.imageUrl.filename});

it works. But if i enable it , it gives this error.

error: TS2339 [ERROR]: Property 'filename' does not exist on type 'FormFile | FormFile[]'. Property 'filename' does not exist on type 'FormFile[]'. console.log(imageUrl ${form?.files.imageUrl.filename});

I know the filename exists, because i can see it on the return from the api.

Screen Shot 2020-07-23 at 11 32 30 AM
gjuoun commented 4 years ago

A field may contain many files, before use that, you need to cast type

(<FormFileV2>form?.files).imageUrl
osorito commented 4 years ago

It works!

Here is the working example

export const createPost2 = async (ctx: any) => { try{ const form: FormV2 = await multiParserV2(ctx.request.serverRequest) as FormV2 ; //console.log(JSON.stringify(form,null,2)); console.log(title ${form?.fields.title}); console.log(content ${form?.fields.content}); console.log(creator ${form?.fields.creator}); console.log(imageUrl ${(<FormFileV2>form?.files.imageUrl).filename}); await Deno.writeFile(./images/test.png, (form?.files.imageUrl).content); ctx.response.status = 201; ctx.response.body = { message: 'Post created succesfully', post: {}, creator: {_id: '233', name: ''},imageUrl:form?.files.imageUrl }; }catch(err){ console.log(err); } };

gjuoun commented 4 years ago

I'm glad it works for you