@vaadin/hilla-generator-plugin-push 24.7.0-alpha9 → 24.7.0-beta3
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/PushProcessor.d.ts +6 -7
- package/PushProcessor.js +94 -118
- package/PushProcessor.js.map +1 -7
- package/index.d.ts +6 -7
- package/index.js +48 -49
- package/index.js.map +1 -7
- package/package.json +11 -32
- package/PushProcessor.d.ts.map +0 -1
- package/index.d.ts.map +0 -1
package/PushProcessor.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import ts from
|
|
1
|
+
import ts from "typescript";
|
|
2
2
|
export type EndpointOperations = {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
methodsToPatch: string[]
|
|
4
|
+
removeInitImport: boolean
|
|
5
5
|
};
|
|
6
6
|
export declare class PushProcessor {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
#private;
|
|
8
|
+
constructor(source: ts.SourceFile, operations: EndpointOperations);
|
|
9
|
+
process(): ts.SourceFile;
|
|
10
10
|
}
|
|
11
|
-
//# sourceMappingURL=PushProcessor.d.ts.map
|
package/PushProcessor.js
CHANGED
|
@@ -4,122 +4,98 @@ import PathManager from "@vaadin/hilla-generator-utils/dependencies/PathManager.
|
|
|
4
4
|
import memoize from "@vaadin/hilla-generator-utils/memoize.js";
|
|
5
5
|
import ts from "typescript";
|
|
6
6
|
const initParameterTypeName = "EndpointRequestInit";
|
|
7
|
-
class PushProcessor {
|
|
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
|
-
static #updateFunctionBody(declaration, doesInitParameterExist) {
|
|
103
|
-
const returnStatement = declaration.body.statements[0];
|
|
104
|
-
const { arguments: args, expression, typeArguments } = returnStatement.expression;
|
|
105
|
-
const call = expression;
|
|
106
|
-
return ts.factory.createBlock([
|
|
107
|
-
ts.factory.createReturnStatement(
|
|
108
|
-
ts.factory.createCallExpression(
|
|
109
|
-
ts.factory.createPropertyAccessExpression(
|
|
110
|
-
call.expression,
|
|
111
|
-
// `subscribe` instead of `call`
|
|
112
|
-
ts.factory.createIdentifier("subscribe")
|
|
113
|
-
),
|
|
114
|
-
typeArguments,
|
|
115
|
-
// remove the `init` parameter
|
|
116
|
-
doesInitParameterExist ? args.slice(0, -1) : args
|
|
117
|
-
)
|
|
118
|
-
)
|
|
119
|
-
]);
|
|
120
|
-
}
|
|
7
|
+
export class PushProcessor {
|
|
8
|
+
#dependencies = new DependencyManager(new PathManager({ extension: ".js" }));
|
|
9
|
+
#operations;
|
|
10
|
+
#source;
|
|
11
|
+
#subscriptionId;
|
|
12
|
+
constructor(source, operations) {
|
|
13
|
+
this.#operations = operations;
|
|
14
|
+
this.#source = source;
|
|
15
|
+
const { imports, paths } = this.#dependencies;
|
|
16
|
+
this.#dependencies.imports.fromCode(source);
|
|
17
|
+
this.#subscriptionId = memoize(() => imports.named.add(paths.createBareModulePath("@vaadin/hilla-frontend", false), "Subscription"));
|
|
18
|
+
}
|
|
19
|
+
process() {
|
|
20
|
+
const otherStatements = this.#source.statements.filter((statement) => !ts.isImportDeclaration(statement)).map((statement) => {
|
|
21
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
22
|
+
const functionName = statement.name?.text;
|
|
23
|
+
if (functionName && this.#operations.methodsToPatch.includes(functionName)) {
|
|
24
|
+
return this.#updateFunction(statement);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return statement;
|
|
28
|
+
});
|
|
29
|
+
let importStatements = this.#dependencies.imports.toCode();
|
|
30
|
+
if (this.#operations.removeInitImport) {
|
|
31
|
+
const importHillaFrontend = importStatements.find((statement) => ts.isImportDeclaration(statement) && statement.moduleSpecifier.text === "@vaadin/hilla-frontend");
|
|
32
|
+
if (importHillaFrontend) {
|
|
33
|
+
const updatedImportStatement = PushProcessor.#removeInitImport(importHillaFrontend);
|
|
34
|
+
if (updatedImportStatement) {
|
|
35
|
+
importStatements = importStatements.map((statement) => {
|
|
36
|
+
if (statement === importHillaFrontend) {
|
|
37
|
+
return updatedImportStatement;
|
|
38
|
+
}
|
|
39
|
+
return statement;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const updatedStatements = [...importStatements, ...otherStatements];
|
|
45
|
+
return createSourceFile(updatedStatements, this.#source.fileName);
|
|
46
|
+
}
|
|
47
|
+
static #doesInitParameterExist(parameters) {
|
|
48
|
+
const last = parameters[parameters.length - 1];
|
|
49
|
+
const lastType = last.type;
|
|
50
|
+
const lastTypeName = lastType.typeName;
|
|
51
|
+
return lastTypeName.text === initParameterTypeName;
|
|
52
|
+
}
|
|
53
|
+
static #removeInitImport(importStatement) {
|
|
54
|
+
const namedImports = importStatement.importClause?.namedBindings;
|
|
55
|
+
if (namedImports && ts.isNamedImports(namedImports)) {
|
|
56
|
+
const updatedElements = namedImports.elements.filter((element) => element.name.text !== "EndpointRequestInit");
|
|
57
|
+
const updatedImportClause = ts.factory.updateImportClause(importStatement.importClause, false, undefined, ts.factory.createNamedImports(updatedElements));
|
|
58
|
+
return ts.factory.updateImportDeclaration(importStatement, undefined, updatedImportClause, importStatement.moduleSpecifier, undefined);
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Replace returned `Promise<Array<T>>` by the `Subscription<T>` type
|
|
64
|
+
* @param declaration -
|
|
65
|
+
*/
|
|
66
|
+
#replacePromiseType(declaration) {
|
|
67
|
+
const [promiseType] = declaration.type.typeArguments;
|
|
68
|
+
const promiseArray = ts.isUnionTypeNode(promiseType) ? promiseType.types[0] : promiseType;
|
|
69
|
+
return ts.factory.createTypeReferenceNode(this.#subscriptionId(), promiseArray.typeArguments);
|
|
70
|
+
}
|
|
71
|
+
#updateFunction(declaration) {
|
|
72
|
+
const { parameters } = declaration;
|
|
73
|
+
const doesInitParameterExist = PushProcessor.#doesInitParameterExist(parameters);
|
|
74
|
+
return ts.factory.createFunctionDeclaration(
|
|
75
|
+
undefined,
|
|
76
|
+
declaration.asteriskToken,
|
|
77
|
+
declaration.name,
|
|
78
|
+
declaration.typeParameters,
|
|
79
|
+
// Remove the `init` parameter
|
|
80
|
+
doesInitParameterExist ? parameters.slice(0, -1) : parameters,
|
|
81
|
+
this.#replacePromiseType(declaration),
|
|
82
|
+
PushProcessor.#updateFunctionBody(declaration, doesInitParameterExist)
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
static #updateFunctionBody(declaration, doesInitParameterExist) {
|
|
86
|
+
const returnStatement = declaration.body.statements[0];
|
|
87
|
+
const { arguments: args, expression, typeArguments } = returnStatement.expression;
|
|
88
|
+
const call = expression;
|
|
89
|
+
return ts.factory.createBlock([ts.factory.createReturnStatement(ts.factory.createCallExpression(
|
|
90
|
+
ts.factory.createPropertyAccessExpression(
|
|
91
|
+
call.expression,
|
|
92
|
+
// `subscribe` instead of `call`
|
|
93
|
+
ts.factory.createIdentifier("subscribe")
|
|
94
|
+
),
|
|
95
|
+
typeArguments,
|
|
96
|
+
// remove the `init` parameter
|
|
97
|
+
doesInitParameterExist ? args.slice(0, -1) : args
|
|
98
|
+
))]);
|
|
99
|
+
}
|
|
121
100
|
}
|
|
122
|
-
|
|
123
|
-
PushProcessor
|
|
124
|
-
};
|
|
125
|
-
//# sourceMappingURL=PushProcessor.js.map
|
|
101
|
+
//# sourceMappingURL=./PushProcessor.js.map
|
package/PushProcessor.js.map
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["src/PushProcessor.ts"],
|
|
4
|
-
"sourcesContent": ["import createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport memoize from '@vaadin/hilla-generator-utils/memoize.js';\nimport ts from 'typescript';\n\nconst initParameterTypeName = 'EndpointRequestInit';\n\nexport type EndpointOperations = {\n methodsToPatch: string[];\n removeInitImport: boolean;\n};\n\nexport class PushProcessor {\n readonly #dependencies = new DependencyManager(new PathManager({ extension: '.js' }));\n readonly #operations: EndpointOperations;\n readonly #source: ts.SourceFile;\n readonly #subscriptionId: () => ts.Identifier;\n\n constructor(source: ts.SourceFile, operations: EndpointOperations) {\n this.#operations = operations;\n this.#source = source;\n\n const { imports, paths } = this.#dependencies;\n\n this.#dependencies.imports.fromCode(source);\n this.#subscriptionId = memoize(() =>\n imports.named.add(paths.createBareModulePath('@vaadin/hilla-frontend', false), 'Subscription'),\n );\n }\n\n process(): ts.SourceFile {\n const otherStatements = this.#source.statements\n .filter((statement) => !ts.isImportDeclaration(statement))\n .map((statement) => {\n if (ts.isFunctionDeclaration(statement)) {\n const functionName = statement.name?.text;\n\n // Checks if the method is in the list of methods to patch\n if (functionName && this.#operations.methodsToPatch.includes(functionName)) {\n return this.#updateFunction(statement);\n }\n }\n\n return statement;\n });\n\n let importStatements = this.#dependencies.imports.toCode();\n\n if (this.#operations.removeInitImport) {\n const importHillaFrontend = importStatements.find(\n (statement) =>\n ts.isImportDeclaration(statement) &&\n (statement.moduleSpecifier as ts.StringLiteral).text === '@vaadin/hilla-frontend',\n );\n\n if (importHillaFrontend) {\n const updatedImportStatement = PushProcessor.#removeInitImport(importHillaFrontend as ts.ImportDeclaration);\n\n if (updatedImportStatement) {\n importStatements = importStatements.map((statement) => {\n if (statement === importHillaFrontend) {\n return updatedImportStatement;\n }\n\n return statement;\n });\n }\n }\n }\n\n const updatedStatements: readonly ts.Statement[] = [...importStatements, ...otherStatements];\n\n return createSourceFile(updatedStatements, this.#source.fileName);\n }\n\n static #doesInitParameterExist(parameters: ts.NodeArray<ts.ParameterDeclaration>): boolean {\n const last = parameters[parameters.length - 1];\n const lastType = last.type as ts.TypeReferenceNode;\n const lastTypeName = lastType.typeName as ts.Identifier;\n\n return lastTypeName.text === initParameterTypeName;\n }\n\n static #removeInitImport(importStatement: ts.ImportDeclaration): ts.Statement | undefined {\n const namedImports = importStatement.importClause?.namedBindings;\n if (namedImports && ts.isNamedImports(namedImports)) {\n const updatedElements = namedImports.elements.filter((element) => element.name.text !== 'EndpointRequestInit');\n\n const updatedImportClause = ts.factory.updateImportClause(\n importStatement.importClause,\n false, // FIXME: could be true, but it is false for regular endpoint calls, so sticking to that for now\n undefined,\n ts.factory.createNamedImports(updatedElements),\n );\n\n return ts.factory.updateImportDeclaration(\n importStatement,\n undefined,\n updatedImportClause,\n importStatement.moduleSpecifier,\n undefined,\n );\n }\n\n return undefined;\n }\n\n /**\n * Replace returned `Promise<Array<T>>` by the `Subscription<T>` type\n * @param declaration -\n */\n #replacePromiseType(declaration: ts.FunctionDeclaration) {\n const promiseType = (declaration.type as ts.TypeReferenceNode).typeArguments![0];\n const promiseArray = (ts.isUnionTypeNode(promiseType) ? promiseType.types[0] : promiseType) as ts.TypeReferenceNode;\n\n return ts.factory.createTypeReferenceNode(this.#subscriptionId(), promiseArray.typeArguments);\n }\n\n #updateFunction(declaration: ts.FunctionDeclaration): ts.FunctionDeclaration {\n const { parameters } = declaration;\n const doesInitParameterExist = PushProcessor.#doesInitParameterExist(parameters);\n\n return ts.factory.createFunctionDeclaration(\n undefined, // no async\n declaration.asteriskToken,\n declaration.name,\n declaration.typeParameters,\n // Remove the `init` parameter\n doesInitParameterExist ? parameters.slice(0, -1) : parameters,\n this.#replacePromiseType(declaration),\n PushProcessor.#updateFunctionBody(declaration, doesInitParameterExist),\n );\n }\n\n static #updateFunctionBody(declaration: ts.FunctionDeclaration, doesInitParameterExist: boolean): ts.Block {\n const returnStatement = declaration.body!.statements[0] as ts.ReturnStatement;\n const { arguments: args, expression, typeArguments } = returnStatement.expression! as ts.CallExpression;\n const call = expression as ts.PropertyAccessExpression;\n\n return ts.factory.createBlock([\n ts.factory.createReturnStatement(\n ts.factory.createCallExpression(\n ts.factory.createPropertyAccessExpression(\n call.expression,\n // `subscribe` instead of `call`\n ts.factory.createIdentifier('subscribe'),\n ),\n typeArguments,\n // remove the `init` parameter\n doesInitParameterExist ? args.slice(0, -1) : args,\n ),\n ),\n ]);\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAA,O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ED,UAAO,GAAG,QAAQ,wBAChB,iBACA,WACA,qBACA,gBAAgB,iBAChB,UACD;EACF;AAED,SAAO;CACR;;;;;CAMD,oBAAoBC,aAAqC;EACvD,MAAM,CAAC,YAAY,GAAI,YAAY,KAA8B;EACjE,MAAM,eAAgB,GAAG,gBAAgB,YAAY,GAAG,YAAY,MAAM,KAAK;AAE/E,SAAO,GAAG,QAAQ,wBAAwB,KAAKT,iBAAiB,EAAE,aAAa,cAAc;CAC9F;CAED,gBAAgBS,aAA6D;EAC3E,MAAM,EAAE,YAAY,GAAG;EACvB,MAAM,yBAAyB,cAAcH,wBAAwB,WAAW;AAEhF,SAAO,GAAG,QAAQ;GAChB;GACA,YAAY;GACZ,YAAY;GACZ,YAAY;;GAEZ,yBAAyB,WAAW,MAAM,IAAI,EAAE,GAAG;GACnD,KAAKI,oBAAoB,YAAY;GACrC,cAAcC,oBAAoB,aAAa,uBAAuB;CACvE;CACF;CAED,OAAOA,oBAAoBF,aAAqCG,wBAA2C;EACzG,MAAM,kBAAkB,YAAY,KAAM,WAAW;EACrD,MAAM,EAAE,WAAW,MAAM,YAAY,eAAe,GAAG,gBAAgB;EACvE,MAAM,OAAO;AAEb,SAAO,GAAG,QAAQ,YAAY,CAC5B,GAAG,QAAQ,sBACT,GAAG,QAAQ;GACT,GAAG,QAAQ;IACT,KAAK;;IAEL,GAAG,QAAQ,iBAAiB,YAAY;CACzC;GACD;;GAEA,yBAAyB,KAAK,MAAM,IAAI,EAAE,GAAG;CAC9C,CACF,AACF,EAAC;CACH;AACF","names":["#dependencies","#operations","#source","#subscriptionId","source: ts.SourceFile","operations: EndpointOperations","#updateFunction","#removeInitImport","updatedStatements: readonly ts.Statement[]","#doesInitParameterExist","parameters: ts.NodeArray<ts.ParameterDeclaration>","importStatement: ts.ImportDeclaration","declaration: ts.FunctionDeclaration","#replacePromiseType","#updateFunctionBody","doesInitParameterExist: boolean"],"sources":["/opt/agent/work/1af72d8adc613024/hilla/packages/ts/generator-plugin-push/src/PushProcessor.ts"],"sourcesContent":["import createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport memoize from '@vaadin/hilla-generator-utils/memoize.js';\nimport ts from 'typescript';\n\nconst initParameterTypeName = 'EndpointRequestInit';\n\nexport type EndpointOperations = {\n methodsToPatch: string[];\n removeInitImport: boolean;\n};\n\nexport class PushProcessor {\n readonly #dependencies = new DependencyManager(new PathManager({ extension: '.js' }));\n readonly #operations: EndpointOperations;\n readonly #source: ts.SourceFile;\n readonly #subscriptionId: () => ts.Identifier;\n\n constructor(source: ts.SourceFile, operations: EndpointOperations) {\n this.#operations = operations;\n this.#source = source;\n\n const { imports, paths } = this.#dependencies;\n\n this.#dependencies.imports.fromCode(source);\n this.#subscriptionId = memoize(() =>\n imports.named.add(paths.createBareModulePath('@vaadin/hilla-frontend', false), 'Subscription'),\n );\n }\n\n process(): ts.SourceFile {\n const otherStatements = this.#source.statements\n .filter((statement) => !ts.isImportDeclaration(statement))\n .map((statement) => {\n if (ts.isFunctionDeclaration(statement)) {\n const functionName = statement.name?.text;\n\n // Checks if the method is in the list of methods to patch\n if (functionName && this.#operations.methodsToPatch.includes(functionName)) {\n return this.#updateFunction(statement);\n }\n }\n\n return statement;\n });\n\n let importStatements = this.#dependencies.imports.toCode();\n\n if (this.#operations.removeInitImport) {\n const importHillaFrontend = importStatements.find(\n (statement) =>\n ts.isImportDeclaration(statement) &&\n (statement.moduleSpecifier as ts.StringLiteral).text === '@vaadin/hilla-frontend',\n );\n\n if (importHillaFrontend) {\n const updatedImportStatement = PushProcessor.#removeInitImport(importHillaFrontend as ts.ImportDeclaration);\n\n if (updatedImportStatement) {\n importStatements = importStatements.map((statement) => {\n if (statement === importHillaFrontend) {\n return updatedImportStatement;\n }\n\n return statement;\n });\n }\n }\n }\n\n const updatedStatements: readonly ts.Statement[] = [...importStatements, ...otherStatements];\n\n return createSourceFile(updatedStatements, this.#source.fileName);\n }\n\n static #doesInitParameterExist(parameters: ts.NodeArray<ts.ParameterDeclaration>): boolean {\n const last = parameters[parameters.length - 1];\n const lastType = last.type as ts.TypeReferenceNode;\n const lastTypeName = lastType.typeName as ts.Identifier;\n\n return lastTypeName.text === initParameterTypeName;\n }\n\n static #removeInitImport(importStatement: ts.ImportDeclaration): ts.Statement | undefined {\n const namedImports = importStatement.importClause?.namedBindings;\n if (namedImports && ts.isNamedImports(namedImports)) {\n const updatedElements = namedImports.elements.filter((element) => element.name.text !== 'EndpointRequestInit');\n\n const updatedImportClause = ts.factory.updateImportClause(\n importStatement.importClause,\n false, // FIXME: could be true, but it is false for regular endpoint calls, so sticking to that for now\n undefined,\n ts.factory.createNamedImports(updatedElements),\n );\n\n return ts.factory.updateImportDeclaration(\n importStatement,\n undefined,\n updatedImportClause,\n importStatement.moduleSpecifier,\n undefined,\n );\n }\n\n return undefined;\n }\n\n /**\n * Replace returned `Promise<Array<T>>` by the `Subscription<T>` type\n * @param declaration -\n */\n #replacePromiseType(declaration: ts.FunctionDeclaration) {\n const [promiseType] = (declaration.type as ts.TypeReferenceNode).typeArguments!;\n const promiseArray = (ts.isUnionTypeNode(promiseType) ? promiseType.types[0] : promiseType) as ts.TypeReferenceNode;\n\n return ts.factory.createTypeReferenceNode(this.#subscriptionId(), promiseArray.typeArguments);\n }\n\n #updateFunction(declaration: ts.FunctionDeclaration): ts.FunctionDeclaration {\n const { parameters } = declaration;\n const doesInitParameterExist = PushProcessor.#doesInitParameterExist(parameters);\n\n return ts.factory.createFunctionDeclaration(\n undefined, // no async\n declaration.asteriskToken,\n declaration.name,\n declaration.typeParameters,\n // Remove the `init` parameter\n doesInitParameterExist ? parameters.slice(0, -1) : parameters,\n this.#replacePromiseType(declaration),\n PushProcessor.#updateFunctionBody(declaration, doesInitParameterExist),\n );\n }\n\n static #updateFunctionBody(declaration: ts.FunctionDeclaration, doesInitParameterExist: boolean): ts.Block {\n const returnStatement = declaration.body!.statements[0] as ts.ReturnStatement;\n const { arguments: args, expression, typeArguments } = returnStatement.expression! as ts.CallExpression;\n const call = expression as ts.PropertyAccessExpression;\n\n return ts.factory.createBlock([\n ts.factory.createReturnStatement(\n ts.factory.createCallExpression(\n ts.factory.createPropertyAccessExpression(\n call.expression,\n // `subscribe` instead of `call`\n ts.factory.createIdentifier('subscribe'),\n ),\n typeArguments,\n // remove the `init` parameter\n doesInitParameterExist ? args.slice(0, -1) : args,\n ),\n ),\n ]);\n }\n}\n"],"version":3}
|
package/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import Plugin from
|
|
2
|
-
import type SharedStorage from
|
|
1
|
+
import Plugin from "@vaadin/hilla-generator-core/Plugin.js";
|
|
2
|
+
import type { SharedStorage } from "@vaadin/hilla-generator-core/SharedStorage.js";
|
|
3
3
|
export default class PushPlugin extends Plugin {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
#private;
|
|
5
|
+
["constructor"]: typeof PushPlugin;
|
|
6
|
+
get path(): string;
|
|
7
|
+
execute(storage: SharedStorage): Promise<void>;
|
|
8
8
|
}
|
|
9
|
-
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -1,52 +1,51 @@
|
|
|
1
1
|
import Plugin from "@vaadin/hilla-generator-core/Plugin.js";
|
|
2
2
|
import { PushProcessor } from "./PushProcessor.js";
|
|
3
|
-
const classesToReplace = [
|
|
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
|
-
|
|
3
|
+
const classesToReplace = ["com.vaadin.hilla.runtime.transfertypes.Flux", "com.vaadin.hilla.runtime.transfertypes.EndpointSubscription"];
|
|
4
|
+
export default class PushPlugin extends Plugin {
|
|
5
|
+
/**
|
|
6
|
+
* Collects methods that must be patched by checking their `x-class-name` value
|
|
7
|
+
*/
|
|
8
|
+
static #collectPatchableMethods(paths) {
|
|
9
|
+
return Object.entries(paths).reduce((acc, [key, path]) => {
|
|
10
|
+
const response = path?.post?.responses[200];
|
|
11
|
+
const schema = response?.content?.["application/json"]?.schema;
|
|
12
|
+
const className = schema?.["x-class-name"];
|
|
13
|
+
const [, endpoint, method] = key.split("/");
|
|
14
|
+
if (className && classesToReplace.includes(className)) {
|
|
15
|
+
if (acc.has(endpoint)) {
|
|
16
|
+
acc.get(endpoint).methodsToPatch.push(method);
|
|
17
|
+
} else {
|
|
18
|
+
acc.set(endpoint, {
|
|
19
|
+
methodsToPatch: [method],
|
|
20
|
+
removeInitImport: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
if (acc.has(endpoint)) {
|
|
25
|
+
acc.get(endpoint).removeInitImport = false;
|
|
26
|
+
} else {
|
|
27
|
+
acc.set(endpoint, {
|
|
28
|
+
methodsToPatch: [],
|
|
29
|
+
removeInitImport: false
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return acc;
|
|
34
|
+
}, new Map());
|
|
35
|
+
}
|
|
36
|
+
get path() {
|
|
37
|
+
return import.meta.url;
|
|
38
|
+
}
|
|
39
|
+
async execute(storage) {
|
|
40
|
+
const { api, sources } = storage;
|
|
41
|
+
const endpointMethodMap = this.constructor.#collectPatchableMethods(api.paths);
|
|
42
|
+
for (let i = 0; i < sources.length; i++) {
|
|
43
|
+
const { fileName } = sources[i];
|
|
44
|
+
const endpoint = fileName.substring(0, fileName.indexOf(".ts"));
|
|
45
|
+
if (endpointMethodMap.has(endpoint)) {
|
|
46
|
+
sources[i] = new PushProcessor(sources[i], endpointMethodMap.get(endpoint)).process();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
48
50
|
}
|
|
49
|
-
|
|
50
|
-
PushPlugin as default
|
|
51
|
-
};
|
|
52
|
-
//# sourceMappingURL=index.js.map
|
|
51
|
+
//# sourceMappingURL=./index.js.map
|
package/index.js.map
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport type SharedStorage from '@vaadin/hilla-generator-core/SharedStorage.js';\nimport type { OpenAPIV3 } from 'openapi-types';\nimport type { ReadonlyObjectDeep } from 'type-fest/source/readonly-deep';\nimport { type EndpointOperations, PushProcessor } from './PushProcessor.js';\n\ntype ExtendedMediaTypeSchema = Readonly<{ 'x-class-name': string }> &\n ReadonlyObjectDeep<OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject>;\n\nconst classesToReplace: readonly string[] = [\n 'com.vaadin.hilla.runtime.transfertypes.Flux',\n 'com.vaadin.hilla.runtime.transfertypes.EndpointSubscription',\n];\n\nexport default class PushPlugin extends Plugin {\n /**\n * Collects methods that must be patched by checking their `x-class-name` value\n */\n static #collectPatchableMethods(paths: ReadonlyObjectDeep<OpenAPIV3.PathsObject>) {\n return Object.entries(paths).reduce((acc, [key, path]) => {\n const response = path?.post?.responses[200] as ReadonlyObjectDeep<OpenAPIV3.ResponseObject> | undefined;\n const schema = response?.content?.['application/json']?.schema as ExtendedMediaTypeSchema | undefined;\n const className = schema?.['x-class-name'];\n const [, endpoint, method] = key.split('/');\n\n if (className && classesToReplace.includes(className)) {\n if (acc.has(endpoint)) {\n acc.get(endpoint)!.methodsToPatch.push(method);\n } else {\n acc.set(endpoint, { methodsToPatch: [method], removeInitImport: true });\n }\n } else {\n // Not all methods will be patched, let's keep the init import\n // eslint-disable-next-line no-lonely-if\n if (acc.has(endpoint)) {\n acc.get(endpoint)!.removeInitImport = false;\n } else {\n acc.set(endpoint, { methodsToPatch: [], removeInitImport: false });\n }\n }\n\n return acc;\n }, new Map<string, EndpointOperations>());\n }\n\n declare ['constructor']: typeof PushPlugin;\n\n override get path(): string {\n return import.meta.url;\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n override async execute(storage: SharedStorage): Promise<void> {\n const { api, sources } = storage;\n const endpointMethodMap = this.constructor.#collectPatchableMethods(api.paths);\n\n for (let i = 0; i < sources.length; i++) {\n const { fileName } = sources[i];\n const endpoint = fileName.substring(0, fileName.indexOf('.ts'));\n\n if (endpointMethodMap.has(endpoint)) {\n sources[i] = new PushProcessor(sources[i], endpointMethodMap.get(endpoint)!).process();\n }\n }\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAA,OAAO,YAAY;AAInB,SAAkC,qBAAqB;AAKvD,MAAM,mBAAsC;AAAA,EAC1C;AAAA,EACA;AACF;AAEA,MAAO,mBAAiC,OAAO;AAAA;AAAA;AAAA;AAAA,EAI7C,OAAO,yBAAyB,OAAkD;AAChF,WAAO,OAAO,QAAQ,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,IAAI,MAAM;AACxD,YAAM,WAAW,MAAM,MAAM,UAAU,GAAG;AAC1C,YAAM,SAAS,UAAU,UAAU,kBAAkB,GAAG;AACxD,YAAM,YAAY,SAAS,cAAc;AACzC,YAAM,CAAC,EAAE,UAAU,MAAM,IAAI,IAAI,MAAM,GAAG;AAE1C,UAAI,aAAa,iBAAiB,SAAS,SAAS,GAAG;AACrD,YAAI,IAAI,I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AAK,KAAK,KAAK;GACxD,MAAM,WAAW,MAAM,MAAM,UAAU;GACvC,MAAM,SAAS,UAAU,UAAU,qBAAqB;GACxD,MAAM,YAAY,SAAS;GAC3B,MAAM,GAAG,UAAU,OAAO,GAAG,IAAI,MAAM,IAAI;AAE3C,OAAI,aAAa,iBAAiB,SAAS,UAAU,EAAE;AACrD,QAAI,IAAI,IAAI,SAAS,EAAE;AACrB,SAAI,IAAI,SAAS,CAAE,eAAe,KAAK,OAAO;IAC/C,OAAM;AACL,SAAI,IAAI,UAAU;MAAE,gBAAgB,CAAC,MAAO;MAAE,kBAAkB;KAAM,EAAC;IACxE;GACF,OAAM;AAGL,QAAI,IAAI,IAAI,SAAS,EAAE;AACrB,SAAI,IAAI,SAAS,CAAE,mBAAmB;IACvC,OAAM;AACL,SAAI,IAAI,UAAU;MAAE,gBAAgB,CAAE;MAAE,kBAAkB;KAAO,EAAC;IACnE;GACF;AAED,UAAO;EACR,GAAE,IAAI,MAAkC;CAC1C;CAID,IAAa,OAAe;AAC1B,SAAO,OAAO,KAAK;CACpB;CAGD,MAAe,QAAQC,SAAuC;EAC5D,MAAM,EAAE,KAAK,SAAS,GAAG;EACzB,MAAM,oBAAoB,KAAK,YAAYF,yBAAyB,IAAI,MAAM;AAE9E,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;GACvC,MAAM,EAAE,UAAU,GAAG,QAAQ;GAC7B,MAAM,WAAW,SAAS,UAAU,GAAG,SAAS,QAAQ,MAAM,CAAC;AAE/D,OAAI,kBAAkB,IAAI,SAAS,EAAE;AACnC,YAAQ,KAAK,IAAI,cAAc,QAAQ,IAAI,kBAAkB,IAAI,SAAS,EAAG,SAAS;GACvF;EACF;CACF;AACF","names":["classesToReplace: readonly string[]","#collectPatchableMethods","paths: ReadonlyObjectDeep<OpenAPIV3.PathsObject>","storage: SharedStorage"],"sources":["/opt/agent/work/1af72d8adc613024/hilla/packages/ts/generator-plugin-push/src/index.ts"],"sourcesContent":["import Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport type { SharedStorage } from '@vaadin/hilla-generator-core/SharedStorage.js';\nimport type { OpenAPIV3 } from 'openapi-types';\nimport type { ReadonlyObjectDeep } from 'type-fest/source/readonly-deep';\nimport { type EndpointOperations, PushProcessor } from './PushProcessor.js';\n\ntype ExtendedMediaTypeSchema = Readonly<{ 'x-class-name': string }> &\n ReadonlyObjectDeep<OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject>;\n\nconst classesToReplace: readonly string[] = [\n 'com.vaadin.hilla.runtime.transfertypes.Flux',\n 'com.vaadin.hilla.runtime.transfertypes.EndpointSubscription',\n];\n\nexport default class PushPlugin extends Plugin {\n /**\n * Collects methods that must be patched by checking their `x-class-name` value\n */\n static #collectPatchableMethods(paths: ReadonlyObjectDeep<OpenAPIV3.PathsObject>) {\n return Object.entries(paths).reduce((acc, [key, path]) => {\n const response = path?.post?.responses[200] as ReadonlyObjectDeep<OpenAPIV3.ResponseObject> | undefined;\n const schema = response?.content?.['application/json']?.schema as ExtendedMediaTypeSchema | undefined;\n const className = schema?.['x-class-name'];\n const [, endpoint, method] = key.split('/');\n\n if (className && classesToReplace.includes(className)) {\n if (acc.has(endpoint)) {\n acc.get(endpoint)!.methodsToPatch.push(method);\n } else {\n acc.set(endpoint, { methodsToPatch: [method], removeInitImport: true });\n }\n } else {\n // Not all methods will be patched, let's keep the init import\n // eslint-disable-next-line no-lonely-if\n if (acc.has(endpoint)) {\n acc.get(endpoint)!.removeInitImport = false;\n } else {\n acc.set(endpoint, { methodsToPatch: [], removeInitImport: false });\n }\n }\n\n return acc;\n }, new Map<string, EndpointOperations>());\n }\n\n declare ['constructor']: typeof PushPlugin;\n\n override get path(): string {\n return import.meta.url;\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n override async execute(storage: SharedStorage): Promise<void> {\n const { api, sources } = storage;\n const endpointMethodMap = this.constructor.#collectPatchableMethods(api.paths);\n\n for (let i = 0; i < sources.length; i++) {\n const { fileName } = sources[i];\n const endpoint = fileName.substring(0, fileName.indexOf('.ts'));\n\n if (endpointMethodMap.has(endpoint)) {\n sources[i] = new PushProcessor(sources[i], endpointMethodMap.get(endpoint)!).process();\n }\n }\n }\n}\n"],"version":3}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/hilla-generator-plugin-push",
|
|
3
|
-
"version": "24.7.0-
|
|
3
|
+
"version": "24.7.0-beta3",
|
|
4
4
|
"description": "A Hilla TypeScript Generator plugin to add push support",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -9,15 +9,13 @@
|
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"clean:build": "git clean -fx . -e .vite -e node_modules",
|
|
12
|
-
"build": "
|
|
13
|
-
"build:esbuild": "tsx ../../../scripts/build.ts",
|
|
14
|
-
"build:dts": "tsc --isolatedModules -p tsconfig.build.json",
|
|
15
|
-
"build:copy": "cd src && copyfiles **/*.d.ts ..",
|
|
12
|
+
"build": "tsx ../../../scripts/fast-build.ts",
|
|
16
13
|
"lint": "eslint src test",
|
|
17
14
|
"lint:fix": "eslint src test --fix",
|
|
18
|
-
"test": "
|
|
19
|
-
"test:
|
|
20
|
-
"test:
|
|
15
|
+
"test": "vitest --run",
|
|
16
|
+
"test:coverage": "vitest --run --coverage",
|
|
17
|
+
"test:update": "vitest --run --update",
|
|
18
|
+
"test:watch": "vitest",
|
|
21
19
|
"typecheck": "tsc --noEmit"
|
|
22
20
|
},
|
|
23
21
|
"exports": {
|
|
@@ -51,30 +49,11 @@
|
|
|
51
49
|
"access": "public"
|
|
52
50
|
},
|
|
53
51
|
"dependencies": {
|
|
54
|
-
"@vaadin/hilla-generator-core": "24.7.0-
|
|
55
|
-
"@vaadin/hilla-generator-plugin-client": "24.7.0-
|
|
56
|
-
"@vaadin/hilla-generator-utils": "24.7.0-
|
|
57
|
-
"fast-deep-equal": "
|
|
58
|
-
"openapi-types": "
|
|
52
|
+
"@vaadin/hilla-generator-core": "24.7.0-beta3",
|
|
53
|
+
"@vaadin/hilla-generator-plugin-client": "24.7.0-beta3",
|
|
54
|
+
"@vaadin/hilla-generator-utils": "24.7.0-beta3",
|
|
55
|
+
"fast-deep-equal": "3.1.3",
|
|
56
|
+
"openapi-types": "12.1.3",
|
|
59
57
|
"typescript": "5.7.3"
|
|
60
|
-
},
|
|
61
|
-
"devDependencies": {
|
|
62
|
-
"@types/chai": "^4.3.20",
|
|
63
|
-
"@types/mocha": "^10.0.10",
|
|
64
|
-
"@types/node": "^20.17.12",
|
|
65
|
-
"@types/sinon": "^10.0.20",
|
|
66
|
-
"@types/sinon-chai": "^3.2.12",
|
|
67
|
-
"@vaadin/hilla-generator-core": "24.7.0-alpha9",
|
|
68
|
-
"@vaadin/hilla-generator-plugin-client": "24.7.0-alpha9",
|
|
69
|
-
"c8": "^10.1.3",
|
|
70
|
-
"chai": "^4.5.0",
|
|
71
|
-
"concurrently": "^9.1.2",
|
|
72
|
-
"copyfiles": "^2.4.1",
|
|
73
|
-
"mocha": "^11.1.0",
|
|
74
|
-
"monocart-coverage-reports": "^2.11.5",
|
|
75
|
-
"pino": "^9.6.0",
|
|
76
|
-
"sinon": "^16.1.3",
|
|
77
|
-
"sinon-chai": "^3.7.0",
|
|
78
|
-
"type-fest": "^4.32.0"
|
|
79
58
|
}
|
|
80
59
|
}
|
package/PushProcessor.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PushProcessor.d.ts","sourceRoot":"","sources":["src/PushProcessor.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,YAAY,CAAC;AAI5B,MAAM,MAAM,kBAAkB,GAAG;IAC/B,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,gBAAgB,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF,qBAAa,aAAa;;gBAMZ,MAAM,EAAE,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,kBAAkB;IAYjE,OAAO,IAAI,EAAE,CAAC,UAAU;CA4HzB"}
|
package/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,wCAAwC,CAAC;AAC5D,OAAO,KAAK,aAAa,MAAM,+CAA+C,CAAC;AAa/E,MAAM,CAAC,OAAO,OAAO,UAAW,SAAQ,MAAM;;IA+BpC,CAAC,aAAa,CAAC,EAAE,OAAO,UAAU,CAAC;IAE3C,IAAa,IAAI,IAAI,MAAM,CAE1B;IAGc,OAAO,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAa9D"}
|