-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend contracts and roles for collection functions
- Loading branch information
1 parent
a4ccb1d
commit 0987925
Showing
20 changed files
with
247 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Context, Pagination, GetAllPayload } from '@aeriajs/types' | ||
|
||
export const makePagination = async ( | ||
payload: GetAllPayload<any>, | ||
documents: any[], | ||
context: Context | ||
): Promise<Pagination> => { | ||
const limit = payload.limit | ||
? payload.limit | ||
: context.config.paginationLimit! | ||
|
||
const offset = payload.offset || 0 | ||
|
||
const recordsTotal = 'count' in context.collection.functions | ||
? await context.collection.functions.count(payload) | ||
: await context.collection.model.countDocuments({ | ||
filters: payload.filters | ||
}) | ||
|
||
return { | ||
recordsCount: documents.length, | ||
recordsTotal, | ||
offset, | ||
limit, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { RoutesMeta, RouteUri } from '@aeriajs/http' | ||
import { getCollections, getRouter } from '@aeriajs/entrypoint' | ||
import { grantedFor } from '@aeriajs/access-control' | ||
import { deepMerge } from '@aeriajs/common' | ||
|
||
export const getEndpoints = async (): Promise<RoutesMeta> => { | ||
const router = await getRouter() | ||
const collections = await getCollections() | ||
|
||
const functions: RoutesMeta = {} | ||
|
||
for( const collectionName in collections ) { | ||
const candidate = collections[collectionName] | ||
const collection = typeof candidate === 'function' | ||
? candidate() | ||
: candidate | ||
|
||
const { | ||
description, | ||
functions: collectionFunctions, | ||
functionContracts | ||
} = collection | ||
|
||
if( collectionFunctions ) { | ||
for( const fnName in collectionFunctions ) { | ||
const endpoint = `/${description.$id}/${fnName}` | ||
const roles = await grantedFor(description.$id, fnName) | ||
|
||
const contract = functionContracts && fnName in functionContracts | ||
? roles | ||
? Object.assign({ roles }, functionContracts[fnName]) | ||
: functionContracts[fnName] | ||
: roles | ||
? { roles } | ||
: null | ||
|
||
functions[endpoint as RouteUri] = { | ||
POST: contract | ||
} | ||
} | ||
} | ||
} | ||
|
||
const result = deepMerge( | ||
functions, | ||
router.routesMeta | ||
) | ||
|
||
return result | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Collection } from '../src/types' | ||
|
||
declare global { | ||
type Collections = Record<string, Collection> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
import type { Property } from '@aeriajs/types' | ||
import type { ContractWithRoles } from '@aeriajs/types' | ||
|
||
export type Contract = | ||
| { response: Property | Property[] } | ||
| { payload: Property } | ||
| { query: Property } | ||
| { | ||
response?: Property | Property[] | ||
payload?: Property | ||
query?: Property | ||
} | ||
|
||
export const defineContract = <const TContract extends Contract>(contract: TContract) => { | ||
export const defineContract = <const TContractWithRoles extends ContractWithRoles>(contract: TContractWithRoles) => { | ||
return contract | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.