@typespec/playground 0.14.0-dev.3 → 0.14.0-dev.5

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.
@@ -1,82 +1,68 @@
1
- import lzutf8 from 'lzutf8';
2
-
1
+ import lzutf8 from "lzutf8";
2
+ //#region src/state-storage.ts
3
+ /**
4
+ * Generic storage mechanism for data in the playground.
5
+ * @param schema Schema of the data to be serialized in the query
6
+ * @returns
7
+ */
3
8
  function createUrlStateStorage(schema) {
4
- return { load, save, resolveSearchParams };
5
- function load() {
6
- const result = {};
7
- const parsed = new URLSearchParams(window.location.search);
8
- for (const [key, item] of Object.entries(schema)) {
9
- const value = parsed.get(item.queryParam);
10
- const decompressed = value && decompress(item, value);
11
- const deserialized = decompressed && deserialize(item, decompressed);
12
- if (deserialized) {
13
- result[key] = deserialized;
14
- }
15
- }
16
- return result;
17
- }
18
- function decompress(item, value) {
19
- if (item.compress) {
20
- try {
21
- return lzutf8.decompress(value, { inputEncoding: "Base64" });
22
- } catch (e) {
23
- console.error(
24
- `Error decompressing query parameter ${item.queryParam} with content:`,
25
- value
26
- );
27
- return void 0;
28
- }
29
- } else {
30
- return value;
31
- }
32
- }
33
- function deserialize(item, value) {
34
- if (item.type === "object") {
35
- try {
36
- return JSON.parse(value);
37
- } catch (e) {
38
- console.error(
39
- `Error decompressing query parameter ${item.queryParam} with content:`,
40
- value
41
- );
42
- return void 0;
43
- }
44
- } else {
45
- return value;
46
- }
47
- }
48
- function save(data) {
49
- const params = resolveSearchParams(data, true);
50
- history.pushState(null, "", window.location.pathname + "?" + params.toString());
51
- }
52
- function resolveSearchParams(data, mergeWithExisting = false) {
53
- const params = new URLSearchParams(mergeWithExisting ? location.search : void 0);
54
- for (const [key, item] of Object.entries(schema)) {
55
- const value = data[key];
56
- if (value) {
57
- const serialized = serialize(item, value);
58
- const compressed = compress(item, serialized);
59
- params.set(item.queryParam, compressed);
60
- } else {
61
- params.delete(item.queryParam);
62
- }
63
- }
64
- return params;
65
- }
66
- function compress(item, value) {
67
- if (item.compress) {
68
- return lzutf8.compress(value, { outputEncoding: "Base64" });
69
- } else {
70
- return value;
71
- }
72
- }
73
- function serialize(item, value) {
74
- if (item.type === "object") {
75
- return JSON.stringify(value);
76
- } else {
77
- return value;
78
- }
79
- }
9
+ return {
10
+ load,
11
+ save,
12
+ resolveSearchParams
13
+ };
14
+ function load() {
15
+ const result = {};
16
+ const parsed = new URLSearchParams(window.location.search);
17
+ for (const [key, item] of Object.entries(schema)) {
18
+ const value = parsed.get(item.queryParam);
19
+ const decompressed = value && decompress(item, value);
20
+ const deserialized = decompressed && deserialize(item, decompressed);
21
+ if (deserialized) result[key] = deserialized;
22
+ }
23
+ return result;
24
+ }
25
+ function decompress(item, value) {
26
+ if (item.compress) try {
27
+ return lzutf8.decompress(value, { inputEncoding: "Base64" });
28
+ } catch (e) {
29
+ console.error(`Error decompressing query parameter ${item.queryParam} with content:`, value);
30
+ return;
31
+ }
32
+ else return value;
33
+ }
34
+ function deserialize(item, value) {
35
+ if (item.type === "object") try {
36
+ return JSON.parse(value);
37
+ } catch (e) {
38
+ console.error(`Error decompressing query parameter ${item.queryParam} with content:`, value);
39
+ return;
40
+ }
41
+ else return value;
42
+ }
43
+ function save(data) {
44
+ const params = resolveSearchParams(data, true);
45
+ history.pushState(null, "", window.location.pathname + "?" + params.toString());
46
+ }
47
+ function resolveSearchParams(data, mergeWithExisting = false) {
48
+ const params = new URLSearchParams(mergeWithExisting ? location.search : void 0);
49
+ for (const [key, item] of Object.entries(schema)) {
50
+ const value = data[key];
51
+ if (value) {
52
+ const compressed = compress(item, serialize(item, value));
53
+ params.set(item.queryParam, compressed);
54
+ } else params.delete(item.queryParam);
55
+ }
56
+ return params;
57
+ }
58
+ function compress(item, value) {
59
+ if (item.compress) return lzutf8.compress(value, { outputEncoding: "Base64" });
60
+ else return value;
61
+ }
62
+ function serialize(item, value) {
63
+ if (item.type === "object") return JSON.stringify(value);
64
+ else return value;
65
+ }
80
66
  }
