Skip to content

Commit

Permalink
fix(http): router callback typing
Browse files Browse the repository at this point in the history
  • Loading branch information
minenwerfer committed Mar 12, 2024
1 parent da9804d commit 9fde2e6
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 66 deletions.
7 changes: 7 additions & 0 deletions packages/aeria-build/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# aeria-build

## 0.0.23

### Patch Changes

- @aeriajs/api@0.0.20
- @aeriajs/builtins@0.0.20

## 0.0.22

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/aeria-build/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aeria-build",
"version": "0.0.22",
"version": "0.0.23",
"description": "",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -46,8 +46,8 @@
"tslib": "^2.5.0"
},
"peerDependencies": {
"@aeriajs/api": "^0.0.19",
"@aeriajs/builtins": "^0.0.19",
"@aeriajs/api": "^0.0.20",
"@aeriajs/builtins": "^0.0.20",
"@aeriajs/common": "^0.0.9",
"@aeriajs/types": "^0.0.8"
}
Expand Down
12 changes: 12 additions & 0 deletions packages/aeria/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# aeria

## 0.0.20

### Patch Changes

- Updated dependencies
- @aeriajs/http@0.0.14
- @aeriajs/api@0.0.20
- @aeriajs/node-http@0.0.14
- @aeriajs/server@0.0.20
- @aeriajs/builtins@0.0.20
- @aeriajs/security@0.0.20

## 0.0.19

### Patch Changes
Expand Down
14 changes: 7 additions & 7 deletions packages/aeria/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aeria",
"version": "0.0.19",
"version": "0.0.20",
"description": "",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -34,14 +34,14 @@
],
"peerDependencies": {
"@aeriajs/access-control": "^0.0.9",
"@aeriajs/api": "^0.0.19",
"@aeriajs/api": "^0.0.20",
"@aeriajs/common": "^0.0.9",
"@aeriajs/entrypoint": "^0.0.9",
"@aeriajs/http": "^0.0.13",
"@aeriajs/node-http": "^0.0.13",
"@aeriajs/security": "^0.0.19",
"@aeriajs/server": "^0.0.19",
"@aeriajs/builtins": "^0.0.19",
"@aeriajs/http": "^0.0.14",
"@aeriajs/node-http": "^0.0.14",
"@aeriajs/security": "^0.0.20",
"@aeriajs/server": "^0.0.20",
"@aeriajs/builtins": "^0.0.20",
"@aeriajs/types": "^0.0.8",
"@aeriajs/validation": "^0.0.12"
}
Expand Down
9 changes: 9 additions & 0 deletions packages/api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @aeriajs/api

## 0.0.20

### Patch Changes

- Updated dependencies
- @aeriajs/http@0.0.14
- @aeriajs/builtins@0.0.20
- @aeriajs/security@0.0.20

## 0.0.19

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeriajs/api",
"version": "0.0.19",
"version": "0.0.20",
"description": "",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -45,9 +45,9 @@
"@aeriajs/access-control": "^0.0.9",
"@aeriajs/common": "^0.0.9",
"@aeriajs/entrypoint": "^0.0.9",
"@aeriajs/http": "^0.0.13",
"@aeriajs/security": "^0.0.19",
"@aeriajs/builtins": "^0.0.19",
"@aeriajs/http": "^0.0.14",
"@aeriajs/security": "^0.0.20",
"@aeriajs/builtins": "^0.0.20",
"@aeriajs/types": "^0.0.8",
"@aeriajs/validation": "^0.0.12",
"jsonwebtoken": "^8.5.1",
Expand Down
6 changes: 6 additions & 0 deletions packages/builtins/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @aeriajs/builtins

## 0.0.20

### Patch Changes

- @aeriajs/api@0.0.20

