@scalar/fastify-api-reference 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.umd.cjs +17 -0
- package/package.json +6 -6
- package/src/fastifyApiReference.test.ts +148 -0
- package/src/fastifyApiReference.ts +60 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
(function(r,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(r=typeof globalThis<"u"?globalThis:r||self,r["@scalar/fastify-api-reference"]=c())})(this,function(){"use strict";const r=t=>{const e=t.specUrl?`<div data-spec-url="${t.specUrl}" />`:`<div data-spec='${JSON.stringify(t.spec)}' />`;return`
|
|
2
|
+
<!DOCTYPE html>
|
|
3
|
+
<html>
|
|
4
|
+
<head>
|
|
5
|
+
<title>${t.title||"API Reference"}</title>
|
|
6
|
+
<meta charset="utf-8" />
|
|
7
|
+
<meta
|
|
8
|
+
name="viewport"
|
|
9
|
+
content="width=device-width, initial-scale=1" />
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<!-- Add your own OpenAPI/Swagger spec file URL here: -->
|
|
13
|
+
${e}
|
|
14
|
+
<script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|
|
17
|
+
`};return(t,e,a)=>{if(!e.apiReference.spec&&!e.apiReference.specUrl){console.warn("[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."),a();return}t.get("/",(s,i)=>{const n=r(e==null?void 0:e.apiReference);i.headers({"Content-Type":"text/html; charset=utf-8"}),i.send(n)}),a()}});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scalar/fastify-api-reference",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Scalar",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -8,15 +8,15 @@
|
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"files": [
|
|
11
|
+
"src",
|
|
11
12
|
"dist"
|
|
12
13
|
],
|
|
13
|
-
"main": "./dist/index.
|
|
14
|
+
"main": "./dist/index.umd.cjs",
|
|
14
15
|
"module": "./dist/index.js",
|
|
15
16
|
"exports": {
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
"development": "./src/index.ts",
|
|
18
|
+
"require": "./dist/index.umd.cjs",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
20
|
},
|
|
21
21
|
"types": "dist/index.d.ts",
|
|
22
22
|
"dependencies": {
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import Fastify from 'fastify'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import fastifyApiReference from './fastifyApiReference'
|
|
5
|
+
|
|
6
|
+
describe('fastifyApiReference', () => {
|
|
7
|
+
it('returns 200 OK', () =>
|
|
8
|
+
new Promise((resolve) => {
|
|
9
|
+
const fastify = Fastify({
|
|
10
|
+
logger: false,
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
fastify.register(fastifyApiReference, {
|
|
14
|
+
prefix: '/api-reference',
|
|
15
|
+
apiReference: {
|
|
16
|
+
specUrl: '/scalar.json',
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
fastify.listen({ port: 0 }, function (err, address) {
|
|
21
|
+
fetch(`${address}/api-reference`).then((response) => {
|
|
22
|
+
expect(response.status).toBe(200)
|
|
23
|
+
resolve(null)
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
it('has the spec URL', () =>
|
|
29
|
+
new Promise((resolve) => {
|
|
30
|
+
const fastify = Fastify({
|
|
31
|
+
logger: false,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
fastify.register(fastifyApiReference, {
|
|
35
|
+
prefix: '/api-reference',
|
|
36
|
+
apiReference: {
|
|
37
|
+
specUrl: '/scalar.json',
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
fastify.listen({ port: 0 }, function (err, address) {
|
|
42
|
+
fetch(`${address}/api-reference`).then(async (response) => {
|
|
43
|
+
expect(await response.text()).toContain(
|
|
44
|
+
'data-spec-url="/scalar.json"',
|
|
45
|
+
)
|
|
46
|
+
resolve(null)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
}))
|
|
50
|
+
|
|
51
|
+
it('has the spec', () =>
|
|
52
|
+
new Promise((resolve) => {
|
|
53
|
+
const spec = {
|
|
54
|
+
openapi: '3.1.0',
|
|
55
|
+
info: {
|
|
56
|
+
title: 'Example',
|
|
57
|
+
},
|
|
58
|
+
paths: {},
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const fastify = Fastify({
|
|
62
|
+
logger: false,
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
fastify.register(fastifyApiReference, {
|
|
66
|
+
prefix: '/api-reference',
|
|
67
|
+
apiReference: {
|
|
68
|
+
spec,
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
fastify.listen({ port: 0 }, function (err, address) {
|
|
73
|
+
fetch(`${address}/api-reference`).then(async (response) => {
|
|
74
|
+
expect(await response.text()).toContain(
|
|
75
|
+
`data-spec='${JSON.stringify(spec)}'`,
|
|
76
|
+
)
|
|
77
|
+
resolve(null)
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
}))
|
|
81
|
+
|
|
82
|
+
it('has the default title', () =>
|
|
83
|
+
new Promise((resolve) => {
|
|
84
|
+
const fastify = Fastify({
|
|
85
|
+
logger: false,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
fastify.register(fastifyApiReference, {
|
|
89
|
+
prefix: '/api-reference',
|
|
90
|
+
apiReference: {
|
|
91
|
+
specUrl: '/scalar.json',
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
fastify.listen({ port: 0 }, function (err, address) {
|
|
96
|
+
fetch(`${address}/api-reference`).then(async (response) => {
|
|
97
|
+
expect(await response.text()).toContain(
|
|
98
|
+
'<title>API Reference</title>',
|
|
99
|
+
)
|
|
100
|
+
resolve(null)
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
}))
|
|
104
|
+
|
|
105
|
+
it('has the custom title', () =>
|
|
106
|
+
new Promise((resolve) => {
|
|
107
|
+
const fastify = Fastify({
|
|
108
|
+
logger: false,
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
fastify.register(fastifyApiReference, {
|
|
112
|
+
prefix: '/api-reference',
|
|
113
|
+
apiReference: {
|
|
114
|
+
title: 'Foobar',
|
|
115
|
+
specUrl: '/scalar.json',
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
fastify.listen({ port: 0 }, function (err, address) {
|
|
120
|
+
fetch(`${address}/api-reference`).then(async (response) => {
|
|
121
|
+
expect(await response.text()).toContain('<title>Foobar</title>')
|
|
122
|
+
resolve(null)
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
}))
|
|
126
|
+
|
|
127
|
+
it('has the correct content type', () =>
|
|
128
|
+
new Promise((resolve) => {
|
|
129
|
+
const fastify = Fastify({
|
|
130
|
+
logger: false,
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
fastify.register(fastifyApiReference, {
|
|
134
|
+
prefix: '/api-reference',
|
|
135
|
+
apiReference: {
|
|
136
|
+
specUrl: '/scalar.json',
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
fastify.listen({ port: 0 }, function (err, address) {
|
|
141
|
+
fetch(`${address}/api-reference`).then(async (response) => {
|
|
142
|
+
expect(response.headers.has('content-type')).toBe(true)
|
|
143
|
+
expect(response.headers.get('content-type')).toContain('text/html')
|
|
144
|
+
resolve(null)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
}))
|
|
148
|
+
})
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type FastifyInstance, type RegisterOptions } from 'fastify'
|
|
2
|
+
|
|
3
|
+
export type FastifyApiReferenceOptions = {
|
|
4
|
+
title?: string
|
|
5
|
+
specUrl?: string
|
|
6
|
+
spec?: Record<string, any>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const getHtmlMarkup = (options: FastifyApiReferenceOptions) => {
|
|
10
|
+
const htmlTag = options.specUrl
|
|
11
|
+
? `<div data-spec-url="${options.specUrl}" />`
|
|
12
|
+
: `<div data-spec='${JSON.stringify(options.spec)}' />`
|
|
13
|
+
|
|
14
|
+
return `
|
|
15
|
+
<!DOCTYPE html>
|
|
16
|
+
<html>
|
|
17
|
+
<head>
|
|
18
|
+
<title>${options.title || 'API Reference'}</title>
|
|
19
|
+
<meta charset="utf-8" />
|
|
20
|
+
<meta
|
|
21
|
+
name="viewport"
|
|
22
|
+
content="width=device-width, initial-scale=1" />
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
<!-- Add your own OpenAPI/Swagger spec file URL here: -->
|
|
26
|
+
${htmlTag}
|
|
27
|
+
<script src="https://cdn.scalar.com/api-reference.standalone.js"></script>
|
|
28
|
+
</body>
|
|
29
|
+
</html>
|
|
30
|
+
`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default (
|
|
34
|
+
fastify: FastifyInstance,
|
|
35
|
+
options: RegisterOptions & {
|
|
36
|
+
apiReference: FastifyApiReferenceOptions
|
|
37
|
+
},
|
|
38
|
+
done: (err?: Error) => void,
|
|
39
|
+
) => {
|
|
40
|
+
if (!options.apiReference.spec && !options.apiReference.specUrl) {
|
|
41
|
+
console.warn(
|
|
42
|
+
'[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.',
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
done()
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fastify.get('/', (_, reply) => {
|
|
50
|
+
const html = getHtmlMarkup(options?.apiReference)
|
|
51
|
+
|
|
52
|
+
reply.headers({
|
|
53
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
reply.send(html)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
done()
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './fastifyApiReference'
|