@pikku/inspector 0.6.4 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/add-channel.d.ts +12 -2
- package/dist/add-channel.js +336 -109
- package/dist/add-functions.d.ts +7 -0
- package/dist/add-functions.js +269 -0
- package/dist/add-http-route.d.ts +15 -3
- package/dist/add-http-route.js +69 -80
- package/dist/add-schedule.d.ts +1 -1
- package/dist/add-schedule.js +14 -4
- package/dist/inspector.js +14 -4
- package/dist/types.d.ts +7 -10
- package/dist/utils.d.ts +21 -27
- package/dist/utils.js +631 -211
- package/dist/visit.d.ts +2 -1
- package/dist/visit.js +9 -4
- package/package.json +2 -2
- package/src/add-channel.ts +442 -140
- package/src/add-functions.ts +376 -0
- package/src/add-http-route.ts +94 -109
- package/src/add-schedule.ts +24 -4
- package/src/inspector.ts +17 -5
- package/src/types.ts +8 -12
- package/src/utils.ts +778 -286
- package/src/visit.ts +16 -6
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { extractFunctionName, getPropertyAssignmentInitializer, } from './utils.js';
|
|
3
|
+
const isValidVariableName = (name) => {
|
|
4
|
+
const regex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
5
|
+
return regex.test(name);
|
|
6
|
+
};
|
|
7
|
+
const nullifyTypes = (type) => {
|
|
8
|
+
if (type === 'void' ||
|
|
9
|
+
type === 'undefined' ||
|
|
10
|
+
type === 'unknown' ||
|
|
11
|
+
type === 'any') {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return type;
|
|
15
|
+
};
|
|
16
|
+
const resolveTypeImports = (type, resolvedTypes, isCustom) => {
|
|
17
|
+
const types = [];
|
|
18
|
+
const visitType = (currentType) => {
|
|
19
|
+
const symbol = currentType.aliasSymbol || currentType.getSymbol();
|
|
20
|
+
if (symbol) {
|
|
21
|
+
const declarations = symbol.getDeclarations();
|
|
22
|
+
const declaration = declarations?.[0];
|
|
23
|
+
if (declaration) {
|
|
24
|
+
const sourceFile = declaration.getSourceFile();
|
|
25
|
+
const path = sourceFile.fileName;
|
|
26
|
+
// Skip built-in utility types or TypeScript lib types
|
|
27
|
+
if (!path.includes('node_modules/typescript') &&
|
|
28
|
+
symbol.getName() !== '__type' &&
|
|
29
|
+
!isPrimitiveType(currentType)) {
|
|
30
|
+
const originalName = symbol.getName();
|
|
31
|
+
// Check if the type is already in the map
|
|
32
|
+
let uniqueName = resolvedTypes.exists(originalName, path);
|
|
33
|
+
if (!uniqueName) {
|
|
34
|
+
if (isCustom) {
|
|
35
|
+
uniqueName = resolvedTypes.addUniqueType(originalName, path);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
resolvedTypes.addType(originalName, path);
|
|
39
|
+
uniqueName = originalName;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
types.push(uniqueName);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (isCustom) {
|
|
47
|
+
// Handle nested utility types like Partial, Pick, etc.
|
|
48
|
+
if (currentType.aliasTypeArguments) {
|
|
49
|
+
currentType.aliasTypeArguments.forEach(visitType);
|
|
50
|
+
}
|
|
51
|
+
// Handle intersections and unions
|
|
52
|
+
if (currentType.isUnionOrIntersection()) {
|
|
53
|
+
currentType.types.forEach(visitType);
|
|
54
|
+
}
|
|
55
|
+
// Handle object types with type arguments
|
|
56
|
+
if (currentType.flags & ts.TypeFlags.Object &&
|
|
57
|
+
currentType.objectFlags & ts.ObjectFlags.Reference) {
|
|
58
|
+
const typeRef = currentType;
|
|
59
|
+
typeRef.typeArguments?.forEach(visitType);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
visitType(type);
|
|
64
|
+
return types;
|
|
65
|
+
};
|
|
66
|
+
const resolveUnionTypes = (checker, type) => {
|
|
67
|
+
const types = [];
|
|
68
|
+
const names = [];
|
|
69
|
+
// Check if it's a union type AND not part of an intersection
|
|
70
|
+
if (type.isUnion() && !(type.flags & ts.TypeFlags.Intersection)) {
|
|
71
|
+
for (const t of type.types) {
|
|
72
|
+
const name = nullifyTypes(checker.typeToString(t));
|
|
73
|
+
if (name) {
|
|
74
|
+
types.push(t);
|
|
75
|
+
names.push(name);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
const name = nullifyTypes(checker.typeToString(type));
|
|
81
|
+
if (name) {
|
|
82
|
+
types.push(type);
|
|
83
|
+
names.push(name);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { types, names };
|
|
87
|
+
};
|
|
88
|
+
const getNamesAndTypes = (checker, typesMap, direction, funcName, type) => {
|
|
89
|
+
if (!type) {
|
|
90
|
+
return { names: [], types: [] };
|
|
91
|
+
}
|
|
92
|
+
// 1) Handle an explicit void (or undefined) type up front
|
|
93
|
+
if (type.flags & ts.TypeFlags.VoidLike) {
|
|
94
|
+
return {
|
|
95
|
+
names: ['void'],
|
|
96
|
+
types: [type],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// 2) For unions, resolve all member names/types
|
|
100
|
+
const { names: rawNames, types: rawTypes } = resolveUnionTypes(checker, type);
|
|
101
|
+
// If the union is exactly [void], we'd have caught it above.
|
|
102
|
+
// If it's e.g. [string, void], rawNames should already include 'void'.
|
|
103
|
+
// 3) If multiple names or the single name isn't a valid identifier,
|
|
104
|
+
// we emit an alias type.
|
|
105
|
+
const firstName = rawNames[0];
|
|
106
|
+
if (rawNames.length > 1 || (firstName && !isValidVariableName(firstName))) {
|
|
107
|
+
const aliasType = rawNames.join(' | ');
|
|
108
|
+
const aliasName = funcName.charAt(0).toUpperCase() + funcName.slice(1) + direction;
|
|
109
|
+
// record the alias in your TypesMap
|
|
110
|
+
const references = rawTypes
|
|
111
|
+
.map((t) => resolveTypeImports(t, typesMap, true))
|
|
112
|
+
.flat();
|
|
113
|
+
typesMap.addCustomType(aliasName, aliasType, references);
|
|
114
|
+
return {
|
|
115
|
+
names: [aliasName],
|
|
116
|
+
types: rawTypes,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// 4) Single, valid name → inline it
|
|
120
|
+
const uniqueNames = rawNames
|
|
121
|
+
.map((name, i) => {
|
|
122
|
+
const t = rawTypes[i];
|
|
123
|
+
if (!t) {
|
|
124
|
+
throw new Error(`Expected type for name "${name}" in ${funcName}`);
|
|
125
|
+
}
|
|
126
|
+
if (isPrimitiveType(t)) {
|
|
127
|
+
return name;
|
|
128
|
+
}
|
|
129
|
+
// non-primitive: import/alias it inline
|
|
130
|
+
return resolveTypeImports(t, typesMap, false);
|
|
131
|
+
})
|
|
132
|
+
.flat();
|
|
133
|
+
return {
|
|
134
|
+
names: uniqueNames,
|
|
135
|
+
types: rawTypes,
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
const isPrimitiveType = (type) => {
|
|
139
|
+
const primitiveFlags = ts.TypeFlags.Number |
|
|
140
|
+
ts.TypeFlags.String |
|
|
141
|
+
ts.TypeFlags.Boolean |
|
|
142
|
+
ts.TypeFlags.BigInt |
|
|
143
|
+
ts.TypeFlags.ESSymbol |
|
|
144
|
+
ts.TypeFlags.Void |
|
|
145
|
+
ts.TypeFlags.Undefined |
|
|
146
|
+
ts.TypeFlags.Null |
|
|
147
|
+
ts.TypeFlags.Any |
|
|
148
|
+
ts.TypeFlags.Unknown |
|
|
149
|
+
ts.TypeFlags.VoidLike;
|
|
150
|
+
return (type.flags & primitiveFlags) !== 0;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* If `type` is a `Promise<T>`, return `T`, otherwise return `type` itself.
|
|
154
|
+
*/
|
|
155
|
+
function unwrapPromise(checker, type) {
|
|
156
|
+
if (!type?.symbol)
|
|
157
|
+
return type;
|
|
158
|
+
const isPromise = type.symbol.name === 'Promise' &&
|
|
159
|
+
checker.getFullyQualifiedName(type.symbol).includes('Promise');
|
|
160
|
+
// aliasTypeArguments covers most Promise<T> cases
|
|
161
|
+
if (isPromise && type.aliasTypeArguments?.length === 1) {
|
|
162
|
+
return type.aliasTypeArguments[0];
|
|
163
|
+
}
|
|
164
|
+
// fallback for raw TypeReference
|
|
165
|
+
if (isPromise && type.typeArguments?.length === 1) {
|
|
166
|
+
return type.typeArguments[0];
|
|
167
|
+
}
|
|
168
|
+
return type;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Inspect pikkuFunc calls, extract input/output and first-arg destructuring,
|
|
172
|
+
* then push into state.functions.meta.
|
|
173
|
+
*/
|
|
174
|
+
export function addFunctions(node, checker, state, filters) {
|
|
175
|
+
if (!ts.isCallExpression(node))
|
|
176
|
+
return;
|
|
177
|
+
const { expression, arguments: args, typeArguments } = node;
|
|
178
|
+
// only handle calls like pikkuFunc(...)
|
|
179
|
+
if (!ts.isIdentifier(expression)) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
// Match identifiers that contain both "pikku" and "func" (case insensitive)
|
|
183
|
+
const pikkuFuncPattern = /pikku.*func/i;
|
|
184
|
+
if (!pikkuFuncPattern.test(expression.text)) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
// only handle calls like pikkuFunc(...)
|
|
188
|
+
if (!ts.isIdentifier(expression) || !expression.text.startsWith('pikku')) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (args.length === 0)
|
|
192
|
+
return;
|
|
193
|
+
const { pikkuFuncName, name } = extractFunctionName(node, checker);
|
|
194
|
+
// determine the actual handler expression:
|
|
195
|
+
// either the `func` prop or the first argument directly
|
|
196
|
+
let handlerNode = args[0];
|
|
197
|
+
if (ts.isObjectLiteralExpression(handlerNode)) {
|
|
198
|
+
const fnProp = getPropertyAssignmentInitializer(handlerNode, 'func', true, checker);
|
|
199
|
+
if (!fnProp ||
|
|
200
|
+
(!ts.isArrowFunction(fnProp) && !ts.isFunctionExpression(fnProp))) {
|
|
201
|
+
console.error(`• No valid 'func' property found for ${pikkuFuncName}.`);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
handlerNode = fnProp;
|
|
205
|
+
}
|
|
206
|
+
if (!ts.isArrowFunction(handlerNode) &&
|
|
207
|
+
!ts.isFunctionExpression(handlerNode)) {
|
|
208
|
+
console.error(`• Handler for ${name} is not a function.`);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const services = {
|
|
212
|
+
optimized: true,
|
|
213
|
+
services: [],
|
|
214
|
+
};
|
|
215
|
+
const firstParam = handlerNode.parameters[0];
|
|
216
|
+
if (firstParam) {
|
|
217
|
+
if (ts.isObjectBindingPattern(firstParam.name)) {
|
|
218
|
+
for (const elem of firstParam.name.elements) {
|
|
219
|
+
const original = elem.propertyName && ts.isIdentifier(elem.propertyName)
|
|
220
|
+
? elem.propertyName.text
|
|
221
|
+
: ts.isIdentifier(elem.name)
|
|
222
|
+
? elem.name.text
|
|
223
|
+
: undefined;
|
|
224
|
+
if (original) {
|
|
225
|
+
services.services.push(original);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else if (ts.isIdentifier(firstParam.name) &&
|
|
230
|
+
!firstParam.name.text.startsWith('_')) {
|
|
231
|
+
services.optimized = false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// --- Generics → ts.Type[], unwrapped from Promise ---
|
|
235
|
+
const genericTypes = (typeArguments ?? [])
|
|
236
|
+
.map((tn) => checker.getTypeFromTypeNode(tn))
|
|
237
|
+
.map((t) => unwrapPromise(checker, t));
|
|
238
|
+
// --- Input Extraction ---
|
|
239
|
+
let { names: inputNames, types: inputTypes } = getNamesAndTypes(checker, state.functions.typesMap, 'Input', name, genericTypes[0]);
|
|
240
|
+
if (inputTypes.length === 0) {
|
|
241
|
+
console.warn(`\x1b[31m• Unknown input type for '${name}', assuming void.\x1b[0m`);
|
|
242
|
+
}
|
|
243
|
+
// --- Output Extraction ---
|
|
244
|
+
let outputNames = [];
|
|
245
|
+
if (genericTypes.length >= 2) {
|
|
246
|
+
outputNames = getNamesAndTypes(checker, state.functions.typesMap, 'Output', name, genericTypes[1]).names;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
const sig = checker.getSignatureFromDeclaration(handlerNode);
|
|
250
|
+
if (sig) {
|
|
251
|
+
const rawRet = checker.getReturnTypeOfSignature(sig);
|
|
252
|
+
const unwrapped = unwrapPromise(checker, rawRet);
|
|
253
|
+
outputNames = getNamesAndTypes(checker, state.functions.typesMap, 'Output', pikkuFuncName, unwrapped).names;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// --- Record metadata ---
|
|
257
|
+
state.functions.files.add(node.getSourceFile().fileName);
|
|
258
|
+
if (inputNames.length > 1) {
|
|
259
|
+
console.warn('More than one input type detected, only the first one will be used as a schema.');
|
|
260
|
+
}
|
|
261
|
+
state.functions.meta[pikkuFuncName] = {
|
|
262
|
+
pikkuFuncName,
|
|
263
|
+
name,
|
|
264
|
+
services,
|
|
265
|
+
schemaName: inputNames[0] ?? null,
|
|
266
|
+
inputs: inputNames.filter((n) => n !== 'void') ?? null,
|
|
267
|
+
outputs: outputNames.filter((n) => n !== 'void') ?? null,
|
|
268
|
+
};
|
|
269
|
+
}
|
package/dist/add-http-route.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { InspectorState, InspectorFilters } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Populate metaInputTypes for a given route based on method, input type,
|
|
5
|
+
* query and params. Returns undefined (we only mutate metaTypes).
|
|
6
|
+
*/
|
|
7
|
+
export declare const getInputTypes: (metaTypes: Map<string, {
|
|
8
|
+
query?: string[];
|
|
9
|
+
params?: string[];
|
|
10
|
+
body?: string[];
|
|
11
|
+
}>, methodType: string, inputType: string | null, queryValues: string[], paramsValues: string[]) => undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Simplified addHTTPRoute: re-uses function metadata from state.functions.meta
|
|
14
|
+
* instead of re-inferring types here.
|
|
15
|
+
*/
|
|
16
|
+
export declare const addHTTPRoute: (node: ts.Node, checker: ts.TypeChecker, state: InspectorState, filters: InspectorFilters) => void;
|
package/dist/add-http-route.js
CHANGED
|
@@ -1,93 +1,82 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
2
|
import { getPropertyValue } from './get-property-value.js';
|
|
3
3
|
import { pathToRegexp } from 'path-to-regexp';
|
|
4
|
-
import {
|
|
4
|
+
import { extractFunctionName, getPropertyAssignmentInitializer, matchesFilters, } from './utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* Populate metaInputTypes for a given route based on method, input type,
|
|
7
|
+
* query and params. Returns undefined (we only mutate metaTypes).
|
|
8
|
+
*/
|
|
5
9
|
export const getInputTypes = (metaTypes, methodType, inputType, queryValues, paramsValues) => {
|
|
6
|
-
if (!inputType)
|
|
7
|
-
return
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
return undefined;
|
|
10
|
+
if (!inputType)
|
|
11
|
+
return;
|
|
12
|
+
metaTypes.set(inputType, {
|
|
13
|
+
query: queryValues,
|
|
14
|
+
params: paramsValues,
|
|
15
|
+
body: ['post', 'put', 'patch'].includes(methodType)
|
|
16
|
+
? [...new Set([...queryValues, ...paramsValues])]
|
|
17
|
+
: [],
|
|
18
|
+
});
|
|
19
|
+
return;
|
|
19
20
|
};
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Simplified addHTTPRoute: re-uses function metadata from state.functions.meta
|
|
23
|
+
* instead of re-inferring types here.
|
|
24
|
+
*/
|
|
25
|
+
export const addHTTPRoute = (node, checker, state, filters) => {
|
|
26
|
+
// only look at calls
|
|
27
|
+
if (!ts.isCallExpression(node))
|
|
22
28
|
return;
|
|
23
|
-
}
|
|
24
|
-
|
|
29
|
+
const { expression, arguments: args } = node;
|
|
30
|
+
if (!ts.isIdentifier(expression) || expression.text !== 'addHTTPRoute')
|
|
31
|
+
return;
|
|
32
|
+
// must pass an object literal
|
|
25
33
|
const firstArg = args[0];
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
if (!firstArg || !ts.isObjectLiteralExpression(firstArg))
|
|
35
|
+
return;
|
|
36
|
+
const obj = firstArg;
|
|
37
|
+
// --- extract HTTP metadata ---
|
|
38
|
+
const route = getPropertyValue(obj, 'route');
|
|
39
|
+
if (!route)
|
|
40
|
+
return;
|
|
41
|
+
const keys = pathToRegexp(route).keys;
|
|
42
|
+
const params = keys.filter((k) => k.type === 'param').map((k) => k.name);
|
|
43
|
+
const method = getPropertyValue(obj, 'method')?.toLowerCase() || 'get';
|
|
44
|
+
const docs = getPropertyValue(obj, 'docs') || undefined;
|
|
45
|
+
const tags = getPropertyValue(obj, 'tags') || undefined;
|
|
46
|
+
const query = getPropertyValue(obj, 'query') || [];
|
|
47
|
+
if (!matchesFilters(filters, { tags }, { type: 'http', name: route })) {
|
|
29
48
|
return;
|
|
30
49
|
}
|
|
31
|
-
|
|
50
|
+
// --- find the referenced function ---
|
|
51
|
+
const funcInitializer = getPropertyAssignmentInitializer(obj, 'func', true, checker);
|
|
52
|
+
if (!funcInitializer) {
|
|
53
|
+
console.error(`• No valid 'func' property for route '${route}'.`);
|
|
32
54
|
return;
|
|
33
55
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (ts.isObjectLiteralExpression(firstArg)) {
|
|
42
|
-
const obj = firstArg;
|
|
43
|
-
routeValue = getPropertyValue(obj, 'route');
|
|
44
|
-
if (!routeValue) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const { keys } = pathToRegexp(routeValue);
|
|
48
|
-
paramsValues = keys.reduce((result, { type, name }) => {
|
|
49
|
-
if (type === 'param') {
|
|
50
|
-
result.push(name);
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
}, []);
|
|
54
|
-
docs = getPropertyValue(obj, 'docs') || undefined;
|
|
55
|
-
methodValue = getPropertyValue(obj, 'method');
|
|
56
|
-
queryValues = getPropertyValue(obj, 'query') || [];
|
|
57
|
-
tags = getPropertyValue(obj, 'tags') || undefined;
|
|
58
|
-
if (!matchesFilters(filters, { tags }, { type: 'http', name: routeValue })) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
let { inputs, outputs, inputTypes } = getFunctionTypes(checker, obj, {
|
|
62
|
-
funcName: 'func',
|
|
63
|
-
inputIndex: 0,
|
|
64
|
-
outputIndex: 1,
|
|
65
|
-
typesMap: state.http.typesMap,
|
|
66
|
-
});
|
|
67
|
-
const input = inputs ? inputs[0] || null : null;
|
|
68
|
-
const output = outputs ? outputs[0] || null : null;
|
|
69
|
-
if (inputs && inputs?.length > 1) {
|
|
70
|
-
console.error(`Only one input type is currently allowed for method '${methodValue}' and route '${routeValue}': \n\t${inputs.join('\n\t')}`);
|
|
71
|
-
}
|
|
72
|
-
if (outputs && outputs?.length > 1) {
|
|
73
|
-
console.error(`Only one output type is currently allowed for method '${methodValue}' and route '${routeValue}': \n\t${outputs.join('\n\t')}`);
|
|
74
|
-
}
|
|
75
|
-
if (inputTypes[0] && !['post', 'put', 'patch'].includes(methodValue)) {
|
|
76
|
-
queryValues = [
|
|
77
|
-
...new Set([...queryValues, ...extractTypeKeys(inputTypes[0])]),
|
|
78
|
-
].filter((query) => !paramsValues?.includes(query));
|
|
79
|
-
}
|
|
80
|
-
state.http.files.add(node.getSourceFile().fileName);
|
|
81
|
-
state.http.meta.push({
|
|
82
|
-
route: routeValue,
|
|
83
|
-
method: methodValue,
|
|
84
|
-
input,
|
|
85
|
-
output,
|
|
86
|
-
params: paramsValues.length > 0 ? paramsValues : undefined,
|
|
87
|
-
query: queryValues.length > 0 ? queryValues : undefined,
|
|
88
|
-
inputTypes: getInputTypes(state.http.metaInputTypes, methodValue, input, queryValues, paramsValues),
|
|
89
|
-
docs,
|
|
90
|
-
tags,
|
|
91
|
-
});
|
|
56
|
+
const funcName = extractFunctionName(funcInitializer, checker).pikkuFuncName;
|
|
57
|
+
// lookup existing function metadata
|
|
58
|
+
const fnMeta = state.functions.meta[funcName];
|
|
59
|
+
if (!fnMeta) {
|
|
60
|
+
console.log(Object.keys(state.functions.meta));
|
|
61
|
+
console.error(`• No function metadata found for '${funcName}'.`);
|
|
62
|
+
return;
|
|
92
63
|
}
|
|
64
|
+
const input = fnMeta.inputs?.[0] || null;
|
|
65
|
+
const output = fnMeta.outputs?.[0] || null;
|
|
66
|
+
// --- compute inputTypes (body/query/params) ---
|
|
67
|
+
const inputTypes = getInputTypes(state.http.metaInputTypes, method, input, query, params);
|
|
68
|
+
// --- record route ---
|
|
69
|
+
state.http.files.add(node.getSourceFile().fileName);
|
|
70
|
+
state.http.meta.push({
|
|
71
|
+
pikkuFuncName: funcName,
|
|
72
|
+
route,
|
|
73
|
+
method: method,
|
|
74
|
+
input,
|
|
75
|
+
output,
|
|
76
|
+
params: params.length > 0 ? params : undefined,
|
|
77
|
+
query: query.length > 0 ? query : undefined,
|
|
78
|
+
inputTypes,
|
|
79
|
+
docs,
|
|
80
|
+
tags,
|
|
81
|
+
});
|
|
93
82
|
};
|
package/dist/add-schedule.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
2
|
import { InspectorFilters, InspectorState } from './types.js';
|
|
3
|
-
export declare const addSchedule: (node: ts.Node,
|
|
3
|
+
export declare const addSchedule: (node: ts.Node, checker: ts.TypeChecker, state: InspectorState, filters: InspectorFilters) => void;
|
package/dist/add-schedule.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
2
|
import { getPropertyValue } from './get-property-value.js';
|
|
3
|
-
import { matchesFilters } from './utils.js';
|
|
4
|
-
export const addSchedule = (node,
|
|
3
|
+
import { extractFunctionName, matchesFilters } from './utils.js';
|
|
4
|
+
export const addSchedule = (node, checker, state, filters) => {
|
|
5
5
|
if (!ts.isCallExpression(node)) {
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
@@ -21,6 +21,15 @@ export const addSchedule = (node, _checker, state, filters) => {
|
|
|
21
21
|
const scheduleValue = getPropertyValue(obj, 'schedule');
|
|
22
22
|
const docs = getPropertyValue(obj, 'docs') || undefined;
|
|
23
23
|
const tags = getPropertyValue(obj, 'tags') || undefined;
|
|
24
|
+
// --- find the referenced function ---
|
|
25
|
+
const funcProp = obj.properties.find((p) => ts.isPropertyAssignment(p) &&
|
|
26
|
+
ts.isIdentifier(p.name) &&
|
|
27
|
+
p.name.text === 'func');
|
|
28
|
+
if (!funcProp || !ts.isIdentifier(funcProp.initializer)) {
|
|
29
|
+
console.error(`• No valid 'func' property for scheduled task '${nameValue}'.`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const pikkuFuncName = extractFunctionName(funcProp.initializer, checker).pikkuFuncName;
|
|
24
33
|
if (!nameValue || !scheduleValue) {
|
|
25
34
|
return;
|
|
26
35
|
}
|
|
@@ -28,11 +37,12 @@ export const addSchedule = (node, _checker, state, filters) => {
|
|
|
28
37
|
return;
|
|
29
38
|
}
|
|
30
39
|
state.scheduledTasks.files.add(node.getSourceFile().fileName);
|
|
31
|
-
state.scheduledTasks.meta
|
|
40
|
+
state.scheduledTasks.meta[nameValue] = {
|
|
41
|
+
pikkuFuncName,
|
|
32
42
|
name: nameValue,
|
|
33
43
|
schedule: scheduleValue,
|
|
34
44
|
docs,
|
|
35
45
|
tags,
|
|
36
|
-
}
|
|
46
|
+
};
|
|
37
47
|
}
|
|
38
48
|
};
|
package/dist/inspector.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { visitSetup, visitRoutes } from './visit.js';
|
|
3
3
|
import { TypesMap } from './types-map.js';
|
|
4
4
|
export const normalizeHTTPTypes = (httpState) => {
|
|
5
5
|
return httpState;
|
|
@@ -18,6 +18,11 @@ export const inspect = (routeFiles, filters) => {
|
|
|
18
18
|
singletonServicesFactories: new Map(),
|
|
19
19
|
sessionServicesFactories: new Map(),
|
|
20
20
|
configFactories: new Map(),
|
|
21
|
+
functions: {
|
|
22
|
+
typesMap: new TypesMap(),
|
|
23
|
+
meta: {},
|
|
24
|
+
files: new Set(),
|
|
25
|
+
},
|
|
21
26
|
http: {
|
|
22
27
|
typesMap: new TypesMap(),
|
|
23
28
|
metaInputTypes: new Map(),
|
|
@@ -28,15 +33,20 @@ export const inspect = (routeFiles, filters) => {
|
|
|
28
33
|
typesMap: new TypesMap(),
|
|
29
34
|
metaInputTypes: new Map(),
|
|
30
35
|
files: new Set(),
|
|
31
|
-
meta:
|
|
36
|
+
meta: {},
|
|
32
37
|
},
|
|
33
38
|
scheduledTasks: {
|
|
34
|
-
meta:
|
|
39
|
+
meta: {},
|
|
35
40
|
files: new Set(),
|
|
36
41
|
},
|
|
37
42
|
};
|
|
43
|
+
// First sweep: add all functions
|
|
44
|
+
for (const sourceFile of sourceFiles) {
|
|
45
|
+
ts.forEachChild(sourceFile, (child) => visitSetup(checker, child, state, filters));
|
|
46
|
+
}
|
|
47
|
+
// Second sweep: add all transports
|
|
38
48
|
for (const sourceFile of sourceFiles) {
|
|
39
|
-
ts.forEachChild(sourceFile, (child) =>
|
|
49
|
+
ts.forEachChild(sourceFile, (child) => visitRoutes(checker, child, state, filters));
|
|
40
50
|
}
|
|
41
51
|
// Normalise the typesMap
|
|
42
52
|
state.http = normalizeHTTPTypes(state.http);
|
package/dist/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ChannelsMeta } from '@pikku/core/channel';
|
|
|
2
2
|
import { HTTPRoutesMeta } from '@pikku/core/http';
|
|
3
3
|
import { ScheduledTasksMeta } from '@pikku/core/scheduler';
|
|
4
4
|
import { TypesMap } from './types-map.js';
|
|
5
|
+
import { FunctionsMeta } from '@pikku/core';
|
|
5
6
|
export type PathToNameAndType = Map<string, {
|
|
6
7
|
variable: string;
|
|
7
8
|
type: string | null;
|
|
@@ -12,22 +13,17 @@ export type MetaInputTypes = Map<string, {
|
|
|
12
13
|
params: string[] | undefined;
|
|
13
14
|
body: string[] | undefined;
|
|
14
15
|
}>;
|
|
15
|
-
export type APIFunctionMeta = Array<{
|
|
16
|
-
name: string;
|
|
17
|
-
input: string;
|
|
18
|
-
output: string;
|
|
19
|
-
file: string;
|
|
20
|
-
}>;
|
|
21
|
-
export type InspectorAPIFunction = {
|
|
22
|
-
typesMap: TypesMap;
|
|
23
|
-
meta: APIFunctionMeta;
|
|
24
|
-
};
|
|
25
16
|
export interface InspectorHTTPState {
|
|
26
17
|
typesMap: TypesMap;
|
|
27
18
|
metaInputTypes: MetaInputTypes;
|
|
28
19
|
meta: HTTPRoutesMeta;
|
|
29
20
|
files: Set<string>;
|
|
30
21
|
}
|
|
22
|
+
export interface InspectorFunctionState {
|
|
23
|
+
typesMap: TypesMap;
|
|
24
|
+
meta: FunctionsMeta;
|
|
25
|
+
files: Set<string>;
|
|
26
|
+
}
|
|
31
27
|
export interface InspectorChannelState {
|
|
32
28
|
typesMap: TypesMap;
|
|
33
29
|
metaInputTypes: MetaInputTypes;
|
|
@@ -45,6 +41,7 @@ export interface InspectorState {
|
|
|
45
41
|
sessionServicesFactories: PathToNameAndType;
|
|
46
42
|
configFactories: PathToNameAndType;
|
|
47
43
|
http: InspectorHTTPState;
|
|
44
|
+
functions: InspectorFunctionState;
|
|
48
45
|
channels: InspectorChannelState;
|
|
49
46
|
scheduledTasks: {
|
|
50
47
|
meta: ScheduledTasksMeta;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,34 +1,28 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import { TypesMap } from './types-map.js';
|
|
3
2
|
import { InspectorFilters } from './types.js';
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
type ExtractedFunctionName = {
|
|
4
|
+
pikkuFuncName: string;
|
|
5
|
+
name: string;
|
|
6
|
+
exportedName: string | null;
|
|
7
|
+
functionName: string | null;
|
|
8
|
+
propertyName: string | null;
|
|
10
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Generate a deterministic "anonymous" name for any expression node,
|
|
12
|
+
* but if it's an Identifier pointing to a function, resolve it back
|
|
13
|
+
* to the function's declaration (so you get the true source location).
|
|
14
|
+
*/
|
|
15
|
+
export declare function makeDeterministicAnonName(start: ts.Node, checker: ts.TypeChecker): string;
|
|
16
|
+
/**
|
|
17
|
+
* Updated function to extract and prioritize function names correctly
|
|
18
|
+
* This function follows the priority:
|
|
19
|
+
* 1. Object with a name property
|
|
20
|
+
* 2. Exported name
|
|
21
|
+
* 3. Fallback to deterministic name
|
|
22
|
+
*/
|
|
23
|
+
export declare function extractFunctionName(callExpr: ts.Node, checker: ts.TypeChecker): ExtractedFunctionName;
|
|
11
24
|
export declare const extractTypeKeys: (type: ts.Type) => string[];
|
|
12
|
-
export declare
|
|
13
|
-
export declare const getNamesAndTypes: (checker: ts.TypeChecker, typesMap: TypesMap, direction: "Input" | "Output", funcName: string, type: ts.Type) => {
|
|
14
|
-
names: string[];
|
|
15
|
-
types: ts.Type[];
|
|
16
|
-
};
|
|
17
|
-
export declare const isPrimitiveType: (type: ts.Type) => boolean;
|
|
18
|
-
export declare const resolveUnionTypes: (checker: ts.TypeChecker, type: ts.Type) => {
|
|
19
|
-
types: ts.Type[];
|
|
20
|
-
names: string[];
|
|
21
|
-
};
|
|
22
|
-
export declare const resolveTypeImports: (type: ts.Type, resolvedTypes: TypesMap, isCustom: boolean) => string[];
|
|
23
|
-
export declare const getPropertyAssignment: (obj: ts.ObjectLiteralExpression, name: string) => ts.ObjectLiteralElementLike | null;
|
|
24
|
-
export declare const getTypeArgumentsOfType: (checker: ts.TypeChecker, type: ts.Type) => readonly ts.Type[] | null;
|
|
25
|
-
export declare const getFunctionTypes: (checker: ts.TypeChecker, obj: ts.ObjectLiteralExpression, { typesMap, funcName, subFunctionName, inputIndex, outputIndex, }: {
|
|
26
|
-
typesMap: TypesMap;
|
|
27
|
-
subFunctionName?: string;
|
|
28
|
-
funcName: string;
|
|
29
|
-
inputIndex: number;
|
|
30
|
-
outputIndex: number;
|
|
31
|
-
}) => FunctionTypes;
|
|
25
|
+
export declare function getPropertyAssignmentInitializer(obj: ts.ObjectLiteralExpression, propName: string, followShorthand?: boolean, checker?: ts.TypeChecker): ts.Expression | undefined;
|
|
32
26
|
export declare const matchesFilters: (filters: InspectorFilters, params: {
|
|
33
27
|
tags?: string[];
|
|
34
28
|
}, meta: {
|