-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generator): enhance route option generator
- enhance: `RouteShorthandOptions` generator now supports function, arrow function, and variable
- Loading branch information
Showing
28 changed files
with
441 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,13 @@ | ||
import { RouteShorthandOptions } from 'fastify'; | ||
import schema from '../interfaces/JSC_IReqPokeHello'; | ||
|
||
export const option: RouteShorthandOptions = { | ||
schema: { | ||
querystring: schema.properties?.Querystring, | ||
body: schema.properties?.Body, | ||
}, | ||
}; | ||
|
||
export async function handler() { | ||
return 'hello'; | ||
} |
This file contains 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 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 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,35 @@ | ||
import type * as tsm from 'ts-morph'; | ||
|
||
interface IFastifyRouteOption { | ||
/** 동기/비동기 함수 정보 */ | ||
kind: 'async' | 'sync'; | ||
|
||
/** 이름 */ | ||
name: string; | ||
|
||
/** 경로 */ | ||
path: string; | ||
} | ||
|
||
export type TFastifyRouteOption = | ||
| (IFastifyRouteOption & { | ||
/** 함수/ 화살표 함수 정보 */ | ||
type: 'variable'; | ||
|
||
/** 실제 노드 정보 */ | ||
node: tsm.VariableDeclaration; | ||
}) | ||
| (IFastifyRouteOption & { | ||
/** 함수/ 화살표 함수 정보 */ | ||
type: 'function'; | ||
|
||
/** 실제 노드 정보 */ | ||
node: tsm.FunctionDeclaration; | ||
}) | ||
| (IFastifyRouteOption & { | ||
/** 함수/ 화살표 함수 정보 */ | ||
type: 'arrow'; | ||
|
||
/** 실제 노드 정보 */ | ||
node: tsm.ArrowFunction; | ||
}); |
This file contains 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 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 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 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 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,32 @@ | ||
import type { TFastifyRouteOption } from '#/compilers/interfaces/TFastifyRouteOption'; | ||
import { findOrThrow } from 'my-easy-fp'; | ||
import type { ExportedDeclarations } from 'ts-morph'; | ||
import { SyntaxKind } from 'ts-morph'; | ||
|
||
/** | ||
* Arrow function에서 identifier 노드를 얻어낸다. identifier 노드를 얻어서 추후 route.ts 파일을 생성할 때 | ||
* identifier 노드에서 name 필드를 얻어서 fastify handler에 전달하는 역할을 한다. | ||
*/ | ||
export function getArrowFunctionOptionNode(nodes: ExportedDeclarations[]): TFastifyRouteOption | undefined { | ||
const identifierNode = findOrThrow(nodes, (node) => node.getKind() === SyntaxKind.Identifier).asKindOrThrow( | ||
SyntaxKind.Identifier, | ||
); | ||
|
||
const expectArrowNode = nodes.find((node) => node.getKind() === SyntaxKind.ArrowFunction); | ||
|
||
if (expectArrowNode == null) { | ||
return undefined; | ||
} | ||
|
||
const arrowFunctionNode = findOrThrow(nodes, (node) => node.getKind() === SyntaxKind.ArrowFunction).asKindOrThrow( | ||
SyntaxKind.ArrowFunction, | ||
); | ||
|
||
return { | ||
path: arrowFunctionNode.getSourceFile().getFilePath().toString(), | ||
kind: arrowFunctionNode.isAsync() ? 'async' : 'sync', | ||
type: 'arrow', | ||
node: arrowFunctionNode, | ||
name: identifierNode.getText(), | ||
}; | ||
} |
This file contains 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,20 @@ | ||
import type { TFastifyRouteOption } from '#/compilers/interfaces/TFastifyRouteOption'; | ||
import { findOrThrow } from 'my-easy-fp'; | ||
import * as tsm from 'ts-morph'; | ||
|
||
export function getFunctionOptionNode(nodes: tsm.ExportedDeclarations[]): TFastifyRouteOption | undefined { | ||
const functionDeclarationNode = findOrThrow( | ||
nodes, | ||
(node) => node.getKind() === tsm.SyntaxKind.FunctionDeclaration, | ||
).asKindOrThrow(tsm.SyntaxKind.FunctionDeclaration); | ||
|
||
const identifierNode = functionDeclarationNode.getNameNodeOrThrow(); | ||
|
||
return { | ||
path: functionDeclarationNode.getSourceFile().getFilePath().toString(), | ||
kind: functionDeclarationNode.isAsync() ? 'async' : 'sync', | ||
type: 'function', | ||
node: functionDeclarationNode, | ||
name: identifierNode.getText(), | ||
}; | ||
} |
This file contains 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
Oops, something went wrong.