permission-node: simplify api report

Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
Vincenzo Scamporlino
2023-02-27 11:30:05 +01:00
parent 915e46622c
commit e837143bc9
3 changed files with 26 additions and 29 deletions
+3 -6
View File
@@ -137,7 +137,9 @@ export type CreatePermissionIntegrationRouterResourceOptions<
resourceType: TResourceType;
permissions?: Array<Permission>;
rules: PermissionRule<TResource, any, NoInfer<TResourceType>>[];
getResources?: GetResourcesFn<TResource>;
getResources?: (
resourceRefs: string[],
) => Promise<Array<TResource | undefined>>;
};
// @public
@@ -150,11 +152,6 @@ export const createPermissionRule: <
rule: PermissionRule<TResource, TQuery, TResourceType, TParams>,
) => PermissionRule<TResource, TQuery, TResourceType, TParams>;
// @public
export type GetResourcesFn<TResource> = (
resourceRefs: string[],
) => Promise<Array<TResource | undefined>>;
// @alpha
export const isAndCriteria: <T>(
criteria: PermissionCriteria<T>,
@@ -24,7 +24,7 @@ import request, { Response } from 'supertest';
import { z } from 'zod';
import {
createPermissionIntegrationRouter,
GetResourcesFn,
CreatePermissionIntegrationRouterResourceOptions,
} from './createPermissionIntegrationRouter';
import { createPermissionRule } from './createPermissionRule';
@@ -53,8 +53,11 @@ const testRule2 = createPermissionRule({
toQuery: () => ({}),
});
const defaultMockedGetResources: GetResourcesFn<{ id: string }> = jest.fn(
async resourceRefs => resourceRefs.map(resourceRef => ({ id: resourceRef })),
const defaultMockedGetResources: CreatePermissionIntegrationRouterResourceOptions<
string,
{ id: string }
>['getResources'] = jest.fn(async resourceRefs =>
resourceRefs.map(resourceRef => ({ id: resourceRef })),
);
const createApp = (
@@ -415,8 +418,11 @@ describe('createPermissionIntegrationRouter', () => {
});
it('returns 200/DENY when resource is not found', async () => {
const mockedGetResources: GetResourcesFn<{ id: string }> = jest.fn(
async resourceRefs => resourceRefs.map(() => undefined),
const mockedGetResources: CreatePermissionIntegrationRouterResourceOptions<
string,
{ id: string }
>['getResources'] = jest.fn(async resourceRefs =>
resourceRefs.map(() => undefined),
);
const response = await request(createApp(mockedGetResources))
@@ -448,13 +454,15 @@ describe('createPermissionIntegrationRouter', () => {
});
it('interleaves responses for present and missing resources', async () => {
const mockedGetResources: GetResourcesFn<{ id: string }> = jest.fn(
async resourceRefs =>
resourceRefs.map(resourceRef =>
resourceRef === 'default:test/missing-resource'
? undefined
: { id: resourceRef },
),
const mockedGetResources: CreatePermissionIntegrationRouterResourceOptions<
string,
{ id: string }
>['getResources'] = jest.fn(async resourceRefs =>
resourceRefs.map(resourceRef =>
resourceRef === 'default:test/missing-resource'
? undefined
: { id: resourceRef },
),
);
const response = await request(createApp(mockedGetResources))
@@ -126,16 +126,6 @@ export type MetadataResponse = {
rules: MetadataResponseSerializedRule[];
};
/**
* Function type for returning an array of resources
* matching the given resourceRefs.
*
* @public
*/
export type GetResourcesFn<TResource> = (
resourceRefs: string[],
) => Promise<Array<TResource | undefined>>;
const applyConditions = <TResourceType extends string, TResource>(
criteria: PermissionCriteria<PermissionCondition<TResourceType>>,
resource: TResource | undefined,
@@ -187,7 +177,9 @@ export type CreatePermissionIntegrationRouterResourceOptions<
// consider any rules whose resource type does not match
// to be an error.
rules: PermissionRule<TResource, any, NoInfer<TResourceType>>[];
getResources?: GetResourcesFn<TResource>;
getResources?: (
resourceRefs: string[],
) => Promise<Array<TResource | undefined>>;
};
/**