-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up todos, route request chains, proper 405s
- Loading branch information
Showing
16 changed files
with
214 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-disable max-len */ | ||
import Response from './response'; | ||
import { Context } from './road'; | ||
|
||
export interface NextCallback { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(): Promise<Response | string> | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
export class RequestChain<fn extends Function> { | ||
/** | ||
* The request chain is an array of functions that are executed in order when `run` is called | ||
*/ | ||
protected _function_chain: fn[]; | ||
|
||
/** | ||
* RequestChain Constructor | ||
* | ||
* Creates a new RequestChain object | ||
*/ | ||
constructor (initial?: fn[]) { | ||
this._function_chain = initial || []; | ||
} | ||
|
||
add (fn: fn) { | ||
this._function_chain.push(fn); | ||
} | ||
|
||
length () { | ||
return this._function_chain.length; | ||
} | ||
|
||
/** | ||
* The `run` function is used to execute the function chain. | ||
* | ||
* The function chain is executed in order, with each function in the chain being called with the parameters provided to the function. | ||
* | ||
* The function function returns a Promise that resolves to a Response object or a string. | ||
* | ||
*/ | ||
getChainStart () { | ||
|
||
let progress = 0; | ||
const next: (context: Context, ...args: any[]) => Promise<Response | string> = async (context, ...args) => { | ||
|
||
if (this._function_chain.length && this._function_chain[progress]) { | ||
return this._function_chain[progress].call(context, ...args, () => { | ||
progress += 1; | ||
return next(context, ...args); | ||
}); | ||
} | ||
|
||
// If next is called and there is nothing next, we should still return a promise, | ||
// it just shouldn't do anything | ||
return new Response('Page not found', 404); | ||
}; | ||
|
||
return next; | ||
} | ||
} |
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
Oops, something went wrong.