@scalar/fastify-api-reference 0.8.1 → 0.8.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @scalar/fastify-api-reference
2
2
 
3
+ ## 0.8.3
4
+
5
+ ### Patch Changes
6
+
7
+ - fc540bf: chore: make routePrefix optional
8
+ - fc540bf: chore: use routePrefix for JavaScript file
9
+
10
+ ## 0.8.2
11
+
12
+ ### Patch Changes
13
+
14
+ - aa699af: fix: common js build
15
+
3
16
  ## 0.8.1
4
17
 
5
18
  ### Patch Changes
@@ -4,9 +4,9 @@ export type FastifyApiReferenceOptions = {
4
4
  /**
5
5
  * Prefix for the registered route
6
6
  *
7
- * @default '/'
7
+ * @default ''
8
8
  */
9
- routePrefix: string;
9
+ routePrefix?: string;
10
10
  configuration?: ReferenceConfiguration;
11
11
  };
12
12
  /**
@@ -16,11 +16,11 @@ export declare const defaultCss = "\n:root {\n --theme-font: 'Inter', var(--sys
16
16
  /**
17
17
  * The HTML to load the @scalar/api-reference JavaScript package.
18
18
  */
19
- export declare const javascript: (configuration: ReferenceConfiguration) => string;
19
+ export declare const javascript: (options: FastifyApiReferenceOptions) => string;
20
20
  /**
21
21
  * The HTML template to render the API Reference.
22
22
  */
23
- export declare function htmlDocument(configuration: ReferenceConfiguration): string;
23
+ export declare function htmlDocument(options: FastifyApiReferenceOptions): string;
24
24
  declare const _default: FastifyPluginAsync<FastifyApiReferenceOptions>;
25
25
  export default _default;
26
26
  //# sourceMappingURL=fastifyApiReference.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AACnE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAOjD,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;OAIG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB,aAAa,CAAC,EAAE,sBAAsB,CAAA;CACvC,CAAA;AAQD;;GAEG;AACH,eAAO,MAAM,UAAU,i8EA+EtB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,UAAU,kBAAmB,sBAAsB,WAgB/D,CAAA;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,aAAa,EAAE,sBAAsB,UAqBjE;;AAmFD,wBAAsC"}
1
+ {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AACnE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAMjD,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,aAAa,CAAC,EAAE,sBAAsB,CAAA;CACvC,CAAA;AAcD;;GAEG;AACH,eAAO,MAAM,UAAU,i8EA+EtB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,UAAU,YAAa,0BAA0B,WAoB7D,CAAA;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,0BAA0B,UAqB/D;;AAuFD,wBAAsC"}
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.create,Object.defineProperty,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.prototype.hasOwnProperty;const e=require("fastify-plugin"),n=require("node:fs"),t=require("node:path");const o={hide:!0},r=e=>`${e??""}/@scalar/fastify-api-reference/js/browser.js`.replace(/\/\//g,"/");function c(e){return`\n<!DOCTYPE html>\n<html>\n <head>\n <title>API Reference</title>\n <meta charset="utf-8" />\n <meta\n name="viewport"\n content="width=device-width, initial-scale=1" />\n <style>\n body {\n margin: 0;\n }\n </style>\n </head>\n <body>\n ${(e=>{var n,t,o,c;const{configuration:a}=e;return console.log(e),`\n <script\n id="api-reference"\n type="application/json"\n data-configuration="${JSON.stringify(a??{}).split('"').join("&quot;")}">${(null==(n=null==a?void 0:a.spec)?void 0:n.content)?"function"==typeof(null==(t=null==a?void 0:a.spec)?void 0:t.content)?JSON.stringify(null==(o=null==a?void 0:a.spec)?void 0:o.content()):JSON.stringify(null==(c=null==a?void 0:a.spec)?void 0:c.content):""}<\/script>\n <script src="${r(e.routePrefix)}"><\/script>\n `})(e)}\n </body>\n</html>\n`}const a=e((async(e,a)=>{var l,s;let{configuration:i}=a;const h=e.hasPlugin("@fastify/swagger");if(e.hasPlugin("fastify-html")||await e.register(import("fastify-html")),!(null==(l=null==i?void 0:i.spec)?void 0:l.content)&&!(null==(s=null==i?void 0:i.spec)?void 0:s.url)&&!h)return void e.log.warn("[@scalar/fastify-api-reference] You didn’t provide a spec.content or spec.url and @fastify/swagger could not be find either. Please provide one of these options.");const d=function(){const e=[t.resolve(`${__dirname}/js/standalone.js`),t.resolve(`${__dirname}/../../dist/js/standalone.js`)].find((e=>n.existsSync(e)));if(void 0===e)throw new Error(`JavaScript file not found: ${t.resolve(`${__dirname}/js/standalone.js`)}`);return n.readFileSync(e,"utf8")}();e.route({method:"GET",url:a.routePrefix??"/",schema:o,async handler(n,t){var o,r;return t.header("Content-Type","text/html; charset=utf-8"),(null==(o=null==i?void 0:i.spec)?void 0:o.content)||(null==(r=null==i?void 0:i.spec)?void 0:r.url)||!h||(i={...i,spec:{content:()=>e.swagger()}}),(null==i?void 0:i.customCss)||(null==i?void 0:i.theme)||(i={...i,customCss:"\n:root {\n --theme-font: 'Inter', var(--system-fonts);\n}\n\n.light-mode {\n color-scheme: light;\n --theme-color-1: #1c1e21;\n --theme-color-2: #757575;\n --theme-color-3: #8e8e8e;\n --theme-color-disabled: #b4b1b1;\n --theme-color-ghost: #a7a7a7;\n --theme-color-accent: #2f8555;\n --theme-background-1: #fff;\n --theme-background-2: #f5f5f5;\n --theme-background-3: #ededed;\n --theme-background-4: rgba(0, 0, 0, 0.06);\n --theme-background-accent: #2f85551f;\n\n --theme-border-color: rgba(0, 0, 0, 0.1);\n --theme-scrollbar-color: rgba(0, 0, 0, 0.18);\n --theme-scrollbar-color-active: rgba(0, 0, 0, 0.36);\n --theme-lifted-brightness: 1;\n --theme-backdrop-brightness: 1;\n\n --theme-shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.11);\n --theme-shadow-2: rgba(0, 0, 0, 0.08) 0px 13px 20px 0px,\n rgba(0, 0, 0, 0.08) 0px 3px 8px 0px, #eeeeed 0px 0 0 1px;\n\n --theme-button-1: rgb(49 53 56);\n --theme-button-1-color: #fff;\n --theme-button-1-hover: rgb(28 31 33);\n\n --theme-color-green: #007300;\n --theme-color-red: #af272b;\n --theme-color-yellow: #b38200;\n --theme-color-blue: #3b8ba5;\n --theme-color-orange: #fb892c;\n --theme-color-purple: #5203d1;\n}\n\n.dark-mode {\n color-scheme: dark;\n --theme-color-1: rgba(255, 255, 255, 0.9);\n --theme-color-2: rgba(255, 255, 255, 0.62);\n --theme-color-3: rgba(255, 255, 255, 0.44);\n --theme-color-disabled: rgba(255, 255, 255, 0.34);\n --theme-color-ghost: rgba(255, 255, 255, 0.26);\n --theme-color-accent: #27c2a0;\n --theme-background-1: #1b1b1d;\n --theme-background-2: #242526;\n --theme-background-3: #3b3b3b;\n --theme-background-4: rgba(255, 255, 255, 0.06);\n --theme-background-accent: #27c2a01f;\n\n --theme-border-color: rgba(255, 255, 255, 0.1);\n --theme-scrollbar-color: rgba(255, 255, 255, 0.24);\n --theme-scrollbar-color-active: rgba(255, 255, 255, 0.48);\n --theme-lifted-brightness: 1.45;\n --theme-backdrop-brightness: 0.5;\n\n --theme-shadow-1: 0 1px 3px 0 rgb(0, 0, 0, 0.1);\n --theme-shadow-2: rgba(15, 15, 15, 0.2) 0px 3px 6px,\n rgba(15, 15, 15, 0.4) 0px 9px 24px, 0 0 0 1px rgba(255, 255, 255, 0.1);\n\n --theme-button-1: #f6f6f6;\n --theme-button-1-color: #000;\n --theme-button-1-hover: #e7e7e7;\n\n --theme-color-green: #26b226;\n --theme-color-red: #fb565b;\n --theme-color-yellow: #ffc426;\n --theme-color-blue: #6ecfef;\n --theme-color-orange: #ff8d4d;\n --theme-color-purple: #b191f9;\n}\n.scalar-card:nth-of-type(3) {\n display: none;\n}\n"}),t.html`${c({...a,configuration:i})}`}}),e.route({method:"GET",url:r(a.routePrefix),schema:o,async handler(e,n){n.header("Content-Type","application/javascript; charset=utf-8"),n.send(d)}})}));module.exports=a;
package/dist/index.js CHANGED
@@ -7,7 +7,6 @@ const __dirname = __getDirname();
7
7
  const __filename = __getFilename();
8
8
  const self = globalThis;
9
9
  const require2 = __createRequire(import.meta.url);
10
- import fastifyHtml from "fastify-html";
11
10
  import fp from "fastify-plugin";
12
11
  import fs from "node:fs";
13
12
  import path from "node:path";
@@ -28,6 +27,10 @@ function getJavaScriptFile() {
28
27
  const schemaToHideRoute = {
29
28
  hide: true
30
29
  };
30
+ const getJavaScriptUrl = (routePrefix) => `${routePrefix ?? ""}/@scalar/fastify-api-reference/js/browser.js`.replace(
31
+ /\/\//g,
32
+ "/"
33
+ );
31
34
  const defaultCss = `
32
35
  :root {
33
36
  --theme-font: 'Inter', var(--system-fonts);
@@ -108,17 +111,19 @@ const defaultCss = `
108
111
  display: none;
109
112
  }
110
113
  `;
111
- const javascript = (configuration) => {
114
+ const javascript = (options) => {
112
115
  var _a, _b, _c, _d;
116
+ const { configuration } = options;
117
+ console.log(options);
113
118
  return `
114
119
  <script
115
120
  id="api-reference"
116
121
  type="application/json"
117
- data-configuration="${JSON.stringify(configuration).split('"').join("&quot;")}">${((_a = configuration.spec) == null ? void 0 : _a.content) ? typeof ((_b = configuration.spec) == null ? void 0 : _b.content) === "function" ? JSON.stringify((_c = configuration.spec) == null ? void 0 : _c.content()) : JSON.stringify((_d = configuration.spec) == null ? void 0 : _d.content) : ""}<\/script>
118
- <script src="/@scalar/fastify-api-reference/browser.js"><\/script>
122
+ data-configuration="${JSON.stringify(configuration ?? {}).split('"').join("&quot;")}">${((_a = configuration == null ? void 0 : configuration.spec) == null ? void 0 : _a.content) ? typeof ((_b = configuration == null ? void 0 : configuration.spec) == null ? void 0 : _b.content) === "function" ? JSON.stringify((_c = configuration == null ? void 0 : configuration.spec) == null ? void 0 : _c.content()) : JSON.stringify((_d = configuration == null ? void 0 : configuration.spec) == null ? void 0 : _d.content) : ""}<\/script>
123
+ <script src="${getJavaScriptUrl(options.routePrefix)}"><\/script>
119
124
  `;
120
125
  };
121
- function htmlDocument(configuration) {
126
+ function htmlDocument(options) {
122
127
  return `
123
128
  <!DOCTYPE html>
124
129
  <html>
@@ -135,7 +140,7 @@ function htmlDocument(configuration) {
135
140
  </style>
136
141
  </head>
137
142
  <body>
138
- ${javascript(configuration)}
143
+ ${javascript(options)}
139
144
  </body>
140
145
  </html>
141
146
  `;
@@ -145,7 +150,7 @@ const fastifyApiReference = async (fastify, options) => {
145
150
  let { configuration } = options;
146
151
  const hasSwaggerPlugin = fastify.hasPlugin("@fastify/swagger");
147
152
  if (!fastify.hasPlugin("fastify-html")) {
148
- await fastify.register(fastifyHtml);
153
+ await fastify.register(import("fastify-html"));
149
154
  }
150
155
  if (!((_a = configuration == null ? void 0 : configuration.spec) == null ? void 0 : _a.content) && !((_b = configuration == null ? void 0 : configuration.spec) == null ? void 0 : _b.url) && !hasSwaggerPlugin) {
151
156
  fastify.log.warn(
@@ -179,12 +184,15 @@ const fastifyApiReference = async (fastify, options) => {
179
184
  customCss: defaultCss
180
185
  };
181
186
  }
182
- return reply.html`${htmlDocument(configuration)}`;
187
+ return reply.html`${htmlDocument({
188
+ ...options,
189
+ configuration
190
+ })}`;
183
191
  }
184
192
  });
185
193
  fastify.route({
186
194
  method: "GET",
187
- url: "/@scalar/fastify-api-reference/browser.js",
195
+ url: getJavaScriptUrl(options.routePrefix),
188
196
  // We don’t know whether @fastify/swagger is registered, but it doesn’t hurt to add a schema anyway.
189
197
  // @ts-ignore
190
198
  schema: schemaToHideRoute,
package/package.json CHANGED
@@ -12,15 +12,16 @@
12
12
  "openapi",
13
13
  "swagger"
14
14
  ],
15
- "version": "0.8.1",
15
+ "version": "0.8.3",
16
16
  "engines": {
17
17
  "node": ">=20"
18
18
  },
19
19
  "type": "module",
20
- "main": "./dist/index.js",
20
+ "main": "./dist/index.cjs",
21
21
  "types": "dist/index.d.ts",
22
22
  "exports": {
23
- "import": "./dist/index.js"
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
24
25
  },
25
26
  "files": [
26
27
  "dist",
@@ -33,20 +34,14 @@
33
34
  "directory": "packages/fastify-api-reference"
34
35
  },
35
36
  "devDependencies": {
36
- "@rollup/plugin-terser": "^0.4.4",
37
- "@types/ejs": "^3.1.3",
38
- "@vitejs/plugin-vue": "^4.4.0",
39
37
  "@vitest/coverage-v8": "^0.34.4",
40
38
  "fastify-html": "^0.2.0",
41
39
  "magic-string": "^0.30.4",
42
- "nodemon": "^3.0.1",
43
40
  "rollup-plugin-node-externals": "^6.1.1",
44
41
  "terser": "^5.24.0",
45
42
  "tsc-alias": "^1.8.8",
46
43
  "typescript": "^5.2.2",
47
44
  "vite": "^4.4.11",
48
- "vite-node": "^0.34.4",
49
- "vite-plugin-css-injected-by-js": "^3.3.0",
50
45
  "vite-plugin-node-polyfills": "^0.14.1",
51
46
  "vite-plugin-static-copy": "^0.17.0",
52
47
  "vitest": "^0.34.4",