Giter VIP home page Giter VIP logo

fastify-loader's Introduction

fastify-loader

The route loader for the cool kids!

NpmLicense David GitHub package version NPM Link Greenkeeper badge

Do you also want an easy way to load routes for fastify? YEAH me too! This is why I made this module.

fastify-loader makes it easy to load routes from a directory.

Features

  • Looks cool
  • No need to module.export = (fastify) => { /* your code here */ }
  • No need to export the fastify instance itself
  • No module.export = { path: '/api/test', handler: (req, reply) => {} }

Please, give me an example!

// File: ./api/hello.js

// YES, really! You don't need to require anything here. 
// The fastify instance gets injected in any file matched by the glob.
fastify.get('/api/hello', async () => {
    // Complex code here
    return { hello: true };
})
// File: ./server.js

const fastify = require('fastify')();

// Just register the plugin and add glob array which files to loud
fastify.register(require('fastify-loader'), {
    paths: ['./api/**/*.js'], // A glob array
    name: "fastify" // [Optional] if you want to do something like this: YOURNAMEHERE.get('/api/test')
});

fastify.listen(1337, err => {
    if (err) console.trace(err);
    console.log('http://127.0.0.1:1337');
});

Installation

Like any other npm module.

yarn add fastify-loader
# or with npm
npm i fastify-loader --save

How does this work?

It uses vm2 to inject the fastify instance and other vars into the scope of each file.

WebStorm/{INSERT OTHER IDE HERE} says that fastify is an unknown variable. What can I do?

If your IDE supports JSDoc, you can append this to the file.

// File: ./api/hello.js
+ /**
+  * @var {fastify.FastifyInstance} fastify
+  */

// noinspection ES6ModulesDependencies
fastify.get('/api/hello', async () => {
    // Complex code here
    return { hello: true };
})

Or better if your IDE supports typescript typings, because then you can do this:

// File: ./api/hello.js
+ /// <reference types="fastify"/>
+ /// <reference types="fastify-loader"/>

// noinspection ES6ModulesDependencies
fastify.get('/api/hello', async () => {
    // Complex code here
    return { hello: true };
})

Do tests exist?

Yes. You can run yarn test or npm run test after you installed the dependencies.

Inject more than just fastify

fastify.register(require('fastify-loader'), {
    paths: ['./api/**/*.js'], // A glob array
    inject: {
        pi: 3.14 // pi is now available as var in each js file in ./api
    }
});

License

The MIT License (MIT)

Copyright (c) 2018 Nils Bergmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fastify-loader's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar thenoim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fastify-loader's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

An in-range update of fast-glob is breaking the build 🚨

The dependency fast-glob was updated from 3.1.1 to 3.2.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

fast-glob is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for 3.2.0

💬 Common

  • An empty pattern now causes an error (#247)

🚀 Improvements

In the #156 issue we've redesigned the deep filter, which controls the reading of directories in depth.

Previously, this filter did not take into account positive patterns and only used their maximum depth. For example, the example below shows how many extra directories we read:

{src,fixtures}/**

src → read
fixtures → read
out → read
node_modules → read

Now we apply positive patterns.

{src,fixtures}/**

src → read
fixtures → read
out → skip
node_modules → skip

Synthetic benchmark

{fixtures,out}/{first,second}/*

sync, ms async, ms stream, ms
3.x.x 13 22 20
3.2.0 5 9 8

{fixtures,out}/**

sync, ms async, ms stream, ms
3.x.x 37 49 52
3.2.0 6 10 12

Real world benchmark

  • Globby
  • Prettier
  • {blocks-*,construct}/**/*.styl (a very large project) 13s → 0.16s

Known issues

  • For some cases, there is a noticeable slowdown of 3-6%.
  • Patterns containing {a..z} (or similar) may introduce some slowdown.

We will work on this in the future.

🎉 Thanks

  • @jonschlinkert for the scan method in picomatch that returns parts of the pattern.
  • @fisker for early beta feedback.
Commits

The new version differs by 23 commits.

  • 1757299 3.2.0
  • 27ab96c Merge pull request #252 from mrmlnc/ISSUE-156_improve_partial_matcher
  • 3c33e23 fix(matchers): pass settings to "isDynamicPattern" method
  • 087c51e test(utils/pattern): add more tests for "isDynamicPattern" method
  • 9444563 refactor(utils): drop unused code
  • f043c84 fix(matchers): correctly handle multiple patterns
  • 6474bf4 fix(matchers): correctly handle pattern with difference levels
  • 0923f9b Merge pull request #249 from mrmlnc/ISSUE-156_partial_matcher
  • d16282c Merge pull request #250 from mrmlnc/ISSUE-247_input_data_validation
  • d5bd15f feat: improve input data validation
  • 2f5f18e refactor(matcher): extract base methods to the abstract class
  • 0d2ee18 test(smoke): add smoke tests for partial matching
  • c884233 build(package): add benchmark for partial matching
  • aefa86d feat(utils/pattern): add method to match pattern parts
  • 8cef9e8 refactor(providers): introduce matchers

There are 23 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

cannot find bunyan and micro-request-bunyan

Fastify: 1.13.4
Node: 10 LTS
Loader Version: 2.3.6

I am unable to run the loader for a directory that is two-levels deep.

The error cited is two fold:

  1. cannot find module bunyan
  2. cannot find module micro-bunyan-request

After I manually satisfy those requirements.

I get this error:

``
/Users/pshah/Projects/wavey/wavvy-tenants-api/node_modules/vm2/lib/main.js:430
throw this._internal.Decontextify.value(e);
^
VMError: Cannot find module '../routes/auth.js'


Let me know if I can help in another way.

Update project

Update the complete project. I haven't done this for a while.

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.