|
| 1 | +import * as ts from 'typescript'; |
| 2 | +const { factory } = ts; |
| 3 | + |
| 4 | +const defaultEndpointBuilder = factory.createIdentifier('build'); |
| 5 | + |
| 6 | +export type ObjectPropertyDefinitions = Record<string, ts.Expression>; |
| 7 | +export function generateObjectProperties(obj: ObjectPropertyDefinitions) { |
| 8 | + return Object.entries(obj).map(([k, v]) => factory.createPropertyAssignment(factory.createIdentifier(k), v)); |
| 9 | +} |
| 10 | + |
| 11 | +export function generateStringLiteralArray(arr: string[]) { |
| 12 | + return factory.createArrayLiteralExpression( |
| 13 | + arr.map((elem) => factory.createStringLiteral(elem)), |
| 14 | + false |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +export function generateImportNode(pkg: string, namedImports: Record<string, string>, defaultImportName?: string) { |
| 19 | + return factory.createImportDeclaration( |
| 20 | + undefined, |
| 21 | + undefined, |
| 22 | + factory.createImportClause( |
| 23 | + false, |
| 24 | + defaultImportName !== undefined ? factory.createIdentifier(defaultImportName) : undefined, |
| 25 | + factory.createNamedImports( |
| 26 | + Object.entries(namedImports).map(([propertyName, name]) => |
| 27 | + factory.createImportSpecifier( |
| 28 | + name === propertyName ? undefined : factory.createIdentifier(propertyName), |
| 29 | + factory.createIdentifier(name) |
| 30 | + ) |
| 31 | + ) |
| 32 | + ) |
| 33 | + ), |
| 34 | + factory.createStringLiteral(pkg) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +export function generateCreateApiCall({ |
| 39 | + exportName, |
| 40 | + reducerPath, |
| 41 | + createApiFn, |
| 42 | + baseQuery, |
| 43 | + entityTypes, |
| 44 | + endpointBuilder = defaultEndpointBuilder, |
| 45 | + endpointDefinitions, |
| 46 | +}: { |
| 47 | + exportName: string; |
| 48 | + reducerPath?: string; |
| 49 | + createApiFn: ts.Expression; |
| 50 | + baseQuery: ts.Expression; |
| 51 | + entityTypes: ts.Expression; |
| 52 | + endpointBuilder?: ts.Identifier; |
| 53 | + endpointDefinitions: ts.ObjectLiteralExpression; |
| 54 | +}) { |
| 55 | + return factory.createVariableStatement( |
| 56 | + [factory.createModifier(ts.SyntaxKind.ExportKeyword)], |
| 57 | + factory.createVariableDeclarationList( |
| 58 | + [ |
| 59 | + factory.createVariableDeclaration( |
| 60 | + factory.createIdentifier(exportName), |
| 61 | + undefined, |
| 62 | + undefined, |
| 63 | + factory.createCallExpression(createApiFn, undefined, [ |
| 64 | + factory.createObjectLiteralExpression( |
| 65 | + generateObjectProperties({ |
| 66 | + ...(!reducerPath ? {} : { reducerPath: factory.createStringLiteral(reducerPath) }), |
| 67 | + baseQuery, |
| 68 | + entityTypes, |
| 69 | + endpoints: factory.createArrowFunction( |
| 70 | + undefined, |
| 71 | + undefined, |
| 72 | + [ |
| 73 | + factory.createParameterDeclaration( |
| 74 | + undefined, |
| 75 | + undefined, |
| 76 | + undefined, |
| 77 | + endpointBuilder, |
| 78 | + undefined, |
| 79 | + undefined, |
| 80 | + undefined |
| 81 | + ), |
| 82 | + ], |
| 83 | + undefined, |
| 84 | + factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), |
| 85 | + factory.createParenthesizedExpression(endpointDefinitions) |
| 86 | + ), |
| 87 | + }), |
| 88 | + true |
| 89 | + ), |
| 90 | + ]) |
| 91 | + ), |
| 92 | + ], |
| 93 | + ts.NodeFlags.Const |
| 94 | + ) |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export function generateEndpointDefinition({ |
| 99 | + operationName, |
| 100 | + type, |
| 101 | + Response, |
| 102 | + QueryArg, |
| 103 | + queryFn, |
| 104 | + endpointBuilder = defaultEndpointBuilder, |
| 105 | + extraEndpointsProps, |
| 106 | +}: { |
| 107 | + operationName: string; |
| 108 | + type: 'query' | 'mutation'; |
| 109 | + Response: ts.TypeReferenceNode; |
| 110 | + QueryArg: ts.TypeReferenceNode; |
| 111 | + queryFn: ts.Expression; |
| 112 | + endpointBuilder?: ts.Identifier; |
| 113 | + extraEndpointsProps: ObjectPropertyDefinitions; |
| 114 | +}) { |
| 115 | + return factory.createPropertyAssignment( |
| 116 | + factory.createIdentifier(operationName), |
| 117 | + |
| 118 | + factory.createCallExpression( |
| 119 | + factory.createPropertyAccessExpression(endpointBuilder, factory.createIdentifier(type)), |
| 120 | + [Response, QueryArg], |
| 121 | + [ |
| 122 | + factory.createObjectLiteralExpression( |
| 123 | + generateObjectProperties({ query: queryFn, ...extraEndpointsProps }), |
| 124 | + true |
| 125 | + ), |
| 126 | + ] |
| 127 | + ) |
| 128 | + ); |
| 129 | +} |
0 commit comments