-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate utils for customizing api route. (#61)
- Loading branch information
1 parent
40e6cfa
commit 03bd5ed
Showing
7 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,77 @@ | ||
import { FibApp } from '../Typo/app'; | ||
|
||
type ApiRouteContext = Parameters<FibApp.FibAppOpts['customizeApiRoute']>[0]; | ||
|
||
type GetHandlerType<T, K extends ApiRouteContext['routeType']> = T extends { | ||
routeType: K | ||
handler: infer H; | ||
} ? { [P in K]: H }[K] : never | ||
|
||
type FibAppApiHandlers = { | ||
[K in ApiRouteContext['routeType']]: GetHandlerType<ApiRouteContext, K> | ||
} | ||
|
||
type BaseFilter = { | ||
basecls: string, | ||
extend?: string, | ||
id?: string, | ||
rid?: string, | ||
}; | ||
|
||
type PostFuncFilter = { func: string | string[] }; | ||
|
||
function onApiRoute<T extends ApiRouteContext['routeType']>( | ||
ctx: ApiRouteContext, | ||
routeType: T, | ||
_filters: T extends 'http-postfunc' ? (BaseFilter & PostFuncFilter) : (string | BaseFilter), | ||
userDefineHandler: FibAppApiHandlers[T] | ||
) { | ||
if (routeType !== ctx.routeType) return null; | ||
|
||
const filters: BaseFilter & Partial<PostFuncFilter> = typeof _filters === 'string' ? { basecls: _filters } : _filters; | ||
|
||
const funcs = (Array.isArray(filters.func) ? filters.func : [filters.func]).filter(Boolean); | ||
|
||
if (routeType === 'http-postfunc' && !funcs.length) { | ||
throw new Error(`[onApiRoute] func name is required for route with type 'http-postfunc'`); | ||
} | ||
|
||
return (...args: Parameters<typeof userDefineHandler>) => { | ||
const [, _maybeBase, _idOrFunc, _maybeExtend, _eid] = args; | ||
if (_maybeBase !== filters.basecls) return ; | ||
|
||
if (filters.extend) { | ||
if (_maybeExtend !== filters.extend) return ; | ||
if (filters.rid && _eid !== filters.rid) return ; | ||
} | ||
|
||
if (routeType === 'http-postfunc' && !funcs.includes(_idOrFunc as string)) { | ||
return ; | ||
} | ||
|
||
return userDefineHandler.apply(null, args); | ||
}; | ||
} | ||
|
||
export function makeCustomizeApiRoute( | ||
maker: (ctx: { | ||
onApiRoute: typeof onApiRoute | ||
}) => FibApp.FibAppOpts['customizeApiRoute'], | ||
options?: { | ||
allowCustomizePostApiRoute?: FibApp.FibAppOpts['customizeApiRoute']['allowCustomizePostApiRoute'] | ||
} | ||
) { | ||
const customize = maker({ | ||
onApiRoute, | ||
}); | ||
|
||
if (options?.allowCustomizePostApiRoute) { | ||
Object.defineProperty(customize, 'allowCustomizePostApiRoute', { | ||
value: true, | ||
writable: false, | ||
configurable: false, | ||
}); | ||
} | ||
|
||
return customize; | ||
} |
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,66 @@ | ||
import { FibApp } from '../Typo/app'; | ||
declare type ApiRouteContext = Parameters<FibApp.FibAppOpts['customizeApiRoute']>[0]; | ||
declare type GetHandlerType<T, K extends ApiRouteContext['routeType']> = T extends { | ||
routeType: K; | ||
handler: infer H; | ||
} ? { | ||
[P in K]: H; | ||
}[K] : never; | ||
declare type FibAppApiHandlers = { | ||
[K in ApiRouteContext['routeType']]: GetHandlerType<ApiRouteContext, K>; | ||
}; | ||
declare type BaseFilter = { | ||
basecls: string; | ||
extend?: string; | ||
id?: string; | ||
rid?: string; | ||
}; | ||
declare type PostFuncFilter = { | ||
func: string | string[]; | ||
}; | ||
declare function onApiRoute<T extends ApiRouteContext['routeType']>(ctx: ApiRouteContext, routeType: T, _filters: T extends 'http-postfunc' ? (BaseFilter & PostFuncFilter) : (string | BaseFilter), userDefineHandler: FibAppApiHandlers[T]): (...args: Parameters<typeof userDefineHandler>) => any; | ||
export declare function makeCustomizeApiRoute(maker: (ctx: { | ||
onApiRoute: typeof onApiRoute; | ||
}) => FibApp.FibAppOpts['customizeApiRoute'], options?: { | ||
allowCustomizePostApiRoute?: FibApp.FibAppOpts['customizeApiRoute']['allowCustomizePostApiRoute']; | ||
}): ((context: { | ||
app: FibApp.FibAppClass; | ||
} & ({ | ||
routeType: "http-rest-post"; | ||
handler: (origReq: FibApp.FibAppHttpRequest, classname: string) => void; | ||
} | { | ||
routeType: "http-rest-get"; | ||
handler: (origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType) => void; | ||
} | { | ||
routeType: "http-rest-put"; | ||
handler: (origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType) => void; | ||
} | { | ||
routeType: "http-rest-delete"; | ||
handler: (origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void; | ||
} | { | ||
routeType: "http-rest-find"; | ||
handler: (origReq: FibApp.FibAppHttpRequest, classname: string) => void; | ||
} | { | ||
routeType: "http-rest-epost"; | ||
handler: (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void; | ||
} | { | ||
routeType: "http-rest-eput"; | ||
withExtendId?: boolean; | ||
handler: (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid?: FibApp.AppIdType) => void; | ||
} | { | ||
routeType: "http-rest-efind"; | ||
handler: (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void; | ||
} | { | ||
routeType: "http-rest-eget"; | ||
withExtendId?: boolean; | ||
handler: (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid?: FibApp.AppIdType) => void; | ||
} | { | ||
routeType: "http-rest-edel"; | ||
handler: (req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid: FibApp.AppIdType) => void; | ||
} | { | ||
routeType: "http-postfunc"; | ||
handler: (origReq: FibApp.FibAppHttpRequest, classname: string, func_name: string) => void; | ||
})) => ((origReq: FibApp.FibAppHttpRequest, classname: string) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid?: FibApp.AppIdType) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid?: FibApp.AppIdType) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid: FibApp.AppIdType) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, func_name: string) => void) | (((origReq: FibApp.FibAppHttpRequest, classname: string) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid?: FibApp.AppIdType) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid?: FibApp.AppIdType) => void) | ((req: FibApp.FibAppHttpRequest, classname: string, id: FibApp.AppIdType, extend: string, rid: FibApp.AppIdType) => void) | ((origReq: FibApp.FibAppHttpRequest, classname: string, func_name: string) => void))[]) & { | ||
allowCustomizePostApiRoute?: boolean; | ||
}; | ||
export {}; |
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