81
-
67
+ //#endregion
82
68
  export { createUrlStateStorage };
package/dist/style.css CHANGED
@@ -175,7 +175,7 @@
175
175
  ._tree-row_16t0l_4 ._column_16t0l_44:last-child {
176
176
  margin-right: 5px;
177
177
  }
178
- ._bar_1gurd_1 {
178
+ /*$vite$:1*/._bar_1gurd_1 {
179
179
  border-bottom: 1px solid var(--colorNeutralStroke1);
180
180
  }
181
181
 
@@ -560,3 +560,4 @@
560
560
  height: 100%;
561
561
  overflow: auto;
562
562
  }
563
+ /*$vite$:1*/
@@ -1,26 +1,27 @@
1
- import { getDirectoryPath } from '@typespec/compiler';
2
- import { readFile, mkdir, writeFile } from 'fs/promises';
3
-
1
+ import { getDirectoryPath } from "@typespec/compiler";
2
+ import { mkdir, readFile, writeFile } from "fs/promises";
3
+ //#region src/tooling/index.ts
4
+ /**
5
+ * @experimental This API is experimental
6
+ */
4
7
  async function buildSamples_experimental(rootDir, output, samples) {
5
- const resolvedSamples = {};
6
- for (const [name, config] of Object.entries(samples)) {
7
- const content = await readFile(`${rootDir}/${config.filename}`, "utf-8");
8
- resolvedSamples[name] = {
9
- filename: config.filename,
10
- content,
11
- preferredEmitter: config.preferredEmitter,
12
- description: config.description,
13
- compilerOptions: config.compilerOptions
14
- };
15
- }
16
- const dir = getDirectoryPath(output);
17
- await mkdir(dir, { recursive: true });
18
- const dts = [
19
- `import type { PlaygroundSample } from "@typespec/playground";`,
20
- `const samples: Record<string, PlaygroundSample> = ${JSON.stringify(resolvedSamples, null, 2)};`,
21
- `export default samples;`
22
- ].join("\n");
23
- await writeFile(output, dts);
8
+ const resolvedSamples = {};
9
+ for (const [name, config] of Object.entries(samples)) {
10
+ const content = await readFile(`${rootDir}/${config.filename}`, "utf-8");
11
+ resolvedSamples[name] = {
12
+ filename: config.filename,
13
+ content,
14
+ preferredEmitter: config.preferredEmitter,
15
+ description: config.description,
16
+ compilerOptions: config.compilerOptions
17
+ };
18
+ }
19
+ await mkdir(getDirectoryPath(output), { recursive: true });
20
+ await writeFile(output, [
21
+ `import type { PlaygroundSample } from "@typespec/playground";`,
22
+ `const samples: Record<string, PlaygroundSample> = ${JSON.stringify(resolvedSamples, null, 2)};`,
23
+ `export default samples;`
24
+ ].join("\n"));
24
25
  }
25
-
26
+ //#endregion
26
27
  export { buildSamples_experimental };
