Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow specifying multiple url params in the same "/" part #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import fastifyPlugin from 'fastify-plugin'
import { FastifyInstance, RouteOptions } from 'fastify'

import {
JsonSchema,
JsonSchemaProperty,
ObjectJsonSchemaProperty,
} from 'type-jsonschema'
import fastifyPlugin from 'fastify-plugin'
import fs from 'fs'
import path from 'path'

import process from 'process'
import path from 'path'
import fs from 'fs'

import { JsonSchemaProperty, ObjectJsonSchemaProperty } from 'type-jsonschema'

export const errorLabel = '[ERROR] fastify-autoload:'

Expand Down Expand Up @@ -99,7 +95,7 @@ function scan(
fastify: FastifyInstance,
baseDir: string,
current: string,
log: boolean = false
log: boolean = false,
) {
const combined = path.join(baseDir, current)
const combinedStat = fs.statSync(combined)
Expand Down Expand Up @@ -139,30 +135,20 @@ function pathToUrl(filePath: string) {
}

function replaceParamsToken(token: string) {
const regex = /{.+}/g

let result
while ((result = regex.exec(token)) !== null) {
token =
token.substring(0, result.index) +
result[0].replace('{', ':').replace('}', '') +
token.substr(result.index + result[0].length)
}

return token
return token.replace(/{([^}]+)}/g, (_, match) => `:${match}`)
}

function autoload(
fastify: FastifyInstance,
fullPath: string,
url: string,
log: boolean
log: boolean,
) {
const module = loadModule(fullPath, log)

if (typeof module !== 'function') {
throw new Error(
`${errorLabel} module ${fullPath} must be valid js/ts module and should export route methods definitions`
`${errorLabel} module ${fullPath} must be valid js/ts module and should export route methods definitions`,
)
}

Expand Down Expand Up @@ -240,7 +226,7 @@ export default fastifyPlugin<FastifyAutoroutesOptions>(

try {
scan(fastify, dirPath, '', options.log)
} catch (error) {
} catch (error: any) {
log && console.error(error.message)

return next(error)
Expand All @@ -251,5 +237,5 @@ export default fastifyPlugin<FastifyAutoroutesOptions>(
{
fastify: '>=3.0.0',
name: 'fastify-autoroutes',
}
},
)
75 changes: 56 additions & 19 deletions tests/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tap.test('simple index', { saveFixture: false }, (t) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
}
},
)
})

Expand All @@ -103,7 +103,7 @@ tap.test('nested routes', { saveFixture: false }, (t) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
}
},
)
})

Expand Down Expand Up @@ -131,7 +131,7 @@ tap.test('nested routes with trailing slashes', { saveFixture: false }, (t) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
}
},
)
})

Expand Down Expand Up @@ -159,7 +159,7 @@ tap.test('nested routes with url parameter', { saveFixture: false }, (t) => {
t.is(err, null)
t.is(res.payload, userId)
t.end()
}
},
)
})

Expand Down Expand Up @@ -205,12 +205,12 @@ tap.test(
t.is(err, null)
t.is(JSON.parse(res.payload).USERID, USERID)
t.end()
}
},
)
}
},
)
}
}
},
)

tap.test(
Expand Down Expand Up @@ -242,9 +242,46 @@ tap.test(
t.is(err, null)
t.is(res.payload, userId)
t.end()
}
},
)
}
},
)

tap.test(
'nested routes with url 2 parameters',
{ saveFixture: false },
(t) => {
const server = fastify()

const dir = t.testdir({
users: {
'{userId}-{userName}': {
'foo': exampleGetRouteJSONParam,
},
},
})

server.register(autoroutes, {
dir: dir,
})

const userId = 'foo'
const userName = 'bar'

server.inject(
{
method: 'GET',
url: `/users/${userId}-${userName}/foo`,
},
(err, res) => {
const payload = JSON.parse(res.payload)
t.is(err, null)
t.is(payload.userId, userId)
// t.is(payload.userName, userName)
t.end()
},
)
},
)

tap.test('example es6 exports default module', { saveFixture: false }, (t) => {
Expand All @@ -267,7 +304,7 @@ tap.test('example es6 exports default module', { saveFixture: false }, (t) => {
t.is(err, null)
t.is(res.payload, 'get')
t.end()
}
},
)
})

Expand Down Expand Up @@ -302,11 +339,11 @@ tap.test(
t.is(res.statusCode, 404)

t.end()
}
},
)
}
},
)
}
},
)

tap.test(
Expand Down Expand Up @@ -340,11 +377,11 @@ tap.test(
(err, res) => {
t.is(res.statusCode, 404)
t.end()
}
},
)
}
},
)
}
},
)

tap.test(
Expand Down Expand Up @@ -379,9 +416,9 @@ tap.test(
(err, res) => {
t.is(res.statusCode, 404)
t.end()
}
},
)
}
},
)
}
},
)