-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
196 additions
and
254 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
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Controller, Post } from '../decorators' | ||
import localBookManager from '../utils/localBookManager' | ||
import bookManager from '../utils/book-manager' | ||
import { BaseController } from './BaseController' | ||
|
||
@Controller('/bookshelf') | ||
export class Bookshelf extends BaseController { | ||
@Post('list') | ||
read() { | ||
return localBookManager.getBookList(this.app.config.bookDir) | ||
return bookManager.getBookList(this.app.config.bookDir) | ||
} | ||
} |
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,25 @@ | ||
import * as fs from 'node:fs' | ||
import * as path from 'node:path' | ||
|
||
// @ts-expect-error | ||
import { ensureFileSync } from 'fs-extra/esm' | ||
import { CACHE_DIR } from '../constants' | ||
import { createCacheDecorator } from './create-cache-decorator' | ||
|
||
function getCachePath(key: string) { | ||
return path.join(CACHE_DIR, key) | ||
} | ||
|
||
export const Cacheable = createCacheDecorator<any>({ | ||
getItem: async (key: string) => { | ||
const cachePath = getCachePath(key) | ||
if (!fs.existsSync(cachePath)) | ||
return | ||
return JSON.parse(fs.readFileSync(cachePath, 'utf-8')) | ||
}, | ||
setItem: async (key: string, value: any) => { | ||
const cachePath = getCachePath(key) | ||
ensureFileSync(cachePath) | ||
fs.writeFileSync(cachePath, JSON.stringify(value), 'utf-8') | ||
}, | ||
}) |
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,90 @@ | ||
import { isConstructor, isFunction } from '../utils/is' | ||
|
||
const PATH_METADATA = 'path' | ||
const METHOD_METADATA = 'method' | ||
|
||
export enum RequestMethod { | ||
GET = 'GET', | ||
POST = 'POST', | ||
} | ||
|
||
export function Controller(path: string): ClassDecorator { | ||
return (target) => { | ||
Reflect.defineMetadata(PATH_METADATA, path, target) | ||
} | ||
} | ||
|
||
interface RequestMappingMetadata { | ||
path?: string | string[] | ||
method?: RequestMethod | ||
} | ||
|
||
const defaultMetadata = { | ||
[PATH_METADATA]: '/', | ||
[METHOD_METADATA]: RequestMethod.GET, | ||
} | ||
|
||
function RequestMapping( | ||
metadata: RequestMappingMetadata = defaultMetadata, | ||
): MethodDecorator { | ||
const pathMetadata = metadata[PATH_METADATA] | ||
const path = pathMetadata && pathMetadata.length ? pathMetadata : '/' | ||
const requestMethod = metadata[METHOD_METADATA] || RequestMethod.GET | ||
|
||
return ( | ||
target: object, | ||
key: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
) => { | ||
Reflect.defineMetadata(PATH_METADATA, path, descriptor.value) | ||
Reflect.defineMetadata(METHOD_METADATA, requestMethod, descriptor.value) | ||
return descriptor | ||
} | ||
} | ||
|
||
function createMappingDecorator(method: RequestMethod) { | ||
return (path?: string | string[]): MethodDecorator => { | ||
return RequestMapping({ | ||
[PATH_METADATA]: path, | ||
[METHOD_METADATA]: method, | ||
}) | ||
} | ||
} | ||
|
||
export const Get = createMappingDecorator(RequestMethod.GET) | ||
export const Post = createMappingDecorator(RequestMethod.POST) | ||
|
||
interface Route { | ||
method: string | ||
route: string | ||
methodName: string | ||
} | ||
|
||
export function mapRoute(controller: object): Route[] { | ||
const instance = Object.create(controller) | ||
const prototype = instance.prototype | ||
const basePath: string = Reflect.getMetadata(PATH_METADATA, controller) | ||
|
||
const methodsNames = Object.getOwnPropertyNames(prototype).filter( | ||
methodName => | ||
!isConstructor(methodName) | ||
&& isFunction(prototype[methodName]) | ||
&& Reflect.getMetadata(PATH_METADATA, prototype[methodName]), | ||
) | ||
|
||
return methodsNames.map((methodName) => { | ||
const fn = prototype[methodName] | ||
const methodPath: string = Reflect.getMetadata(PATH_METADATA, fn) | ||
let route = basePath.replace(/^\//, '') // 移出前缀斜杠 | ||
if (methodPath.startsWith('/')) | ||
route = methodPath // 方法定义完整路径 | ||
else route = `${route}/${methodPath}` // 补充 controller 路径 | ||
|
||
const method = Reflect.getMetadata(METHOD_METADATA, fn) | ||
return { | ||
method, | ||
route, | ||
methodName, | ||
} | ||
}) | ||
} |
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,52 @@ | ||
export interface CreateCacheDecoratorOptions<T> { | ||
getItem: (key: string) => Promise<T> | ||
setItem: (key: string, value: T) => Promise<void> | ||
} | ||
|
||
export function createCacheDecorator<T>( | ||
options: CreateCacheDecoratorOptions<T>, | ||
) { | ||
return (decoratorArgs: { | ||
cacheKey: (arg: { | ||
className: string | ||
methodName: string | ||
args: any[] | ||
}) => string | ||
}): MethodDecorator => { | ||
return ( | ||
_target: unknown, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
) => { | ||
const fn = descriptor.value | ||
|
||
descriptor.value = async function (...args: unknown[]) { | ||
const methodName = propertyKey.toString() | ||
let cacheKey = '' | ||
if (typeof decoratorArgs.cacheKey === 'function') { | ||
cacheKey = decoratorArgs.cacheKey({ | ||
className: this.constructor.name, | ||
methodName, | ||
args, | ||
}) | ||
} | ||
else { | ||
cacheKey = `${this.constructor.name}_${methodName}` | ||
} | ||
|
||
const cachedResult = await options.getItem(cacheKey) | ||
|
||
if (cachedResult !== undefined) { | ||
return cachedResult | ||
} | ||
else { | ||
const result = await fn.apply(this, args) | ||
await options.setItem(cacheKey, result) | ||
return result | ||
} | ||
} | ||
|
||
return descriptor | ||
} | ||
} | ||
} |
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,90 +1,2 @@ | ||
import { isConstructor, isFunction } from '../utils/is' | ||
|
||
const PATH_METADATA = 'path' | ||
const METHOD_METADATA = 'method' | ||
|
||
export enum RequestMethod { | ||
GET = 'GET', | ||
POST = 'POST', | ||
} | ||
|
||
export function Controller(path: string): ClassDecorator { | ||
return (target) => { | ||
Reflect.defineMetadata(PATH_METADATA, path, target) | ||
} | ||
} | ||
|
||
interface RequestMappingMetadata { | ||
path?: string | string[] | ||
method?: RequestMethod | ||
} | ||
|
||
const defaultMetadata = { | ||
[PATH_METADATA]: '/', | ||
[METHOD_METADATA]: RequestMethod.GET, | ||
} | ||
|
||
function RequestMapping( | ||
metadata: RequestMappingMetadata = defaultMetadata, | ||
): MethodDecorator { | ||
const pathMetadata = metadata[PATH_METADATA] | ||
const path = pathMetadata && pathMetadata.length ? pathMetadata : '/' | ||
const requestMethod = metadata[METHOD_METADATA] || RequestMethod.GET | ||
|
||
return ( | ||
target: object, | ||
key: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
) => { | ||
Reflect.defineMetadata(PATH_METADATA, path, descriptor.value) | ||
Reflect.defineMetadata(METHOD_METADATA, requestMethod, descriptor.value) | ||
return descriptor | ||
} | ||
} | ||
|
||
function createMappingDecorator(method: RequestMethod) { | ||
return (path?: string | string[]): MethodDecorator => { | ||
return RequestMapping({ | ||
[PATH_METADATA]: path, | ||
[METHOD_METADATA]: method, | ||
}) | ||
} | ||
} | ||
|
||
export const Get = createMappingDecorator(RequestMethod.GET) | ||
export const Post = createMappingDecorator(RequestMethod.POST) | ||
|
||
interface Route { | ||
method: string | ||
route: string | ||
methodName: string | ||
} | ||
|
||
export function mapRoute(controller: object): Route[] { | ||
const instance = Object.create(controller) | ||
const prototype = instance.prototype | ||
const basePath: string = Reflect.getMetadata(PATH_METADATA, controller) | ||
|
||
const methodsNames = Object.getOwnPropertyNames(prototype).filter( | ||
methodName => | ||
!isConstructor(methodName) | ||
&& isFunction(prototype[methodName]) | ||
&& Reflect.getMetadata(PATH_METADATA, prototype[methodName]), | ||
) | ||
|
||
return methodsNames.map((methodName) => { | ||
const fn = prototype[methodName] | ||
const methodPath: string = Reflect.getMetadata(PATH_METADATA, fn) | ||
let route = basePath.replace(/^\//, '') // 移出前缀斜杠 | ||
if (methodPath.startsWith('/')) | ||
route = methodPath // 方法定义完整路径 | ||
else route = `${route}/${methodPath}` // 补充 controller 路径 | ||
|
||
const method = Reflect.getMetadata(METHOD_METADATA, fn) | ||
return { | ||
method, | ||
route, | ||
methodName, | ||
} | ||
}) | ||
} | ||
export * from './controller' | ||
export * from './cache' |
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.