@bielu/scalar-grpc 0.1.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/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # @bielu/scalar-grpc
2
+
3
+ An interactive **gRPC console** plugin for the [Scalar](https://scalar.com) API Reference.
4
+
5
+ It reads the `grpc` bindings from your AsyncAPI document(s) and renders a panel where you can pick
6
+ a service, edit a JSON request prefilled from the payload schema, and invoke unary and
7
+ server-streaming methods live — over **gRPC-Web** (browsers cannot speak native gRPC). Client- and
8
+ bidirectional-streaming methods are rendered as documentation with a "not invokable from the
9
+ browser" badge.
10
+
11
+ Because AsyncAPI payload schemas are JSON Schema (no protobuf field numbers), the console encodes
12
+ wire messages from real protobuf descriptors fetched from the companion .NET package's
13
+ `{assetsPath}/descriptors` endpoint (a serialized `FileDescriptorSet`), using
14
+ `@bufbuild/protobuf` + `@connectrpc/connect-web` — fully dynamic, no codegen in the browser.
15
+
16
+ This package is produced from, and consumed by, the
17
+ [`Bielu.AspNetCore.AsyncApi.Scalar.Grpc`](https://github.com/bielu/Bielu.AspNetCore.AsyncApi)
18
+ .NET package (which embeds the built bundle), but it is also published standalone to npm so it can
19
+ be loaded from a CDN or bundled by hand.
20
+
21
+ ## Server prerequisites
22
+
23
+ The target ASP.NET Core app must:
24
+
25
+ 1. enable gRPC-Web: `app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true })` (package
26
+ `Grpc.AspNetCore.Web`), and
27
+ 2. serve the descriptor endpoint: `app.MapScalarGrpcAssets()` (package
28
+ `Bielu.AspNetCore.AsyncApi.Scalar.Grpc`).
29
+
30
+ When the page and the gRPC server are on different origins, CORS must allow the gRPC-Web response
31
+ headers: `grpc-status` and `grpc-message` need to be exposed
32
+ (`policy.WithExposedHeaders("Grpc-Status", "Grpc-Message", "Grpc-Encoding", "Grpc-Accept-Encoding")`).
33
+
34
+ ## Standalone usage (CDN)
35
+
36
+ ```html
37
+ <div id="app"></div>
38
+ <script src="https://cdn.jsdelivr.net/npm/@bielu/scalar-grpc"></script>
39
+ <script>
40
+ window.__BIELU_SCALAR_GRPC__ = {
41
+ documents: [{ name: 'grpc', url: '/asyncapi/grpc.json' }],
42
+ }
43
+ Scalar.createApiReference('#app', {
44
+ sources: [{ url: '/asyncapi/grpc.json' }],
45
+ })
46
+ </script>
47
+ ```
48
+
49
+ The gRPC documents can be provided either via the global `window.__BIELU_SCALAR_GRPC__` or inline
50
+ on the Scalar config as `config.grpc`.
51
+
52
+ ## Programmatic usage
53
+
54
+ ```ts
55
+ import { createGrpcPlugin } from '@bielu/scalar-grpc'
56
+
57
+ Scalar.createApiReference('#app', {
58
+ sources: [{ url: '/asyncapi/grpc.json' }],
59
+ plugins: [createGrpcPlugin()],
60
+ })
61
+ ```
62
+
63
+ ## Auth
64
+
65
+ The console picks up the credentials entered in Scalar's Authentication panel (Scalar ≥ 2.16.12)
66
+ and maps them onto gRPC-Web call metadata: header API keys, `Authorization: Bearer …` and HTTP
67
+ Basic all map directly (gRPC-Web metadata is plain HTTP headers). Additional per-call metadata can
68
+ be entered in the console's metadata editor.
69
+
70
+ ## Building
71
+
72
+ ```bash
73
+ npm install
74
+ npm run build # → dist/plugin.js (IIFE) + dist/standalone.js (Scalar + plugin) + dist/types
75
+ ```
76
+
77
+ The build inlines the private `@bielu/scalar-core` workspace package (document discovery, auth
78
+ state, schema examples, Scalar bootstrap) — see that package's README for the shared contract.
79
+
80
+ `dist/plugin.js` hooks an already-loaded Scalar (`MapScalarGrpcAssets()` serves it next to
81
+ Scalar's own bundle). `dist/standalone.js` prepends Scalar's prebuilt browser bundle for pages
82
+ where nothing else loads Scalar — it is what `ScalarAspireOptions.BundleUrl` is pointed at in the
83
+ Aspire setup, since that option *replaces* Scalar's bundle.