@scalar/fastify-api-reference 0.2.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/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/fastifyApiReference.d.ts +11 -0
- package/dist/fastifyApiReference.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Scalar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Scalar Fastify API Reference Plugin
|
|
2
|
+
|
|
3
|
+
This plugin provides an easy way to render a beautiful API reference based on a OpenAPI/Swagger file with Fastify.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @scalar/fastify-api-reference
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
If you have a OpenAPI/Swagger file already, you can pass an URL to the plugin:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// Render an API reference for a given OpenAPI/Swagger spec URL
|
|
17
|
+
fastify.register(require('@scalar/fastify-api-reference'), {
|
|
18
|
+
prefix: '/api-reference',
|
|
19
|
+
apiReference: {
|
|
20
|
+
title: 'Our API Reference',
|
|
21
|
+
specUrl: '/swagger.json',
|
|
22
|
+
},
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
With the [@fastify/swagger](https://github.com/fastify/fastify-swagger) you can even generate your Swagger spec from the registered routes and directly pass it to the plugin:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
await fastify.register(require('@scalar/fastify-api-reference'), {
|
|
30
|
+
prefix: '/api-reference',
|
|
31
|
+
apiReference: {
|
|
32
|
+
spec: fastify.swagger(),
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type FastifyInstance, type RegisterOptions } from 'fastify';
|
|
2
|
+
export type FastifyApiReferenceOptions = {
|
|
3
|
+
title?: string;
|
|
4
|
+
specUrl?: string;
|
|
5
|
+
spec?: Record<string, any>;
|
|
6
|
+
};
|
|
7
|
+
declare const _default: (fastify: FastifyInstance, options: RegisterOptions & {
|
|
8
|
+
apiReference: FastifyApiReferenceOptions;
|
|
9
|
+
}, done: (err?: Error) => void) => void;
|
|
10
|
+
export default _default;
|
|
11
|
+
//# sourceMappingURL=fastifyApiReference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAA;AAEpE,MAAM,MAAM,0BAA0B,GAAG;IACvC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC3B,CAAA;kCA2BU,eAAe,WACf,eAAe,GAAG;IACzB,YAAY,EAAE,0BAA0B,CAAA;CACzC,eACY,KAAK,KAAK,IAAI;AAL7B,wBA2BC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const s = (t) => {
|
|
2
|
+
const e = t.specUrl ? `<div data-spec-url="${t.specUrl}" />` : `<div data-spec='${JSON.stringify(t.spec)}' />`;
|
|
3
|
+
return `
|
|
4
|
+
<!DOCTYPE html>
|
|
5
|
+
<html>
|
|
6
|
+
<head>
|
|
7
|
+
<title>${t.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
|
+
${e}
|
|
16
|
+
<script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
19
|
+
`;
|
|
20
|
+
}, d = (t, e, r) => {
|
|
21
|
+
if (!e.apiReference.spec && !e.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
|
+
), r();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
t.get("/", (l, a) => {
|
|
28
|
+
const c = s(e == null ? void 0 : e.apiReference);
|
|
29
|
+
a.headers({
|
|
30
|
+
"Content-Type": "text/html; charset=utf-8"
|
|
31
|
+
}), a.send(c);
|
|
32
|
+
}), r();
|
|
33
|
+
};
|
|
34
|
+
export {
|
|
35
|
+
d as default
|
|
36
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scalar/fastify-api-reference",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"author": "Scalar",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=18"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"require": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"fastify": "^4.22.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"nodemon": "^3.0.1",
|
|
27
|
+
"tsc-alias": "1.8.6",
|
|
28
|
+
"typescript": "^5.2.2",
|
|
29
|
+
"vite": "4.4.8",
|
|
30
|
+
"vite-node": "^0.34.3",
|
|
31
|
+
"vitest": "0.34.1"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",
|
|
35
|
+
"lint:check": "eslint .",
|
|
36
|
+
"lint:fix": "eslint . --fix",
|
|
37
|
+
"test": "vitest",
|
|
38
|
+
"types:build": "tsc -p tsconfig.build.json",
|
|
39
|
+
"types:check": "tsc --noEmit --skipLibCheck"
|
|
40
|
+
}
|
|
41
|
+
}
|