1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const ts_morph_1 = require("ts-morph");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function getSelfAndLocalDependencies(file, visitedFiles = []) {
|
12 | return (file
|
13 | .getImportDeclarations()
|
14 |
|
15 | .filter(id => id.isModuleSpecifierRelative())
|
16 |
|
17 |
|
18 | .map(id => id.getModuleSpecifierSourceFileOrThrow())
|
19 | .reduce((acc, curr) => {
|
20 | if (acc.some(f => f.getFilePath() === curr.getFilePath())) {
|
21 | return acc;
|
22 | }
|
23 | else {
|
24 | return getSelfAndLocalDependencies(curr, acc);
|
25 | }
|
26 | }, visitedFiles.concat(file)));
|
27 | }
|
28 | exports.getSelfAndLocalDependencies = getSelfAndLocalDependencies;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function getClassWithDecoratorOrThrow(file, decoratorName) {
|
36 | const matchingKlasses = file
|
37 | .getClasses()
|
38 | .filter(k => k.getDecorator(decoratorName) !== undefined);
|
39 | if (matchingKlasses.length !== 1) {
|
40 | throw new Error(`expected a decorator @${decoratorName} to be used once, found ${matchingKlasses.length} usages`);
|
41 | }
|
42 | return matchingKlasses[0];
|
43 | }
|
44 | exports.getClassWithDecoratorOrThrow = getClassWithDecoratorOrThrow;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function getPropertyWithDecorator(klass, decoratorName) {
|
53 | const matchingProps = klass
|
54 | .getProperties()
|
55 | .filter(p => p.getDecorator(decoratorName) !== undefined);
|
56 | if (matchingProps.length > 1) {
|
57 | throw new Error(`expected a decorator @${decoratorName} to be used only once, found ${matchingProps.length} usages`);
|
58 | }
|
59 | return matchingProps.length === 1 ? matchingProps[0] : undefined;
|
60 | }
|
61 | exports.getPropertyWithDecorator = getPropertyWithDecorator;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function getMethodWithDecorator(klass, decoratorName) {
|
69 | const matchingMethods = klass
|
70 | .getMethods()
|
71 | .filter(m => m.getDecorator(decoratorName) !== undefined);
|
72 | if (matchingMethods.length > 1) {
|
73 | throw new Error(`expected a decorator @${decoratorName} to be used only once, found ${matchingMethods.length} usages`);
|
74 | }
|
75 | return matchingMethods.length === 1 ? matchingMethods[0] : undefined;
|
76 | }
|
77 | exports.getMethodWithDecorator = getMethodWithDecorator;
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function getParamWithDecorator(method, decoratorName) {
|
86 | const matchingParams = method
|
87 | .getParameters()
|
88 | .filter(p => p.getDecorator(decoratorName) !== undefined);
|
89 | if (matchingParams.length > 1) {
|
90 | throw new Error(`expected a decorator @${decoratorName} to be used only once, found ${matchingParams.length} usages`);
|
91 | }
|
92 | return matchingParams.length === 1 ? matchingParams[0] : undefined;
|
93 | }
|
94 | exports.getParamWithDecorator = getParamWithDecorator;
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function getParameterTypeAsTypeLiteralOrThrow(parameter) {
|
102 | const typeNode = parameter.getTypeNodeOrThrow();
|
103 | if (!ts_morph_1.TypeGuards.isTypeLiteralNode(typeNode)) {
|
104 | throw new Error("expected parameter value to be an type literal object");
|
105 | }
|
106 | return typeNode;
|
107 | }
|
108 | exports.getParameterTypeAsTypeLiteralOrThrow = getParameterTypeAsTypeLiteralOrThrow;
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function getDecoratorConfigOrThrow(decorator) {
|
118 |
|
119 | if (!decorator.isDecoratorFactory()) {
|
120 | throw new Error("expected decorator factory");
|
121 | }
|
122 |
|
123 | const decoratorArgs = decorator.getArguments();
|
124 | if (decoratorArgs.length !== 1) {
|
125 | throw new Error(`expected exactly one argument, got ${decoratorArgs.length}`);
|
126 | }
|
127 |
|
128 | const decoratorArg = decoratorArgs[0];
|
129 | if (!ts_morph_1.TypeGuards.isObjectLiteralExpression(decoratorArg)) {
|
130 | throw new Error(`expected decorator factory configuration argument to be an object literal`);
|
131 | }
|
132 | return decoratorArg;
|
133 | }
|
134 | exports.getDecoratorConfigOrThrow = getDecoratorConfigOrThrow;
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function getObjLiteralProp(objectLiteral, propertyName) {
|
145 | const property = objectLiteral.getProperty(propertyName);
|
146 | if (!property) {
|
147 | return undefined;
|
148 | }
|
149 | if (!ts_morph_1.TypeGuards.isPropertyAssignment(property)) {
|
150 | throw new Error("expected property assignment");
|
151 | }
|
152 | return property;
|
153 | }
|
154 | exports.getObjLiteralProp = getObjLiteralProp;
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | function getObjLiteralPropOrThrow(objectLiteral, propertyName) {
|
164 | const property = objectLiteral.getPropertyOrThrow(propertyName);
|
165 | if (!ts_morph_1.TypeGuards.isPropertyAssignment(property)) {
|
166 | throw new Error("expected property assignment");
|
167 | }
|
168 | return property;
|
169 | }
|
170 | exports.getObjLiteralPropOrThrow = getObjLiteralPropOrThrow;
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function getPropValueAsStringOrThrow(property) {
|
178 | return property.getInitializerIfKindOrThrow(ts_morph_1.ts.SyntaxKind.StringLiteral);
|
179 | }
|
180 | exports.getPropValueAsStringOrThrow = getPropValueAsStringOrThrow;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | function getPropValueAsNumberOrThrow(property) {
|
187 | return property.getInitializerIfKindOrThrow(ts_morph_1.ts.SyntaxKind.NumericLiteral);
|
188 | }
|
189 | exports.getPropValueAsNumberOrThrow = getPropValueAsNumberOrThrow;
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | function getPropValueAsArrayOrThrow(property) {
|
196 | return property.getInitializerIfKindOrThrow(ts_morph_1.ts.SyntaxKind.ArrayLiteralExpression);
|
197 | }
|
198 | exports.getPropValueAsArrayOrThrow = getPropValueAsArrayOrThrow;
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | function getPropValueAsObjectOrThrow(property) {
|
205 | return property.getInitializerIfKindOrThrow(ts_morph_1.ts.SyntaxKind.ObjectLiteralExpression);
|
206 | }
|
207 | exports.getPropValueAsObjectOrThrow = getPropValueAsObjectOrThrow;
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function getPropertyName(property) {
|
215 | return property.getNameNode().getSymbolOrThrow().getEscapedName();
|
216 | }
|
217 | exports.getPropertyName = getPropertyName;
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | function getJsDoc(node) {
|
226 | const jsDocs = node.getJsDocs();
|
227 | if (jsDocs.length > 1) {
|
228 | throw new Error(`expected at most 1 jsDoc node, got ${jsDocs.length}`);
|
229 | }
|
230 | else if (jsDocs.length === 1) {
|
231 | return jsDocs[0];
|
232 | }
|
233 | return undefined;
|
234 | }
|
235 | exports.getJsDoc = getJsDoc;
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function isHttpMethod(method) {
|
243 | switch (method) {
|
244 | case "GET":
|
245 | case "POST":
|
246 | case "PUT":
|
247 | case "PATCH":
|
248 | case "DELETE":
|
249 | return true;
|
250 | default:
|
251 | return false;
|
252 | }
|
253 | }
|
254 | exports.isHttpMethod = isHttpMethod;
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function isQueryParamArrayStrategy(strategy) {
|
261 | switch (strategy) {
|
262 | case "ampersand":
|
263 | case "comma":
|
264 | return true;
|
265 | default:
|
266 | return false;
|
267 | }
|
268 | }
|
269 | exports.isQueryParamArrayStrategy = isQueryParamArrayStrategy;
|