Skip to content

Add overloads for { parseAs: "string" } or { parseAs: "buffer" }#1162

Merged
allevo merged 5 commits intofastify:masterfrom
jineshshah36:content-type-parser
Sep 16, 2018
Merged

Add overloads for { parseAs: "string" } or { parseAs: "buffer" }#1162
allevo merged 5 commits intofastify:masterfrom
jineshshah36:content-type-parser

Conversation

@jineshshah36
Copy link
Copy Markdown
Contributor

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

all tests passed

Comment thread test/types/index.ts
Comment thread test/types/index.ts
done!(null, {})
})

server.addContentTypeParser('foo/bar', { parseAs: 'string' }, (req, done) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicts with your docs. It says:

As you can see, now the function signature is (req, body, done) instead of (req, done)

Additionally, if this is allowed, it seems like the body is getting parsed as a string and then ignored. What's the point?

Comment thread test/types/index.ts
done!(null, {})
})

server.addContentTypeParser('foo/bar', { bodyLimit: 20 }, (req, done) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicts with your docs. It says:

As you can see, now the function signature is (req, body, done) instead of (req, done)

@jineshshah36
Copy link
Copy Markdown
Contributor Author

Wait I’m confused, is parseAs not required if 3 arguments are passed? If not, what will the body get parsed as? Also, if you do provide three arguments, doesn’t the callback require three arguments?

@allevo
Copy link
Copy Markdown
Member

allevo commented Sep 15, 2018

@jineshshah36
Copy link
Copy Markdown
Contributor Author

I already read that but it is incomplete. For example, if I provide parseAs: 'string', but only 2 arguments in the callback, is it just swallowing the body? When does it even make sense to do this:

server.addContentTypeParser('foo/bar', { parseAs: 'string' }, (req, done) => {
  ...
})

@allevo
Copy link
Copy Markdown
Member

allevo commented Sep 16, 2018

Maybe the documentation is not so clear.
Anyway reading the code

} else {
if (parser.asString === true || parser.asBuffer === true) {
rawBody(
request,
reply,
reply.context._parserOptions,
parser,
done
)
} else {
var result = parser.fn(request.raw, done)
if (result && typeof result.then === 'function') {
result.then(body => done(null, body), done)
}
}

The signature is:

  • (req, done) / (req) => Promise if the option is not given or if the resultAs is not specified
  • (req, body, done) / (req, body) => Promise if and only if the option.resultAs is string or buffer

@jineshshah36
Copy link
Copy Markdown
Contributor Author

Ok so based on that the following test would be invalid:

server.addContentTypeParser('foo/bar', { parseAs: 'string' }, (req, done) => {
    ...
})

since it should take 3 arguments in the callback.

I've updated the rest of the types and tests accordingly.

Copy link
Copy Markdown
Member

@allevo allevo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost done!

Comment thread test/types/index.ts
Comment thread fastify.d.ts Outdated
@jineshshah36
Copy link
Copy Markdown
Contributor Author

Fixed the "The array parameter is supported everywhere in addContentTypeParser". And, I added back "server.addContentTypeParser('foo/bar', {}, (req, done) => {" already.

Comment thread test/types/index.ts
Copy link
Copy Markdown
Member

@allevo allevo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks!

LGTM

If the content type parser documentation is not clear, would you mind changing it?

@allevo allevo merged commit c21bb9f into fastify:master Sep 16, 2018
@jineshshah36
Copy link
Copy Markdown
Contributor Author

I would say that it's clear to use from the docs. It was complicated here because the type system needs to account for every possible valid way to use the api. Most of the calls in the tests are redundant ways to do the same thing. It's important that typescript types account for them, but I don't really think it matters if the docs provide them.

@allevo
Copy link
Copy Markdown
Member

allevo commented Sep 16, 2018

The maintainers and the collaborators are not mainly typescript users. If you are interested, we'll be happy to review a PR about a rework of the typescript test suite!

@delvedor delvedor added the typescript TypeScript related label Sep 17, 2018
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants