@scalar/fastify-api-reference 0.6.0 → 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.
- package/dist/fastifyApiReference.d.ts.map +1 -1
- package/dist/fastifyApiReference.js +12 -30
- package/dist/templates/fastify-api-reference.js +102544 -0
- package/dist/templates/index.ejs +25 -0
- package/dist/templates/render.d.ts +2 -0
- package/dist/templates/render.d.ts.map +1 -0
- package/dist/templates/render.js +69 -0
- package/dist/vite-plugins/index.d.ts +3 -0
- package/dist/vite-plugins/index.d.ts.map +1 -0
- package/dist/vite-plugins/index.js +2 -0
- package/dist/vite-plugins/nodeExternals.d.ts +8 -0
- package/dist/vite-plugins/nodeExternals.d.ts.map +1 -0
- package/dist/vite-plugins/nodeExternals.js +14 -0
- package/dist/vite-plugins/nodeShims.d.ts +6 -0
- package/dist/vite-plugins/nodeShims.d.ts.map +1 -0
- package/dist/vite-plugins/nodeShims.js +32 -0
- package/package.json +19 -6
- package/dist/index.umd.cjs +0 -17
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title><%= options.apiReference.title || 'API Reference' %></title>
|
|
5
|
+
<meta charset="utf-8" />
|
|
6
|
+
<meta
|
|
7
|
+
name="viewport"
|
|
8
|
+
content="width=device-width, initial-scale=1" />
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<% if (options.apiReference?.specUrl) { %>
|
|
12
|
+
<script id="api-reference" data-url="<%= options.apiReference?.specUrl %>"></script>
|
|
13
|
+
<% } else if (typeof options.apiReference?.spec === 'object') { %>
|
|
14
|
+
<script id="api-reference" type="application/json">
|
|
15
|
+
<%- JSON.stringify(options.apiReference?.spec) %>
|
|
16
|
+
</script>
|
|
17
|
+
<% } else if (options.apiReference?.spec) { %>
|
|
18
|
+
<script id="api-reference" type="application/json">
|
|
19
|
+
<%- JSON.stringify(options.apiReference?.spec()) %>
|
|
20
|
+
</script>
|
|
21
|
+
<% } %>
|
|
22
|
+
|
|
23
|
+
<script type="module" src="<%= options.routePrefix %>/fastify-api-reference.js"></script>
|
|
24
|
+
</body>
|
|
25
|
+
</html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../../src/templates/render.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ApiReference } from '@scalar/api-reference';
|
|
2
|
+
import { createApp } from 'vue';
|
|
3
|
+
const specScriptTag = document.querySelector('#api-reference');
|
|
4
|
+
const specElement = document.querySelector('[data-spec]');
|
|
5
|
+
const specUrlElement = document.querySelector('[data-spec-url]');
|
|
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;');
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const properties = getSpec()
|
|
47
|
+
? {
|
|
48
|
+
spec: getSpec(),
|
|
49
|
+
}
|
|
50
|
+
: {
|
|
51
|
+
specUrl: getSpecUrl(),
|
|
52
|
+
};
|
|
53
|
+
document.querySelector('body')?.classList.add('light-mode');
|
|
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
|
+
}
|
|
69
|
+
}
|
|
@@ -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,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 @@
|
|
|
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,23 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scalar/fastify-api-reference",
|
|
3
|
-
"description": "a fastify plugin to render an API reference from
|
|
4
|
-
"version": "0.6.
|
|
3
|
+
"description": "a fastify plugin to render an API reference from a Swagger spec",
|
|
4
|
+
"version": "0.6.2",
|
|
5
5
|
"author": "Scalar (https://github.com/scalar)",
|
|
6
6
|
"bugs": "https://github.com/scalar/scalar/issues/new",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"ejs": "3.1.9",
|
|
9
|
+
"@scalar/api-reference": "0.6.3"
|
|
10
|
+
},
|
|
7
11
|
"devDependencies": {
|
|
12
|
+
"@vitejs/plugin-vue": "4.3.4",
|
|
8
13
|
"@vitest/coverage-v8": "0.34.4",
|
|
14
|
+
"magic-string": "0.30.4",
|
|
9
15
|
"nodemon": "3.0.1",
|
|
16
|
+
"rollup-plugin-node-externals": "6.1.1",
|
|
10
17
|
"tsc-alias": "1.8.8",
|
|
11
18
|
"typescript": "5.2.2",
|
|
12
19
|
"vite": "4.4.9",
|
|
13
20
|
"vite-node": "0.34.4",
|
|
21
|
+
"vite-plugin-css-injected-by-js": "3.3.0",
|
|
22
|
+
"vite-plugin-node-polyfills": "0.14.1",
|
|
23
|
+
"vite-plugin-singlefile": "0.13.5",
|
|
24
|
+
"vite-plugin-static-copy": "0.17.0",
|
|
14
25
|
"vitest": "0.34.4"
|
|
15
26
|
},
|
|
16
27
|
"engines": {
|
|
17
28
|
"node": ">=18"
|
|
18
29
|
},
|
|
19
30
|
"exports": {
|
|
20
|
-
"require": "./dist/index.umd.cjs",
|
|
21
31
|
"import": "./dist/index.js"
|
|
22
32
|
},
|
|
23
33
|
"files": [
|
|
@@ -35,8 +45,10 @@
|
|
|
35
45
|
"main": "./dist/index.umd.cjs",
|
|
36
46
|
"module": "./dist/index.js",
|
|
37
47
|
"peerDependencies": {
|
|
48
|
+
"@types/ejs": "3.1.3",
|
|
38
49
|
"fastify": "4.23.2",
|
|
39
|
-
"fastify-plugin": "4.5.1"
|
|
50
|
+
"fastify-plugin": "4.5.1",
|
|
51
|
+
"vue": "3.3.4"
|
|
40
52
|
},
|
|
41
53
|
"repository": {
|
|
42
54
|
"type": "git",
|
|
@@ -46,10 +58,11 @@
|
|
|
46
58
|
"type": "module",
|
|
47
59
|
"types": "dist/index.d.ts",
|
|
48
60
|
"scripts": {
|
|
49
|
-
"build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",
|
|
61
|
+
"build": "vite build && pnpm run build:template && pnpm types:build && tsc-alias -p tsconfig.build.json",
|
|
62
|
+
"build:template": "vite build -c vite.template.config.ts",
|
|
50
63
|
"lint:check": "eslint .",
|
|
51
64
|
"lint:fix": "eslint . --fix",
|
|
52
|
-
"test": "
|
|
65
|
+
"test": "vitest",
|
|
53
66
|
"types:build": "tsc -p tsconfig.build.json",
|
|
54
67
|
"types:check": "tsc --noEmit --skipLibCheck"
|
|
55
68
|
}
|
package/dist/index.umd.cjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
<!DOCTYPE html>
|
|
3
|
-
<html>
|
|
4
|
-
<head>
|
|
5
|
-
<title>${e.apiReference.title||"API Reference"}</title>
|
|
6
|
-
<meta charset="utf-8" />
|
|
7
|
-
<meta
|
|
8
|
-
name="viewport"
|
|
9
|
-
content="width=device-width, initial-scale=1" />
|
|
10
|
-
</head>
|
|
11
|
-
<body>
|
|
12
|
-
<!-- Add your own OpenAPI/Swagger spec file URL here: -->
|
|
13
|
-
${t}
|
|
14
|
-
<script src="https://www.unpkg.com/@scalar/api-reference"><\/script>
|
|
15
|
-
</body>
|
|
16
|
-
</html>
|
|
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))})})});
|