generated from toomuchdesign/npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
55 lines (49 loc) · 1.54 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Fastify from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import { schemas } from './definitions/petstore/schemas-autogenerated/fastify-integration';
import { petRoutes } from './routes/pet';
export async function makeServer() {
const server = Fastify({
logger: true,
});
await server.register(fastifySwagger, {
openapi: {
info: {
title: 'Test swagger',
version: '0.1.0',
},
servers: [
{
url: 'http://localhost',
},
],
},
/**
* Register schemas with $id "/components/schemas/xxx" under OpenAPI's "components.schemas"
* https://github.com/fastify/fastify-swagger?tab=readme-ov-file#managing-your-refs
*/
refResolver: {
buildLocalReference: (json, baseUri, fragment, i) => {
const OPEN_API_COMPONENTS_SCHEMAS_PATH = '/components/schemas/';
if (
typeof json.$id === 'string' &&
json.$id.startsWith(OPEN_API_COMPONENTS_SCHEMAS_PATH)
) {
return json.$id.replace(OPEN_API_COMPONENTS_SCHEMAS_PATH, '');
}
// @TODO Support naming component schemas different than "components.schema"
return `def-${i}`;
},
},
});
await server.register(fastifySwaggerUI, {
routePrefix: '/documentation',
});
// Register `$ref` schemas individually so that they can be resolved at runtime
schemas.forEach((schema) => {
server.addSchema(schema);
});
await server.register(petRoutes);
return server;
}