Skip to content

Commit

Permalink
✨ add helper function to reverse outside of fastify app and export ro…
Browse files Browse the repository at this point in the history
…utes
  • Loading branch information
dimonnwc3 committed Jul 26, 2020
1 parent f1a9a15 commit 11a5a2e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as fastify from "fastify"
import { Server, IncomingMessage, ServerResponse } from "http"
import { PathFunction } from "path-to-regexp"

declare const reverse: fastify.Plugin<
declare const routes: Map<string, PathFunction<object>>

declare function reverse<Args>(name: string, args?: Args): string

declare const plugin: fastify.Plugin<
Server,
IncomingMessage,
ServerResponse,
Expand All @@ -19,3 +24,4 @@ declare module "fastify" {
}

export default reverse
export { plugin, routes }
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const fp = require("fastify-plugin")
const pathToRegexp = require("path-to-regexp")

function plugin(fastify, _, next) {
const routes = new Map()

function reverse(name, args, opts) {
const toPath = routes.get(name)
const routes = new Map()

if (!toPath) {
throw new Error(`Route with name ${name} is not registered`)
}
function reverse(name, args, opts) {
const toPath = routes.get(name)

return toPath(args, opts)
if (!toPath) {
throw new Error(`Route with name ${name} is not registered`)
}

return toPath(args, opts)
}

function plugin(fastify, _, next) {
fastify.decorate("reverse", reverse)

fastify.addHook("onRoute", routeOptions => {
fastify.addHook("onRoute", (routeOptions) => {
if (routeOptions.name) {
if (routes.has(routeOptions.name)) {
throw new Error(
Expand All @@ -31,7 +31,9 @@ function plugin(fastify, _, next) {
next()
}

module.exports = fp(plugin, {
module.exports = reverse
module.exports.routes = routes
module.exports.plugin = fp(plugin, {
fastify: ">= 1.6.0",
name: "reverse",
})
16 changes: 10 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Fastify = require("fastify")
const fastifyReverseRoutes = require("./index")
const reverse = require("./index")

let fastify

Expand All @@ -13,15 +13,15 @@ afterEach(async () => {

describe("Reverse routes", () => {
it("Add reverse method", async () => {
await fastify.register(fastifyReverseRoutes).ready()
await fastify.register(reverse.plugin).ready()

expect(fastify.reverse).toBeDefined()
expect(typeof fastify.reverse).toBe("function")
})

it("Reverse pattern to path", async () => {
await fastify
.register(fastifyReverseRoutes)
.register(reverse.plugin)
.route({
url: "/frameworks/:name",
method: "GET",
Expand All @@ -33,12 +33,16 @@ describe("Reverse routes", () => {
expect(fastify.reverse("frameworks", { name: "fastify" })).toBe(
"/frameworks/fastify",
)

expect(reverse("frameworks", { name: "fastify" })).toBe(
"/frameworks/fastify",
)
})

it("Plugin throws for duplicated routes", async () => {
try {
await fastify
.register(fastifyReverseRoutes)
.register(reverse.plugin)
.route({
url: "/frameworks/:name",
method: "GET",
Expand All @@ -59,7 +63,7 @@ describe("Reverse routes", () => {

it("Reverse throws for non registered route", async () => {
await fastify
.register(fastifyReverseRoutes)
.register(reverse.plugin)
.route({
url: "/frameworks/:name",
method: "GET",
Expand All @@ -68,7 +72,7 @@ describe("Reverse routes", () => {
.ready()

expect(() => {
expect(fastify.reverse("frameworks", { name: "fastify" })).toBe(
expect(fastify.reverse("books", { name: "fastify" })).toBe(
"/frameworks/fastify",
)
}).toThrow()
Expand Down

0 comments on commit 11a5a2e

Please sign in to comment.