UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

49 lines (48 loc) 1.99 kB
import { watch } from "vue"; import { buildRequest, getEnvironmentVariables } from "@scalar/workspace-store/request-example"; import { buildSafeBodyRequest } from "@scalar/helpers/http/can-method-have-body"; //#region src/helpers/map-config-plugins.ts /** * Maps API reference configuration callbacks to client plugins. * * This function transforms the legacy onBeforeRequest and onRequestSent callbacks * into the new plugin hook system. The mapping is reactive, so changes to the * configuration will automatically update the plugin hooks. * * Note: onRequestSent is mapped to responseReceived hook. This is not a perfect * one-to-one mapping, but it maintains backward compatibility with the old API. * The old callback receives only the URL string, while the new hook receives * the full response object. * * @param config - Reactive configuration object containing optional hook callbacks * @returns Array containing a single plugin with the mapped hooks */ var mapConfigPlugins = (config, environment) => { const plugin = { hooks: {} }; watch([ () => config.value.onBeforeRequest, () => config.value.onRequestSent, () => environment.value ], ([onBeforeRequest, onRequestSent, environment]) => { const envVariables = getEnvironmentVariables(environment); if (!plugin.hooks) plugin.hooks = {}; plugin.hooks.beforeRequest = onBeforeRequest ? async (payload) => { await onBeforeRequest({ request: buildSafeBodyRequest(...buildRequest(payload.requestBuilder, { envVariables }).requestPayload), requestBuilder: payload.requestBuilder, envVariables }); } : void 0; /** * Maps onRequestSent to responseReceived hook. * The old API only passed the URL string, so we extract it from the request. */ plugin.hooks.responseReceived = onRequestSent ? (payload) => { onRequestSent(payload.request.url); } : void 0; }, { immediate: true }); return [plugin]; }; //#endregion export { mapConfigPlugins }; //# sourceMappingURL=map-config-plugins.js.map