-
Notifications
You must be signed in to change notification settings - Fork 2
technical/apicraft-class into release/apicraft/1.5.3 🚀 add axios class #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vitrivdolkom
merged 12 commits into
release/apicraft/1.5.3
from
technical/apicraft-class
Mar 1, 2026
+1,477
−907
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0ca5fd5
technical/apicraft-class 🚀 add axios class draft
vitrivdolkom d3a4ba1
technical/apicraft-class 🚀 support class variant for tanstack
vitrivdolkom d2eb2cb
technical/apicraft-class 🚀 self review fixes
vitrivdolkom 439a65f
technical/apicraft-class 🚀 support runtime instance for axios class
vitrivdolkom 037015e
technical/apicraft-class 🚀 add generation examples
vitrivdolkom a51a2f6
technical/apicraft-class 🚀 rework
vitrivdolkom da9fed1
technical/apicraft-class 🚀 rework plugins for 2 cases: class and comp…
vitrivdolkom 9050e3a
technical/apicraft-class 🚀 camelCase for _
vitrivdolkom 67950cc
technical/apicraft-class 🚀 review fixes
vitrivdolkom 02325c4
technical/apicraft-class 🚀 fix path request name generation
vitrivdolkom eddfd0a
technical/apicraft-class 🚀 remove generated
vitrivdolkom ed1df5b
technical/apicraft-class 🚀 self review fixes
vitrivdolkom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,7 +208,7 @@ public | |
| gatsby-types.d.ts | ||
|
|
||
| # Generated | ||
| !generated | ||
| generated | ||
|
|
||
| # Turbo | ||
| .turbo | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import * as nodePath from 'node:path'; | ||
| import ts from 'typescript'; | ||
|
|
||
| import { | ||
| capitalize, | ||
| generateRequestName, | ||
| getImportRuntimeInstance, | ||
| getRequestInfo | ||
| } from '@/bin/plugins/helpers'; | ||
|
|
||
| import type { AxiosPlugin } from '../types'; | ||
|
|
||
| import { | ||
| getAxiosRequestCallExpression, | ||
| getAxiosRequestParameterDeclaration, | ||
| getAxiosRequestParamsType, | ||
| getImportAxios, | ||
| getImportAxiosRequestParams | ||
| } from '../helpers'; | ||
|
|
||
| const CLASS_NAME = 'ApiInstance'; | ||
|
|
||
| export const classHandler: AxiosPlugin['Handler'] = ({ plugin }) => { | ||
| const classFilePath = nodePath.normalize(`${plugin.output}/instance`); | ||
| const classFolderPath = nodePath.dirname(`${plugin.config.generateOutput}/${classFilePath}`); | ||
| const classFile = plugin.createFile({ | ||
| id: 'axiosInstance', | ||
| path: classFilePath | ||
| }); | ||
|
|
||
| const typeImportNames = new Set<string>(); | ||
| const typeStatements: ts.Statement[] = []; | ||
| const classElements: ts.ClassElement[] = []; | ||
|
|
||
| plugin.forEach('operation', (event) => { | ||
| if (event.type !== 'operation') return; | ||
|
|
||
| const request = event.operation; | ||
| const requestName = generateRequestName(request, plugin.config.nameBy); | ||
| const requestInfo = getRequestInfo({ request }); | ||
|
|
||
| const requestDataTypeName = `${capitalize(request.id)}Data`; | ||
| typeImportNames.add(requestDataTypeName); | ||
|
|
||
| const requestResponseTypeName = `${capitalize(request.id)}Response`; | ||
| if (requestInfo.hasResponse) typeImportNames.add(requestResponseTypeName); | ||
|
|
||
| const requestParamsTypeName = `${capitalize(requestName)}RequestParams`; | ||
| typeStatements.push( | ||
| getAxiosRequestParamsType({ | ||
| requestDataTypeName, | ||
| requestParamsTypeName | ||
| }) | ||
| ); | ||
|
|
||
| // ({ path, body, query, config }: RequestParams) | ||
| const requestParameter = getAxiosRequestParameterDeclaration({ | ||
| request, | ||
| requestInfo, | ||
| requestParamsTypeName | ||
| }); | ||
|
|
||
| const requestBody = ts.factory.createBlock( | ||
| [ | ||
| ts.factory.createReturnStatement( | ||
| // instance.request({ method, url, data, params }) | ||
| getAxiosRequestCallExpression({ | ||
| request, | ||
| requestInfo, | ||
| requestResponseTypeName, | ||
| instanceVariant: 'class' | ||
| }) | ||
| ) | ||
| ], | ||
| true | ||
| ); | ||
|
|
||
| classElements.push( | ||
| ts.factory.createMethodDeclaration( | ||
| undefined, | ||
| undefined, | ||
| ts.factory.createIdentifier(requestName), | ||
| undefined, | ||
| undefined, | ||
| [requestParameter], | ||
| undefined, | ||
| requestBody | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| // import type { AxiosRequestParams } from '@siberiacancode/apicraft'; | ||
| const importAxiosRequestParams = getImportAxiosRequestParams(); | ||
|
|
||
| // import type { RequestData, RequestResponse, ... } from './types.gen'; | ||
| const importTypes = ts.factory.createImportDeclaration( | ||
| undefined, | ||
| ts.factory.createImportClause( | ||
| true, | ||
| undefined, | ||
| ts.factory.createNamedImports( | ||
| Array.from(typeImportNames).map((typeImportName) => | ||
| ts.factory.createImportSpecifier( | ||
| false, | ||
| undefined, | ||
| ts.factory.createIdentifier(typeImportName) | ||
| ) | ||
| ) | ||
| ) | ||
| ), | ||
| ts.factory.createStringLiteral('./types.gen') | ||
| ); | ||
|
|
||
| // import type { AxiosInstance, CreateAxiosDefaults } from 'axios'; | ||
| const importAxiosTypes = ts.factory.createImportDeclaration( | ||
| undefined, | ||
| ts.factory.createImportClause( | ||
| true, | ||
| undefined, | ||
| ts.factory.createNamedImports([ | ||
| ts.factory.createImportSpecifier( | ||
| false, | ||
| undefined, | ||
| ts.factory.createIdentifier('AxiosInstance') | ||
| ), | ||
| ...(!plugin.config.runtimeInstancePath | ||
| ? [ | ||
| ts.factory.createImportSpecifier( | ||
| false, | ||
| undefined, | ||
| ts.factory.createIdentifier('CreateAxiosDefaults') | ||
| ) | ||
| ] | ||
| : []) | ||
| ]) | ||
| ), | ||
| ts.factory.createStringLiteral('axios'), | ||
| undefined | ||
| ); | ||
|
|
||
| // private instance: AxiosInstance; | ||
| const classInstanceProperty = ts.factory.createPropertyDeclaration( | ||
| [ts.factory.createModifier(ts.SyntaxKind.PrivateKeyword)], | ||
| ts.factory.createIdentifier('instance'), | ||
| undefined, | ||
| ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('AxiosInstance'), undefined), | ||
| undefined | ||
| ); | ||
|
|
||
| // constructor(config?: CreateAxiosDefaults) { this.instance = axios.create(config); } | ||
| const constructorDeclaration = ts.factory.createConstructorDeclaration( | ||
| undefined, | ||
| !plugin.config.runtimeInstancePath | ||
| ? [ | ||
| ts.factory.createParameterDeclaration( | ||
| undefined, | ||
| undefined, | ||
| ts.factory.createIdentifier('config'), | ||
| ts.factory.createToken(ts.SyntaxKind.QuestionToken), | ||
| ts.factory.createTypeReferenceNode( | ||
| ts.factory.createIdentifier('CreateAxiosDefaults'), | ||
| undefined | ||
| ), | ||
| undefined | ||
| ) | ||
| ] | ||
| : [], | ||
| ts.factory.createBlock( | ||
| [ | ||
| ts.factory.createExpressionStatement( | ||
| ts.factory.createBinaryExpression( | ||
| ts.factory.createPropertyAccessExpression( | ||
| ts.factory.createThis(), | ||
| ts.factory.createIdentifier('instance') | ||
| ), | ||
| ts.factory.createToken(ts.SyntaxKind.EqualsToken), | ||
| plugin.config.runtimeInstancePath | ||
| ? ts.factory.createIdentifier('runtimeInstance') | ||
| : ts.factory.createCallExpression( | ||
| ts.factory.createPropertyAccessExpression( | ||
| ts.factory.createIdentifier('axios'), | ||
| ts.factory.createIdentifier('create') | ||
| ), | ||
| undefined, | ||
| !plugin.config.runtimeInstancePath ? [ts.factory.createIdentifier('config')] : [] | ||
| ) | ||
| ) | ||
| ) | ||
| ], | ||
| true | ||
| ) | ||
| ); | ||
|
|
||
| // export class ApiInstance {...} | ||
| const classDeclaration = ts.factory.createClassDeclaration( | ||
| [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
| ts.factory.createIdentifier(CLASS_NAME), | ||
| undefined, | ||
| undefined, | ||
| [classInstanceProperty, constructorDeclaration, ...classElements] | ||
| ); | ||
|
|
||
| // export const instance = new ApiInstance(); | ||
| const classInstance = ts.factory.createVariableStatement( | ||
| [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
| ts.factory.createVariableDeclarationList( | ||
| [ | ||
| ts.factory.createVariableDeclaration( | ||
| ts.factory.createIdentifier('instance'), | ||
| undefined, | ||
| undefined, | ||
| ts.factory.createNewExpression(ts.factory.createIdentifier(CLASS_NAME), undefined, []) | ||
| ) | ||
| ], | ||
| ts.NodeFlags.Const | ||
| ) | ||
| ); | ||
|
|
||
| classFile.add(importAxiosRequestParams); | ||
| classFile.add(importTypes); | ||
| classFile.add(importAxiosTypes); | ||
|
|
||
| if (plugin.config.runtimeInstancePath) { | ||
| // import { instance as runtimeInstance } from runtimeInstancePath; | ||
| classFile.add( | ||
| getImportRuntimeInstance({ | ||
| folderPath: classFolderPath, | ||
| runtimeInstancePath: plugin.config.runtimeInstancePath | ||
| }) | ||
| ); | ||
| } | ||
| if (!plugin.config.runtimeInstancePath) { | ||
| // import axios from 'axios'; | ||
| classFile.add(getImportAxios()); | ||
| } | ||
|
|
||
| typeStatements.forEach((alias) => classFile.add(alias)); | ||
| classFile.add(classDeclaration); | ||
| classFile.add(classInstance); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
по факту это generateOutput для функции, опять у кирпичей хотелось один нейминг сохранить, постараться если это возможно
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тоже не понял про нейминг, там где мы тупо возвразаем сразу ts factory назвал get
Там где какие то расчеты есть назвал generate как у хукво, могу везде get назвать