@scalar/fastify-api-reference 0.4.1 → 0.5.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/README.md +14 -3
- package/dist/fastifyApiReference.d.ts +8 -8
- package/dist/fastifyApiReference.d.ts.map +1 -1
- package/dist/index.js +15 -11
- package/dist/index.umd.cjs +2 -2
- package/package.json +30 -33
- package/src/esm.test.ts +0 -27
- package/src/fastifyApiReference.test.ts +0 -148
- package/src/fastifyApiReference.ts +0 -65
- package/src/index.ts +0 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ If you have a OpenAPI/Swagger file already, you can pass an URL to the plugin:
|
|
|
20
20
|
```ts
|
|
21
21
|
// Render an API reference for a given OpenAPI/Swagger spec URL
|
|
22
22
|
fastify.register(require('@scalar/fastify-api-reference'), {
|
|
23
|
-
prefix: '/
|
|
23
|
+
prefix: '/reference',
|
|
24
24
|
apiReference: {
|
|
25
25
|
title: 'Our API Reference',
|
|
26
26
|
specUrl: '/swagger.json',
|
|
@@ -32,9 +32,20 @@ With the [@fastify/swagger](https://github.com/fastify/fastify-swagger) you can
|
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
34
|
await fastify.register(require('@scalar/fastify-api-reference'), {
|
|
35
|
-
prefix: '/
|
|
35
|
+
prefix: '/reference',
|
|
36
36
|
apiReference: {
|
|
37
|
-
spec: fastify.swagger(),
|
|
37
|
+
spec: () => fastify.swagger(),
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or, if you just have a static OpenAPI spec, you can directly pass it, too:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
await fastify.register(require('@scalar/fastify-api-reference'), {
|
|
46
|
+
prefix: '/reference',
|
|
47
|
+
apiReference: {
|
|
48
|
+
spec: { … },
|
|
38
49
|
},
|
|
39
50
|
})
|
|
40
51
|
```
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
type
|
|
1
|
+
import { type FastifyPluginAsync } from 'fastify';
|
|
2
|
+
export type ApiReferencePlugin = {
|
|
3
|
+
apiReference: ApiReferenceOptions;
|
|
4
|
+
};
|
|
5
|
+
export type ApiReferenceOptions = {
|
|
3
6
|
title?: string;
|
|
4
7
|
specUrl?: string;
|
|
5
|
-
spec?: Record<string, any
|
|
8
|
+
spec?: Record<string, any> | (() => Record<string, any>);
|
|
6
9
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}>;
|
|
10
|
-
declare const fastifyApiReference: FastifyApiReferenceOptions;
|
|
11
|
-
export default fastifyApiReference;
|
|
10
|
+
declare const fastifyApiReferencePlugin: FastifyPluginAsync<ApiReferencePlugin>;
|
|
11
|
+
export default fastifyApiReferencePlugin;
|
|
12
12
|
//# sourceMappingURL=fastifyApiReference.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,kBAAkB,EAExB,MAAM,SAAS,CAAA;AAEhB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,mBAAmB,CAAA;CAClC,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;CACzD,CAAA;AA8BD,QAAA,MAAM,yBAAyB,EAAE,kBAAkB,CACjD,kBAAkB,CAuBnB,CAAA;AAED,eAAe,yBAAyB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const t = e.specUrl ? `<div data-spec-url="${e.specUrl}" />` : `<div data-spec='${JSON.stringify(e.spec)}'
|
|
1
|
+
const d = (e) => {
|
|
2
|
+
const t = e.specUrl ? `<div data-spec-url="${e.specUrl}" />` : typeof e.spec == "object" ? `<div data-spec='${JSON.stringify(e.spec)}' />` : e.spec ? `<div data-spec='${JSON.stringify(e.spec())}' />` : "";
|
|
3
3
|
return `
|
|
4
4
|
<!DOCTYPE html>
|
|
5
5
|
<html>
|
|
@@ -17,16 +17,20 @@ const s = (e) => {
|
|
|
17
17
|
</body>
|
|
18
18
|
</html>
|
|
19
19
|
`;
|
|
20
|
-
},
|
|
21
|
-
!t.apiReference.spec && !t.apiReference.specUrl
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
}, l = async (e, t) => {
|
|
21
|
+
if (!t.apiReference.spec && !t.apiReference.specUrl) {
|
|
22
|
+
console.warn(
|
|
23
|
+
"[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."
|
|
24
|
+
);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
e.addHook("onSend", (r, a, c, s) => {
|
|
28
|
+
a.header("Content-Type", "text/html; charset=utf-8"), s();
|
|
29
|
+
}), e.get("/", async (r, a) => {
|
|
30
|
+
const c = d(t == null ? void 0 : t.apiReference);
|
|
27
31
|
a.send(c);
|
|
28
|
-
})
|
|
32
|
+
});
|
|
29
33
|
};
|
|
30
34
|
export {
|
|
31
|
-
|
|
35
|
+
l as default
|
|
32
36
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(r
|
|
1
|
+
(function(c,r){typeof exports=="object"&&typeof module<"u"?module.exports=r():typeof define=="function"&&define.amd?define(r):(c=typeof globalThis<"u"?globalThis:c||self,c["@scalar/fastify-api-reference"]=r())})(this,function(){"use strict";const c=e=>{const t=e.specUrl?`<div data-spec-url="${e.specUrl}" />`:typeof e.spec=="object"?`<div data-spec='${JSON.stringify(e.spec)}' />`:e.spec?`<div data-spec='${JSON.stringify(e.spec())}' />`:"";return`
|
|
2
2
|
<!DOCTYPE html>
|
|
3
3
|
<html>
|
|
4
4
|
<head>
|
|
@@ -14,4 +14,4 @@
|
|
|
14
14
|
<script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
|
|
15
15
|
</body>
|
|
16
16
|
</html>
|
|
17
|
-
`};return(e,t
|
|
17
|
+
`};return async(e,t)=>{if(!t.apiReference.spec&&!t.apiReference.specUrl){console.warn("[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.");return}e.addHook("onSend",(s,a,n,i)=>{a.header("Content-Type","text/html; charset=utf-8"),i()}),e.get("/",async(s,a)=>{const n=c(t==null?void 0:t.apiReference);a.send(n)})}});
|
package/package.json
CHANGED
|
@@ -1,52 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scalar/fastify-api-reference",
|
|
3
3
|
"description": "a fastify plugin to render an API reference from an automatically generated OpenAPI spec",
|
|
4
|
-
"
|
|
5
|
-
"fastify",
|
|
6
|
-
"openapi",
|
|
7
|
-
"swagger",
|
|
8
|
-
"api",
|
|
9
|
-
"documentation"
|
|
10
|
-
],
|
|
11
|
-
"version": "0.4.1",
|
|
4
|
+
"version": "0.5.0",
|
|
12
5
|
"author": "Scalar (https://github.com/scalar)",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"url": "https://github.com/scalar/scalar.git",
|
|
17
|
-
"directory": "packages/fastify-api-reference"
|
|
6
|
+
"bugs": "https://github.com/scalar/scalar/issues/new",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"fastify": "4.23.2"
|
|
18
9
|
},
|
|
19
|
-
"
|
|
20
|
-
"
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@vitest/coverage-v8": "0.34.4",
|
|
12
|
+
"nodemon": "3.0.1",
|
|
13
|
+
"tsc-alias": "1.8.8",
|
|
14
|
+
"typescript": "5.2.2",
|
|
15
|
+
"vite": "4.4.9",
|
|
16
|
+
"vite-node": "0.34.4",
|
|
17
|
+
"vitest": "0.34.4"
|
|
21
18
|
},
|
|
22
|
-
"license": "MIT",
|
|
23
19
|
"engines": {
|
|
24
20
|
"node": ">=18"
|
|
25
21
|
},
|
|
26
|
-
"
|
|
22
|
+
"exports": {
|
|
23
|
+
"require": "./dist/index.umd.cjs",
|
|
24
|
+
"import": "./dist/index.js"
|
|
25
|
+
},
|
|
27
26
|
"files": [
|
|
28
|
-
"src",
|
|
29
27
|
"dist"
|
|
30
28
|
],
|
|
29
|
+
"homepage": "https://github.com/scalar/scalar",
|
|
30
|
+
"keywords": [
|
|
31
|
+
"api",
|
|
32
|
+
"documentation",
|
|
33
|
+
"fastify",
|
|
34
|
+
"openapi",
|
|
35
|
+
"swagger"
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT",
|
|
31
38
|
"main": "./dist/index.umd.cjs",
|
|
32
39
|
"module": "./dist/index.js",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/scalar/scalar.git",
|
|
43
|
+
"directory": "packages/fastify-api-reference"
|
|
37
44
|
},
|
|
45
|
+
"type": "module",
|
|
38
46
|
"types": "dist/index.d.ts",
|
|
39
|
-
"dependencies": {
|
|
40
|
-
"fastify": "^4.22.1"
|
|
41
|
-
},
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"nodemon": "^3.0.1",
|
|
44
|
-
"tsc-alias": "1.8.6",
|
|
45
|
-
"typescript": "^5.2.2",
|
|
46
|
-
"vite": "4.4.8",
|
|
47
|
-
"vite-node": "^0.34.3",
|
|
48
|
-
"vitest": "0.34.1"
|
|
49
|
-
},
|
|
50
47
|
"scripts": {
|
|
51
48
|
"build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",
|
|
52
49
|
"lint:check": "eslint .",
|
package/src/esm.test.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import Fastify from 'fastify'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import fastifyApiReference from '../dist/index'
|
|
5
|
-
|
|
6
|
-
describe('esm base support', () => {
|
|
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
|
-
})
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import Fastify from 'fastify'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import fastifyApiReference from './index'
|
|
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
|
-
})
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { type FastifyPluginCallback, type RegisterOptions } from 'fastify'
|
|
2
|
-
|
|
3
|
-
type ApiReferenceOptions = {
|
|
4
|
-
title?: string
|
|
5
|
-
specUrl?: string
|
|
6
|
-
spec?: Record<string, any>
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type FastifyApiReferenceOptions = FastifyPluginCallback<
|
|
10
|
-
Partial<RegisterOptions> & {
|
|
11
|
-
apiReference: ApiReferenceOptions
|
|
12
|
-
}
|
|
13
|
-
>
|
|
14
|
-
|
|
15
|
-
const getHtmlMarkup = (options: ApiReferenceOptions) => {
|
|
16
|
-
const htmlTag = options.specUrl
|
|
17
|
-
? `<div data-spec-url="${options.specUrl}" />`
|
|
18
|
-
: `<div data-spec='${JSON.stringify(options.spec)}' />`
|
|
19
|
-
|
|
20
|
-
return `
|
|
21
|
-
<!DOCTYPE html>
|
|
22
|
-
<html>
|
|
23
|
-
<head>
|
|
24
|
-
<title>${options.title || 'API Reference'}</title>
|
|
25
|
-
<meta charset="utf-8" />
|
|
26
|
-
<meta
|
|
27
|
-
name="viewport"
|
|
28
|
-
content="width=device-width, initial-scale=1" />
|
|
29
|
-
</head>
|
|
30
|
-
<body>
|
|
31
|
-
<!-- Add your own OpenAPI/Swagger spec file URL here: -->
|
|
32
|
-
${htmlTag}
|
|
33
|
-
<script src="https://cdn.scalar.com/api-reference.standalone.js"></script>
|
|
34
|
-
</body>
|
|
35
|
-
</html>
|
|
36
|
-
`
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const fastifyApiReference: FastifyApiReferenceOptions = (
|
|
40
|
-
fastify,
|
|
41
|
-
options,
|
|
42
|
-
done,
|
|
43
|
-
) => {
|
|
44
|
-
if (!options.apiReference.spec && !options.apiReference.specUrl) {
|
|
45
|
-
console.warn(
|
|
46
|
-
'[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.',
|
|
47
|
-
)
|
|
48
|
-
done()
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
fastify.addHook('onSend', (request, reply, payload, next) => {
|
|
52
|
-
reply.header('Content-Type', 'text/html; charset=utf-8')
|
|
53
|
-
next()
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
fastify.get('/', (_, reply) => {
|
|
57
|
-
const html = getHtmlMarkup(options?.apiReference)
|
|
58
|
-
|
|
59
|
-
reply.send(html)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
done()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export default fastifyApiReference
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './fastifyApiReference'
|