MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeHandler

Function makeHandler

packages/effect/src/unstable/httpapi/HttpApiScalar.ts:142–196  ·  view source on GitHub ↗
(options: {
  readonly api: HttpApi.HttpApi<Id, Groups>
  readonly source: ScalarSource
  readonly scalar: ScalarConfig | undefined
})

Source from the content-addressed store, hash-verified

140 }
141
142const makeHandler = <Id extends string, Groups extends HttpApiGroup.Constraint>(options: {
143 readonly api: HttpApi.HttpApi<Id, Groups>
144 readonly source: ScalarSource
145 readonly scalar: ScalarConfig | undefined
146}) => {
147 const spec = OpenApi.fromApi(options.api)
148 const { customFetch, ...scalar } = options.scalar ?? {}
149 const scalarConfig = {
150 _integration: "html",
151 ...scalar
152 }
153 const scalarScript = options.source._tag === "Cdn"
154 ? `<script src="${
155 Html.escapeAttribute(
156 `https://cdn.jsdelivr.net/npm/@scalar/api-reference@${
157 encodeURIComponent(options.source.version ?? "latest")
158 }/dist/browser/standalone.min.js`
159 )
160 }" crossorigin></script>`
161 : `<script>${options.source.source}</script>`
162 const response = HttpServerResponse.html(`<!doctype html>
163<html>
164 <head>
165 <meta charset="utf-8" />
166 <title>${Html.escape(spec.info.title)}</title>
167 ${
168 !spec.info.description
169 ? ""
170 : `<meta name="description" content="${Html.escapeAttribute(spec.info.description)}"/>`
171 }
172 ${
173 !spec.info.description
174 ? ""
175 : `<meta name="og:description" content="${Html.escapeAttribute(spec.info.description)}"/>`
176 }
177 <meta
178 name="viewport"
179 content="width=device-width, initial-scale=1" />
180 </head>
181 <body>
182 <div id="api-reference-container"></div>
183 ${scalarScript}
184 <script>
185 window.Scalar.createApiReference(document.getElementById('api-reference-container'), {
186 ...${Html.escapeJson(scalarConfig)},
187 content: ${Html.escapeJson(spec)}${
188 customFetch === undefined ? "" : `,
189 customFetch: ${customFetch}`
190 }
191 })
192 </script>
193 </body>
194</html>`)
195 return Effect.succeed(response)
196}
197
198/**
199 * Mounts a Scalar API reference page for an `HttpApi` using the bundled Scalar script.

Callers 2

layerFunction · 0.70
layerCdnFunction · 0.70

Calls 1

succeedMethod · 0.45

Tested by

no test coverage detected