Skip to content

Commit

Permalink
feat(fab-spec#386): add support for typed settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Kennedy committed Apr 27, 2021
1 parent d18dc7d commit d3a3c7c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
27 changes: 19 additions & 8 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ResponseInterceptor,
} from '@fab/core'
import { Key, pathToRegexp } from 'path-to-regexp'
import { FabSettings } from './types'

export enum Priority {
LAST,
Expand All @@ -29,8 +30,8 @@ export type RuntimeImports = Array<{
args: PluginArgs
}>

export class FABRouter {
private pipeline: { [order in Priority]: FabRequestResponder[] }
export class FABRouter<Settings extends FabSettings = FabSettings> {
private pipeline: { [order in Priority]: FabRequestResponder<Settings>[] }

constructor() {
this.pipeline = {
Expand All @@ -52,19 +53,26 @@ export class FABRouter {
]
}

addToPipeline(responder: FabRequestResponder, priority: Priority = Priority.MIDDLE) {
addToPipeline(
responder: FabRequestResponder<Settings>,
priority: Priority = Priority.MIDDLE
) {
this.pipeline[priority].push(responder)
}

on(route: string, responder: FabRequestResponderWithParams, priority?: Priority) {
on(
route: string,
responder: FabRequestResponderWithParams<Settings>,
priority?: Priority
) {
if (route === '*') {
// Make this an alias for .onAll, with an empty params object
this.onAll((context) => responder({ ...context, params: {} }), priority)
} else {
// Otherwise compile the route and generate a responder
const groups: Key[] = []
const regexp = pathToRegexp(route, groups)
this.addToPipeline(async (context: FabResponderArgs) => {
this.addToPipeline(async (context: FabResponderArgs<Settings>) => {
const { pathname } = context.url
// Only execute if this request matches our route
const match = regexp.exec(pathname)
Expand All @@ -80,7 +88,7 @@ export class FABRouter {
}
}

onAll(responder: FabRequestResponder, priority?: Priority) {
onAll(responder: FabRequestResponder<Settings>, priority?: Priority) {
this.addToPipeline(responder, priority)
}

Expand All @@ -106,10 +114,13 @@ class NoopCache implements FabCache {
getStream = async () => undefined
}

export class FABRuntime<T extends PluginMetadata = PluginMetadata> {
export class FABRuntime<
T extends PluginMetadata = PluginMetadata,
Settings extends FabSettings = FabSettings
> {
Metadata: T
FileMetadata: FabFileMetadata
Router: FABRouter
Router: FABRouter<Settings>
Cache: FabCache
ServerContext: FABServerContext

Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export type FabResponderMutableContext = {
export type Cookies = {
[key: string]: string
}
export type FabResponderArgs = {
export type FabResponderArgs<Settings extends FabSettings = FabSettings> = {
request: Request
settings: FabSettings
settings: Settings
url: URL
context: FabResponderMutableContext
cookies: Cookies
Expand All @@ -80,15 +80,15 @@ export type FabResponderArgs = {
* a Response. If this responder should not handle this request, it
* returns undefined and the next renderer is invoked.
* */
export type FabRequestResponder = (
context: FabResponderArgs
export type FabRequestResponder<Settings extends FabSettings = FabSettings> = (
context: FabResponderArgs<Settings>
) => Promise<undefined | Request | Response | Directive>

export type MatchParams = { [match_name: string]: string }

export type FabRequestResponderWithParams = (
contextWithParams: FabResponderArgs & { params: MatchParams }
) => ReturnType<FabRequestResponder>
export type FabRequestResponderWithParams<Settings extends FabSettings = FabSettings> = (
contextWithParams: FabResponderArgs<Settings> & { params: MatchParams }
) => ReturnType<FabRequestResponder<Settings>>

export type ResponseInterceptor = (response: Response) => Promise<Response>

Expand Down

0 comments on commit d3a3c7c

Please sign in to comment.