@scalar/fastify-api-reference 0.3.1 → 0.3.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.
@@ -1,11 +1,12 @@
1
- import { type FastifyInstance, type RegisterOptions } from 'fastify';
2
- export type FastifyApiReferenceOptions = {
1
+ import { type FastifyPluginCallback, type RegisterOptions } from 'fastify';
2
+ type ApiReferenceOptions = {
3
3
  title?: string;
4
4
  specUrl?: string;
5
5
  spec?: Record<string, any>;
6
6
  };
7
- declare const _default: (fastify: FastifyInstance, options: RegisterOptions & {
8
- apiReference: FastifyApiReferenceOptions;
9
- }, done: (err?: Error) => void) => void;
10
- export default _default;
7
+ export type FastifyApiReferenceOptions = {
8
+ apiReference: ApiReferenceOptions;
9
+ } & RegisterOptions;
10
+ declare const fastifyApiReference: FastifyPluginCallback<FastifyApiReferenceOptions>;
11
+ export default fastifyApiReference;
11
12
  //# sourceMappingURL=fastifyApiReference.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"fastifyApiReference.d.ts","sourceRoot":"","sources":["../src/fastifyApiReference.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,qBAAqB,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAA;AAE1E,KAAK,mBAAmB,GAAG;IACzB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,0BAA0B,GAAG;IACvC,YAAY,EAAE,mBAAmB,CAAA;CAClC,GAAG,eAAe,CAAA;AA0BnB,QAAA,MAAM,mBAAmB,EAAE,qBAAqB,CAAC,0BAA0B,CAwB1E,CAAA;AAED,eAAe,mBAAmB,CAAA"}
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- const s = (t) => {
2
- const e = t.specUrl ? `<div data-spec-url="${t.specUrl}" />` : `<div data-spec='${JSON.stringify(t.spec)}' />`;
1
+ const s = (e) => {
2
+ const t = e.specUrl ? `<div data-spec-url="${e.specUrl}" />` : `<div data-spec='${JSON.stringify(e.spec)}' />`;
3
3
  return `
4
4
  <!DOCTYPE html>
5
5
  <html>
6
6
  <head>
7
- <title>${t.title || "API Reference"}</title>
7
+ <title>${e.title || "API Reference"}</title>
8
8
  <meta charset="utf-8" />
9
9
  <meta
10
10
  name="viewport"
@@ -12,25 +12,21 @@ const s = (t) => {
12
12
  </head>
13
13
  <body>
14
14
  <!-- Add your own OpenAPI/Swagger spec file URL here: -->
15
- ${e}
15
+ ${t}
16
16
  <script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
17
17
  </body>
18
18
  </html>
19
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);
20
+ }, n = (e, t, r) => {
21
+ !t.apiReference.spec && !t.apiReference.specUrl && (console.warn(
22
+ "[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."
23
+ ), r()), e.addHook("onSend", (d, a, c, l) => {
24
+ a.header("Content-Type", "text/html; charset=utf-8"), l();
25
+ }), e.get("/", (d, a) => {
26
+ const c = s(t == null ? void 0 : t.apiReference);
27
+ a.send(c);
32
28
  }), r();
33
29
  };
34
30
  export {
35
- d as default
31
+ n as default
36
32
  };
@@ -1,8 +1,8 @@
1
- (function(r,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(r=typeof globalThis<"u"?globalThis:r||self,r["@scalar/fastify-api-reference"]=c())})(this,function(){"use strict";const r=t=>{const e=t.specUrl?`<div data-spec-url="${t.specUrl}" />`:`<div data-spec='${JSON.stringify(t.spec)}' />`;return`
1
+ (function(r,c){typeof exports=="object"&&typeof module<"u"?module.exports=c():typeof define=="function"&&define.amd?define(c):(r=typeof globalThis<"u"?globalThis:r||self,r["@scalar/fastify-api-reference"]=c())})(this,function(){"use strict";const r=e=>{const t=e.specUrl?`<div data-spec-url="${e.specUrl}" />`:`<div data-spec='${JSON.stringify(e.spec)}' />`;return`
2
2
  <!DOCTYPE html>
3
3
  <html>
4
4
  <head>
5
- <title>${t.title||"API Reference"}</title>
5
+ <title>${e.title||"API Reference"}</title>
6
6
  <meta charset="utf-8" />
7
7
  <meta
8
8
  name="viewport"
@@ -10,8 +10,8 @@
10
10
  </head>
11
11
  <body>
12
12
  <!-- Add your own OpenAPI/Swagger spec file URL here: -->
13
- ${e}
13
+ ${t}
14
14
  <script src="https://cdn.scalar.com/api-reference.standalone.js"><\/script>
15
15
  </body>
16
16
  </html>
17
- `};return(t,e,a)=>{if(!e.apiReference.spec&&!e.apiReference.specUrl){console.warn("[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."),a();return}t.get("/",(s,i)=>{const n=r(e==null?void 0:e.apiReference);i.headers({"Content-Type":"text/html; charset=utf-8"}),i.send(n)}),a()}});
17
+ `};return(e,t,n)=>{!t.apiReference.spec&&!t.apiReference.specUrl&&(console.warn("[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options."),n()),e.addHook("onSend",(s,a,i,d)=>{a.header("Content-Type","text/html; charset=utf-8"),d()}),e.get("/",(s,a)=>{const i=r(t==null?void 0:t.apiReference);a.send(i)}),n()}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalar/fastify-api-reference",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "author": "Scalar",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -34,7 +34,7 @@
34
34
  "build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",
35
35
  "lint:check": "eslint .",
36
36
  "lint:fix": "eslint . --fix",
37
- "test": "vitest",
37
+ "test": "pnpm build && vitest",
38
38
  "types:build": "tsc -p tsconfig.build.json",
39
39
  "types:check": "tsc --noEmit --skipLibCheck"
40
40
  }
@@ -0,0 +1,27 @@
1
+ import Fastify from 'fastify'
2
+ import { describe, expect, it } from 'vitest'
3
+
4
+ import fastifyApiReference from '../dist/index'
5
+
6
+ describe('esm base support', () => {
7
+ it('returns 200 OK', () =>
8
+ new Promise((resolve) => {
9
+ const fastify = Fastify({
10
+ logger: false,
11
+ })
12
+
13
+ fastify.register(fastifyApiReference, {
14
+ prefix: '/api-reference',
15
+ apiReference: {
16
+ specUrl: '/scalar.json',
17
+ },
18
+ })
19
+
20
+ fastify.listen({ port: 0 }, function (err, address) {
21
+ fetch(`${address}/api-reference`).then((response) => {
22
+ expect(response.status).toBe(200)
23
+ resolve(null)
24
+ })
25
+ })
26
+ }))
27
+ })
@@ -1,12 +1,16 @@
1
- import { type FastifyInstance, type RegisterOptions } from 'fastify'
1
+ import { type FastifyPluginCallback, type RegisterOptions } from 'fastify'
2
2
 
3
- export type FastifyApiReferenceOptions = {
3
+ type ApiReferenceOptions = {
4
4
  title?: string
5
5
  specUrl?: string
6
6
  spec?: Record<string, any>
7
7
  }
8
8
 
9
- const getHtmlMarkup = (options: FastifyApiReferenceOptions) => {
9
+ export type FastifyApiReferenceOptions = {
10
+ apiReference: ApiReferenceOptions
11
+ } & RegisterOptions
12
+
13
+ const getHtmlMarkup = (options: ApiReferenceOptions) => {
10
14
  const htmlTag = options.specUrl
11
15
  ? `<div data-spec-url="${options.specUrl}" />`
12
16
  : `<div data-spec='${JSON.stringify(options.spec)}' />`
@@ -30,31 +34,30 @@ const getHtmlMarkup = (options: FastifyApiReferenceOptions) => {
30
34
  `
31
35
  }
32
36
 
33
- export default (
34
- fastify: FastifyInstance,
35
- options: RegisterOptions & {
36
- apiReference: FastifyApiReferenceOptions
37
- },
38
- done: (err?: Error) => void,
37
+ const fastifyApiReference: FastifyPluginCallback<FastifyApiReferenceOptions> = (
38
+ fastify,
39
+ options,
40
+ done,
39
41
  ) => {
40
42
  if (!options.apiReference.spec && !options.apiReference.specUrl) {
41
43
  console.warn(
42
44
  '[@scalar/fastify-api-reference] You didn’t provide a spec or specUrl. Please provide one of these options.',
43
45
  )
44
-
45
46
  done()
46
- return
47
47
  }
48
48
 
49
+ fastify.addHook('onSend', (request, reply, payload, next) => {
50
+ reply.header('Content-Type', 'text/html; charset=utf-8')
51
+ next()
52
+ })
53
+
49
54
  fastify.get('/', (_, reply) => {
50
55
  const html = getHtmlMarkup(options?.apiReference)
51
56
 
52
- reply.headers({
53
- 'Content-Type': 'text/html; charset=utf-8',
54
- })
55
-
56
57
  reply.send(html)
57
58
  })
58
59
 
59
60
  done()
60
61
  }
62
+
63
+ export default fastifyApiReference