## 0.0.19

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/builtins/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeriajs/builtins",
"version": "0.0.19",
"version": "0.0.20",
"description": "## Installation",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -51,7 +51,7 @@
"mongodb": "^6.1.0"
},
"peerDependencies": {
"@aeriajs/api": "^0.0.19",
"@aeriajs/api": "^0.0.20",
"@aeriajs/access-control": "^0.0.9",
"@aeriajs/entrypoint": "^0.0.9",
"@aeriajs/common": "^0.0.9",
Expand Down
6 changes: 6 additions & 0 deletions packages/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @aeriajs/http

## 0.0.14

### Patch Changes

- Fix router callback typing

## 0.0.13

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeriajs/http",
"version": "0.0.13",
"version": "0.0.14",
"description": "## Installation",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
51 changes: 31 additions & 20 deletions packages/http/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,20 @@ type TypedContext<TContractWithRoles extends ContractWithRoles> = Omit<Context,
export type ProxiedRouter<TRouter> = TRouter & Record<
RequestMethod,
<
TCallback extends (context: TypedContext<TContractWithRoles>)=> TContractWithRoles extends { response: infer Response }
? InferResponse<Response>
: any,
const TContractWithRoles extends ContractWithRoles,
const TContractWithRoles extends ContractWithRoles | {},
TCallback extends (
TContractWithRoles extends { response: infer Response }
? InferResponse<Response>
: any
) extends infer Response
? 'roles' extends keyof TContractWithRoles
? TContractWithRoles['roles'] extends readonly (infer Role)[]
? 'guest' extends Role
? (context: TypedContext<TContractWithRoles>)=> Response
: (context: TypedContext<TContractWithRoles> & { token: { authenticated: true } })=> Response
: never
: (context: TypedContext<TContractWithRoles>)=> Response
: never,
>(
exp: RouteUri,
cb: TCallback,
Expand Down Expand Up @@ -107,21 +117,12 @@ export const matches = <TRequest extends GenericRequest>(
}
}

export const registerRoute = async <
const TContractWithRoles extends ContractWithRoles,
TCallback extends 'roles' extends keyof TContractWithRoles
? TContractWithRoles['roles'] extends readonly (infer Role)[]
? 'guest' extends Role
? (context: Context)=> any
: (context: Context & { token: { authenticated: true } })=> any
: never
: (context: Context)=> any,
>(
export const registerRoute = async (
context: Context,
method: RequestMethod | RequestMethod[],
exp: RouteUri,
cb: TCallback,
contract?: TContractWithRoles,
cb: (context: Context)=> any,
contract?: ContractWithRoles,
options: RouterOptions = {},
) => {
const match = matches(context.request, method, exp, options)
Expand Down Expand Up @@ -243,10 +244,20 @@ export const createRouter = (options: Partial<RouterOptions> = {}) => {
const routesMeta = {} as RoutesMeta

const route = <
TCallback extends (context: TypedContext<TContractWithRoles>)=> TContractWithRoles extends { response: infer Response }
? InferResponse<Response>
: TContractWithRoles,
const TContractWithRoles extends ContractWithRoles,
const TContractWithRoles extends ContractWithRoles | {},
TCallback extends (
TContractWithRoles extends { response: infer Response }
? InferResponse<Response>
: any
) extends infer Response
? 'roles' extends keyof TContractWithRoles
? TContractWithRoles['roles'] extends readonly (infer Role)[]
? 'guest' extends Role
? (context: TypedContext<TContractWithRoles>)=> Response
: (context: TypedContext<TContractWithRoles> & { token: { authenticated: true } })=> Response
: never
: (context: TypedContext<TContractWithRoles>)=> Response
: never,
>(
method: RequestMethod | RequestMethod[],
exp: RouteUri,
Expand Down
7 changes: 7 additions & 0 deletions packages/node-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @aeriajs/node-http

## 0.0.14

### Patch Changes

- Updated dependencies
- @aeriajs/http@0.0.14

## 0.0.13

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/node-http/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeriajs/node-http",
"version": "0.0.13",
"version": "0.0.14",
"description": "## Installation",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -32,6 +32,6 @@
"dist"
],
"peerDependencies": {
"@aeriajs/http": "^0.0.13"
"@aeriajs/http": "^0.0.14"
}
}
6 changes: 6 additions & 0 deletions packages/security/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @aeriajs/security

## 0.0.20

### Patch Changes

- @aeriajs/api@0.0.20

## 0.0.19

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/security/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeriajs/security",
"version": "0.0.19",
"version": "0.0.20",
"description": "",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -32,7 +32,7 @@
"dist"
],
"peerDependencies": {
"@aeriajs/api": "^0.0.19",
"@aeriajs/api": "^0.0.20",
"@aeriajs/common": "^0.0.9",
"@aeriajs/types": "^0.0.8",
"mongodb": "^6.1.0"
Expand Down
10 changes: 10 additions & 0 deletions packages/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @aeriajs/server

## 0.0.20

### Patch Changes

- Updated dependencies
- @aeriajs/http@0.0.14
- @aeriajs/api@0.0.20
- @aeriajs/server@0.0.20
- @aeriajs/builtins@0.0.20

## 0.0.19

### Patch Changes
Expand Down
12 changes: 6 additions & 6 deletions packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeriajs/server",
"version": "0.0.19",
"version": "0.0.20",
"description": "",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -33,17 +33,17 @@
],
"peerDependencies": {
"@aeriajs/access-control": "^0.0.9",
"@aeriajs/api": "^0.0.19",
"@aeriajs/api": "^0.0.20",
"@aeriajs/common": "^0.0.9",
"@aeriajs/entrypoint": "^0.0.9",
"@aeriajs/http": "^0.0.13",
"@aeriajs/server": "^0.0.19",
"@aeriajs/builtins": "^0.0.19",
"@aeriajs/http": "^0.0.14",
"@aeriajs/server": "^0.0.20",
"@aeriajs/builtins": "^0.0.20",
"@aeriajs/types": "^0.0.8",
"mongodb": "^6.1.0"
},
"optionalDependencies": {
"@aeriajs/node-http": "^0.0.13"
"@aeriajs/node-http": "^0.0.14"
},
"devDependencies": {
"mongodb": "^6.1.0"
Expand Down
Loading

0 comments on commit 9fde2e6

Please sign in to comment.