@scalar/fastify-api-reference 0.5.0 → 0.6.1

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/README.md CHANGED
@@ -20,7 +20,7 @@ If you have a OpenAPI/Swagger file already, you can pass an URL to the plugin:
20
20
  ```ts
21
21
  // Render an API reference for a given OpenAPI/Swagger spec URL
22
22
  fastify.register(require('@scalar/fastify-api-reference'), {
23
- prefix: '/reference',
23
+ routePrefix: '/reference',
24
24
  apiReference: {
25
25
  title: 'Our API Reference',
26
26
  specUrl: '/swagger.json',
@@ -32,7 +32,7 @@ With the [@fastify/swagger](https://github.com/fastify/fastify-swagger) you can
32
32
 
33
33
  ```ts
34
34
  await fastify.register(require('@scalar/fastify-api-reference'), {
35
- prefix: '/reference',
35
+ routePrefix: '/reference',
36
36
  apiReference: {
37
37
  spec: () => fastify.swagger(),
38
38
  },
@@ -43,7 +43,7 @@ Or, if you just have a static OpenAPI spec, you can directly pass it, too:
43
43
 
44
44
  ```ts
45
45
  await fastify.register(require('@scalar/fastify-api-reference'), {
46
- prefix: '/reference',
46
+ routePrefix: '/reference',
47
47
  apiReference: {
48
48
  spec: { … },
49
49
  },
@@ -1,12 +1,17 @@
1
- import { type FastifyPluginAsync } from 'fastify';
2
- export type ApiReferencePlugin = {
3
- apiReference: ApiReferenceOptions;
4
- };
1
+ import type { FastifyPluginAsync } from 'fastify';
5
2
  export type ApiReferenceOptions = {
6
- title?: string;
7
- specUrl?: string;
8
- spec?: Record<string, any> | (() => Record<string, any>);
3
+ /**
4
+ * Prefix for the registered route
5
+ *
6
+ * @default '/'
7
+ */
8
+ routePrefix: string;
9
+ apiReference: {
10
+ specUrl?: string;
11
+ spec?: Record<string, any> | (() => Record<string, any>);
12
+ title?: string;
13
+ };
9
14
  };
10
- declare const fastifyApiReferencePlugin: FastifyPluginAsync<ApiReferencePlugin>;
11
- export default fastifyApiReferencePlugin;
15
+ declare const _default: FastifyPluginAsync<ApiReferenceOptions>;
16
+ export default _default;
12
17
  //# sourceMappingURL=fastifyApiReference.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,kBAAkB,EAExB,MAAM,SAAS,CAAA;AAEhB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,mBAAmB,CAAA;CAClC,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;CACzD,CAAA;AA8BD,QAAA,MAAM,yBAAyB,EAAE,kBAAkB,CACjD,kBAAkB,CAuBnB,CAAA;AAED,eAAe,yBAAyB,CAAA"}
1
+ {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAKjD,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE;QACZ,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;QACxD,KAAK,CAAC,EAAE,MAAM,CAAA;KACf,CAAA;CACF,CAAA;;AA4CD,wBAAsC"}
@@ -0,0 +1,24 @@
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}/templates/index.ejs`), {
13
+ options,
14
+ });
15
+ reply.send(html);
16
+ });
17
+ console.log((options.routePrefix ?? '/') + '/fastify-api-reference.js');
18
+ fastify.get((options.routePrefix ?? '/') + '/fastify-api-reference.js', async (_, reply) => {
19
+ reply.header('Content-Type', 'application/javascript; charset=utf-8');
20
+ const content = fs.readFileSync(path.resolve(`${__dirname}/../dist/templates/fastify-api-reference.js`), 'utf8');
21
+ reply.send(content);
22
+ });
23
+ };
24
+ export default fp(fastifyApiReference);
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { default } from './fastifyApiReference';
1
+ export { default } from './fastifyApiReference.js';
2
2
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,36 +1 @@
1
- const d = (e) => {
2
- const t = e.specUrl ? `<div data-spec-url="${e.specUrl}" />` : typeof e.spec == "object" ? `<div data-spec='${JSON.stringify(e.spec)}' />` : e.spec ? `<div data-spec='${JSON.stringify(e.spec())}' />` : "";
3
- return `
4
- <!DOCTYPE html>
5
- <html>
6
- <head>
7
- <title>${e.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
- ${t}
16
- <script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
17
- </body>
18
- </html>
19
- `;
20
- }, l = async (e, t) => {
21
- if (!t.apiReference.spec && !t.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
- );
25
- return;
26
- }
27
- e.addHook("onSend", (r, a, c, s) => {
28
- a.header("Content-Type", "text/html; charset=utf-8"), s();
29
- }), e.get("/", async (r, a) => {
30
- const c = d(t == null ? void 0 : t.apiReference);
31
- a.send(c);
32
- });
33
- };
34
- export {
35
- l as default
36
- };
1
+ export { default } from './fastifyApiReference.js';