Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 1x 1x 12x 12x 12x 12x 12x | import {
ContractType,
ManageableFields,
AuthInput,
AuthenticationDefinition,
Implementation,
HandleResult
} from './globalTypes.js'
export const authorizedByPermission = (
auth: AuthenticationDefinition,
authInput: AuthInput
) =>
typeof auth === 'boolean' ||
auth.some(x => (authInput.permissions || []).some(y => x === y))
export const getUserIdFields = (fields: ManageableFields): string[] =>
Object.entries(fields).filter(x => x[1]).map(x => x[0])
export const filterToAccess = (
input: any[],
auth: AuthenticationDefinition,
authInput: AuthInput,
fields: ManageableFields
): any[] =>
authorizedByPermission(auth, authInput) ? input : input.filter((x: any) =>
getUserIdFields(fields).some(y => x[y] === authInput.sub)
)
export const keyId = (index: string, id: string): string =>
`${index}:records:${id}`
export type Get<IMPL extends Implementation> = <IN, OUT>(
contract: ContractType<'GET', IMPL, IN, OUT>,
auth: AuthInput,
id: string | string[] | undefined,
body?: IN
) => Promise<HandleResult<OUT>>
export type Post<IMPL extends Implementation> = <IN, OUT>(
contract: ContractType<'POST', IMPL, IN, OUT>,
auth: AuthInput,
id: string | undefined,
body: IN
) => Promise<HandleResult<OUT>>
export type Delete<IMPL extends Implementation> = <IN, OUT>(
contract: ContractType<'DELETE', IMPL, IN, OUT>,
auth: AuthInput,
id: string | string[]
) => Promise<HandleResult<OUT>>
export type Patch<IMPL extends Implementation> = <IN, OUT>(
contract: ContractType<'PATCH', IMPL, IN, OUT>,
auth: AuthInput,
id: string,
body: IN
) => Promise<HandleResult<OUT>>
export type Put<IMPL extends Implementation> = <IN, OUT>(
contract: ContractType<'PUT', IMPL, IN, OUT>,
auth: AuthInput,
id: string,
body: IN
) => Promise<HandleResult<OUT>>
export type AbstractBackend<K extends Implementation> = {
get: Get<K>
post: Post<K>
delete: Delete<K>
patch: Patch<K>
put: Put<K>
}
export const forbidden = (data: any, errors: string[] = ['forbidden']) => ({
errorType: 'forbidden',
data,
status: 403,
errors
})
export const notFound = (data: any, errors: string[] = ['not found']) => ({
errorType: 'notFound',
data,
status: 404,
errors
})
|