@@ -1,91 +1,67 @@
1
- import { typespecBundlePlugin } from '@typespec/bundler/vite';
2
- import react from '@vitejs/plugin-react';
3
-
1
+ import { typespecBundlePlugin } from "@typespec/bundler/vite";
2
+ import react from "@vitejs/plugin-react";
3
+ //#region src/vite/index.ts
4
4
  function definePlaygroundViteConfig(config) {
5
- return {
6
- base: "./",
7
- build: {
8
- target: "esnext",
9
- chunkSizeWarningLimit: 5e3,
10
- rollupOptions: {
11
- output: {
12
- manualChunks(id) {
13
- if (id.includes("/node_modules/monaco-editor/esm/vs/editor")) {
14
- return "monaco";
15
- }
16
- return void 0;
17
- }
18
- }
19
- }
20
- },
21
- esbuild: {
22
- logOverride: { "this-is-undefined-in-esm": "silent" }
23
- },
24
- assetsInclude: [/\.tsp$/],
25
- optimizeDeps: {
26
- exclude: ["swagger-ui"]
27
- },
28
- plugins: [
29
- react({}),
30
- playgroundManifestPlugin(config),
31
- !config.skipBundleLibraries ? typespecBundlePlugin({
32
- folderName: "libs",
33
- libraries: config.libraries
34
- }) : void 0
35
- ],
36
- server: {
37
- fs: {
38
- strict: false
39
- }
40
- }
41
- };
5
+ return {
6
+ base: "./",
7
+ build: {
8
+ target: "esnext",
9
+ chunkSizeWarningLimit: 5e3,
10
+ rollupOptions: { output: { manualChunks(id) {
11
+ if (id.includes("/node_modules/monaco-editor/esm/vs/editor")) return "monaco";
12
+ } } }
13
+ },
14
+ esbuild: { logOverride: { "this-is-undefined-in-esm": "silent" } },
15
+ assetsInclude: [/\.tsp$/],
16
+ optimizeDeps: { exclude: ["swagger-ui"] },
17
+ plugins: [
18
+ react({}),
19
+ playgroundManifestPlugin(config),
20
+ !config.skipBundleLibraries ? typespecBundlePlugin({
21
+ folderName: "libs",
22
+ libraries: config.libraries
23
+ }) : void 0
24
+ ],
25
+ server: { fs: { strict: false } }
26
+ };
42
27
  }
