render-playground-page.js 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.renderPlaygroundPage = void 0;
  15. var xss_1 = require("xss");
  16. var get_loading_markup_1 = require("./get-loading-markup");
  17. var filter = function (val) {
  18. return xss_1.filterXSS(val, {
  19. // @ts-ignore
  20. whiteList: [],
  21. stripIgnoreTag: true,
  22. stripIgnoreTagBody: ["script"]
  23. });
  24. };
  25. var loading = get_loading_markup_1.default();
  26. var reactPackageName = '@apollographql/graphql-playground-react';
  27. var getCdnMarkup = function (_a) {
  28. var version = _a.version, _b = _a.cdnUrl, cdnUrl = _b === void 0 ? '//cdn.jsdelivr.net/npm' : _b, faviconUrl = _a.faviconUrl;
  29. var buildCDNUrl = function (packageName, suffix) { return filter(cdnUrl + "/" + packageName + (version ? "@" + version : '') + "/" + suffix || ''); };
  30. return "\n <link\n rel=\"stylesheet\"\n href=\"" + buildCDNUrl(reactPackageName, 'build/static/css/index.css') + "\"\n />\n " + (typeof faviconUrl === 'string' ? "<link rel=\"shortcut icon\" href=\"" + filter(faviconUrl || '') + "\" />" : '') + "\n " + (faviconUrl === undefined ? "<link rel=\"shortcut icon\" href=\"" + buildCDNUrl(reactPackageName, 'build/favicon.png') + "\" />" : '') + "\n <script\n src=\"" + buildCDNUrl(reactPackageName, 'build/static/js/middleware.js') + "\"\n ></script>\n";
  31. };
  32. var renderConfig = function (config) {
  33. return '<div id="playground-config" style="display: none;">' + xss_1.filterXSS(JSON.stringify(config), {
  34. // @ts-ignore
  35. whiteList: [],
  36. }) + '</div>';
  37. };
  38. function renderPlaygroundPage(options) {
  39. var extendedOptions = __assign(__assign({}, options), { canSaveConfig: false });
  40. // for compatibility
  41. if (options.subscriptionsEndpoint) {
  42. extendedOptions.subscriptionEndpoint = filter(options.subscriptionsEndpoint || '');
  43. }
  44. if (options.config) {
  45. extendedOptions.configString = JSON.stringify(options.config, null, 2);
  46. }
  47. if (!extendedOptions.endpoint && !extendedOptions.configString) {
  48. /* tslint:disable-next-line */
  49. console.warn("WARNING: You didn't provide an endpoint and don't have a .graphqlconfig. Make sure you have at least one of them.");
  50. }
  51. else if (extendedOptions.endpoint) {
  52. extendedOptions.endpoint = filter(extendedOptions.endpoint || '');
  53. }
  54. return "\n <!DOCTYPE html>\n <html>\n <head>\n <meta charset=utf-8 />\n <meta name=\"viewport\" content=\"user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui\">\n <link href=\"https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700|Source+Code+Pro:400,700\" rel=\"stylesheet\">\n <title>" + (filter(extendedOptions.title) || 'GraphQL Playground') + "</title>\n " + (extendedOptions.env === 'react' || extendedOptions.env === 'electron'
  55. ? ''
  56. : getCdnMarkup(extendedOptions)) + "\n </head>\n <body>\n <style type=\"text/css\">\n html {\n font-family: \"Open Sans\", sans-serif;\n overflow: hidden;\n }\n \n body {\n margin: 0;\n background: #172a3a;\n }\n \n .playgroundIn {\n -webkit-animation: playgroundIn 0.5s ease-out forwards;\n animation: playgroundIn 0.5s ease-out forwards;\n }\n \n @-webkit-keyframes playgroundIn {\n from {\n opacity: 0;\n -webkit-transform: translateY(10px);\n -ms-transform: translateY(10px);\n transform: translateY(10px);\n }\n to {\n opacity: 1;\n -webkit-transform: translateY(0);\n -ms-transform: translateY(0);\n transform: translateY(0);\n }\n }\n \n @keyframes playgroundIn {\n from {\n opacity: 0;\n -webkit-transform: translateY(10px);\n -ms-transform: translateY(10px);\n transform: translateY(10px);\n }\n to {\n opacity: 1;\n -webkit-transform: translateY(0);\n -ms-transform: translateY(0);\n transform: translateY(0);\n }\n }\n </style>\n " + loading.container + "\n " + renderConfig(extendedOptions) + "\n <div id=\"root\" />\n <script type=\"text/javascript\">\n window.addEventListener('load', function (event) {\n " + loading.script + "\n \n const root = document.getElementById('root');\n root.classList.add('playgroundIn');\n const configText = document.getElementById('playground-config').innerText\n if(configText && configText.length) {\n try {\n GraphQLPlayground.init(root, JSON.parse(configText))\n }\n catch(err) {\n console.error(\"could not find config\")\n }\n }\n })\n </script>\n </body>\n </html>\n";
  57. }
  58. exports.renderPlaygroundPage = renderPlaygroundPage;
  59. //# sourceMappingURL=render-playground-page.js.map