giovannicardamone / fastify-autoroutes Goto Github PK
View Code? Open in Web Editor NEWfastest way to map directories to URLs in fastify
Home Page: https://giovannicardamone.github.io/fastify-autoroutes/
License: MIT License
fastest way to map directories to URLs in fastify
Home Page: https://giovannicardamone.github.io/fastify-autoroutes/
License: MIT License
"Prefix" feature, allow a base such as "/api" without nesting all routes in the /routes/api folder
ease of use, similar to nextjs.
Implementation:
app.register(fastifyAutoRoutes, {
dir: path.join(staticPath, 'routes'),
ignorePattern: /.*\.test\.js/,
prefix: "/api/"
});
Support for generics for types in methods.
Something similar to what fastify already has for their method routes.
for instances:
server.get<{
Querystring: IQuerystring;
Headers: IHeaders;
Reply: ReplyBodyType;
}>(fullpath, async (request, reply) => {
console.log("name");
reply.status(200).send({ success: true, message: "hello world" });
});
This types all of the querystring, headers, and reply using a very nice and simple and neat syntax.
Typing is quite neat and useful in TS. And this way and syntax is also very nice.
Thanks
Currently, you cannot install along Fastify 4.0, dependency resolution fails. Can we have support for Fastify 4.0?
Route directories when they contain a :key path ignore all others in the directory.
I'm hoping for support for something like this, this is just an example.
/users/
/users/{uuid}/
/users/create/
users/create
isn't available because it'd be passed to users/{uuid}
.
More complex and detailed API structures.
List all the routes in the subdir
The route is missing from the list of available routes
I just added a new file to the directory with the same content as health.ts
Hello,
I have just installed fast-autoroutes in a new project but the post installation (using npm) displays:
> npm install --save fastify-autoroutes
added 16 packages, and audited 921 packages in 2s
52 packages are looking for funding
run `npm fund` for details
7 moderate severity vulnerabilities
To address all issues (including breaking changes), run:
npm audit fix --force
Run `npm audit` for details.
> npm audit
# npm audit report
ansi-regex >2.1.1 <5.0.1
Severity: moderate
Inefficient Regular Expression Complexity in chalk/ansi-regex - https://github.com/advisories/GHSA-93q8-gq69-wqmw
fix available via `npm audit fix --force`
Will install [email protected], which is a breaking change
node_modules/fastify-autoroutes/node_modules/ansi-regex
strip-ansi 4.0.0 - 5.2.0
Depends on vulnerable versions of ansi-regex
node_modules/fastify-autoroutes/node_modules/strip-ansi
cliui 4.0.0 - 5.0.0
Depends on vulnerable versions of strip-ansi
Depends on vulnerable versions of wrap-ansi
node_modules/fastify-autoroutes/node_modules/cliui
yargs 10.1.0 - 15.0.0
Depends on vulnerable versions of cliui
Depends on vulnerable versions of string-width
node_modules/fastify-autoroutes/node_modules/yargs
fastify-autoroutes >=2.0.0
Depends on vulnerable versions of yargs
node_modules/fastify-autoroutes
string-width 2.1.0 - 4.1.0
Depends on vulnerable versions of strip-ansi
node_modules/fastify-autoroutes/node_modules/string-width
wrap-ansi 3.0.0 - 6.1.0
Depends on vulnerable versions of string-width
Depends on vulnerable versions of strip-ansi
node_modules/fastify-autoroutes/node_modules/wrap-ansi
7 moderate severity vulnerabilities
It seems you have an unused dependencies in package.json: json5, pluralize and yargs.
You could probably cleanup the dev dependancies as well.
Also note that the npm package contains unrelated (old) files in the dist/ folder.
Hello is this lib stable and maintained?
I can seem to get the package working correctly when using ES modules:
server.js
:
...
import autoroutes from 'fastify-autoroutes'
...
...
try {
await server.register(autoroutes, {
dir:'./routes',
})
} catch (error) {
console.log('ERROR:', error)
}
...
When the server is started, the following exception:
"Error [ERR_REQUIRE_ESM]: require() of ES Module .../src/routes/index.js from .../node_modules/fastify-autoroutes/dist/index.js not supported"
from this repo: https://github.com/karmabadger/fastify-routes-test
when running:
npm run dev
> [email protected] dev
> nodemon index.ts
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node index.ts`
C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4
return new (P || (P = Promise))(function (resolve, reject) {
^
Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\index.ts\src\routes does not exists
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:116:19
at Generator.next (<anonymous>)
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71
at new Promise (<anonymous>)
at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12)
at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\src\index.ts:98:7)
at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19)
at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
[nodemon] app crashed - waiting for file changes before starting...
when running:
npm run start```
[email protected] start
node ./build/index.js
C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:44
return next(new Error(${exports.ERROR_LABEL} dir ${dirPath} does not exists
));
^
Error: fastify-autoroutes dir C:\Users\wenxu\Projects\test\fileroutes\build\index.js\src\routes does not exists
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:44:21
at Generator.next ()
at C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:8:71
at new Promise ()
at __awaiter (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:4:12)
at exports.default.fastify_plugin_1.default.fastify (C:\Users\wenxu\Projects\test\fileroutes\node_modules\fastify-autoroutes\dist\index.js:31:72)
at Plugin.exec (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:132:19)
at Boot.loadPlugin (C:\Users\wenxu\Projects\test\fileroutes\node_modules\avvio\plugin.js:274:10)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Am I doing this correctly? Perhaps I'm missing something?
I feel like on windows only, it's not resolving the paths to each of the routes correctly when it's doing the dynamic routes imports. ```C:\Users\wenxu\Projects\test\fileroutes\index.ts\src\routes does not exists.``` as we can see here, it's getting the absolute path to the file not the directly which causes issues here.
I know that the node module filesystem will also complain if ```file://``` isn't prepended at the beginning to tell it the protocol if it's an absolute file path (at least I think that could be an issue)).
Does this issue even occur on linux or mac?
If only on windows I could try to figure out what's wrong and make a PR, tho I'm a bit new to the Open source dev scene so Ill have to figure out how it works haha.
Thanks
Hello! Sorry about my english.
Basically, if i create a route folder and a index, when i remove the last slash of url, route is not found.
Looks like its enter in other route without a sub route.
Example:
routes/users/index.ts
http://localhost:3333/users --- doesn't work
http://localhost:3333/users/ --- works
routes/users.ts
http://localhost:3333/users --- works
http://localhost:3333/users/ --- doesn't work
I think its better if works with and without slash at the end of url.
fastify-autoroute: 2.1.3
node: 18.1.0
os: docker [Alpine]
terminal: sh
Working autorouting
Not working everything
import fastifyFramework from 'fastify';
import fastifyAutoRoutes from 'fastify-autoroutes'
import fastifyStatic from "@fastify/static";
import fastifyCors from '@fastify/cors';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const fastify = fastifyFramework({
logger: process.env.ENVIRONMENT !== 'production',
});
fastify.register(fastifyAutoRoutes, {
dir: '../src/routes',
});
fastify.register(fastifyCors, {
origin: true,
credentials: true
});
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'public'),
prefix: '/public/'
});
const start = async () => {
try {
await fastify.listen(80, '0.0.0.0');
} catch (e) {
fastify.log.error(e);
setTimeout(start, 5000);
}
}
await start();
{
"name": "deal",
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "nodemon index.js",
"start": "node index.js"
},
"dependencies": {
"@fastify/cors": "^7.0.0",
"@fastify/static": "^5.0.2",
"fastify": "^3.29.0",
"fastify-autoroutes": "^2.1.3"
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.