UNPKG

@muban/storybook

Version:
46 lines (45 loc) 2.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import {} from '@muban/muban'; import {} from '@storybook/types'; import { fetchStoryHtmlUsingGetJson } from '../fetch/fetchGetJson.js'; import {} from '../types.js'; import { getInjectedServerTemplate } from '../utils/getInjectedServerTemplate.js'; import { getServerTemplateArgs } from '../utils/getTemplateArgs.js'; export function renderServerTemplate(app, container, componentStory, // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types data, { serverConfig, options, }) { return __awaiter(this, void 0, void 0, function* () { const { id, name, kind, storyContext, storyContext: { parameters, argTypes, undecoratedStoryFn }, } = options; const { server: { id: storyId, params, configs }, } = parameters; let { server: { url, fetchStoryHtml = fetchStoryHtmlUsingGetJson }, } = parameters; if (serverConfig && configs[serverConfig]) { url = configs[serverConfig].url || url; fetchStoryHtml = configs[serverConfig].fetchStoryHtml || fetchStoryHtml; } const sanitizedStoryArgs = getServerTemplateArgs(data, argTypes); const fetchId = storyId || id; const storyParams = Object.assign(Object.assign({}, params), sanitizedStoryArgs); const serverTemplate = yield fetchStoryHtml(url, fetchId, storyParams, storyContext); if (serverTemplate) { const finalServerTemplate = getInjectedServerTemplate(undecoratedStoryFn, storyContext, data, componentStory, serverTemplate, options); if (finalServerTemplate) { container.innerHTML = finalServerTemplate; return app === null || app === void 0 ? void 0 : app.mount(container); } } else { options.showError({ title: `Expecting an HTML snippet from the story: "${name}" of "${kind}".`, description: `Did you forget to return any HTML from the server?`, }); } }); }