@nestia/editor 5.0.0 → 6.0.0

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,130 +1,130 @@
1
- import type { OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
2
- import * as fs from "fs";
3
-
4
- export namespace NestiaEditorModule {
5
- export const setup = async (props: {
6
- path: string;
7
- application: INestApplication;
8
- swagger:
9
- | string
10
- | SwaggerV2.IDocument
11
- | OpenApiV3.IDocument
12
- | OpenApiV3_1.IDocument;
13
- package?: string;
14
- simulate?: boolean;
15
- e2e?: boolean;
16
- }): Promise<void> => {
17
- const prefix: string =
18
- "/" +
19
- [getGlobalPrefix(props.application), props.path]
20
- .join("/")
21
- .split("/")
22
- .filter((str) => str.length !== 0)
23
- .join("/");
24
- const adaptor: INestHttpAdaptor = props.application.getHttpAdapter();
25
- const staticFiles: IStaticFile[] = [
26
- {
27
- path: "/index.html",
28
- type: "text/html",
29
- content: await getIndex(props),
30
- },
31
- {
32
- path: "/swagger.json",
33
- type: "application/json",
34
- content: JSON.stringify(
35
- typeof props.swagger === "string"
36
- ? await getSwagger(props.swagger)
37
- : props.swagger,
38
- null,
39
- 2,
40
- ),
41
- },
42
- await getJavaScript(),
43
- ];
44
- for (const f of staticFiles) {
45
- adaptor.get(prefix + f.path, (_: any, res: any) => {
46
- res.type(f.type);
47
- return res.send(f.content);
48
- });
49
- }
50
- for (const p of ["", "/"])
51
- adaptor.get(prefix + p, (_: any, res: any) => {
52
- return res.redirect(prefix + "/index.html");
53
- });
54
- };
55
-
56
- const getGlobalPrefix = (app: INestApplication): string =>
57
- typeof (app as any).config?.globalPrefix === "string"
58
- ? (app as any).config.globalPrefix
59
- : "";
60
- }
61
-
62
- interface INestApplication {
63
- use(...args: any[]): this;
64
- getUrl(): Promise<string>;
65
- getHttpAdapter(): INestHttpAdaptor;
66
- setGlobalPrefix(prefix: string, options?: any): this;
67
- }
68
- interface INestHttpAdaptor {
69
- getType(): string;
70
- close(): any;
71
- init?(): Promise<void>;
72
- get: Function;
73
- post: Function;
74
- put: Function;
75
- patch: Function;
76
- delete: Function;
77
- head: Function;
78
- all: Function;
79
- }
80
- interface IStaticFile {
81
- path: string;
82
- type: string;
83
- content: string;
84
- }
85
-
86
- const getIndex = async (props: {
87
- package?: string;
88
- simulate?: boolean;
89
- e2e?: boolean;
90
- }): Promise<string> => {
91
- const content: string = await fs.promises.readFile(
92
- `${__dirname}/../dist/index.html`,
93
- "utf8",
94
- );
95
- return content
96
- .replace(
97
- `"@ORGANIZATION/PROJECT"`,
98
- JSON.stringify(props.package ?? "@ORGANIZATION/PROJECT"),
99
- )
100
- .replace("window.simulate = false", `window.simulate = ${!!props.simulate}`)
101
- .replace("window.e2e = false", `window.e2e = ${!!props.e2e}`);
102
- };
103
-
104
- const getJavaScript = async (): Promise<IStaticFile> => {
105
- const directory: string[] = await fs.promises.readdir(
106
- `${__dirname}/../dist/assets`,
107
- );
108
- const path: string | undefined = directory[0];
109
- if (path === undefined)
110
- throw new Error("Unreachable code, no JS file exists.");
111
- return {
112
- path: `/assets/${path}`,
113
- type: "application/javascript",
114
- content: await fs.promises.readFile(
115
- `${__dirname}/../dist/assets/${path}`,
116
- "utf8",
117
- ),
118
- };
119
- };
120
-
121
- const getSwagger = async (
122
- url: string,
123
- ): Promise<
124
- SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument
125
- > => {
126
- const response: Response = await fetch(url);
127
- if (response.status !== 200)
128
- throw new Error(`Failed to fetch Swagger document from ${url}`);
129
- return response.json();
130
- };
1
+ import type { OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
2
+ import * as fs from "fs";
3
+
4
+ export namespace NestiaEditorModule {
5
+ export const setup = async (props: {
6
+ path: string;
7
+ application: INestApplication;
8
+ swagger:
9
+ | string
10
+ | SwaggerV2.IDocument
11
+ | OpenApiV3.IDocument
12
+ | OpenApiV3_1.IDocument;
13
+ package?: string;
14
+ simulate?: boolean;
15
+ e2e?: boolean;
16
+ }): Promise<void> => {
17
+ const prefix: string =
18
+ "/" +
19
+ [getGlobalPrefix(props.application), props.path]
20
+ .join("/")
21
+ .split("/")
22
+ .filter((str) => str.length !== 0)
23
+ .join("/");
24
+ const adaptor: INestHttpAdaptor = props.application.getHttpAdapter();
25
+ const staticFiles: IStaticFile[] = [
26
+ {
27
+ path: "/index.html",
28
+ type: "text/html",
29
+ content: await getIndex(props),
30
+ },
31
+ {
32
+ path: "/swagger.json",
33
+ type: "application/json",
34
+ content: JSON.stringify(
35
+ typeof props.swagger === "string"
36
+ ? await getSwagger(props.swagger)
37
+ : props.swagger,
38
+ null,
39
+ 2,
40
+ ),
41
+ },
42
+ await getJavaScript(),
43
+ ];
44
+ for (const f of staticFiles) {
45
+ adaptor.get(prefix + f.path, (_: any, res: any) => {
46
+ res.type(f.type);
47
+ return res.send(f.content);
48
+ });
49
+ }
50
+ for (const p of ["", "/"])
51
+ adaptor.get(prefix + p, (_: any, res: any) => {
52
+ return res.redirect(prefix + "/index.html");
53
+ });
54
+ };
55
+
56
+ const getGlobalPrefix = (app: INestApplication): string =>
57
+ typeof (app as any).config?.globalPrefix === "string"
58
+ ? (app as any).config.globalPrefix
59
+ : "";
60
+ }
61
+
62
+ interface INestApplication {
63
+ use(...args: any[]): this;
64
+ getUrl(): Promise<string>;
65
+ getHttpAdapter(): INestHttpAdaptor;
66
+ setGlobalPrefix(prefix: string, options?: any): this;
67
+ }
68
+ interface INestHttpAdaptor {
69
+ getType(): string;
70
+ close(): any;
71
+ init?(): Promise<void>;
72
+ get: Function;
73
+ post: Function;
74
+ put: Function;
75
+ patch: Function;
76
+ delete: Function;
77
+ head: Function;
78
+ all: Function;
79
+ }
80
+ interface IStaticFile {
81
+ path: string;
82
+ type: string;
83
+ content: string;
84
+ }
85
+
86
+ const getIndex = async (props: {
87
+ package?: string;
88
+ simulate?: boolean;
89
+ e2e?: boolean;
90
+ }): Promise<string> => {
91
+ const content: string = await fs.promises.readFile(
92
+ `${__dirname}/../dist/index.html`,
93
+ "utf8",
94
+ );
95
+ return content
96
+ .replace(
97
+ `"@ORGANIZATION/PROJECT"`,
98
+ JSON.stringify(props.package ?? "@ORGANIZATION/PROJECT"),
99
+ )
100
+ .replace("window.simulate = false", `window.simulate = ${!!props.simulate}`)
101
+ .replace("window.e2e = false", `window.e2e = ${!!props.e2e}`);
102
+ };
103
+
104
+ const getJavaScript = async (): Promise<IStaticFile> => {
105
+ const directory: string[] = await fs.promises.readdir(
106
+ `${__dirname}/../dist/assets`,
107
+ );
108
+ const path: string | undefined = directory[0];
109
+ if (path === undefined)
110
+ throw new Error("Unreachable code, no JS file exists.");
111
+ return {
112
+ path: `/assets/${path}`,
113
+ type: "application/javascript",
114
+ content: await fs.promises.readFile(
115
+ `${__dirname}/../dist/assets/${path}`,
116
+ "utf8",
117
+ ),
118
+ };
119
+ };
120
+
121
+ const getSwagger = async (
122
+ url: string,
123
+ ): Promise<
124
+ SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument
125
+ > => {
126
+ const response: Response = await fetch(url);
127
+ if (response.status !== 200)
128
+ throw new Error(`Failed to fetch Swagger document from ${url}`);
129
+ return response.json();
130
+ };
@@ -1,145 +1,145 @@
1
- import {
2
- Button,
3
- FormControl,
4
- FormControlLabel,
5
- FormLabel,
6
- Radio,
7
- RadioGroup,
8
- Switch,
9
- TextField,
10
- } from "@mui/material";
11
- import { OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
12
- import StackBlitzSDK from "@stackblitz/sdk";
13
- import React from "react";
14
-
15
- import { NestiaEditorComposer } from "./internal/NestiaEditorComposer";
16
- import { NestiaEditorFileUploader } from "./internal/NestiaEditorFileUploader";
17
-
18
- export function NestiaEditorUploader(props: NestiaEditorUploader.IProps) {
19
- // PARAMETERS
20
- const [mode, setMode] = React.useState<"nest" | "sdk">("sdk");
21
- const [simulate, setSimulate] = React.useState(true);
22
- const [e2e, setE2e] = React.useState(true);
23
- const [name, setName] = React.useState("@ORGINIZATION/PROJECT");
24
-
25
- // RESULT
26
- const [document, setDocument] = React.useState<
27
- SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument | null
28
- >(null);
29
- const [progress, setProgress] = React.useState(false);
30
-
31
- const handleError = (error: string) => {
32
- if (props.onError) props.onError(error);
33
- else alert(error);
34
- };
35
- const handleSwagger = (
36
- document:
37
- | SwaggerV2.IDocument
38
- | OpenApiV3.IDocument
39
- | OpenApiV3_1.IDocument
40
- | null,
41
- error: string | null,
42
- ) => {
43
- setDocument(document);
44
- if (error !== null) handleError(error);
45
- };
46
-
47
- const generate = async () => {
48
- if (document === null) return;
49
-
50
- setProgress(true);
51
- try {
52
- const result = await NestiaEditorComposer[mode]({
53
- document,
54
- e2e,
55
- simulate,
56
- package: name,
57
- });
58
- if (result.success === true) {
59
- StackBlitzSDK.openProject(
60
- {
61
- title: document.info?.title ?? "Nestia Editor",
62
- template: "node",
63
- files: result.data.files,
64
- },
65
- {
66
- newWindow: true,
67
- openFile: result.data.openFile,
68
- startScript: result.data.startScript as any,
69
- },
70
- );
71
- } else {
72
- handleError(JSON.stringify(result.errors, null, 2));
73
- }
74
- } catch (exp) {
75
- handleError(exp instanceof Error ? exp.message : "unknown error");
76
- }
77
- setProgress(false);
78
- };
79
-
80
- return (
81
- <>
82
- <NestiaEditorFileUploader onChange={handleSwagger} />
83
- <br />
84
- <FormControl fullWidth style={{ paddingLeft: 15 }}>
85
- <TextField
86
- onChange={(e) => setName(e.target.value)}
87
- defaultValue={name}
88
- label="Package Name"
89
- variant="outlined"
90
- />
91
- <FormLabel style={{ paddingTop: 20 }}> Mode </FormLabel>
92
- <RadioGroup
93
- defaultValue={mode}
94
- onChange={(_e, value) => setMode(value as "nest" | "sdk")}
95
- style={{ paddingLeft: 15 }}
96
- >
97
- <FormControlLabel
98
- value="sdk"
99
- control={<Radio />}
100
- label="Software Development Kit"
101
- />
102
- <FormControlLabel
103
- value="nest"
104
- control={<Radio />}
105
- label="NestJS Project"
106
- />
107
- </RadioGroup>
108
- <FormLabel style={{ paddingTop: 20 }}> Options </FormLabel>
109
- <FormControlLabel
110
- label="Mockup Simulator"
111
- style={{ paddingTop: 5, paddingLeft: 15 }}
112
- control={
113
- <Switch
114
- checked={simulate}
115
- onChange={() => setSimulate(!simulate)}
116
- />
117
- }
118
- />
119
- <FormControlLabel
120
- label="E2E Test Functions"
121
- style={{ paddingLeft: 15 }}
122
- control={<Switch checked={e2e} onChange={() => setE2e(!e2e)} />}
123
- />
124
- </FormControl>
125
- <br />
126
- <br />
127
- <Button
128
- component="a"
129
- fullWidth
130
- variant="contained"
131
- color={"info"}
132
- size="large"
133
- disabled={progress === true || document === null}
134
- onClick={() => generate()}
135
- >
136
- {progress ? "Generating..." : "Generate Editor"}
137
- </Button>
138
- </>
139
- );
140
- }
141
- export namespace NestiaEditorUploader {
142
- export interface IProps {
143
- onError?: (error: string) => void;
144
- }
145
- }
1
+ import {
2
+ Button,
3
+ FormControl,
4
+ FormControlLabel,
5
+ FormLabel,
6
+ Radio,
7
+ RadioGroup,
8
+ Switch,
9
+ TextField,
10
+ } from "@mui/material";
11
+ import { OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
12
+ import StackBlitzSDK from "@stackblitz/sdk";
13
+ import React from "react";
14
+
15
+ import { NestiaEditorComposer } from "./internal/NestiaEditorComposer";
16
+ import { NestiaEditorFileUploader } from "./internal/NestiaEditorFileUploader";
17
+
18
+ export function NestiaEditorUploader(props: NestiaEditorUploader.IProps) {
19
+ // PARAMETERS
20
+ const [mode, setMode] = React.useState<"nest" | "sdk">("sdk");
21
+ const [simulate, setSimulate] = React.useState(true);
22
+ const [e2e, setE2e] = React.useState(true);
23
+ const [name, setName] = React.useState("@ORGINIZATION/PROJECT");
24
+
25
+ // RESULT
26
+ const [document, setDocument] = React.useState<
27
+ SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument | null
28
+ >(null);
29
+ const [progress, setProgress] = React.useState(false);
30
+
31
+ const handleError = (error: string) => {
32
+ if (props.onError) props.onError(error);
33
+ else alert(error);
34
+ };
35
+ const handleSwagger = (
36
+ document:
37
+ | SwaggerV2.IDocument
38
+ | OpenApiV3.IDocument
39
+ | OpenApiV3_1.IDocument
40
+ | null,
41
+ error: string | null,
42
+ ) => {
43
+ setDocument(document);
44
+ if (error !== null) handleError(error);
45
+ };
46
+
47
+ const generate = async () => {
48
+ if (document === null) return;
49
+
50
+ setProgress(true);
51
+ try {
52
+ const result = await NestiaEditorComposer[mode]({
53
+ document,
54
+ e2e,
55
+ simulate,
56
+ package: name,
57
+ });
58
+ if (result.success === true) {
59
+ StackBlitzSDK.openProject(
60
+ {
61
+ title: document.info?.title ?? "Nestia Editor",
62
+ template: "node",
63
+ files: result.data.files,
64
+ },
65
+ {
66
+ newWindow: true,
67
+ openFile: result.data.openFile,
68
+ startScript: result.data.startScript as any,
69
+ },
70
+ );
71
+ } else {
72
+ handleError(JSON.stringify(result.errors, null, 2));
73
+ }
74
+ } catch (exp) {
75
+ handleError(exp instanceof Error ? exp.message : "unknown error");
76
+ }
77
+ setProgress(false);
78
+ };
79
+
80
+ return (
81
+ <>
82
+ <NestiaEditorFileUploader onChange={handleSwagger} />
83
+ <br />
84
+ <FormControl fullWidth style={{ paddingLeft: 15 }}>
85
+ <TextField
86
+ onChange={(e) => setName(e.target.value)}
87
+ defaultValue={name}
88
+ label="Package Name"
89
+ variant="outlined"
90
+ />
91
+ <FormLabel style={{ paddingTop: 20 }}> Mode </FormLabel>
92
+ <RadioGroup
93
+ defaultValue={mode}
94
+ onChange={(_e, value) => setMode(value as "nest" | "sdk")}
95
+ style={{ paddingLeft: 15 }}
96
+ >
97
+ <FormControlLabel
98
+ value="sdk"
99
+ control={<Radio />}
100
+ label="Software Development Kit"
101
+ />
102
+ <FormControlLabel
103
+ value="nest"
104
+ control={<Radio />}
105
+ label="NestJS Project"
106
+ />
107
+ </RadioGroup>
108
+ <FormLabel style={{ paddingTop: 20 }}> Options </FormLabel>
109
+ <FormControlLabel
110
+ label="Mockup Simulator"
111
+ style={{ paddingTop: 5, paddingLeft: 15 }}
112
+ control={
113
+ <Switch
114
+ checked={simulate}
115
+ onChange={() => setSimulate(!simulate)}
116
+ />
117
+ }
118
+ />
119
+ <FormControlLabel
120
+ label="E2E Test Functions"
121
+ style={{ paddingLeft: 15 }}
122
+ control={<Switch checked={e2e} onChange={() => setE2e(!e2e)} />}
123
+ />
124
+ </FormControl>
125
+ <br />
126
+ <br />
127
+ <Button
128
+ component="a"
129
+ fullWidth
130
+ variant="contained"
131
+ color={"info"}
132
+ size="large"
133
+ disabled={progress === true || document === null}
134
+ onClick={() => generate()}
135
+ >
136
+ {progress ? "Generating..." : "Generate Editor"}
137
+ </Button>
138
+ </>
139
+ );
140
+ }
141
+ export namespace NestiaEditorUploader {
142
+ export interface IProps {
143
+ onError?: (error: string) => void;
144
+ }
145
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./NestiaEditorIframe";
2
- export * from "./NestiaEditorUploader";
1
+ export * from "./NestiaEditorIframe";
2
+ export * from "./NestiaEditorUploader";