@davstack/openapi-react-query-codegen
Version:
OpenAPI React Query Codegen using --useOptions (fork from @7nohe/openapi-react-query-codegen)
71 lines (70 loc) • 6.59 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPrefetchQuery = void 0;
const typescript_1 = __importDefault(require("typescript"));
const factory = typescript_1.default.factory;
const createPrefetchQuery = () => {
return [
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
factory.createImportSpecifier(false, undefined, factory.createIdentifier('dehydrate')),
factory.createImportSpecifier(false, undefined, factory.createIdentifier('Hydrate')),
factory.createImportSpecifier(false, undefined, factory.createIdentifier('QueryKey')),
])), factory.createStringLiteral('@tanstack/react-query'), undefined),
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
factory.createImportSpecifier(false, undefined, factory.createIdentifier('getQueryClient')),
])), factory.createStringLiteral('./get-query-client'), undefined),
factory.createFunctionDeclaration([
factory.createToken(typescript_1.default.SyntaxKind.ExportKeyword),
factory.createToken(typescript_1.default.SyntaxKind.AsyncKeyword),
], undefined, factory.createIdentifier('prefetchQuery'), [
factory.createTypeParameterDeclaration(undefined, factory.createIdentifier('T'), factory.createFunctionTypeNode(undefined, [], factory.createTypeReferenceNode(factory.createIdentifier('Promise'), [factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.AnyKeyword)])), factory.createFunctionTypeNode(undefined, [], factory.createTypeReferenceNode(factory.createIdentifier('Promise'), [factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.AnyKeyword)]))),
], [
factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('key'), undefined, factory.createTypeReferenceNode(factory.createIdentifier('QueryKey'), undefined), undefined),
factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('fetcher'), undefined, factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined), undefined),
], undefined, factory.createBlock([
factory.createVariableStatement(undefined, factory.createVariableDeclarationList([
factory.createVariableDeclaration(factory.createIdentifier('queryClient'), undefined, undefined, factory.createCallExpression(factory.createIdentifier('getQueryClient'), undefined, [])),
], typescript_1.default.NodeFlags.Const |
typescript_1.default.NodeFlags.AwaitContext |
typescript_1.default.NodeFlags.ContextFlags |
typescript_1.default.NodeFlags.TypeExcludesFlags)),
factory.createExpressionStatement(factory.createAwaitExpression(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier('queryClient'), factory.createIdentifier('prefetchQuery')), undefined, [
factory.createIdentifier('key'),
factory.createIdentifier('fetcher'),
]))),
factory.createVariableStatement(undefined, factory.createVariableDeclarationList([
factory.createVariableDeclaration(factory.createIdentifier('dehydratedState'), undefined, undefined, factory.createCallExpression(factory.createIdentifier('dehydrate'), undefined, [factory.createIdentifier('queryClient')])),
], typescript_1.default.NodeFlags.Const |
typescript_1.default.NodeFlags.AwaitContext |
typescript_1.default.NodeFlags.ContextFlags |
typescript_1.default.NodeFlags.TypeExcludesFlags)),
factory.createReturnStatement(factory.createObjectLiteralExpression([
factory.createPropertyAssignment(factory.createIdentifier('HydrateState'), factory.createArrowFunction(undefined, undefined, [
factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('props'), undefined, factory.createTypeLiteralNode([
factory.createPropertySignature(undefined, factory.createIdentifier('children'), undefined, factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.AnyKeyword)),
]), undefined),
], undefined, factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), factory.createParenthesizedExpression(factory.createJsxFragment(factory.createJsxOpeningFragment(), [
factory.createJsxText('\
', true),
factory.createJsxElement(factory.createJsxOpeningElement(factory.createIdentifier('Hydrate'), undefined, factory.createJsxAttributes([
factory.createJsxAttribute(factory.createIdentifier('state'), factory.createJsxExpression(undefined, factory.createIdentifier('dehydratedState'))),
])), [
factory.createJsxExpression(undefined, factory.createPropertyAccessExpression(factory.createIdentifier('props'), factory.createIdentifier('children'))),
], factory.createJsxClosingElement(factory.createIdentifier('Hydrate'))),
factory.createJsxText('\
', true),
], factory.createJsxJsxClosingFragment())))),
factory.createShorthandPropertyAssignment(factory.createIdentifier('dehydratedState'), undefined),
factory.createPropertyAssignment(factory.createIdentifier('getData'), factory.createArrowFunction(undefined, undefined, [], undefined, factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), factory.createAsExpression(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier('queryClient'), factory.createIdentifier('getQueryData')), undefined, [factory.createIdentifier('key')]), factory.createFunctionTypeNode(undefined, [], factory.createTypeReferenceNode(factory.createIdentifier('Awaited'), [
factory.createTypeReferenceNode(factory.createIdentifier('ReturnType'), [
factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined),
]),
]))))),
], true)),
], true)),
];
};
exports.createPrefetchQuery = createPrefetchQuery;