-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
272 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import type { OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi"; | ||
import * as fs from "fs"; | ||
|
||
export namespace NestiaEditorModule { | ||
export const setup = async (props: { | ||
path: string; | ||
application: INestApplication; | ||
swagger: | ||
| string | ||
| SwaggerV2.IDocument | ||
| OpenApiV3.IDocument | ||
| OpenApiV3_1.IDocument; | ||
package?: string; | ||
simulate?: boolean; | ||
e2e?: boolean; | ||
}): Promise<void> => { | ||
const prefix: string = | ||
"/" + | ||
[getGlobalPrefix(props.application), props.path] | ||
.join("/") | ||
.split("/") | ||
.filter((str) => str.length !== 0) | ||
.join("/"); | ||
const adaptor: INestHttpAdaptor = props.application.getHttpAdapter(); | ||
const staticFiles: IStaticFile[] = [ | ||
{ | ||
path: "/index.html", | ||
type: "text/html", | ||
content: await getIndex(props), | ||
}, | ||
{ | ||
path: "/swagger.json", | ||
type: "application/json", | ||
content: JSON.stringify( | ||
typeof props.swagger === "string" | ||
? await getSwagger(props.swagger) | ||
: props.swagger, | ||
null, | ||
2, | ||
), | ||
}, | ||
await getJavaScript(), | ||
]; | ||
for (const f of staticFiles) { | ||
adaptor.get(prefix + f.path, (_: any, res: any) => { | ||
res.type(f.type); | ||
return res.send(f.content); | ||
}); | ||
} | ||
for (const p of ["", "/"]) | ||
adaptor.get(prefix + p, (_: any, res: any) => { | ||
return res.redirect(prefix + "/index.html"); | ||
}); | ||
}; | ||
|
||
const getGlobalPrefix = (app: INestApplication): string => | ||
typeof (app as any).config?.globalPrefix === "string" | ||
? (app as any).config.globalPrefix | ||
: ""; | ||
} | ||
|
||
interface INestApplication { | ||
use(...args: any[]): this; | ||
getUrl(): Promise<string>; | ||
getHttpAdapter(): INestHttpAdaptor; | ||
setGlobalPrefix(prefix: string, options?: any): this; | ||
} | ||
interface INestHttpAdaptor { | ||
getType(): string; | ||
close(): any; | ||
init?(): Promise<void>; | ||
get: Function; | ||
post: Function; | ||
put: Function; | ||
patch: Function; | ||
delete: Function; | ||
head: Function; | ||
all: Function; | ||
} | ||
interface IStaticFile { | ||
path: string; | ||
type: string; | ||
content: string; | ||
} | ||
|
||
const getIndex = async (props: { | ||
package?: string; | ||
simulate?: boolean; | ||
e2e?: boolean; | ||
}): Promise<string> => { | ||
const content: string = await fs.promises.readFile( | ||
`${__dirname}/../dist/index.html`, | ||
"utf8", | ||
); | ||
return content | ||
.replace( | ||
`"@ORGANIZATION/PROJECT"`, | ||
JSON.stringify(props.package ?? "@ORGANIZATION/PROJECT"), | ||
) | ||
.replace("window.simulate = false", `window.simulate = ${!!props.simulate}`) | ||
.replace("window.e2e = false", `window.e2e = ${!!props.e2e}`); | ||
}; | ||
|
||
const getJavaScript = async (): Promise<IStaticFile> => { | ||
const directory: string[] = await fs.promises.readdir( | ||
`${__dirname}/../dist/assets`, | ||
); | ||
const path: string | undefined = directory[0]; | ||
if (path === undefined) | ||
throw new Error("Unreachable code, no JS file exists."); | ||
return { | ||
path: `/assets/${path}`, | ||
type: "application/javascript", | ||
content: await fs.promises.readFile( | ||
`${__dirname}/../dist/assets/${path}`, | ||
"utf8", | ||
), | ||
}; | ||
}; | ||
|
||
const getSwagger = async ( | ||
url: string, | ||
): Promise< | ||
SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument | ||
> => { | ||
const response: Response = await fetch(url); | ||
if (response.status !== 200) | ||
throw new Error(`Failed to fetch Swagger document from ${url}`); | ||
return response.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { NestFactory } from "@nestjs/core"; | ||
|
||
import { NestiaEditorModule } from "../src/NestiaEditorModule"; | ||
|
||
@Module({}) | ||
class MyModule {} | ||
|
||
const main = async (): Promise<void> => { | ||
const app = await NestFactory.create(MyModule, { logger: false }); | ||
await NestiaEditorModule.setup({ | ||
path: "editor", | ||
application: app, | ||
swagger: | ||
"https://raw.githubusercontent.com/samchon/openapi/refs/heads/master/examples/v3.1/shopping.json", | ||
}); | ||
await app.listen(3_001); | ||
}; | ||
main().catch((exp) => { | ||
console.error(exp); | ||
process.exit(-1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { NestFactory } from "@nestjs/core"; | ||
import { FastifyAdapter } from "@nestjs/platform-fastify"; | ||
|
||
import { NestiaEditorModule } from "../src/NestiaEditorModule"; | ||
|
||
@Module({}) | ||
class MyModule {} | ||
|
||
const main = async (): Promise<void> => { | ||
const app = await NestFactory.create(MyModule, new FastifyAdapter(), { | ||
logger: false, | ||
}); | ||
await NestiaEditorModule.setup({ | ||
path: "editor", | ||
application: app, | ||
swagger: | ||
"https://raw.githubusercontent.com/samchon/openapi/refs/heads/master/examples/v3.1/shopping.json", | ||
}); | ||
await app.listen(3_001); | ||
}; | ||
main().catch((exp) => { | ||
console.error(exp); | ||
process.exit(-1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"root":["./src/nestiaeditorapplication.tsx","./src/nestiaeditoriframe.tsx","./src/nestiaeditoruploader.tsx","./src/index.ts","./src/main.tsx","./src/vite-env.d.ts","./src/internal/nestiaeditorcomposer.ts","./src/internal/nestiaeditorfileuploader.tsx"],"errors":true,"version":"5.6.3"} | ||
{"root":["./src/nestiaeditorapplication.tsx","./src/nestiaeditoriframe.tsx","./src/nestiaeditormodule.ts","./src/nestiaeditoruploader.tsx","./src/index.ts","./src/main.tsx","./src/vite-env.d.ts","./src/internal/nestiaeditorcomposer.ts","./src/internal/nestiaeditorfileuploader.tsx"],"version":"5.6.3"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "./tsconfig.lib.json", | ||
"compilerOptions": { | ||
"target": "ES2015", | ||
"module": "CommonJS", | ||
"outDir": "bin", | ||
"noEmit": true, | ||
}, | ||
"include": ["src", "test"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.