question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically wrap route files

See original GitHub issue

šŸš€ Feature Proposal

We’re migrating an API to fastify and somehow the boilerplate

module.exports = function (fastify, opts, next) {
  fastify.route({
    method: 'GET',
    url: '/',
    handler
  })
}

feels a bit clunky. I’d like to propose just the route content in a file:

module.exports = {
  method: 'GET',
  url: '/',
  handler
}

For most cases this is totally sufficient. If additional config/options are required, a regular plugin/route function can be exported.

Motivation

As mentioned above, the plugin structure feels a bit clunky in each and every file.

Example

The default type is a function and the new proposal would be an object (method is always required anyway), so I’m thinking somewhere along the lines of (https://github.com/fastify/fastify-autoload/blob/master/index.js#L110):

const content = require(file)
let plugin

if (content && typeof content === 'object' && content.method) {
  plugin = function (fastify, opts, next) {
    fastify.route(content)
    next()
  }
} else {
  plugin = content
}

This is non-breaking, but to be safe it could also be flagged with an new option ā€œwrapRoutesā€ or something like that.

I’m testing this locally, but I’m currently getting an error when running npm test?

test/error.js ......................................... 1/2
  not ok should match pattern provided
    found: >-
      Unexpected token '}' at
      /Users/patrick/Sites/fastify-autoload/test/error/lib/a.js:6
    pattern: '/Unexpected token \} at .*\/test\/error\/lib\/a.js:6/'
    at:
      line: 14
      column: 5
      file: test/error.js
    stack: |
      test/error.js:14:5
      Object._encapsulateThreeParam (node_modules/avvio/boot.js:408:13)
      Boot.callWithCbOrNextTick (node_modules/avvio/boot.js:339:5)
      release (node_modules/fastq/queue.js:127:16)
      Object.resume (node_modules/fastq/queue.js:61:7)
      node_modules/avvio/boot.js:155:20
      node_modules/avvio/plugin.js:178:7
      done (node_modules/avvio/plugin.js:136:5)
      check (node_modules/avvio/plugin.js:147:7)
    source: |
      t.match(err.message, /Unexpected token \} at .*\/test\/error\/lib\/a.js:6/)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jsumnerscommented, Dec 5, 2019

I thought this issue was on the core repo. I didn’t realize it is in the autoload plugin repo. I know nothing of how this plugin works.

0reactions
PatrickHeneisecommented, Dec 6, 2019

Done. See #55

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flutter auto_route | How do I wrap a route with BlocProvider?
The answer depends on how your routes are constructed, I'll show you how ... You can wrap your bloc provider(s) around the child...
Read more >
auto_route | Flutter Package - Pub.dev
AutoRoute is a declarative routing solution, where everything needed for navigation is automatically generated for you.
Read more >
File-based routing with React Router - Omar Elhawary
Routes are automatically updated by adding/removing/renaming files at the pages directory; Routes are represented in an easy-to-follow file treeĀ ...
Read more >
Ultimate React Router v6 Guide
React Router is the most popular routing library in React, but it can be a bit complicated to wrap your head around some...
Read more >
React Router DOM: How to handle routing in web apps
js , I'm going to import this component from react-router-dom and use it to wrap the <App> component: import React from 'react';Ā ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found