@scalar/fastify-api-reference 0.5.0 → 0.6.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 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: '/reference',
23
+ routePrefix: '/reference',
24
24
  apiReference: {
25
25
  title: 'Our API Reference',
26
26
  specUrl: '/swagger.json',
@@ -32,7 +32,7 @@ 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: '/reference',
35
+ routePrefix: '/reference',
36
36
  apiReference: {
37
37
  spec: () => fastify.swagger(),
38
38
  },
@@ -43,7 +43,7 @@ Or, if you just have a static OpenAPI spec, you can directly pass it, too:
43
43
 
44
44
  ```ts
45
45
  await fastify.register(require('@scalar/fastify-api-reference'), {
46
- prefix: '/reference',
46
+ routePrefix: '/reference',
47
47
  apiReference: {
48
48
  spec: { … },
49
49
  },
@@ -1,12 +1,17 @@
1
- import { type FastifyPluginAsync } from 'fastify';
2
- export type ApiReferencePlugin = {
3
- apiReference: ApiReferenceOptions;
4
- };
1
+ import type { FastifyPluginAsync } from 'fastify';
5
2
  export type ApiReferenceOptions = {
6
- title?: string;
7
- specUrl?: string;
8
- spec?: Record<string, any> | (() => Record<string, any>);
3
+ /**
4
+ * Prefix for the registered route
5
+ *
6
+ * @default '/'
7
+ */
8
+ routePrefix: string;
9
+ apiReference: {
10
+ specUrl?: string;
11
+ spec?: Record<string, any> | (() => Record<string, any>);
12
+ title?: string;
13
+ };
9
14
  };
10
- declare const fastifyApiReferencePlugin: FastifyPluginAsync<ApiReferencePlugin>;
11
- export default fastifyApiReferencePlugin;
15
+ declare const _default: FastifyPluginAsync<ApiReferenceOptions>;
16
+ export default _default;
12
17
  //# sourceMappingURL=fastifyApiReference.d.ts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAGjD,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE;QACZ,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;QACxD,KAAK,CAAC,EAAE,MAAM,CAAA;KACf,CAAA;CACF,CAAA;;AAoDD,wBAAsC"}
@@ -0,0 +1,41 @@
1
+ import fp from 'fastify-plugin';
2
+ const getHtmlMarkup = (options) => {
3
+ const htmlTag = options.apiReference?.specUrl
4
+ ? `<div data-spec-url="${options.apiReference?.specUrl}" />`
5
+ : typeof options.apiReference?.spec === 'object'
6
+ ? `<div data-spec='${JSON.stringify(options.apiReference?.spec)}' />`
7
+ : options.apiReference?.spec
8
+ ? `<div data-spec='${JSON.stringify(options.apiReference?.spec())}' />`
9
+ : '';
10
+ return `
11
+ <!DOCTYPE html>
12
+ <html>
13
+ <head>
14
+ <title>${options.apiReference.title || 'API Reference'}</title>
15
+ <meta charset="utf-8" />
16
+ <meta
17
+ name="viewport"
18
+ content="width=device-width, initial-scale=1" />
19
+ </head>
20
+ <body>
21
+ <!-- Add your own OpenAPI/Swagger spec file URL here: -->
22
+ ${htmlTag}
23
+ <script src="https://www.unpkg.com/@scalar/api-reference"></script>
24
+ </body>
25
+ </html>
26
+ `;
27
+ };
28
+ const fastifyApiReference = async (fastify, options) => {
29
+ if (!options.apiReference?.spec && !options.apiReference?.specUrl) {
30
+ console.warn('[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.');
31
+ return;
32
+ }
33
+ fastify.addHook('onSend', (request, reply, payload, next) => {
34
+ reply.header('Content-Type', 'text/html; charset=utf-8');
35
+ next();
36
+ });
37
+ fastify.get(options.routePrefix ?? '/', async (_, reply) => {
38
+ reply.send(getHtmlMarkup(options));
39
+ });
40
+ };
41
+ export default fp(fastifyApiReference);
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { default } from './fastifyApiReference';
1
+ export { default } from './fastifyApiReference.js';
2
2
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,36 +1 @@
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
- return `
4
- <!DOCTYPE html>
5
- <html>
6
- <head>
7
- <title>${e.title || "API Reference"}</title>
8
- <meta charset="utf-8" />
9
- <meta
10
- name="viewport"
11
- content="width=device-width, initial-scale=1" />
12
- </head>
13
- <body>
14
- <!-- Add your own OpenAPI/Swagger spec file URL here: -->
15
- ${t}
16
- <script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
17
- </body>
18
- </html>
19
- `;
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);
31
- a.send(c);
32
- });
33
- };
34
- export {
35
- l as default
36
- };
1
+ export { default } from './fastifyApiReference.js';
@@ -1,8 +1,8 @@
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`
1
+ (function(s,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(s=typeof globalThis<"u"?globalThis:s||self,s["@scalar/fastify-api-reference"]=c())})(this,function(){"use strict";function s(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var c={exports:{}},l={exports:{}};const d=/at\s{1}(?:.*\.)?plugin\s{1}.*\n\s*(.*)/,m=/(\w*(\.\w*)*)\..*/;l.exports=function(t){if(t.name.length>0)return t.name;const r=Error.stackTraceLimit;Error.stackTraceLimit=10;try{throw new Error("anonymous function")}catch(a){return Error.stackTraceLimit=r,u(a.stack)}};function u(e){const t=e.match(d);return t?t[1].split(/[/\\]/).slice(-1)[0].match(m)[1]:"anonymous"}l.exports.extractPluginName=u;var y=l.exports,g=function(t){return t[0]==="@"&&(t=t.slice(1).replace("/","-")),t.replace(/-(.)/g,function(a,i){return i.toUpperCase()})};const h=y,w=g;let x=0;function o(e,t={}){let r=!1;if(typeof e.default<"u"&&(e=e.default),typeof e!="function")throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof e}'`);if(typeof t=="string"&&(t={fastify:t}),typeof t!="object"||Array.isArray(t)||t===null)throw new TypeError("The options object should be an object");if(t.fastify!==void 0&&typeof t.fastify!="string")throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof t.fastify}'`);t.name||(r=!0,t.name=h(e)+"-auto-"+x++),e[Symbol.for("skip-override")]=t.encapsulate!==!0,e[Symbol.for("fastify.display-name")]=t.name,e[Symbol.for("plugin-meta")]=t,e.default||(e.default=e);const a=w(t.name);return!r&&!e[a]&&(e[a]=e),e}c.exports=o,c.exports.default=o,c.exports.fastifyPlugin=o;var P=c.exports;const R=s(P),v=e=>{var r,a,i,n,p,f;const t=(r=e.apiReference)!=null&&r.specUrl?`<div data-spec-url="${(a=e.apiReference)==null?void 0:a.specUrl}" />`:typeof((i=e.apiReference)==null?void 0:i.spec)=="object"?`<div data-spec='${JSON.stringify((n=e.apiReference)==null?void 0:n.spec)}' />`:(p=e.apiReference)!=null&&p.spec?`<div data-spec='${JSON.stringify((f=e.apiReference)==null?void 0:f.spec())}' />`:"";return`
2
2
  <!DOCTYPE html>
3
3
  <html>
4
4
  <head>
5
- <title>${e.title||"API Reference"}</title>
5
+ <title>${e.apiReference.title||"API Reference"}</title>
6
6
  <meta charset="utf-8" />
7
7
  <meta
8
8
  name="viewport"
@@ -11,7 +11,7 @@
11
11
  <body>
12
12
  <!-- Add your own OpenAPI/Swagger spec file URL here: -->
13
13
  ${t}
14
- <script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
14
+ <script src="https://www.unpkg.com/@scalar/api-reference"><\/script>
15
15
  </body>
16
16
  </html>
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)})}});
17
+ `};return R(async(e,t)=>{var r,a;if(!((r=t.apiReference)!=null&&r.spec)&&!((a=t.apiReference)!=null&&a.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,n,p,f)=>{n.header("Content-Type","text/html; charset=utf-8"),f()}),e.get(t.routePrefix??"/",async(i,n)=>{n.send(v(t))})})});
package/package.json CHANGED
@@ -1,12 +1,9 @@
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
- "version": "0.5.0",
4
+ "version": "0.6.0",
5
5
  "author": "Scalar (https://github.com/scalar)",
6
6
  "bugs": "https://github.com/scalar/scalar/issues/new",
7
- "dependencies": {
8
- "fastify": "4.23.2"
9
- },
10
7
  "devDependencies": {
11
8
  "@vitest/coverage-v8": "0.34.4",
12
9
  "nodemon": "3.0.1",
@@ -37,6 +34,10 @@
37
34
  "license": "MIT",
38
35
  "main": "./dist/index.umd.cjs",
39
36
  "module": "./dist/index.js",
37
+ "peerDependencies": {
38
+ "fastify": "4.23.2",
39
+ "fastify-plugin": "4.5.1"
40
+ },
40
41
  "repository": {
41
42
  "type": "git",
42
43
  "url": "https://github.com/scalar/scalar.git",