Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ConstraintStrategy typing #180

Merged
merged 5 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('', () => {}));
}