Skip to content

Commit

Permalink
update ConstraintStrategy typing (#180)
Browse files Browse the repository at this point in the history
* update ConstraintStrategy typing

* add tests for custom constraints with objects as values

* remove constraint with objects test

* add type tests for custom contraints

* update variable names of the constraint strategy interface
  • Loading branch information
matthyk committed Mar 29, 2021
1 parent f068c73 commit bfe4a02
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ declare namespace Router {
store: any
) => void;

interface ConstraintStrategy<V extends HTTPVersion> {
interface ConstraintStrategy<V extends HTTPVersion, T = string> {
name: string,
mustMatchWhenDerived?: boolean,
storage() : {
get(version: String) : Handler<V> | null,
set(version: String, store: Handler<V>) : void,
del(version: String) : void,
get(value: T) : Handler<V> | null,
set(value: T, handler: Handler<V>) : void,
del(value: T) : void,
empty() : void
},
validate(value: unknown): void,
deriveConstraint<Context>(req: Req<V>, ctx?: Context) : String,
deriveConstraint<Context>(req: Req<V>, ctx?: Context) : T,
}

interface Config<V extends HTTPVersion> {
Expand Down
58 changes: 58 additions & 0 deletions test/types/router.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,61 @@ let http2Res!: Http2ServerResponse;
expectType<string>(router.prettyPrint())

}

// Custom Constraint
{
let handler: Router.Handler<Router.HTTPVersion.V1>

interface AcceptAndContentType { accept?: string, contentType?: string }

const customConstraintWithObject: Router.ConstraintStrategy<Router.HTTPVersion.V1, AcceptAndContentType> = {
name: "customConstraintWithObject",
deriveConstraint<Context>(req: Router.Req<Router.HTTPVersion.V1>, ctx: Context | undefined): AcceptAndContentType {
return {
accept: req.headers.accept,
contentType: req.headers["content-type"]
}
},
validate(value: unknown): void {},
storage () {
return {
get (version) { return handler },
set (version, handler) {},
del (version) {},
empty () {}
}
}
}
const storageWithObject = customConstraintWithObject.storage()
const acceptAndContentType: AcceptAndContentType = { accept: 'application/json', contentType: 'application/xml' }

expectType<AcceptAndContentType>(customConstraintWithObject.deriveConstraint(http1Req, http1Res))
expectType<void>(storageWithObject.empty())
expectType<void>(storageWithObject.del(acceptAndContentType));
expectType<Router.Handler<Router.HTTPVersion.V1> | null>(storageWithObject.get(acceptAndContentType));
expectType<void>(storageWithObject.set(acceptAndContentType, () => {}));

const customConstraintWithDefault: Router.ConstraintStrategy<Router.HTTPVersion.V1> = {
name: "customConstraintWithObject",
deriveConstraint<Context>(req: Router.Req<Router.HTTPVersion.V1>, ctx: Context | undefined): string {
return req.headers.accept ?? ''
},
validate(value: unknown): void {},
storage () {
return {
get (version) { return handler },
set (version, handler) {},
del (version) {},
empty () {}
}
}
}

const storageWithDefault = customConstraintWithDefault.storage()

expectType<string>(customConstraintWithDefault.deriveConstraint(http1Req, http1Res))
expectType<void>(storageWithDefault.empty())
expectType<void>(storageWithDefault.del(''));
expectType<Router.Handler<Router.HTTPVersion.V1> | null>(storageWithDefault.get(''));
expectType<void>(storageWithDefault.set('', () => {}));
}

0 comments on commit bfe4a02

Please sign in to comment.