deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.2k stars 123 forks source link

Support matching multiple http routes depending on pattern #277

Closed devonpmack closed 2 years ago

devonpmack commented 2 years ago
const pattern = /one|two/;

export class Home {
  @http.GET('/:page')
  page(page: string & Pattern<typeof pattern>) {}

  @http.GET('/:page')
  other(page: string) {}
}

Expected

GET /three would route to the other method

GET /two routes to the page method

Actual

Validation error because three doesn't match one|two

marcj commented 2 years ago

This is supported, see HttpRegExp https://deepkit-book.herokuapp.com/deepkit-book-english.html#_path_parameters

example


    class Controller {
        @http.GET('string/:text')
        text(text: HttpRegExp<string, '[a-zA-Z]*'>) {
            return [text];
        }

        @http.GET('number/:number')
        number(number: HttpRegExp<number, '[0-9]*'>) {
            return [number];
        }
    }
marcj commented 2 years ago

I made it possible to pass an actual RegExp object to HttpRegExp in https://github.com/deepkit/deepkit-framework/commit/ba98832bc33662914145e3fb7654bd9a040d52e8

    const pattern = /one|two/;

    class Controller {
        @http.GET('type/:type')
        type(type: HttpRegExp<string, typeof pattern>) {
            return [type];
        }
    }
devonpmack commented 2 years ago

Great! Thanks, I missed that.