-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
No module augmentation #471
base: main
Are you sure you want to change the base?
Changes from all commits
5b00bce
d4ba889
0d4a04f
9c0ef6d
c27e7ec
f6322dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,41 @@ | ||
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest, FastifyReply } from 'fastify' | ||
import { Server } from 'http' | ||
import fastify, { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify' | ||
import { Stats } from 'fs' | ||
import { expectAssignable, expectError, expectType } from 'tsd' | ||
import * as fastifyStaticStar from '..' | ||
import fastifyStatic, { | ||
FastifyStaticPlugin, | ||
FastifyStaticOptions, | ||
fastifyStatic as fastifyStaticNamed | ||
fastifyStatic as fastifyStaticNamed, | ||
FastifyStaticPluginDecorators | ||
} from '..' | ||
// TODO: remove after we land in fastify-plugin | ||
import { createPlugin } from 'fastify-plugin' | ||
|
||
import fastifyStaticCjsImport = require('..') | ||
const fastifyStaticCjs = require('..') | ||
|
||
const app: FastifyInstance = fastify() | ||
|
||
app.register(fastifyStatic) | ||
app.register(fastifyStaticNamed) | ||
app.register(fastifyStaticCjs) | ||
app.register(fastifyStaticCjsImport.default) | ||
app.register(fastifyStaticCjsImport.fastifyStatic) | ||
app.register(fastifyStaticStar.default) | ||
app.register(fastifyStaticStar.fastifyStatic) | ||
|
||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStatic) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticNamed) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.default) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.fastifyStatic) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticStar.default) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>( | ||
fastifyStaticStar.fastifyStatic | ||
) | ||
app.register(fastifyStatic, { root: '/' }) | ||
app.register(fastifyStaticNamed, { root: '/' }) | ||
app.register(fastifyStaticCjs, { root: '/' }) | ||
app.register(fastifyStaticCjsImport.default, { root: '/' }) | ||
app.register(fastifyStaticCjsImport.fastifyStatic, { root: '/' }) | ||
app.register(fastifyStaticStar.default, { root: '/' }) | ||
app.register(fastifyStaticStar.fastifyStatic, { root: '/' }) | ||
|
||
expectType<FastifyStaticPlugin>(fastifyStatic) | ||
expectType<FastifyStaticPlugin>(fastifyStaticNamed) | ||
expectType<FastifyStaticPlugin>(fastifyStaticCjsImport.default) | ||
expectType<FastifyStaticPlugin>(fastifyStaticCjsImport.fastifyStatic) | ||
expectType<FastifyStaticPlugin>(fastifyStaticStar.default) | ||
expectType<FastifyStaticPlugin>(fastifyStaticStar.fastifyStatic) | ||
expectType<any>(fastifyStaticCjs) | ||
|
||
// make sure instance properties are preserved | ||
const serverWithHttp2 = fastify({ http2: true }) | ||
expectAssignable<typeof serverWithHttp2>(serverWithHttp2.register(fastifyStatic, { root: '/' })) | ||
|
||
const appWithImplicitHttp = fastify() | ||
const options: FastifyStaticOptions = { | ||
acceptRanges: true, | ||
|
@@ -119,8 +124,12 @@ expectAssignable<FastifyStaticOptions>({ | |
|
||
appWithImplicitHttp | ||
.register(fastifyStatic, options) | ||
.after(() => { | ||
appWithImplicitHttp.get('/', (request, reply) => { | ||
.after((err, instance) => { | ||
if (err) { | ||
// handle error | ||
} | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.sendFile('some-file-name') | ||
}) | ||
}) | ||
|
@@ -129,20 +138,24 @@ const appWithHttp2 = fastify({ http2: true }) | |
|
||
appWithHttp2 | ||
.register(fastifyStatic, options) | ||
.after(() => { | ||
appWithHttp2.get('/', (request, reply) => { | ||
.after((err, instance) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not see which test case this is, the code is obscured with the error message. Can you provide a test case that i can take a look at? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It refers to the test case on line |
||
if (err) { | ||
// handle error | ||
} | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.sendFile('some-file-name') | ||
}) | ||
|
||
appWithHttp2.get('/download', (request, reply) => { | ||
instance.get('/download', (request, reply) => { | ||
reply.download('some-file-name') | ||
}) | ||
|
||
appWithHttp2.get('/download/1', (request, reply) => { | ||
instance.get('/download/1', (request, reply) => { | ||
reply.download('some-file-name', { maxAge: '2 days' }) | ||
}) | ||
|
||
appWithHttp2.get('/download/2', (request, reply) => { | ||
instance.get('/download/2', (request, reply) => { | ||
reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true }) | ||
}) | ||
}) | ||
|
@@ -152,28 +165,32 @@ options.root = [''] | |
|
||
multiRootAppWithImplicitHttp | ||
.register(fastifyStatic, options) | ||
.after(() => { | ||
multiRootAppWithImplicitHttp.get('/', (request, reply) => { | ||
.after((err, instance) => { | ||
if (err) { | ||
// handle error | ||
} | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.sendFile('some-file-name') | ||
}) | ||
|
||
multiRootAppWithImplicitHttp.get('/', (request, reply) => { | ||
instance.get('/', (request, reply) => { | ||
reply.sendFile('some-file-name', { cacheControl: false, acceptRanges: true }) | ||
}) | ||
|
||
multiRootAppWithImplicitHttp.get('/', (request, reply) => { | ||
instance.get('/', (request, reply) => { | ||
reply.sendFile('some-file-name', 'some-root-name', { cacheControl: false, acceptRanges: true }) | ||
}) | ||
|
||
multiRootAppWithImplicitHttp.get('/download', (request, reply) => { | ||
instance.get('/download', (request, reply) => { | ||
reply.download('some-file-name') | ||
}) | ||
|
||
multiRootAppWithImplicitHttp.get('/download/1', (request, reply) => { | ||
instance.get('/download/1', (request, reply) => { | ||
reply.download('some-file-name', { maxAge: '2 days' }) | ||
}) | ||
|
||
multiRootAppWithImplicitHttp.get('/download/2', (request, reply) => { | ||
instance.get('/download/2', (request, reply) => { | ||
reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true }) | ||
}) | ||
}) | ||
|
@@ -210,3 +227,13 @@ defaultIndexApp | |
reply.send('<h1>fastify-static</h1>') | ||
}) | ||
}) | ||
|
||
const pluginWithFastifyStaticDependency = createPlugin((instance) => | ||
instance.get('/', (req, res) => { | ||
expectType<FastifyStaticPluginDecorators['reply']['sendFile']>(res.sendFile) | ||
expectType<FastifyStaticPluginDecorators['reply']['download']>(res.download) | ||
}), { dependencies: [fastifyStatic] }) | ||
|
||
expectError(fastify().register(pluginWithFastifyStaticDependency)) | ||
|
||
fastify().register(fastifyStatic, { root: '' }).register(pluginWithFastifyStaticDependency) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I properly understand the change, we now require an object to be always passed when we register a fastify plugin (so it's no longer optional). This is a breaking change, we should really careful if we want to ship it as it is, since it will break existing TS codebases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An object is not necessary, fastify static plugin has a mandatory root param. Removing the object leads to
Making root optional fixes the error.
If anything, the possibility of not providing root was a bug as far as i can tell.