• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

touchifyapp / fastify-oapi / 4662355291

pending completion
4662355291

push

github

Maxime LUCE
feat!: upgrade plugin to fastify v4

191 of 214 branches covered (89.25%)

Branch coverage included in aggregate %.

13 of 13 new or added lines in 5 files covered. (100.0%)

269 of 283 relevant lines covered (95.05%)

307.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/lib/plugin.ts
1
import type { FastifyInstance } from "fastify";
2

5✔
3
import parse, { type ParsedRoute } from "./parser";
5✔
4
import { type ControllerOptions, createHandler, type AnyRouteHandler } from "./resolution";
5✔
5
import { stripResponseFormats } from "./util";
5✔
6

5✔
7
export interface FastifyOApiOptions extends ControllerOptions {
8
    specification: string;
49✔
9
    prefix?: string;
48✔
10
}
48✔
11

1✔
12
export async function plugin(fastify: FastifyInstance, options: FastifyOApiOptions): Promise<void> {
13
    const config = await parse(options.specification);
47!
14
    const routeConf: RouterConfig = {};
×
15

16
    if (options.prefix) {
48✔
17
        routeConf.prefix = options.prefix;
18
    } else if (config.prefix) {
48✔
19
        routeConf.prefix = config.prefix;
45✔
20
    }
21

48✔
22
    fastify.register(generateRoutes, routeConf);
427✔
23

148✔
24
    async function generateRoutes(instance: FastifyInstance): Promise<void> {
25
        if (config.shared) {
427✔
26
            instance.addSchema(config.shared);
419✔
27
        }
28

29
        for (const route of config.routes) {
30
            if (route.schema.response) {
31
                stripResponseFormats(route.schema.response as Record<string, any>);
32
            }
33

34
            const controllerHandler = await createHandler(route, options);
35

36
            instance.route({
5✔
37
                ...route,
38
                handler: createWrappedHandler(route, controllerHandler),
419✔
39
                config: {
396✔
40
                    oapi: route.openapiSource,
41
                },
23✔
42
            });
23✔
43
        }
1✔
44
    }
1✔
45
}
46

47
function createWrappedHandler(route: ParsedRoute, controllerHandler: AnyRouteHandler): AnyRouteHandler {
48
    if (!route.wildcard) {
49
        return controllerHandler;
50
    }
51

52
    const wildcard = route.wildcard;
53

54
    return function (req, reply) {
55
        (req.params as any)[wildcard] = (req.params as any)["*"];
56

57
        return controllerHandler.call(this, req, reply);
58
    };
59
}
60

61
interface RouterConfig {
62
    prefix?: string;
63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc