@scalar/fastify-api-reference 0.6.1 → 0.6.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.
@@ -9,11 +9,15 @@
9
9
  </head>
10
10
  <body>
11
11
  <% if (options.apiReference?.specUrl) { %>
12
- <div data-spec-url="<%= options.apiReference?.specUrl %>" />
12
+ <script id="api-reference" data-url="<%= options.apiReference?.specUrl %>"></script>
13
13
  <% } else if (typeof options.apiReference?.spec === 'object') { %>
14
- <div data-spec='<%= JSON.stringify(options.apiReference?.spec) %>' />
14
+ <script id="api-reference" type="application/json">
15
+ <%- JSON.stringify(options.apiReference?.spec) %>
16
+ </script>
15
17
  <% } else if (options.apiReference?.spec) { %>
16
- <div data-spec='<%= JSON.stringify(options.apiReference?.spec()) %>' />
18
+ <script id="api-reference" type="application/json">
19
+ <%- JSON.stringify(options.apiReference?.spec()) %>
20
+ </script>
17
21
  <% } %>
18
22
 
19
23
  <script type="module" src="<%= options.routePrefix %>/fastify-api-reference.js"></script>
@@ -1,19 +1,69 @@
1
1
  import { ApiReference } from '@scalar/api-reference';
2
2
  import { createApp } from 'vue';
3
+ const specScriptTag = document.querySelector('#api-reference');
3
4
  const specElement = document.querySelector('[data-spec]');
4
5
  const specUrlElement = document.querySelector('[data-spec-url]');
5
- if (!specUrlElement && !specElement) {
6
- console.error('Couldn’t find a [data-spec] or [data-spec-url] element. Try adding it like this: %c<div data-spec-url="https://petstore.swagger.io/v2/swagger.json" />', 'font-family: monospace;');
6
+ const getSpecUrl = () => {
7
+ // <script id="api-reference" data-url="/scalar.json" />
8
+ if (specScriptTag) {
9
+ const urlFromScriptTag = specScriptTag.getAttribute('data-url');
10
+ if (urlFromScriptTag) {
11
+ return urlFromScriptTag;
12
+ }
13
+ }
14
+ // <div data-spec-url="/scalar.json" />
15
+ if (specUrlElement) {
16
+ console.warn('[@scalar/api-reference] The [data-spec-url] HTML API is deprecated. Use the new <script id="api-reference" data-url="/scalar.json" /> API instead.');
17
+ const urlFromSpecUrlElement = specUrlElement.getAttribute('data-spec-url');
18
+ if (urlFromSpecUrlElement) {
19
+ return urlFromSpecUrlElement;
20
+ }
21
+ }
22
+ return null;
23
+ };
24
+ const getSpec = () => {
25
+ // <script id="api-reference" type="application/json">{"openapi":"3.1.0","info":{"title":"Example"},"paths":{}}</script>
26
+ if (specScriptTag) {
27
+ const specFromScriptTag = specScriptTag.innerHTML;
28
+ if (specFromScriptTag) {
29
+ return specFromScriptTag.trim();
30
+ }
31
+ }
32
+ // <div data-spec='{"openapi":"3.1.0","info":{"title":"Example"},"paths":{}}' />
33
+ if (specElement) {
34
+ console.warn('[@scalar/api-reference] The [data-spec] HTML API is deprecated. Use the new <script id="api-reference" type="application/json">{"openapi":"3.1.0","info":{"title":"Example"},"paths":{}}</script> API instead.');
35
+ const specFromSpecElement = specElement.getAttribute('data-spec');
36
+ if (specFromSpecElement) {
37
+ return specFromSpecElement;
38
+ }
39
+ }
40
+ return null;
41
+ };
42
+ if (!specUrlElement && !specElement && !specScriptTag) {
43
+ console.error('Couldn’t find a [data-spec], [data-spec-url] or <script id="api-reference" /> element. Try adding it like this: %c<div data-spec-url="https://petstore.swagger.io/v2/swagger.json" />', 'font-family: monospace;');
7
44
  }
8
45
  else {
9
- const properties = specElement
46
+ const properties = getSpec()
10
47
  ? {
11
- spec: specElement.getAttribute('data-spec'),
48
+ spec: getSpec(),
12
49
  }
13
50
  : {
14
- specUrl: specUrlElement?.getAttribute('data-spec-url') ?? '',
51
+ specUrl: getSpecUrl(),
15
52
  };
16
53
  document.querySelector('body')?.classList.add('light-mode');
17
- const container = specElement ? '[data-spec]' : '[data-spec-url]';
18
- createApp(ApiReference, properties).mount(container);
54
+ // If it’s a script tag, we can’t mount the Vue.js app inside that tag.
55
+ // We need to add a new container div before the script tag.
56
+ if (specScriptTag) {
57
+ const container = document.createElement('div');
58
+ specScriptTag?.parentNode?.insertBefore(container, specScriptTag);
59
+ createApp(ApiReference, properties).mount(container);
60
+ }
61
+ else {
62
+ const container = specElement
63
+ ? '[data-spec]'
64
+ : specUrlElement
65
+ ? '[data-spec-url]'
66
+ : 'body';
67
+ createApp(ApiReference, properties).mount(container);
68
+ }
19
69
  }
@@ -0,0 +1,3 @@
1
+ export * from './nodeExternals.js';
2
+ export * from './nodeShims.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/vite-plugins/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './nodeExternals.js';
2
+ export * from './nodeShims.js';
@@ -0,0 +1,8 @@
1
+ import { type Plugin } from 'vite';
2
+ /**
3
+ * Here, to simplify, we use the existing rollup-plugin-node-externals plugin. It can exclude node dependencies and
4
+ * will automatically exclude based on the dependencies and devDependencies in package.json. However, some minor
5
+ * compatibility modifications for Vite are needed.
6
+ */
7
+ export declare const nodeExternals: () => Plugin;
8
+ //# sourceMappingURL=nodeExternals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodeExternals.d.ts","sourceRoot":"","sources":["../../src/vite-plugins/nodeExternals.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC;;;;GAIG;AACH,eAAO,MAAM,aAAa,QAAO,MAOhC,CAAA"}
@@ -0,0 +1,14 @@
1
+ import { nodeExternals as RollupNodeExternals } from 'rollup-plugin-node-externals';
2
+ /**
3
+ * Here, to simplify, we use the existing rollup-plugin-node-externals plugin. It can exclude node dependencies and
4
+ * will automatically exclude based on the dependencies and devDependencies in package.json. However, some minor
5
+ * compatibility modifications for Vite are needed.
6
+ */
7
+ export const nodeExternals = () => {
8
+ return {
9
+ ...RollupNodeExternals(),
10
+ name: 'node-externals',
11
+ enforce: 'pre',
12
+ apply: 'build',
13
+ };
14
+ };
@@ -0,0 +1,6 @@
1
+ import { type Plugin } from 'vite';
2
+ /**
3
+ * Build-time polyfill for cjs features
4
+ */
5
+ export declare const nodeShims: () => Plugin;
6
+ //# sourceMappingURL=nodeShims.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodeShims.d.ts","sourceRoot":"","sources":["../../src/vite-plugins/nodeShims.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC;;GAEG;AACH,eAAO,MAAM,SAAS,QAAO,MA8B5B,CAAA"}
@@ -0,0 +1,32 @@
1
+ import MagicString from 'magic-string';
2
+ /**
3
+ * Build-time polyfill for cjs features
4
+ */
5
+ export const nodeShims = () => {
6
+ return {
7
+ name: 'node-shims',
8
+ renderChunk(code, chunk) {
9
+ if (!chunk.fileName.endsWith('.js')) {
10
+ return;
11
+ }
12
+ const s = new MagicString(code);
13
+ s.prepend(`
14
+ import __path from 'path'
15
+ import { fileURLToPath as __fileURLToPath } from 'url'
16
+ import { createRequire as __createRequire } from 'module'
17
+
18
+ const __getFilename = () => __fileURLToPath(import.meta.url)
19
+ const __getDirname = () => __path.dirname(__getFilename())
20
+ const __dirname = __getDirname()
21
+ const __filename = __getFilename()
22
+ const self = globalThis
23
+ const require = __createRequire(import.meta.url)
24
+ `);
25
+ return {
26
+ code: s.toString(),
27
+ map: s.generateMap(),
28
+ };
29
+ },
30
+ apply: 'build',
31
+ };
32
+ };
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "name": "@scalar/fastify-api-reference",
3
3
  "description": "a fastify plugin to render an API reference from a Swagger spec",
4
- "version": "0.6.1",
4
+ "version": "0.6.2",
5
5
  "author": "Scalar (https://github.com/scalar)",
6
6
  "bugs": "https://github.com/scalar/scalar/issues/new",
7
7
  "dependencies": {
8
8
  "ejs": "3.1.9",
9
- "@scalar/api-reference": "0.6.2"
9
+ "@scalar/api-reference": "0.6.3"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@vitejs/plugin-vue": "4.3.4",
13
13
  "@vitest/coverage-v8": "0.34.4",
14
+ "magic-string": "0.30.4",
14
15
  "nodemon": "3.0.1",
16
+ "rollup-plugin-node-externals": "6.1.1",
15
17
  "tsc-alias": "1.8.8",
16
18
  "typescript": "5.2.2",
17
19
  "vite": "4.4.9",