-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
187 lines (172 loc) · 6.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import path from 'path';
import bodyParser from 'body-parser';
import fs from 'fs-extra';
import type * as http from 'http';
import type { Application, Request, RequestHandler, Response } from 'express';
import type { Query, RouteParameters } from 'express-serve-static-core';
import errorhandler from 'errorhandler';
export interface MiddlewareOptions {
name: string;
urlPath?: string;
reportDir?: string;
root?: string;
}
export interface SetupMiddlewareOptions extends MiddlewareOptions {
buildHandlers?(
options: MiddlewareOptions
): Array<
RequestHandler<
RouteParameters<string>,
any,
any,
Query,
Record<string, any>
>
>;
}
interface EmberCliAddon {
project: { root: string };
}
/**
* Sets up the middleware hooks that are required by the ember-cli addon. The return value of this function should
* be merged into the object returned from an ember app or addon's index.js file.
*
* @example
*
* 'use strict';
*
* const { setupMiddlewareHooks } = require('ember-setup-middleware-reporter');
*
* module.exports = {
* name: require('./package').name,
*
* ...setupMiddlewareHooks({
* name: 'deprecations',
* reportDir: 'deprecation-reports'
* }),
* };
*
* @param options - An options object that contains necessary information for the middleware to run.
* @param options.name - The name of the middleware.
* @param [options.urlPath] - The url that the middleware should respond to. If url is not provided, options.name will be used.
* @param [options.reportDir] - The directory where the reports should be written to. If reportDir is not provided, options.name will be used.
* @param [options.buildHandlers] - A function that takes the options object and returns an array of handlers.
* @returns An object containing a serverMiddleware and testemMiddleware functions that setup the middleware.
*/
export function setupMiddlewareHooks(
options: SetupMiddlewareOptions
): Record<string, any> {
return {
serverMiddleware(
this: EmberCliAddon,
startOptions: { app: Application; server: http.Server }
) {
setupMiddleware(startOptions.app, {
root: this.project.root,
...options,
});
},
testemMiddleware(this: EmberCliAddon, app: Application) {
setupMiddleware(app, {
root: this.project.root,
...options,
});
},
};
}
/**
* A utility function that sets up posting to a specific middleware endpoint for the ember-cli addon.
*
* @param app - The express application.
* @param options - An options object that contains necessary information for the middleware to run.
* @param options.name - The name of the middleware.
* @param [options.urlPath] - The url that the middleware should respond to. If url is not provided, options.name will be used.
* @param [options.reportDir] - The directory where the reports should be written to. If reportDir is not provided, options.name will be used.
* @param [options.buildHandlers] - A function that takes the options object and returns an array of handlers.
* @returns An object containing a serverMiddleware and testemMiddleware functions that setup the middleware.
*/
export function setupMiddleware(
app: Application,
options: SetupMiddlewareOptions
): void {
const buildHandlers = options.buildHandlers ?? buildDefaultHandlers;
app.post(buildUrlPathFromOptions(options), ...buildHandlers(app, options));
}
/**
* Builds an array of default handlers that can be leveraged out of the box by the ember-cli addon.
* The default handlers include one that writes the response to a file.
*
* @param app - The express application.
* @param options - An options object that contains necessary information for the middleware to run.
* @param options.name - The name of the middleware.
* @param [options.urlPath] - The url that the middleware should respond to. If url is not provided, options.name will be used.
* @param [options.reportDir] - The directory where the reports should be written to. If reportDir is not provided, options.name will be used.
* @returns An array of default handlers.
*/
export function buildDefaultHandlers(
app: Application,
options: MiddlewareOptions
): Array<
RequestHandler<RouteParameters<string>, any, any, Query, Record<string, any>>
> {
app.use(errorhandler());
return [
bodyParser.json({ limit: '50mb' }),
(req: Request, res: Response) => {
const outputDir = buildReportDirFromOptions(options);
const REPORT_TIMESTAMP = new Date().toISOString().replace(/[.:]/g, '-');
const outputPath = path.resolve(
path.join(outputDir, `${REPORT_TIMESTAMP}.json`)
);
fs.ensureDirSync(outputDir);
fs.writeJsonSync(outputPath, req.body);
res.send({
outputPath,
});
},
];
}
/**
* Builds the root directory for the middleware report.
*
* @param options - An options object that contains necessary information for the middleware to run.
* @param options.name - The name of the middleware.
* @param [options.urlPath] - The url that the middleware should respond to. If url is not provided, options.name will be used.
* @param [options.reportDir] - The directory where the reports should be written to. If reportDir is not provided, options.name will be used.
* @returns A string containing the root directory for the middleware report.
*/
export function buildRootFromOptions(options: MiddlewareOptions): string {
return options.root ?? process.cwd();
}
/**
* Builds the URL for the middleware report.
*
* @param options - An options object that contains necessary information for the middleware to run.
* @param options.name - The name of the middleware.
* @param [options.urlPath] - The url that the middleware should respond to. If url is not provided, options.name will be used.
* @param [options.reportDir] - The directory where the reports should be written to. If reportDir is not provided, options.name will be used.
* @returns A string containing the url for the middleware report.
*/
export function buildUrlPathFromOptions(options: MiddlewareOptions): string {
if (options.urlPath) {
return options.urlPath.charAt(0) !== '/'
? `/${options.urlPath}`
: options.urlPath;
}
return `/${options.name}`;
}
/**
* Builds the report directory for the middleware report.
*
* @param options - An options object that contains necessary information for the middleware to run.
* @param options.name - The name of the middleware.
* @param [options.urlPath] - The url that the middleware should respond to. If url is not provided, options.name will be used.
* @param [options.reportDir] - The directory where the reports should be written to. If reportDir is not provided, options.name will be used.
* @returns A string containing the report directory for the middleware report.
*/
export function buildReportDirFromOptions(options: MiddlewareOptions): string {
return path.join(
buildRootFromOptions(options),
options.reportDir ?? options.name
);
}