Comments (19)
Alright, I'll look into what can be done to resolve the issue -- might take me a bit since I'm swamped with my 9-5 right now, and I'm going to have to look into fastify-plugin to determine what's going on from that end.
from fastify-autoload.
I think there might be a bug in the code! Would you like to send a fix?
from fastify-autoload.
I can look into it, but if you know what it is it may just be faster for you to. I hadn't looked too hard into the code, I just figured maybe I was screwing up somewhere
from fastify-autoload.
In fiddling around some more it seems to actually be an issue with how fastify-plugin
interacts with fastify-autoload
. I'll look into it some more as it may be worth noting this for any TypeScript users, but if it is the case then it's likely not any fault of autload
from fastify-autoload.
autoPrefix
works in JavaScript here, so it's likely a bug in the typescript integration.
from fastify-autoload.
the issue seems to come down to the fastify-plugin
module. It modifies imported objects in a way that seems sort of odd let me see if I can hop a screenshot.
Edit Gonna try something. will let you know if it works or not. If it does I'll
from fastify-autoload.
Working
the first line is for a TS file that is wrapping the exported function in fp()
from fastify-plugin (in the real-use scenario, I need to use the fastify-plugin plugin because I need access to the decorators added on in other files). The second line is a route that works as it is displayed, but will not work if I wrap it in fp()
as the object has the same sort of decorator information presented. See the screenshot below.
Not Working
This will no longer work -- if compatibility with the fastify-plugin
module is expected of this, then this is an issue. If it is not expected, then this is an odd bug, but you are able to get past it if you only use the fastify-plugin
module only for things that need to be available in other plugins, and not for web-direct things (i.e. splitting out things like db instances into files that use that the fp()
wrapper and using just a raw export for anything that defines a web route).
from fastify-autoload.
@mcollina not sure if you saw my other comments.
from fastify-autoload.
Compatibility with fastify-plugin is expected. It works in Javascript, so it should work on TS as well.
from fastify-autoload.
Random Update:
This appears to be a problem not only with TypeScript, but with autoloads interactions with fastify-plugin
in general. I was trying to build a quick example application that had some plugins and services, and I found that I could not access a route that I had wrapped in fastify-plugin out of habit. Removing the fastify-plugin wrapper makes the expected route work.
Doesn't Work
when checking localhost:3000/api/users
=> "nothing found"
// services/users.js
const fp = require('fastify-plugin');
const AsyncFn = require('./asyncFn');
module.exports = fp((fastify, opts, next) => {
fastify.get('/', async ()=>{
return await AsyncFn();
});
});
module.exports.autoPrefix = '/api/users'
Works
when checking localhost:3000/api/users
=> expected results.
// services/users.js
const AsyncFn = require('./asyncFn');
module.exports = (fastify, opts, next) => {
fastify.get('/', async ()=>{
return await AsyncFn();
});
};
module.exports.autoPrefix = '/api/users';
from fastify-autoload.
That looks like a bug! Would you like to send a PR to fix it?
from fastify-autoload.
I'll try to. Looking into how it all works together to determine if it's something that should be fixed on fastify plugin's side or autoload's side.
from fastify-autoload.
After some fiddling I'm left with a question: What exactly does the Symbol.for('skip-override')
part do? It seems to be that is the cause of the issue with routes not listening to the prefix, though I'm fuzzy as to why. If I take a service that currently does work with the autoPrefix
, and add module.exports[Symbol.for('skip-override')] = true
it fails just as though I was using fastify-plugin.
I've read the only available doc that I could find (https://github.com/fastify/fastify/blob/master/docs/Plugins.md) but it doesn't really do a whole lot in-depth.
from fastify-autoload.
from fastify-autoload.
I honestly can't see anything in the fastify-autoload code that would be causing that, so this bug might lead me down into the regular fastify repo. I'm at work now, so I'll check after I'm done for the day.
from fastify-autoload.
I decided to test my hypothesis by creating a quick example that did not use fastify autoload or plugin but did use the Symbol.for('skip-override')
.
The results are the same -- a plugin called with the skip-override set to true ignores it's passed prefix, and a plugin registered without it does not. I'm assuming this bug is therefore an issue with the fastify base itself, and not with fastify-autoload
so I'll be closing this issue.
from fastify-autoload.
@jhechtf it's not a bug, it's how fastify works. It's documented in https://www.fastify.io/docs/latest/Plugins/#route-prefixing-option.
from fastify-autoload.
@jhechtf I just started running into this problem today in a new project. Did you ever get around this?
from fastify-autoload.
@jhechtf it's not a bug, it's how fastify works. It's documented in https://www.fastify.io/docs/latest/Plugins/#route-prefixing-option.
Now we get 404 there
from fastify-autoload.
Related Issues (20)
- Dependency from folder?! HOT 2
- Cannot find packages imported by autoloaded plugins in Vercel Serverless Function HOT 4
- Fastify autoload throwing errors about ts in projects that don't use ts HOT 4
- babel-node + typescript. Error only with autoloads : throw new Error(@fastify/autoload cannot import plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.) HOT 5
- Add matchPattern to load only some specific paths HOT 2
- dirNameRoutePrefix option doesn't work when set to false HOT 2
- Mock plugins with autoload HOT 2
- Property '<property>' does not exist on type FastifyInstance when loading TS plugin HOT 1
- Two fastify instances, two `indexPattern` that seems been ignored HOT 2
- Autoload cannot read absolute paths from tsconfig throws error - "Error","message":"Cannot find module '@/Model/User/UserVerifyService' HOT 2
- Node 20 auto-loader not able to transpile TS to JS when using ts-node/esm HOT 24
- Params validation HOT 7
- Not able to have top-level await in a ts ESM file? HOT 3
- Fastify Autoload does not work with Bun.js or deno HOT 2
- Allow concatenation of child plugin prefixes when encapsulate is set to false HOT 2
- setNotFoundHandler is not encapsulated when called inside autohooks.js HOT 3
- autoloader breaks node --test --experimental-test-coverage HOT 2
- Cannot use with esm only HOT 1
- ts-node error with plugins using custom instance field via declaration merging HOT 1
- fileNameRoutePrefix HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-autoload.