43
28
  function playgroundManifestPlugin(config) {
44
- const { samples, ...manifest } = config;
45
- let viteConfig;
46
- return {
47
- name: "playground-manifest",
48
- enforce: "pre",
49
- // Need to run before resolving library imports to stub `@typespec/playground/manifest`
50
- async configResolved(c) {
51
- viteConfig = c;
52
- },
53
- resolveId(id) {
54
- if (id === "@typespec/playground/manifest") {
55
- return id;
56
- }
57
- return null;
58
- },
59
- load(id) {
60
- if (id === `@typespec/playground/manifest`) {
61
- const sampleImport = Object.values(samples ?? {}).map(
62
- (sampleValue, index) => `import s${index} from "${viteConfig.root}/${sampleValue.filename}?raw"`
63
- ).join("\n");
64
- const sampleObj = [
65
- "{",
66
- ...Object.entries(samples ?? {}).map(
67
- ([label, config2], index) => `${JSON.stringify(label)}: {
68
- fileName: ${JSON.stringify(config2.filename)},
69
- preferredEmitter: ${config2.preferredEmitter ? JSON.stringify(config2.preferredEmitter) : "undefined"},
29
+ const { samples, ...manifest } = config;
30
+ let viteConfig;
31
+ return {
32
+ name: "playground-manifest",
33
+ enforce: "pre",
34
+ async configResolved(c) {
35
+ viteConfig = c;
36
+ },
37
+ resolveId(id) {
38
+ if (id === "@typespec/playground/manifest") return id;
39
+ return null;
40
+ },
41
+ load(id) {
42
+ if (id === `@typespec/playground/manifest`) {
43
+ const sampleImport = Object.values(samples ?? {}).map((sampleValue, index) => `import s${index} from "${viteConfig.root}/${sampleValue.filename}?raw"`).join("\n");
44
+ const sampleObj = [
45
+ "{",
46
+ ...Object.entries(samples ?? {}).map(([label, config], index) => `${JSON.stringify(label)}: {
47
+ fileName: ${JSON.stringify(config.filename)},
48
+ preferredEmitter: ${config.preferredEmitter ? JSON.stringify(config.preferredEmitter) : "undefined"},
70
49
  content: s${index},
71
- ${config2.compilerOptions ? `compilerOptions: ${JSON.stringify(config2.compilerOptions)},` : ""}
50
+ ${config.compilerOptions ? `compilerOptions: ${JSON.stringify(config.compilerOptions)},` : ""}
72
51
 
73
- }, `
74
- ),
75
- "}"
76
- ].join("\n");
77
- const file = `
52
+ }, `),
53
+ "}"
54
+ ].join("\n");
55
+ return `
78
56
  ${sampleImport};
79
57
 
80
58
  export default {
81
59
  ...${JSON.stringify(manifest)},
82
60
  samples: ${sampleObj}
83
61
  };`;
84
- return file;
85
- }
86
- return void 0;
87
- }
88
- };
62
+ }
63
+ }
64
+ };
89
65
  }
90
-
66
+ //#endregion
91
67
  export { definePlaygroundViteConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typespec/playground",
3
- "version": "0.14.0-dev.3",
3
+ "version": "0.14.0-dev.5",
4
4
  "author": "Microsoft Corporation",
5
5
  "description": "TypeSpec playground UI components.",
6
6
  "homepage": "https://typespec.io",
@@ -61,15 +61,15 @@
61
61
  "dependencies": {
62
62
  "@fluentui/react-components": "~9.73.3",
63
63
  "@fluentui/react-icons": "^2.0.321",
64
- "@typespec/bundler": "^0.5.1 || >= 0.5.2-dev.2",
65
- "@typespec/compiler": "^1.10.0 || >= 1.11.0-dev.4",
66
- "@typespec/html-program-viewer": "^0.80.0 || >= 0.81.0-dev.1",
67
- "@typespec/http": "^1.10.0 || >= 1.11.0-dev.2",
68
- "@typespec/openapi": "^1.10.0 || >= 1.11.0-dev.1",
69
- "@typespec/openapi3": "^1.10.0 || >= 1.11.0-dev.3",
70
- "@typespec/protobuf": "^0.80.0 || >= 0.81.0-dev.1",
71
- "@typespec/rest": "^0.80.0 || >= 0.81.0-dev.1",
72
- "@typespec/versioning": "^0.80.0 || >= 0.81.0-dev.1",
64
+ "@typespec/bundler": "^0.5.1 || >= 0.5.2-dev.4",
65
+ "@typespec/compiler": "^1.10.0 || >= 1.11.0-dev.6",
66
+ "@typespec/html-program-viewer": "^0.80.0 || >= 0.81.0-dev.3",
67
+ "@typespec/http": "^1.10.0 || >= 1.11.0-dev.3",
68
+ "@typespec/openapi": "^1.10.0 || >= 1.11.0-dev.2",
69
+ "@typespec/openapi3": "^1.10.0 || >= 1.11.0-dev.4",
70
+ "@typespec/protobuf": "^0.80.0 || >= 0.81.0-dev.2",
71
+ "@typespec/rest": "^0.80.0 || >= 0.81.0-dev.2",
72
+ "@typespec/versioning": "^0.80.0 || >= 0.81.0-dev.2",
73
73
  "clsx": "^2.1.1",
74
74
  "debounce": "~3.0.0",
75
75
  "lzutf8": "0.6.3",
@@ -95,15 +95,15 @@
95
95
  "@types/react": "~19.2.14",
96
96
  "@types/react-dom": "~19.2.3",
97
97
  "@types/swagger-ui-dist": "~3.30.6",
98
- "@typespec/bundler": "^0.5.1 || >= 0.5.2-dev.2",
99
- "@vitejs/plugin-react": "~5.1.2",
98
+ "@typespec/bundler": "^0.5.1 || >= 0.5.2-dev.4",
99
+ "@vitejs/plugin-react": "~6.0.1",
100
100
  "c8": "^11.0.0",
101
101
  "cross-env": "~10.1.0",
102
102
  "es-module-shims": "~2.8.0",
103
103
  "rimraf": "~6.1.3",
104
104
  "storybook": "^10.2.19",
105
105
  "typescript": "~5.9.3",
106
- "vite": "^7.2.7",
106
+ "vite": "^8.0.1",
107
107
  "vite-plugin-checker": "^0.12.0",
108
108
  "vite-plugin-dts": "4.5.4",
109
109
  "vitest": "^4.1.0",
@@ -1,20 +0,0 @@
1
- import { jsx } from 'react/jsx-runtime';
2
- import { useRef, useEffect } from 'react';
3
- import SwaggerUIBundle from 'swagger-ui-dist/swagger-ui-es-bundle.js';
4
-
5
- const reactWrapper = (props) => {
6
- const uiRef = useRef(null);
7
- const uiInstance = useRef(null);
8
- useEffect(() => {
9
- if (uiInstance.current === null) {
10
- uiInstance.current = SwaggerUIBundle({
11
- domNode: uiRef.current,
12
- spec: {}
13
- });
14
- }
15
- uiInstance.current.specActions.updateSpec(props.spec);
16
- }, [props.spec]);
17
- return /* @__PURE__ */ jsx("div", { ref: uiRef });
18
- };
19
-
20
- export { reactWrapper as default };