@scalar/fastify-api-reference 0.6.2 → 0.6.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/dist/index.cjs +37 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +47 -1
- package/package.json +5 -4
- package/dist/fastifyApiReference.js +0 -23
- package/dist/templates/render.d.ts +0 -2
- package/dist/templates/render.d.ts.map +0 -1
- package/dist/templates/render.js +0 -69
- package/dist/vite-plugins/index.d.ts +0 -3
- package/dist/vite-plugins/index.d.ts.map +0 -1
- package/dist/vite-plugins/index.js +0 -2
- package/dist/vite-plugins/nodeExternals.d.ts +0 -8
- package/dist/vite-plugins/nodeExternals.d.ts.map +0 -1
- package/dist/vite-plugins/nodeExternals.js +0 -14
- package/dist/vite-plugins/nodeShims.d.ts +0 -6
- package/dist/vite-plugins/nodeShims.d.ts.map +0 -1
- package/dist/vite-plugins/nodeShims.js +0 -32
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const ejs = require("ejs");
|
|
3
|
+
const fp = require("fastify-plugin");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const fastifyApiReference = async (fastify, options) => {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
if (!((_a = options.apiReference) == null ? void 0 : _a.spec) && !((_b = options.apiReference) == null ? void 0 : _b.specUrl)) {
|
|
9
|
+
console.warn(
|
|
10
|
+
"[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."
|
|
11
|
+
);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
fastify.get(options.routePrefix ?? "/", async (_, reply) => {
|
|
15
|
+
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
16
|
+
const html = await ejs.renderFile(
|
|
17
|
+
path.resolve(`${__dirname}/../dist/templates/index.ejs`),
|
|
18
|
+
{
|
|
19
|
+
options
|
|
20
|
+
}
|
|
21
|
+
);
|
|
22
|
+
reply.send(html);
|
|
23
|
+
});
|
|
24
|
+
fastify.get(
|
|
25
|
+
(options.routePrefix ?? "/") + "/fastify-api-reference.js",
|
|
26
|
+
async (_, reply) => {
|
|
27
|
+
reply.header("Content-Type", "application/javascript; charset=utf-8");
|
|
28
|
+
const content = fs.readFileSync(
|
|
29
|
+
path.resolve(`${__dirname}/../dist/templates/fastify-api-reference.js`),
|
|
30
|
+
"utf8"
|
|
31
|
+
);
|
|
32
|
+
reply.send(content);
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
const fastifyApiReference$1 = fp(fastifyApiReference);
|
|
37
|
+
module.exports = fastifyApiReference$1;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { default } from './fastifyApiReference
|
|
1
|
+
export { default } from './fastifyApiReference';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1 +1,47 @@
|
|
|
1
|
-
|
|
1
|
+
import __path from "path";
|
|
2
|
+
import { fileURLToPath as __fileURLToPath } from "url";
|
|
3
|
+
import { createRequire as __createRequire } from "module";
|
|
4
|
+
const __getFilename = () => __fileURLToPath(import.meta.url);
|
|
5
|
+
const __getDirname = () => __path.dirname(__getFilename());
|
|
6
|
+
const __dirname = __getDirname();
|
|
7
|
+
const __filename = __getFilename();
|
|
8
|
+
const self = globalThis;
|
|
9
|
+
const require2 = __createRequire(import.meta.url);
|
|
10
|
+
import ejs from "ejs";
|
|
11
|
+
import fp from "fastify-plugin";
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
const fastifyApiReference = async (fastify, options) => {
|
|
15
|
+
var _a, _b;
|
|
16
|
+
if (!((_a = options.apiReference) == null ? void 0 : _a.spec) && !((_b = options.apiReference) == null ? void 0 : _b.specUrl)) {
|
|
17
|
+
console.warn(
|
|
18
|
+
"[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."
|
|
19
|
+
);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
fastify.get(options.routePrefix ?? "/", async (_, reply) => {
|
|
23
|
+
reply.header("Content-Type", "text/html; charset=utf-8");
|
|
24
|
+
const html = await ejs.renderFile(
|
|
25
|
+
path.resolve(`${__dirname}/../dist/templates/index.ejs`),
|
|
26
|
+
{
|
|
27
|
+
options
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
reply.send(html);
|
|
31
|
+
});
|
|
32
|
+
fastify.get(
|
|
33
|
+
(options.routePrefix ?? "/") + "/fastify-api-reference.js",
|
|
34
|
+
async (_, reply) => {
|
|
35
|
+
reply.header("Content-Type", "application/javascript; charset=utf-8");
|
|
36
|
+
const content = fs.readFileSync(
|
|
37
|
+
path.resolve(`${__dirname}/../dist/templates/fastify-api-reference.js`),
|
|
38
|
+
"utf8"
|
|
39
|
+
);
|
|
40
|
+
reply.send(content);
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
const fastifyApiReference$1 = fp(fastifyApiReference);
|
|
45
|
+
export {
|
|
46
|
+
fastifyApiReference$1 as default
|
|
47
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.
|
|
4
|
+
"version": "0.6.3",
|
|
5
5
|
"author": "Scalar (https://github.com/scalar)",
|
|
6
6
|
"bugs": "https://github.com/scalar/scalar/issues/new",
|
|
7
7
|
"dependencies": {
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"node": ">=18"
|
|
29
29
|
},
|
|
30
30
|
"exports": {
|
|
31
|
-
"import": "./dist/index.js"
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
32
33
|
},
|
|
33
34
|
"files": [
|
|
34
35
|
"dist"
|
|
@@ -42,7 +43,7 @@
|
|
|
42
43
|
"swagger"
|
|
43
44
|
],
|
|
44
45
|
"license": "MIT",
|
|
45
|
-
"main": "./dist/index.
|
|
46
|
+
"main": "./dist/index.cjs",
|
|
46
47
|
"module": "./dist/index.js",
|
|
47
48
|
"peerDependencies": {
|
|
48
49
|
"@types/ejs": "3.1.3",
|
|
@@ -58,7 +59,7 @@
|
|
|
58
59
|
"type": "module",
|
|
59
60
|
"types": "dist/index.d.ts",
|
|
60
61
|
"scripts": {
|
|
61
|
-
"build": "vite build && pnpm run build:template &&
|
|
62
|
+
"build": "vite build && pnpm run build:template && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
62
63
|
"build:template": "vite build -c vite.template.config.ts",
|
|
63
64
|
"lint:check": "eslint .",
|
|
64
65
|
"lint:fix": "eslint . --fix",
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import ejs from 'ejs';
|
|
2
|
-
import fp from 'fastify-plugin';
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
const fastifyApiReference = async (fastify, options) => {
|
|
6
|
-
if (!options.apiReference?.spec && !options.apiReference?.specUrl) {
|
|
7
|
-
console.warn('[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.');
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
fastify.get(options.routePrefix ?? '/', async (_, reply) => {
|
|
11
|
-
reply.header('Content-Type', 'text/html; charset=utf-8');
|
|
12
|
-
const html = await ejs.renderFile(path.resolve(`${__dirname}/../dist/templates/index.ejs`), {
|
|
13
|
-
options,
|
|
14
|
-
});
|
|
15
|
-
reply.send(html);
|
|
16
|
-
});
|
|
17
|
-
fastify.get((options.routePrefix ?? '/') + '/fastify-api-reference.js', async (_, reply) => {
|
|
18
|
-
reply.header('Content-Type', 'application/javascript; charset=utf-8');
|
|
19
|
-
const content = fs.readFileSync(path.resolve(`${__dirname}/../dist/templates/fastify-api-reference.js`), 'utf8');
|
|
20
|
-
reply.send(content);
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
export default fp(fastifyApiReference);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../../src/templates/render.ts"],"names":[],"mappings":""}
|
package/dist/templates/render.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,32 +0,0 @@
|
|
|
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
|
-
};
|