import { Dict , makeArray , Quester } from 'koishi' ; import { AxiosRequestConfig } from 'axios' ; type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' ; export class Internal { static define ( routes ) { for ( const path in routes ) { for ( const key in routes [ path ] ) { const method = key as Method ; for ( const name of makeArray ( routes [ path ] [ method ] ) ) { Internal . prototype [ name ] = function ( this , ... args ) { const raw = args . join ( ', ' ) ; const url = path . replace ( /\{([^}]+)\}/g , ( ) => { if ( ! args . length ) throw new Error ( `too few arguments for ${ path }, received ${ raw }` ) ; return args . shift ( ) ; } ) ; const config = { } ; if ( args . length === 1 ) { if ( method === 'GET' || method === 'DELETE' ) { config . params = args [ 0 ] ; } else { config . data = args [ 0 ] ; } } else if ( args . length === 2 && method !== 'GET' && method !== 'DELETE' ) { config . data = args [ 0 ] ; config . params = args [ 1 ] ; } else if ( args . length > 1 ) { throw new Error ( `too many arguments for ${ path }, received ${ raw }` ) ; } return this . http ( method , url , config ) ; } ; } } } } constructor ( private http ) { } }