diff --git a/API Tests/BGApp/Auth/Login.yml b/API Tests/BGApp/Auth/Login.yml index f52223f..7382301 100644 --- a/API Tests/BGApp/Auth/Login.yml +++ b/API Tests/BGApp/Auth/Login.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/auth/login" + url: "{{BASE_URL}}/{{SECTOR}}/login" headers: - name: Content-Type value: application/json diff --git a/API Tests/BGApp/Auth/Token.yml b/API Tests/BGApp/Auth/Token.yml index 71983e9..f81a87b 100644 --- a/API Tests/BGApp/Auth/Token.yml +++ b/API Tests/BGApp/Auth/Token.yml @@ -5,7 +5,7 @@ info: http: method: GET - url: "{{BASE_URL}}/api/auth/token" + url: "{{BASE_URL}}/{{SECTOR}}/token" headers: - name: Cookie value: "{{REFRESH_COOKIE}}" diff --git a/API Tests/BGApp/Auth/folder.yml b/API Tests/BGApp/Auth/folder.yml index 6b7dae0..67ae3fa 100644 --- a/API Tests/BGApp/Auth/folder.yml +++ b/API Tests/BGApp/Auth/folder.yml @@ -2,3 +2,8 @@ info: name: Auth type: folder seq: 1 + +request: + variables: + - name: SECTOR + value: auth diff --git a/API Tests/BGApp/Collections/Add game.yml b/API Tests/BGApp/Collections/Add game.yml index d8586a2..9a8c605 100644 --- a/API Tests/BGApp/Collections/Add game.yml +++ b/API Tests/BGApp/Collections/Add game.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/collection/{{CollectionID}}/add" + url: "{{BASE_URL}}/{{SECTOR}}/{{CollectionID}}/add" body: type: json data: |- diff --git a/API Tests/BGApp/Collections/Create.yml b/API Tests/BGApp/Collections/Create.yml index 5ce5b0e..1a7d299 100644 --- a/API Tests/BGApp/Collections/Create.yml +++ b/API Tests/BGApp/Collections/Create.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/collection" + url: "{{BASE_URL}}/{{SECTOR}}" body: type: json data: |- diff --git a/API Tests/BGApp/Collections/Delete.yml b/API Tests/BGApp/Collections/Delete.yml index 22745b5..b98e2f1 100644 --- a/API Tests/BGApp/Collections/Delete.yml +++ b/API Tests/BGApp/Collections/Delete.yml @@ -5,7 +5,7 @@ info: http: method: DELETE - url: "{{BASE_URL}}/api/collection/{{CollectionID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{CollectionID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/Collections/Get.yml b/API Tests/BGApp/Collections/Get.yml index 4065842..9cc3802 100644 --- a/API Tests/BGApp/Collections/Get.yml +++ b/API Tests/BGApp/Collections/Get.yml @@ -5,11 +5,7 @@ info: http: method: GET - url: "{{BASE_URL}}/api/collection/{{CollectionID}}" - params: - - name: "" - value: "" - type: query + url: "{{BASE_URL}}/{{SECTOR}}/{{CollectionID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/Collections/List.yml b/API Tests/BGApp/Collections/List.yml index 24acd0a..e421ec5 100644 --- a/API Tests/BGApp/Collections/List.yml +++ b/API Tests/BGApp/Collections/List.yml @@ -5,15 +5,15 @@ info: http: method: GET - url: "{{BASE_URL}}/api/collection/list/{{PageSize}}/{{Page}}" + url: "{{BASE_URL}}/{{SECTOR}}/list/{{PageSize}}/{{Page}}" auth: inherit runtime: variables: - name: PageSize - value: "1" + value: "5" - name: Page - value: "0" + value: "1" settings: encodeUrl: true diff --git a/API Tests/BGApp/Collections/Remove game.yml b/API Tests/BGApp/Collections/Remove game.yml index f29a246..b5bde0e 100644 --- a/API Tests/BGApp/Collections/Remove game.yml +++ b/API Tests/BGApp/Collections/Remove game.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/collection/{{CollectionID}}/remove" + url: "{{BASE_URL}}/{{SECTOR}}/{{CollectionID}}/remove" body: type: json data: |- diff --git a/API Tests/BGApp/Collections/Update.yml b/API Tests/BGApp/Collections/Update.yml index dc2ba1e..810c669 100644 --- a/API Tests/BGApp/Collections/Update.yml +++ b/API Tests/BGApp/Collections/Update.yml @@ -5,7 +5,7 @@ info: http: method: PATCH - url: "{{BASE_URL}}/api/collection/{{CollectionID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{CollectionID}}" body: type: json data: |- diff --git a/API Tests/BGApp/Collections/folder.yml b/API Tests/BGApp/Collections/folder.yml index de23efa..a0322eb 100644 --- a/API Tests/BGApp/Collections/folder.yml +++ b/API Tests/BGApp/Collections/folder.yml @@ -1,7 +1,10 @@ info: name: Collections type: folder - seq: 6 + seq: 1 request: auth: inherit + variables: + - name: SECTOR + value: collections diff --git a/API Tests/BGApp/Game/Create.yml b/API Tests/BGApp/Game/Create.yml index a5dd099..348d2ba 100644 --- a/API Tests/BGApp/Game/Create.yml +++ b/API Tests/BGApp/Game/Create.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/game" + url: "{{BASE_URL}}/{{SECTOR}}" body: type: json data: |- diff --git a/API Tests/BGApp/Game/Delete.yml b/API Tests/BGApp/Game/Delete.yml index a6333bc..9ef3def 100644 --- a/API Tests/BGApp/Game/Delete.yml +++ b/API Tests/BGApp/Game/Delete.yml @@ -5,7 +5,7 @@ info: http: method: DELETE - url: "{{BASE_URL}}/api/game/{{GameID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{GameID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/Game/Get.yml b/API Tests/BGApp/Game/Get.yml index 31fd3fb..be73af6 100644 --- a/API Tests/BGApp/Game/Get.yml +++ b/API Tests/BGApp/Game/Get.yml @@ -5,7 +5,7 @@ info: http: method: GET - url: "{{BASE_URL}}/api/game/{{GameID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{GameID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/Game/Search.yml b/API Tests/BGApp/Game/Search.yml index 8a6ac87..2bf06d0 100644 --- a/API Tests/BGApp/Game/Search.yml +++ b/API Tests/BGApp/Game/Search.yml @@ -5,7 +5,7 @@ info: http: method: GET - url: "{{BASE_URL}}/api/game/search/{{Query}}/{{PageSize}}/{{Page}}" + url: "{{BASE_URL}}/{{SECTOR}}/search/{{Query}}/{{PageSize}}/{{Page}}" auth: inherit runtime: @@ -13,9 +13,9 @@ runtime: - name: Query value: test - name: PageSize - value: "2" + value: "5" - name: Page - value: "2" + value: "1" settings: encodeUrl: true diff --git a/API Tests/BGApp/Game/Update.yml b/API Tests/BGApp/Game/Update.yml index 00ae16b..2f0d62f 100644 --- a/API Tests/BGApp/Game/Update.yml +++ b/API Tests/BGApp/Game/Update.yml @@ -5,7 +5,7 @@ info: http: method: PATCH - url: "{{BASE_URL}}/api/game/{{GameID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{GameID}}" body: type: json data: |- diff --git a/API Tests/BGApp/Game/folder.yml b/API Tests/BGApp/Game/folder.yml index e2873d8..361c885 100644 --- a/API Tests/BGApp/Game/folder.yml +++ b/API Tests/BGApp/Game/folder.yml @@ -1,7 +1,10 @@ info: name: Game type: folder - seq: 4 + seq: 1 request: auth: inherit + variables: + - name: SECTOR + value: games diff --git a/API Tests/BGApp/Invites/Accept.yml b/API Tests/BGApp/Invites/Accept.yml index 512d8c8..4d74aea 100644 --- a/API Tests/BGApp/Invites/Accept.yml +++ b/API Tests/BGApp/Invites/Accept.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/invite/accept" + url: "{{BASE_URL}}/{{SECTOR}}/accept" body: type: json data: |- diff --git a/API Tests/BGApp/Invites/Create.yml b/API Tests/BGApp/Invites/Create.yml index d000b29..a8f277a 100644 --- a/API Tests/BGApp/Invites/Create.yml +++ b/API Tests/BGApp/Invites/Create.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/invite" + url: "{{BASE_URL}}/{{SECTOR}}" body: type: json data: |- diff --git a/API Tests/BGApp/Invites/folder.yml b/API Tests/BGApp/Invites/folder.yml index 80b4290..f90a502 100644 --- a/API Tests/BGApp/Invites/folder.yml +++ b/API Tests/BGApp/Invites/folder.yml @@ -5,3 +5,6 @@ info: request: auth: inherit + variables: + - name: SECTOR + value: invites diff --git a/API Tests/BGApp/Players/Create.yml b/API Tests/BGApp/Players/Create.yml index 8b62b00..81f235b 100644 --- a/API Tests/BGApp/Players/Create.yml +++ b/API Tests/BGApp/Players/Create.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/player" + url: "{{BASE_URL}}/{{SECTOR}}" body: type: json data: |- diff --git a/API Tests/BGApp/Players/Delete.yml b/API Tests/BGApp/Players/Delete.yml index adf2df8..abb8ede 100644 --- a/API Tests/BGApp/Players/Delete.yml +++ b/API Tests/BGApp/Players/Delete.yml @@ -5,7 +5,7 @@ info: http: method: DELETE - url: "{{BASE_URL}}/api/player/{{PlayerID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{PlayerID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/Players/Get.yml b/API Tests/BGApp/Players/Get.yml index 2d4e90d..4178f04 100644 --- a/API Tests/BGApp/Players/Get.yml +++ b/API Tests/BGApp/Players/Get.yml @@ -5,7 +5,7 @@ info: http: method: GET - url: "{{BASE_URL}}/api/player/{{PlayerID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{PlayerID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/Players/List.yml b/API Tests/BGApp/Players/List.yml index 9cc53f8..73b2418 100644 --- a/API Tests/BGApp/Players/List.yml +++ b/API Tests/BGApp/Players/List.yml @@ -5,9 +5,16 @@ info: http: method: GET - url: "{{BASE_URL}}/api/player/list" + url: "{{BASE_URL}}/{{SECTOR}}/list/{{PageSize}}/{{Page}}" auth: inherit +runtime: + variables: + - name: PageSize + value: "100" + - name: Page + value: "1" + settings: encodeUrl: true timeout: 0 diff --git a/API Tests/BGApp/Players/Update.yml b/API Tests/BGApp/Players/Update.yml index 5ce7747..a9d9456 100644 --- a/API Tests/BGApp/Players/Update.yml +++ b/API Tests/BGApp/Players/Update.yml @@ -5,7 +5,7 @@ info: http: method: PATCH - url: "{{BASE_URL}}/api/player/{{PlayerID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{PlayerID}}" body: type: json data: |- diff --git a/API Tests/BGApp/Players/folder.yml b/API Tests/BGApp/Players/folder.yml index e1a66bb..5261570 100644 --- a/API Tests/BGApp/Players/folder.yml +++ b/API Tests/BGApp/Players/folder.yml @@ -1,7 +1,10 @@ info: name: Players type: folder - seq: 2 + seq: 1 request: auth: inherit + variables: + - name: SECTOR + value: players diff --git a/API Tests/BGApp/User/Create.yml b/API Tests/BGApp/User/Create.yml index 83fa399..05d6492 100644 --- a/API Tests/BGApp/User/Create.yml +++ b/API Tests/BGApp/User/Create.yml @@ -5,7 +5,7 @@ info: http: method: POST - url: "{{BASE_URL}}/api/user" + url: "{{BASE_URL}}/{{SECTOR}}" body: type: json data: |- diff --git a/API Tests/BGApp/User/Delete.yml b/API Tests/BGApp/User/Delete.yml index 5ed3239..dfe5249 100644 --- a/API Tests/BGApp/User/Delete.yml +++ b/API Tests/BGApp/User/Delete.yml @@ -5,7 +5,7 @@ info: http: method: DELETE - url: "{{BASE_URL}}/api/user/{{UserID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{UserID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/User/Get.yml b/API Tests/BGApp/User/Get.yml index 770c2ba..8d6d91e 100644 --- a/API Tests/BGApp/User/Get.yml +++ b/API Tests/BGApp/User/Get.yml @@ -5,7 +5,7 @@ info: http: method: GET - url: "{{BASE_URL}}/api/user/{{UserID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{UserID}}" auth: inherit runtime: diff --git a/API Tests/BGApp/User/Update.yml b/API Tests/BGApp/User/Update.yml index 0b4a30e..75952de 100644 --- a/API Tests/BGApp/User/Update.yml +++ b/API Tests/BGApp/User/Update.yml @@ -5,7 +5,7 @@ info: http: method: PATCH - url: "{{BASE_URL}}/api/user/{{UserID}}" + url: "{{BASE_URL}}/{{SECTOR}}/{{UserID}}" body: type: json data: |- diff --git a/API Tests/BGApp/User/folder.yml b/API Tests/BGApp/User/folder.yml index eb4e6b0..66aed83 100644 --- a/API Tests/BGApp/User/folder.yml +++ b/API Tests/BGApp/User/folder.yml @@ -1,7 +1,10 @@ info: name: User type: folder - seq: 3 + seq: 1 request: auth: inherit + variables: + - name: SECTOR + value: users diff --git a/API Tests/BGApp/environments/BGApp.yml b/API Tests/BGApp/environments/BGApp.yml index 770c2f0..4865ed7 100644 --- a/API Tests/BGApp/environments/BGApp.yml +++ b/API Tests/BGApp/environments/BGApp.yml @@ -5,4 +5,4 @@ variables: - name: REFRESH_COOKIE value: "" - name: BASE_URL - value: http://localhost:3000 + value: http://localhost:3000/api diff --git a/src/endpoints/auth.ts b/src/endpoints/auth.ts index 49dab6e..be91267 100644 --- a/src/endpoints/auth.ts +++ b/src/endpoints/auth.ts @@ -23,7 +23,7 @@ async function login(request: UnwrappedRequest): Promise u: verify.userId.raw, r: verify.refreshCount, }, - process.env.JWT_SECRET_KEY as string, + process.env.JWT_REFRESH_KEY as string, { expiresIn: `${tokenLifeSpanInDays * 24}h` }, ); const cookies = request?.request?.cookies; @@ -52,7 +52,7 @@ async function token(request: UnwrappedRequest): Promise { const refreshToken: { u: string; r: string; - } = jwt.verify(refreshCookie, process.env.JWT_SECRET_KEY as string) as { u: string; r: string }; + } = jwt.verify(refreshCookie, process.env.JWT_REFRESH_KEY as string) as { u: string; r: string }; if (!(await orm.users.verifyRefreshCount(UserId.fromID(refreshToken.u), refreshToken.r))) { const response = new UnauthorizedResponse('Invalid refresh token'); diff --git a/src/index.ts b/src/index.ts index 1733b1a..b9d5277 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,21 +5,24 @@ import players from './routes/players'; import games from './routes/games'; import invites from './routes/invites'; import collections from './routes/collections'; +import { buildRoute } from './utilities/routeBuilder'; const server = Bun.serve({ - routes: { - ...auth, - ...users, - ...players, - ...games, - ...invites, - ...collections, - '/test': { + routes: buildRoute({ + [process.env.API_ROOT_PATH ?? '']:{ + auth, + users, + players, + games, + invites, + collections, + }, + 'test': { GET: () => { return new OkResponse(); }, }, - }, + }) as any, // (optional) fallback for unmatched routes: fetch(): Response { diff --git a/src/orm/claims.ts b/src/orm/claims.ts index cb788d9..3f18bb1 100644 --- a/src/orm/claims.ts +++ b/src/orm/claims.ts @@ -1,6 +1,6 @@ import { sql } from 'bun'; import { ClaimDefinition } from '../utilities/claimDefinitions'; -import { SecureId, UserId } from '../utilities/secureIds'; +import { UserId } from '../utilities/secureIds'; export class Claims extends ClaimDefinition { userId?: UserId; diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 78302ba..87510e5 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -4,19 +4,18 @@ import { OkResponse } from '../utilities/responseHelper'; import { Claims } from '../orm/claims'; export default { - '/api/auth/login': { + login: { POST: unwrapMethod(auth.login), }, - '/api/auth/token': { + token: { GET: unwrapMethod(auth.token), }, - '/api/auth/logout': { + logout: { POST: unwrapMethod(auth.logout), }, - '/api/auth/changePassword/:id': { - PATCH: guard(auth.changePassword, [Claims.ADMIN, Claims.USERS.SELF.UPDATE]), - }, - '/api/auth/test': { - GET: () => new OkResponse(), + changePassword: { + ':id': { + PATCH: guard(auth.changePassword, [Claims.ADMIN, Claims.USERS.SELF.UPDATE]), + }, }, }; diff --git a/src/routes/collections.ts b/src/routes/collections.ts index ef7c08e..443956f 100644 --- a/src/routes/collections.ts +++ b/src/routes/collections.ts @@ -3,29 +3,28 @@ import { Claims } from '../orm/claims'; import collections from '../endpoints/collections'; export default { - '/api/collection': { - POST: guard(collections.create, [Claims.ADMIN, Claims.COLLECTIONS.CREATE]), - }, - '/api/collection/:id': { + 'POST': guard(collections.create, [Claims.ADMIN, Claims.COLLECTIONS.CREATE]), + ':id': { GET: guard(collections.get, [Claims.ADMIN, Claims.COLLECTIONS.UNOWNED.READ, Claims.COLLECTIONS.OWNED.READ]), - // PATCH: guard(collections.update, [Claims.ADMIN, Claims.PLAYERS.OTHER.UPDATE, Claims.PLAYERS.SELF.UPDATE]), - // DELETE: guard(collections.drop, [Claims.ADMIN, Claims.PLAYERS.OTHER.DELETE, Claims.PLAYERS.SELF.DELETE]), + PATCH: guard(collections.update, [Claims.ADMIN, Claims.PLAYERS.OTHER.UPDATE, Claims.PLAYERS.SELF.UPDATE]), + DELETE: guard(collections.drop, [Claims.ADMIN, Claims.PLAYERS.OTHER.DELETE, Claims.PLAYERS.SELF.DELETE]), + add: { + POST: guard(collections.addGame, [ + Claims.ADMIN, + Claims.COLLECTIONS.UNOWNED.GAME.ADD, + Claims.COLLECTIONS.OWNED.GAME.ADD, + ]), + }, + remove: { + POST: guard(collections.removeGame, [ + Claims.ADMIN, + Claims.COLLECTIONS.UNOWNED.GAME.REMOVE, + Claims.COLLECTIONS.OWNED.GAME.REMOVE, + ]), + }, }, - '/api/collection/:id/add': { - POST: guard(collections.addGame, [ - Claims.ADMIN, - Claims.COLLECTIONS.UNOWNED.GAME.ADD, - Claims.COLLECTIONS.OWNED.GAME.ADD, - ]), - }, - '/api/collection/:id/remove': { - POST: guard(collections.removeGame, [ - Claims.ADMIN, - Claims.COLLECTIONS.UNOWNED.GAME.REMOVE, - Claims.COLLECTIONS.OWNED.GAME.REMOVE, - ]), - }, - '/api/collection/list/:pageSize/:page': { + 'list': { + variants: [':pageSize/:page', ':page'], GET: guard(collections.list, [Claims.ADMIN, Claims.COLLECTIONS.OWNED.LIST]), }, }; diff --git a/src/routes/games.ts b/src/routes/games.ts index c3b3092..cddcf3f 100644 --- a/src/routes/games.ts +++ b/src/routes/games.ts @@ -3,15 +3,16 @@ import { Claims } from '../orm/claims'; import games from '../endpoints/games'; export default { - '/api/game': { - POST: guard(games.create, [Claims.ADMIN, Claims.GAMES.CREATE]), - }, - '/api/game/:id': { + 'POST': guard(games.create, [Claims.ADMIN, Claims.GAMES.CREATE]), + ':id': { GET: guard(games.get, [Claims.ADMIN, Claims.GAMES.READ]), PATCH: guard(games.update, [Claims.ADMIN, Claims.GAMES.UPDATE]), DELETE: guard(games.drop, [Claims.ADMIN, Claims.GAMES.DELETE]), }, - '/api/game/search/:query/:pageSize/:page': { - GET: guard(games.query, [Claims.ADMIN, Claims.GAMES.READ]), + 'search': { + ':query': { + variants: [':pageSize/:page', ':page'], + GET: guard(games.query, [Claims.ADMIN, Claims.GAMES.READ]), + }, }, }; diff --git a/src/routes/invites.ts b/src/routes/invites.ts index e9f17ec..24c6025 100644 --- a/src/routes/invites.ts +++ b/src/routes/invites.ts @@ -3,10 +3,8 @@ import { Claims } from '../orm/claims'; import invite from '../endpoints/invites'; export default { - '/api/invite': { - POST: guard(invite.create, [Claims.ADMIN, Claims.USERS.INVITE]), - }, - '/api/invite/accept': { + POST: guard(invite.create, [Claims.ADMIN, Claims.USERS.INVITE]), + accept: { POST: unwrapMethod(invite.accept), }, }; diff --git a/src/routes/players.ts b/src/routes/players.ts index 3fd3472..6e55c2d 100644 --- a/src/routes/players.ts +++ b/src/routes/players.ts @@ -3,15 +3,14 @@ import { Claims } from '../orm/claims'; import player from '../endpoints/players'; export default { - '/api/player': { - POST: guard(player.create, [Claims.ADMIN, Claims.PLAYERS.CREATE]), - }, - '/api/player/:id': { + 'POST': guard(player.create, [Claims.ADMIN, Claims.PLAYERS.CREATE]), + ':id': { GET: guard(player.get, [Claims.ADMIN, Claims.PLAYERS.OTHER.READ, Claims.PLAYERS.SELF.READ]), PATCH: guard(player.update, [Claims.ADMIN, Claims.PLAYERS.OTHER.UPDATE, Claims.PLAYERS.SELF.UPDATE]), DELETE: guard(player.drop, [Claims.ADMIN, Claims.PLAYERS.OTHER.DELETE, Claims.PLAYERS.SELF.DELETE]), }, - '/api/player/list/:pageSize/:page': { + 'list': { + variants: [':pageSize/:page', ':page'], GET: guard(player.list, [Claims.ADMIN, Claims.PLAYERS.OTHER.READ]), }, }; diff --git a/src/routes/users.ts b/src/routes/users.ts index 55325a9..d05e732 100644 --- a/src/routes/users.ts +++ b/src/routes/users.ts @@ -3,10 +3,8 @@ import user from '../endpoints/users'; import { Claims } from '../orm/claims'; export default { - '/api/user': { - POST: guard(user.create, [Claims.ADMIN, Claims.USERS.CREATE]), - }, - '/api/user/:id': { + 'POST': guard(user.create, [Claims.ADMIN, Claims.USERS.CREATE]), + ':id': { GET: guard(user.get, [Claims.ADMIN, Claims.USERS.OTHER.READ, Claims.USERS.SELF.READ]), PATCH: guard(user.update, [Claims.ADMIN, Claims.USERS.OTHER.UPDATE, Claims.USERS.SELF.UPDATE]), DELETE: guard(user.drop, [Claims.ADMIN, Claims.USERS.OTHER.UPDATE, Claims.USERS.SELF.UPDATE]), diff --git a/src/utilities/routeBuilder.ts b/src/utilities/routeBuilder.ts new file mode 100644 index 0000000..fb994dd --- /dev/null +++ b/src/utilities/routeBuilder.ts @@ -0,0 +1,41 @@ +export function buildRoute( + route: any, + currentPath: string = '', +): { + [x: string]: { + POST?: Function; + GET?: Function; + PUT?: Function; + DELETE?: Function; + }; +} { + let returnValue: { [x: string]: any } = {}; + + const keys = Object.keys(route); + for (let i in keys) { + const key = keys[i]; + if (key === 'POST' || key === 'GET' || key === 'PUT' || key === 'DELETE' || key === 'variants') { + continue; + } + returnValue = { + ...returnValue, + ...buildRoute(route[key], `${currentPath}/${key}`), + }; + } + + if (route.variants || route.POST || route.GET || route.PUT || route.DELETE) { + const variants: string[] = route.variants ?? []; + const endpointDefinition = { + POST: route.POST, + GET: route.GET, + PUT: route.PUT, + DELETE: route.DELETE, + }; + returnValue[currentPath] = endpointDefinition; + for (let key in variants) { + returnValue[`${currentPath}/${variants[key]}`] = endpointDefinition; + } + } + + return returnValue; +}