@@ -3,80 +3,80 @@ import { sha256, log } from "./libs";
33import { Config , Update , localhost } from "./types" ;
44
55export default class Handler {
6- configs : Partial < Config > [ ] ;
6+ configs : Partial < Config > [ ] ;
77
8- constructor ( configs : Partial < Config > [ ] ) {
9- this . configs = configs ;
10- }
8+ constructor ( configs : Partial < Config > [ ] ) {
9+ this . configs = configs ;
10+ }
1111
12- getResponse = async (
13- _request ?: Request ,
14- _bot ?: BotApi
15- ) : Promise < Response > => {
16- this . getAccessKeys ( this . configs ) . then ( ( access_keys ) =>
17- Object . keys ( access_keys ) . forEach ( ( key ) =>
18- log (
19- `${ access_keys [ key ] . bot_name } ${ ( ( request_url ) =>
20- `${ request_url . origin } ${ request_url . pathname } ` ) (
21- new URL ( _request ?. url ?? localhost )
22- ) } ${ key } `
23- )
24- )
25- ) ;
26- if ( _bot ?. webhook . token ) {
27- return _bot . webhook . process ( new URL ( _request ?. url ?? localhost ) ) ;
28- }
29- return this . responses . default ( ) ;
30- } ;
12+ getResponse = async (
13+ _request ?: Request ,
14+ _bot ?: BotApi
15+ ) : Promise < Response > => {
16+ this . getAccessKeys ( this . configs ) . then ( ( access_keys ) =>
17+ Object . keys ( access_keys ) . forEach ( ( key ) =>
18+ log (
19+ `${ access_keys [ key ] . bot_name } ${ ( ( request_url ) =>
20+ `${ request_url . origin } ${ request_url . pathname } ` ) (
21+ new URL ( _request ?. url ?? localhost )
22+ ) } ${ key } `
23+ )
24+ )
25+ ) ;
26+ if ( _bot ?. webhook . token ) {
27+ return _bot . webhook . process ( new URL ( _request ?. url ?? localhost ) ) ;
28+ }
29+ return this . responses . default ( ) ;
30+ } ;
3131
32- postResponse = async (
33- _request ?: Request ,
34- _bot ?: BotApi
35- ) : Promise < Response > =>
36- _bot ?. webhook . token === ""
37- ? this . responses . default ( )
38- : _request
39- ? _request
40- . json ( )
41- . then ( ( update ) => ( _bot as BotApi ) . update ( update as Update ) )
42- : this . responses . default ( ) ;
32+ postResponse = async (
33+ _request ?: Request ,
34+ _bot ?: BotApi
35+ ) : Promise < Response > =>
36+ _bot ?. webhook . token === ""
37+ ? this . responses . default ( )
38+ : _request
39+ ? _request
40+ . json ( )
41+ . then ( ( update ) => ( _bot as BotApi ) . update ( update as Update ) )
42+ : this . responses . default ( ) ;
4343
44- responses : Record <
45- string ,
46- ( _request ?: Request , _bot ?: BotApi ) => Promise < Response >
47- > = {
48- GET : this . getResponse ,
49- POST : this . postResponse ,
50- default : ( ) => new Promise ( ( ) => new Response ( ) ) ,
51- } ;
44+ responses : Record <
45+ string ,
46+ ( _request ?: Request , _bot ?: BotApi ) => Promise < Response >
47+ > = {
48+ GET : this . getResponse ,
49+ POST : this . postResponse ,
50+ default : ( ) => new Promise ( ( ) => new Response ( ) ) ,
51+ } ;
5252
53- getAccessKeys = async (
54- configs : Partial < Config > [ ]
55- ) : Promise < Record < string , Config > | Record < string , never > > =>
56- Promise . all (
57- configs . map ( ( bot_config : Partial < Config > ) =>
58- sha256 ( bot_config . webhook ?. token ?? "" ) . then ( ( hash ) => {
59- console . log ( hash ) ;
60- return [ hash , bot_config ] ;
61- } )
62- )
63- ) . then ( ( result ) => Object . fromEntries ( result ) ) ;
53+ getAccessKeys = async (
54+ configs : Partial < Config > [ ]
55+ ) : Promise < Record < string , Config > | Record < string , never > > =>
56+ Promise . all (
57+ configs . map ( ( bot_config : Partial < Config > ) =>
58+ sha256 ( bot_config . webhook ?. token ?? "" ) . then ( ( hash ) => {
59+ console . log ( hash ) ;
60+ return [ hash , bot_config ] ;
61+ } )
62+ )
63+ ) . then ( ( result ) => Object . fromEntries ( result ) ) ;
6464
65- // handles the request
66- handle = async ( request : Request ) : Promise < Response > =>
67- this . getAccessKeys ( this . configs ) . then ( ( access_keys ) =>
68- Object . keys ( this . responses ) . includes ( request . method )
69- ? this . responses [ request . method ] (
70- request ,
71- ( ( key ) => {
72- return new access_keys [ key ] . api ( {
73- ...new Config ( ) ,
74- ...access_keys [ key ] ,
75- url : new URL ( new URL ( request . url ) . origin ) , // worker url
76- handler : this ,
77- } ) ;
78- } ) ( new URL ( request . url ) . pathname . substring ( 1 ) )
79- )
80- : this . responses . default ( )
81- ) ;
65+ // handles the request
66+ handle = async ( request : Request ) : Promise < Response > =>
67+ this . getAccessKeys ( this . configs ) . then ( ( access_keys ) =>
68+ Object . keys ( this . responses ) . includes ( request . method )
69+ ? this . responses [ request . method ] (
70+ request ,
71+ ( ( key ) => {
72+ return new access_keys [ key ] . api ( {
73+ ...new Config ( ) ,
74+ ...access_keys [ key ] ,
75+ url : new URL ( new URL ( request . url ) . origin ) , // worker url
76+ handler : this ,
77+ } ) ;
78+ } ) ( new URL ( request . url ) . pathname . substring ( 1 ) )
79+ )
80+ : this . responses . default ( )
81+ ) ;
8282}
0 commit comments