|
| 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 | +}; |
0 commit comments