Giter Site home page Giter Site logo

Comments (19)

jhechtf avatar jhechtf commented on May 18, 2024 1

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.

mcollina avatar mcollina commented on May 18, 2024

I think there might be a bug in the code! Would you like to send a fix?

from fastify-autoload.

jhechtf avatar jhechtf commented on May 18, 2024

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.

jhechtf avatar jhechtf commented on May 18, 2024

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.

mcollina avatar mcollina commented on May 18, 2024

autoPrefix works in JavaScript here, so it's likely a bug in the typescript integration.

from fastify-autoload.

jhechtf avatar jhechtf commented on May 18, 2024

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.

jhechtf avatar jhechtf commented on May 18, 2024

Working

image

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

image

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.

jhechtf avatar jhechtf commented on May 18, 2024

@mcollina not sure if you saw my other comments.

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

Compatibility with fastify-plugin is expected. It works in Javascript, so it should work on TS as well.

from fastify-autoload.

jhechtf avatar jhechtf commented on May 18, 2024

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.

mcollina avatar mcollina commented on May 18, 2024

That looks like a bug! Would you like to send a PR to fix it?

from fastify-autoload.

jhechtf avatar jhechtf commented on May 18, 2024

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.

jhechtf avatar jhechtf commented on May 18, 2024

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.

mcollina avatar mcollina commented on May 18, 2024

from fastify-autoload.

jhechtf avatar jhechtf commented on May 18, 2024

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.

jhechtf avatar jhechtf commented on May 18, 2024

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.

mcollina avatar mcollina commented on May 18, 2024

@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.

mrob11 avatar mrob11 commented on May 18, 2024

@jhechtf I just started running into this problem today in a new project. Did you ever get around this?

from fastify-autoload.

diego-betto avatar diego-betto commented on May 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.