Skip to content

Commit 7176815

Browse files
committed
Revert "Updated .d.ts, js files. Left to update one function and write tests"
This reverts commit f1250f2.
1 parent 23a9ae4 commit 7176815

File tree

3 files changed

+79
-96
lines changed

3 files changed

+79
-96
lines changed

index.cjs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ var { OpenApiBuilder } = require("openapi3-ts/oas31"),
22
{
33
DeclarativeResponse,
44
registerAbort,
5+
seeOtherMethods,
56
} = require("@ublitzjs/core"),
6-
{ sendFile } = require("@ublitzjs/static"),
7+
{ basicSendFile, sendFile } = require("@ublitzjs/static"),
78
{ staticServe, analyzeFolder } = require("@ublitzjs/static/serving"),
89
{ exit } = require("node:process"),
9-
{ createWriteStream } = require("node:fs"),
10-
{ stat } = require("node:fs/promises");
11-
var serverExtension = (opts) => ({
12-
openapi: {
13-
builder: new OpenApiBuilder(opts),
14-
async serve(prefix, opts = {}) {
10+
{ createWriteStream, write } = require("node:fs"),
11+
path = require("node:path"),
12+
{ stat } = require("node:fs/promises"),
13+
serverExtension = (opts) => ({
14+
openApiBuilder: new OpenApiBuilder(opts),
15+
async serveOpenApi(prefix, opts = {}) {
1516
var specController;
1617
if (opts.path) {
1718
var length;
@@ -56,8 +57,8 @@ var serverExtension = (opts) => ({
5657
)
5758
);
5859
},
59-
async build(filePath, exitFromNodejs) {
60-
var spec = this.builder.getSpecAsJson();
60+
async buildOpenApi(filePath, exitFromNodejs) {
61+
var spec = this.openApiBuilder.getSpecAsJson();
6162
delete this.openApiBuilder;
6263
var writeStream = createWriteStream(filePath);
6364
var offset = 0;
@@ -67,17 +68,15 @@ var serverExtension = (opts) => ({
6768
if (!ok)
6869
await new Promise((resolve) => writeStream.once("drain", resolve));
6970
}
70-
7171
return new Promise((resolve) => {
7272
writeStream.end(() => {
7373
if (exitFromNodejs) exit(0);
7474
resolve(spec.length);
7575
});
7676
});
7777
},
78-
},
79-
});
80-
var toOpenapiPath = (v) => v.replace(/:([a-zA-Z0-9_]+)/g, "{$1}");
78+
}),
79+
toOpenapiPath = (v) => v.replace(/:([a-zA-Z0-9_]+)/g, "{$1}");
8180
function RouterPlugin(methods) {
8281
var route = this.paths[this._currentPath];
8382
var openApiPath = route?.openapi || {};

index.d.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,21 @@ export type methodAddOns = { openapi?: Partial<OperationObject> };
9696
* @see https://github.com/ublitzjs/openapi/blob/main/examples/index.ts
9797
*/
9898
export function serverExtension(opts: OpenAPIObject): {
99-
openapi: {
100-
/**
101-
* Builder from "openapi3-ts" npm package. You can use it to dynamically insert paths, components, etc.
102-
* */
103-
builder: OpenApiBuilder;
104-
/**
105-
* @param prefix prefix for openapi UI to be served at.
106-
* @param opts : build (if to save open api to some file and serve it after this), path (to an openapi json file), clearMimes (same as in 'static' package "clearMimesList"), uiPath - path to folder with ui (defaults to node_modules/@ublitzjs/openapi/ui)
107-
*/
108-
serve(
109-
/**
110-
* url on which openapi will be served. openapi.json is served on http://hostname:port/PREFIX/openapi.json. Main page - http://hostname:port/PREFIX/ AND It ENDS with slash (uWS's quirk, not mine) OR http://hostname:port/PREFIX/index.html
111-
* */
112-
prefix: string,
113-
opts?: {
114-
build?: boolean;
115-
path?: string;
116-
clearMimes?: boolean;
117-
uiPath?: string;
118-
}
119-
): Promise<void>;
120-
/**
121-
* build openapi.json file in specified filePath AND if needed - exit afterwards.
122-
* @returns File size
123-
* */
124-
build(filePath: string, exitFromNodejs: boolean): Promise<number>;
125-
126-
}
99+
openApiBuilder: OpenApiBuilder;
100+
/**
101+
* @param prefix url on which openapi will be served. You should note that an access to html page is kinda tricky: if prefix = "/docs", then you can't access http://localhost:port/docs BUT can access http://localhost:port/docs/ with the last slash. Or, if you want, just use http://localhost:port/docs/index.html - definitely works. Such a peculiarity of uWS wildcards.
102+
* @param opts use if don't need to dynamically take openapi from code. Properties: build (whether to build an openapi and to serve after this), path (to an openapi json file), clearMimes (same as in 'static' package "clearMimesList"), uiPath - path to folder with ui (defaults to node_modules/@ublitzjs/openapi/ui)
103+
*/
104+
serveOpenApi(
105+
prefix: string,
106+
opts?: {
107+
build?: boolean;
108+
path?: string;
109+
clearMimes?: boolean;
110+
uiPath?: string;
111+
}
112+
): Promise<void>;
113+
buildOpenApi(filePath: string, exitFromNodejs: boolean): Promise<number>;
127114
};
128115
/**
129116
* This function goes to ExtendedRouter from @ublitzjs/router. registers all methods to openapi
@@ -137,8 +124,6 @@ export function RouterPlugin(methods: string[]): void;
137124
export function routePlugin<method extends HttpMethods>(
138125
route: routeFNOpts<method> & methodAddOns,
139126
server: Server & {
140-
openapi: {
141-
builder: OpenApiBuilder;
142-
}
127+
openApiBuilder: OpenApiBuilder;
143128
}
144129
): void;

index.mjs

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,63 @@ import { OpenApiBuilder } from "openapi3-ts/oas31";
22
import {
33
DeclarativeResponse,
44
registerAbort,
5+
seeOtherMethods,
56
} from "@ublitzjs/core";
6-
import { sendFile } from "@ublitzjs/static";
7+
import { basicSendFile, sendFile } from "@ublitzjs/static";
78
import { staticServe, analyzeFolder } from "@ublitzjs/static/serving";
89
import { exit } from "node:process";
9-
import { createWriteStream } from "node:fs";
10+
import { createWriteStream, write } from "node:fs";
11+
import path from "node:path";
1012
import { stat } from "node:fs/promises";
1113
var serverExtension = (opts) => ({
12-
openapi: {
13-
builder: new OpenApiBuilder(opts),
14-
async serve(prefix, opts = {}) {
15-
var specController;
16-
if (opts.path) {
17-
var length;
18-
if (opts.build) length = await this.buildOpenApi(opts.path, false);
19-
else length = (await stat(opts.path)).size;
20-
specController = async (res) => {
21-
registerAbort(res);
22-
await sendFile({
23-
res,
24-
path: opts.path,
25-
contentType: "application/json",
26-
maxSize: length,
27-
});
28-
};
29-
} else {
30-
const spec = this.openApiBuilder.getSpecAsJson();
31-
delete this.openApiBuilder;
32-
specController = new DeclarativeResponse()
33-
.writeHeaders({ "Content-Type": "application/json" })
34-
.end(spec);
35-
}
36-
var HERE = opts.uiPath || "node_modules/@ublitzjs/openapi/ui";
37-
var paths = await analyzeFolder(HERE, {
38-
deleteMimesList: opts?.clearMimes,
39-
});
40-
const methods = staticServe({
41-
paths,
42-
fullRoute: prefix,
43-
dirPath: HERE,
44-
});
14+
openApiBuilder: new OpenApiBuilder(opts),
15+
async serveOpenApi(prefix, opts = {}) {
16+
var specController;
17+
if (opts.path) {
18+
var length;
19+
if (opts.build) length = await this.buildOpenApi(opts.path, false);
20+
else length = (await stat(opts.path)).size;
21+
specController = async (res) => {
22+
registerAbort(res);
23+
await sendFile({
24+
res,
25+
path: opts.path,
26+
contentType: "application/json",
27+
maxSize: length,
28+
});
29+
};
30+
} else {
31+
const spec = this.openApiBuilder.getSpecAsJson();
32+
delete this.openApiBuilder;
33+
specController = new DeclarativeResponse()
34+
.writeHeaders({ "Content-Type": "application/json" })
35+
.end(spec);
36+
}
37+
var HERE = opts.uiPath || "node_modules/@ublitzjs/openapi/ui";
38+
var paths = await analyzeFolder(HERE, {
39+
deleteMimesList: opts?.clearMimes,
40+
});
41+
const methods = staticServe({
42+
paths,
43+
fullRoute: prefix,
44+
dirPath: HERE,
45+
});
4546

46-
this.get(prefix + "/*", methods.get)
47-
.head(prefix + "/*", methods.head)
48-
.any(prefix + "/*", methods.any)
49-
.get(prefix + "/openapi.json", specController)
50-
.get(
51-
prefix + "/swagger-initializer.js",
52-
new DeclarativeResponse()
53-
.writeHeader("Content-Type", "text/javascript")
54-
.end(
55-
`window.onload = async function(){window.ui=SwaggerUIBundle({url:"${prefix}/openapi.json",dom_id:"#swagger-ui",deepLinking:true,presets:[SwaggerUIBundle.presets.apis,SwaggerUIStandalonePreset],plugins:[SwaggerUIBundle.plugins.DownloadUrl],layout:"StandaloneLayout"})}`
56-
)
57-
);
58-
},
59-
async build(filePath, exitFromNodejs) {
60-
var spec = this.builder.getSpecAsJson();
47+
this.get(prefix + "/*", methods.get)
48+
.head(prefix + "/*", methods.head)
49+
.any(prefix + "/*", methods.any)
50+
.get(prefix + "/openapi.json", specController)
51+
.get(
52+
prefix + "/swagger-initializer.js",
53+
new DeclarativeResponse()
54+
.writeHeader("Content-Type", "text/javascript")
55+
.end(
56+
`window.onload = async function(){window.ui=SwaggerUIBundle({url:"${prefix}/openapi.json",dom_id:"#swagger-ui",deepLinking:true,presets:[SwaggerUIBundle.presets.apis,SwaggerUIStandalonePreset],plugins:[SwaggerUIBundle.plugins.DownloadUrl],layout:"StandaloneLayout"})}`
57+
)
58+
);
59+
},
60+
async buildOpenApi(filePath, exitFromNodejs) {
61+
var spec = this.openApiBuilder.getSpecAsJson();
6162
delete this.openApiBuilder;
6263
var writeStream = createWriteStream(filePath);
6364
var offset = 0;
@@ -67,15 +68,13 @@ var serverExtension = (opts) => ({
6768
if (!ok)
6869
await new Promise((resolve) => writeStream.once("drain", resolve));
6970
}
70-
7171
return new Promise((resolve) => {
7272
writeStream.end(() => {
7373
if (exitFromNodejs) exit(0);
7474
resolve(spec.length);
7575
});
7676
});
7777
},
78-
},
7978
});
8079
var toOpenapiPath = (v) => v.replace(/:([a-zA-Z0-9_]+)/g, "{$1}");
8180
function RouterPlugin(methods) {

0 commit comments

Comments
 (0)