Skip to content

Commit

Permalink
feat(generator): enhance route option generator
Browse files Browse the repository at this point in the history
- enhance: `RouteShorthandOptions` generator now supports function, arrow function, and variable
  • Loading branch information
imjuni committed Feb 5, 2024
1 parent 94b2567 commit 0291095
Show file tree
Hide file tree
Showing 28 changed files with 441 additions and 98 deletions.
10 changes: 10 additions & 0 deletions examples/handlers/hello/put.ts
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';
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
}
},
"files": [
"dist"
"dist",
"templates"
],
"bugs": {
"url": "https://github.com/imjuni/fast-maker/issues"
Expand Down
3 changes: 2 additions & 1 deletion src/compilers/interfaces/IFastifyRouteOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TFastifyRouteOption } from '#/compilers/interfaces/TFastifyRouteOption';
import type * as tsm from 'ts-morph';

export interface IFastifyRouteOptions {
Expand All @@ -8,7 +9,7 @@ export interface IFastifyRouteOptions {
};

node: {
option?: tsm.Node;
option?: TFastifyRouteOption;
methods?: tsm.Node;
map?: tsm.Node;
};
Expand Down
35 changes: 35 additions & 0 deletions src/compilers/interfaces/TFastifyRouteOption.ts
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;
});
4 changes: 2 additions & 2 deletions src/compilers/navigate/__tests__/local.module.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getResolvedInFileImportedModules } from '#/compilers/navigate/getResolvedInFileImportedModules';
import { getRouteNode } from '#/compilers/routes/getRouteNode';
import { getRouteFunction } from '#/compilers/routes/getRouteFunction';
import { getTypeReferences } from '#/compilers/type-tools/getTypeReferences';
import { CE_EXT_KIND } from '#/configs/const-enum/CE_EXT_KIND';
import { atOrThrow, orThrow } from 'my-easy-fp';
Expand Down Expand Up @@ -41,7 +41,7 @@ export function handler(req: FastifyRequest<{ Querystring: TQuerystring, Params:
project.createSourceFile(path.join('examples', name), code, { overwrite });

const sourceFile = create(filename01, source01, true);
const node = orThrow(getRouteNode(sourceFile));
const node = orThrow(getRouteFunction(sourceFile));
const parameters = node.node.getParameters();
const parameter = atOrThrow(parameters, 0);
const types = getTypeReferences(parameter);
Expand Down
10 changes: 5 additions & 5 deletions src/compilers/navigate/__tests__/property.signature.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPropertySignatures } from '#/compilers/navigate/getPropertySignatures';
import { getRouteNodeOrThrow } from '#/compilers/routes/getRouteNodeOrThrow';
import { getRouteFunctionOrThrow } from '#/compilers/routes/getRouteFunctionOrThrow';
import { atOrThrow } from 'my-easy-fp';
import { randomUUID } from 'node:crypto';
import path from 'node:path';
Expand Down Expand Up @@ -29,7 +29,7 @@ export function handler(req: { Q: { name: string }; B: { age: number} }) {
project.createSourceFile(path.join('examples', name), code, { overwrite });

const sourceFile02 = create(filename01, source01.trim(), true);
const node = getRouteNodeOrThrow(sourceFile02);
const node = getRouteFunctionOrThrow(sourceFile02);
const parameter = atOrThrow(node.node.getParameters(), 0);

const r01 = getPropertySignatures(parameter);
Expand All @@ -49,7 +49,7 @@ export function handler(req: ITestInfoType01) {
project.createSourceFile(path.join('examples', name), code, { overwrite });

const sourceFile02 = create(filename01, source01.trim(), true);
const node = getRouteNodeOrThrow(sourceFile02);
const node = getRouteFunctionOrThrow(sourceFile02);
const parameter = atOrThrow(node.node.getParameters(), 0);

const r01 = getPropertySignatures(parameter);
Expand All @@ -69,7 +69,7 @@ export function handler(req: FastifyRequest<ITestInfoType01>) {
project.createSourceFile(path.join('examples', name), code, { overwrite });

const sourceFile02 = create(filename01, source01.trim(), true);
const node = getRouteNodeOrThrow(sourceFile02);
const node = getRouteFunctionOrThrow(sourceFile02);
const parameter = atOrThrow(node.node.getParameters(), 0);

const r01 = getPropertySignatures(parameter);
Expand All @@ -89,7 +89,7 @@ export function handler(req: FastifyRequest) {
project.createSourceFile(path.join('examples', name), code, { overwrite });

const sourceFile02 = create(filename01, source01.trim(), true);
const node = getRouteNodeOrThrow(sourceFile02);
const node = getRouteFunctionOrThrow(sourceFile02);
const parameter = atOrThrow(node.node.getParameters(), 0);

const r01 = getPropertySignatures(parameter);
Expand Down
10 changes: 5 additions & 5 deletions src/compilers/navigate/__tests__/resolved.module.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getResolvedImportedModules } from '#/compilers/navigate/getResolvedImportedModules';
import { getRouteNode } from '#/compilers/routes/getRouteNode';
import { getRouteFunction } from '#/compilers/routes/getRouteFunction';
import { getTypeReferences } from '#/compilers/type-tools/getTypeReferences';
import { CE_EXT_KIND } from '#/configs/const-enum/CE_EXT_KIND';
import { posixJoin } from '#/tools/posixJoin';
Expand Down Expand Up @@ -41,7 +41,7 @@ export function handler(req: FastifyRequest<{ Querystring: ITestInfoType01 }>) {
create(filename01, abilityInterfaceSourceCode, true);
const sourceFile02 = create(filename02, source02.trim(), true);

const node = orThrow(getRouteNode(sourceFile02));
const node = orThrow(getRouteFunction(sourceFile02));
const parameters = node.node.getParameters();
const parameter = atOrThrow(parameters, 0);
const types = getTypeReferences(parameter);
Expand Down Expand Up @@ -104,7 +104,7 @@ export function handler(req: FastifyRequest<{ Querystring: ICompany }>) {
create(filename01, abilityInterfaceSourceCode, true);
const sourceFile02 = create(filename02, source02.trim(), true);

const node = orThrow(getRouteNode(sourceFile02));
const node = orThrow(getRouteFunction(sourceFile02));
const parameters = node.node.getParameters();
const parameter = atOrThrow(parameters, 0);
const types = getTypeReferences(parameter);
Expand Down Expand Up @@ -168,7 +168,7 @@ export function handler(req: FastifyRequest<{ Querystring: ICompany }, Server>)
create(filename01, abilityInterfaceSourceCode, true);
const sourceFile02 = create(filename02, source02.trim(), true);

const node = orThrow(getRouteNode(sourceFile02));
const node = orThrow(getRouteFunction(sourceFile02));
const parameters = node.node.getParameters();
const parameter = atOrThrow(parameters, 0);
const types = getTypeReferences(parameter);
Expand Down Expand Up @@ -250,7 +250,7 @@ export function handler(req: FastifyRequest<{ Querystring: ICompany }, Server>)
create(filename01, abilityInterfaceSourceCode, true);
const sourceFile02 = create(filename02, source02.trim(), true);

const node = orThrow(getRouteNode(sourceFile02));
const node = orThrow(getRouteFunction(sourceFile02));
const parameters = node.node.getParameters();
const parameter = atOrThrow(parameters, 0);
const types = getTypeReferences(parameter);
Expand Down
8 changes: 4 additions & 4 deletions src/compilers/navigate/__tests__/symbol.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSymbol } from '#/compilers/navigate/getSymbol';
import { getRouteNode } from '#/compilers/routes/getRouteNode';
import { getRouteFunction } from '#/compilers/routes/getRouteFunction';
import { atOrThrow } from 'my-easy-fp';
import { randomUUID } from 'node:crypto';
import path from 'node:path';
Expand Down Expand Up @@ -33,7 +33,7 @@ export function handler(param: { Querystring: IAbility }) {

project.createSourceFile(filename01, abilityInterfaceSourceCode, { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const node = getRouteNode(sourceFile02);
const node = getRouteFunction(sourceFile02);
const parameters = node?.node.getParameters() ?? [];
const parameter = atOrThrow(parameters, 0);
const r01 = getSymbol(parameter.getType());
Expand All @@ -56,7 +56,7 @@ export function handler(param: FastifyRequest<{ Querystring: IAbility }>) {

project.createSourceFile(filename01, abilityInterfaceSourceCode, { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const node = getRouteNode(sourceFile02);
const node = getRouteFunction(sourceFile02);
const parameters = node?.node.getParameters() ?? [];
const parameter = atOrThrow(parameters, 0);
const r01 = getSymbol(parameter.getType());
Expand All @@ -73,7 +73,7 @@ export function handler(param: FastifyRequest<{ Querystring: IAbility }>) {
`.trim();

const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const node = getRouteNode(sourceFile02);
const node = getRouteFunction(sourceFile02);
const parameters = node?.node.getParameters() ?? [];
const parameter = atOrThrow(parameters, 0);
const r01 = getSymbol(parameter.getType());
Expand Down
32 changes: 32 additions & 0 deletions src/compilers/navigate/getArrowFunctionOptionNode.ts
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(),
};
}
20 changes: 20 additions & 0 deletions src/compilers/navigate/getFunctionOptionNode.ts
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(),
};
}
18 changes: 9 additions & 9 deletions src/compilers/routes/__tests__/route.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRouteNode } from '#/compilers/routes/getRouteNode';
import { getRouteFunction } from '#/compilers/routes/getRouteFunction';
import { posixJoin } from '#/tools/posixJoin';
import { randomUUID } from 'node:crypto';
import * as tsm from 'ts-morph';
Expand Down Expand Up @@ -47,7 +47,7 @@ export const option: RouteShorthandOptions = {
},
};`.trim();

describe('getRouteOptions', () => {
describe('getRouteFunction', () => {
it('synchronous function', () => {
const uuid = randomUUID();

Expand All @@ -62,7 +62,7 @@ export function handler(req: FastifyRequest<{ Body: IAbility }>) {
project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });

const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

// @ts-expect-error
delete r01?.node;
Expand All @@ -88,7 +88,7 @@ export async function handler(req: FastifyRequest<{ Body: IAbility }>) {
project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });

const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

// @ts-expect-error
delete r01?.node;
Expand All @@ -113,7 +113,7 @@ export const handler = (req: FastifyRequest<{ Body: IAbility }>) => {

project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

// @ts-expect-error
delete r01?.node;
Expand All @@ -138,7 +138,7 @@ export const handler = async (req: FastifyRequest<{ Body: IAbility }>) => {

project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

// @ts-expect-error
delete r01?.node;
Expand All @@ -161,7 +161,7 @@ export type handler = { a: number };`;

project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

expect(r01).toBeUndefined();
});
Expand All @@ -177,7 +177,7 @@ export const handler: number = 1;`;

project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

expect(r01).toBeUndefined();
});
Expand All @@ -193,7 +193,7 @@ export type hello = { a: number };`;

project.createSourceFile(filename01, abilityInterfaceSourceCode.trim(), { overwrite: true });
const sourceFile02 = project.createSourceFile(filename02, source02.trim(), { overwrite: true });
const r01 = getRouteNode(sourceFile02);
const r01 = getRouteFunction(sourceFile02);

expect(r01).toBeUndefined();
});
Expand Down
Loading

0 comments on commit 0291095

Please sign in to comment.