@scalar/fastify-api-reference 0.4.0 → 0.4.2

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 CHANGED
@@ -1,5 +1,10 @@
1
1
  # Scalar Fastify API Reference Plugin
2
2
 
3
+ ![Version](https://img.shields.io/npm/v/%40scalar/fastify-api-reference)
4
+ ![Downloads](https://img.shields.io/npm/dm/%40scalar/fastify-api-reference)
5
+ ![License](https://img.shields.io/npm/l/%40scalar%2Ffastify-api-reference)
6
+ [![Discord](https://img.shields.io/discord/1135330207960678410?style=flat&color=5865F2)](https://discord.gg/mw6FQRPh)
7
+
3
8
  This plugin provides an easy way to render a beautiful API reference based on a OpenAPI/Swagger file with Fastify.
4
9
 
5
10
  ## Installation
@@ -1,12 +1,12 @@
1
- import { type FastifyPluginCallback, type RegisterOptions } from 'fastify';
2
- type ApiReferenceOptions = {
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
8
  spec?: Record<string, any>;
6
9
  };
7
- export type FastifyApiReferenceOptions = {
8
- apiReference: ApiReferenceOptions;
9
- } & RegisterOptions;
10
- declare const fastifyApiReference: FastifyPluginCallback<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,EAAE,KAAK,qBAAqB,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAA;AAE1E,KAAK,mBAAmB,GAAG;IACzB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,0BAA0B,GAAG;IACvC,YAAY,EAAE,mBAAmB,CAAA;CAClC,GAAG,eAAe,CAAA;AA0BnB,QAAA,MAAM,mBAAmB,EAAE,qBAAqB,CAAC,0BAA0B,CAwB1E,CAAA;AAED,eAAe,mBAAmB,CAAA"}
1
+ {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,kBAAkB,EAA6B,MAAM,SAAS,CAAA;AAE5E,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,CAAA;CAC3B,CAAA;AA0BD,QAAA,MAAM,yBAAyB,EAAE,kBAAkB,CACjD,kBAAkB,CAoBnB,CAAA;AAED,eAAe,yBAAyB,CAAA"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const s = (e) => {
1
+ const l = (e) => {
2
2
  const t = e.specUrl ? `<div data-spec-url="${e.specUrl}" />` : `<div data-spec='${JSON.stringify(e.spec)}' />`;
3
3
  return `
4
4
  <!DOCTYPE html>
@@ -17,15 +17,19 @@ const s = (e) => {
17
17
  </body>
18
18
  </html>
19
19
  `;
20
- }, n = (e, t, r) => {
21
- !t.apiReference.spec && !t.apiReference.specUrl && (console.warn(
22
- "[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."
23
- ), r()), e.addHook("onSend", (d, a, c, l) => {
24
- a.header("Content-Type", "text/html; charset=utf-8"), l();
25
- }), e.get("/", (d, a) => {
26
- const c = s(t == null ? void 0 : t.apiReference);
27
- a.send(c);
28
- }), r();
20
+ }, n = 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", (c, a, r, d) => {
28
+ a.header("Content-Type", "text/html; charset=utf-8"), d();
29
+ }), e.get("/", (c, a) => {
30
+ const r = l(t == null ? void 0 : t.apiReference);
31
+ a.send(r);
32
+ });
29
33
  };
30
34
  export {
31
35
  n as default
@@ -1,4 +1,4 @@
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=e=>{const t=e.specUrl?`<div data-spec-url="${e.specUrl}" />`:`<div data-spec='${JSON.stringify(e.spec)}' />`;return`
1
+ (function(r,n){typeof exports=="object"&&typeof module<"u"?module.exports=n():typeof define=="function"&&define.amd?define(n):(r=typeof globalThis<"u"?globalThis:r||self,r["@scalar/fastify-api-reference"]=n())})(this,function(){"use strict";const r=e=>{const t=e.specUrl?`<div data-spec-url="${e.specUrl}" />`:`<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,n)=>{!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."),n()),e.addHook("onSend",(s,a,i,d)=>{a.header("Content-Type","text/html; charset=utf-8"),d()}),e.get("/",(s,a)=>{const i=r(t==null?void 0:t.apiReference);a.send(i)}),n()}});
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",(i,c,a,s)=>{c.header("Content-Type","text/html; charset=utf-8"),s()}),e.get("/",(i,c)=>{const a=r(t==null?void 0:t.apiReference);c.send(a)})}});
package/package.json CHANGED
@@ -1,20 +1,35 @@
1
1
  {
2
2
  "name": "@scalar/fastify-api-reference",
3
- "version": "0.4.0",
4
- "author": "Scalar",
3
+ "description": "a fastify plugin to render an API reference from an automatically generated OpenAPI spec",
4
+ "keywords": [
5
+ "fastify",
6
+ "openapi",
7
+ "swagger",
8
+ "api",
9
+ "documentation"
10
+ ],
11
+ "version": "0.4.2",
12
+ "author": "Scalar (https://github.com/scalar)",
13
+ "homepage": "https://github.com/scalar/scalar",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/scalar/scalar.git",
17
+ "directory": "packages/fastify-api-reference"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/scalar/scalar/issues/new"
21
+ },
5
22
  "license": "MIT",
6
23
  "engines": {
7
24
  "node": ">=18"
8
25
  },
9
26
  "type": "module",
10
27
  "files": [
11
- "src",
12
28
  "dist"
13
29
  ],
14
30
  "main": "./dist/index.umd.cjs",
15
31
  "module": "./dist/index.js",
16
32
  "exports": {
17
- "development": "./src/index.ts",
18
33
  "require": "./dist/index.umd.cjs",
19
34
  "import": "./dist/index.js"
20
35
  },
@@ -28,7 +43,8 @@
28
43
  "typescript": "^5.2.2",
29
44
  "vite": "4.4.8",
30
45
  "vite-node": "^0.34.3",
31
- "vitest": "0.34.1"
46
+ "vitest": "0.34.1",
47
+ "@vitest/coverage-v8": "^0.34.4"
32
48
  },
33
49
  "scripts": {
34
50
  "build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",
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 './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
- })
@@ -1,63 +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 = {
10
- apiReference: ApiReferenceOptions
11
- } & RegisterOptions
12
-
13
- const getHtmlMarkup = (options: ApiReferenceOptions) => {
14
- const htmlTag = options.specUrl
15
- ? `<div data-spec-url="${options.specUrl}" />`
16
- : `<div data-spec='${JSON.stringify(options.spec)}' />`
17
-
18
- return `
19
- <!DOCTYPE html>
20
- <html>
21
- <head>
22
- <title>${options.title || 'API Reference'}</title>
23
- <meta charset="utf-8" />
24
- <meta
25
- name="viewport"
26
- content="width=device-width, initial-scale=1" />
27
- </head>
28
- <body>
29
- <!-- Add your own OpenAPI/Swagger spec file URL here: -->
30
- ${htmlTag}
31
- <script src="https://cdn.scalar.com/api-reference.standalone.js"></script>
32
- </body>
33
- </html>
34
- `
35
- }
36
-
37
- const fastifyApiReference: FastifyPluginCallback<FastifyApiReferenceOptions> = (
38
- fastify,
39
- options,
40
- done,
41
- ) => {
42
- if (!options.apiReference.spec && !options.apiReference.specUrl) {
43
- console.warn(
44
- '[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.',
45
- )
46
- done()
47
- }
48
-
49
- fastify.addHook('onSend', (request, reply, payload, next) => {
50
- reply.header('Content-Type', 'text/html; charset=utf-8')
51
- next()
52
- })
53
-
54
- fastify.get('/', (_, reply) => {
55
- const html = getHtmlMarkup(options?.apiReference)
56
-
57
- reply.send(html)
58
- })
59
-
60
- done()
61
- }
62
-
63
- export default fastifyApiReference
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export { default } from './fastifyApiReference'