简体   繁体   English

Typescript Hapijs 中的多部分/表单数据处理

[英]multipart/form-data handling in Typescript Hapijs

I am trying to make an API that can accept and parse a CSV file using HapiJS in typescript.我正在尝试制作一个可以在打字稿中使用 HapiJS 接受和解析 CSV 文件的 API。 I tested the following API out in nodeJS and it turned out pretty fine.我在 nodeJS 中测试了以下 API,结果很好。

    server.route({
    path: '/file',
    method: 'POST',
    config: {
        handler: (req, h) => {
            let results = []
            let count = 0
            const payload = req.payload
            const data = payload.files
            data.pipe(csv({ headers: false }))
                .on('data', (data) => results.push(data))
                .on('end', () => {
                    results.forEach(result => {
                        console.log(result['0'])
                    })
                });
            return "file read"
        },
        payload: {
            output: 'stream',
            parse: true,
            allow: 'multipart/form-data'
        }
    }
})

I tried to replicate this in Typescript and has been at it for quite long.我试图在 Typescript 中复制它,并且已经使用了很长时间。 Ive got the following questions:我有以下问题:

  1. How do I cast a payload object into some usable type?如何将有效负载对象转换为某种可用类型? I have my payload object throwing:我有我的有效载荷对象抛出:
     Element implicitly has an 'any' type because expression of type '"files"' can't be used to index type 'string | object | Buffer | Readable'.
      Property 'files' does not exist on type 'string | object | Buffer | Readable'.
    
    37                 const data = payload['files'] 

I have created an interface but has no idea how to force Payload to use this.我创建了一个接口,但不知道如何强制 Payload 使用它。 I also cant find a class the payload interface could extend.我也找不到有效负载接口可以扩展的类。

  1. This is the biggest issue.这是最大的问题。 The allow property of the payload option isnt working for me somehow.以某种方式,有效载荷选项的允许属性对我不起作用。
              payload: {
                  output: 'stream',
                  parse: true,
                  //allow: 'multipart/form-data',
              },

As soon as I uncomment the allow property and make a POST request, POSTMAN throws a 415 Unsupported Media Type error.一旦我取消对 allow 属性的注释并发出 POST 请求,POSTMAN 就会抛出 415 Unsupported Media Type 错误。 Even though POSTMAN request does have a valid multipart/form-data.即使 POSTMAN 请求确实有一个有效的 multipart/form-data。 How do i retrieve this request?我如何检索此请求?

I think the payload config is incorrect.我认为有效负载配置不正确。 Version 19.0.0 introduced a breaking change with regards to multipart configuration.版本 19.0.0引入了关于多部分配置的重大更改。

From this version and onwards the multipart property of the payload configuration is by default false meaning you have to specify it to be something else than false to make it accept multipart payloads.从这个版本开始,payload 配置的multipart属性默认为false这意味着你必须将它指定为false以外的其他东西,以使其接受 multipart 有效负载。

According to the docs it should be set to true or using an object containing a specification for the output property.根据文档,它应该设置为true或使用包含output属性规范的对象。

So try this:所以试试这个:

payload: {
    parse: true,
    allow: 'multipart/form-data',
    multipart: { output: 'stream' },
}

As for the compile error, you could use the any type for converting:至于编译错误,您可以使用any类型进行转换:

const payload: any = req.payload
const data = payload.files

For the second point, you can try to enforce the type of your payload.对于第二点,您可以尝试强制执行有效负载的类型。

Example:例子:

const { files } = request.paylad as YourInterface;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM