Update API reports

Signed-off-by: Harry Hogg <hhogg@spotify.com>
This commit is contained in:
Harry Hogg
2022-10-04 12:50:08 +01:00
parent 5a8a8010ee
commit 26e5513c32
8 changed files with 130 additions and 50 deletions
@@ -28,8 +28,9 @@ import {
PermissionCondition,
PermissionCriteria,
} from '@backstage/plugin-permission-common';
import { NoInfer, PermissionRule } from '../types';
import { PermissionRule } from '../types';
import {
NoInfer,
createGetRule,
isAndCriteria,
isNotCriteria,
@@ -22,6 +22,14 @@ import {
} from '@backstage/plugin-permission-common';
import { PermissionRule } from '../types';
/**
* Prevent use of type parameter from contributing to type inference.
*
* https://github.com/Microsoft/TypeScript/issues/14829#issuecomment-980401795
* @ignore
*/
export type NoInfer<T> = T extends infer S ? S : never;
/**
* Utility function used to parse a PermissionCriteria
* @param criteria - a PermissionCriteria
+21 -16
View File
@@ -19,14 +19,23 @@ import type {
PermissionRuleParams,
} from '@backstage/plugin-permission-common';
import { z } from 'zod';
import { NoInfer } from './integration/util';
/**
* Prevent use of type parameter from contributing to type inference.
* A ZodSchema that reflects the structure of the parameters that are passed to
* into a {@link PermissionRule}.
*
* https://github.com/Microsoft/TypeScript/issues/14829#issuecomment-980401795
* @ignore
* @public
*/
export type NoInfer<T> = T extends infer S ? S : never;
export type PermissionRuleSchema<TParams> = z.ZodObject<{
// Parameters can be optional, however we we want to make sure that the
// parameters are always present in the schema, even if they are undefined.
// We remove the optional flag from the schema, and then add it back in
// with an optional zod type.
[P in keyof TParams]-?: TParams[P] extends undefined
? z.ZodOptionalType<z.ZodType<TParams[P]>>
: z.ZodType<TParams[P]>;
}>;
/**
* A conditional rule that can be provided in an
@@ -49,15 +58,6 @@ export type PermissionRule<
TQuery,
TResourceType extends string,
TParams extends PermissionRuleParams = PermissionRuleParams,
TSchema extends z.ZodType = z.ZodObject<{
// Parameters can be optional, however we we want to make sure that the
// parameters are always present in the schema, even if they are undefined.
// We remove the optional flag from the schema, and then add it back in
// with an optional zod type.
[P in keyof TParams]-?: TParams[P] extends undefined
? z.ZodOptionalType<z.ZodType<TParams[P]>>
: z.ZodType<TParams[P]>;
}>,
> = {
name: string;
description: string;
@@ -66,19 +66,24 @@ export type PermissionRule<
/**
* A ZodSchema that documents the parameters that this rule accepts.
*/
schema: TSchema;
schema: PermissionRuleSchema<TParams>;
/**
* Apply this rule to a resource already loaded from a backing data source. The params are
* arguments supplied for the rule; for example, a rule could be `isOwner` with entityRefs as the
* params.
*/
apply(resource: TResource, params: NoInfer<z.input<TSchema>>): boolean;
apply(
resource: TResource,
params: NoInfer<z.input<PermissionRuleSchema<TParams>>>,
): boolean;
/**
* Translate this rule to criteria suitable for use in querying a backing data store. The criteria
* can be used for loading a collection of resources efficiently with conditional criteria already
* applied.
*/
toQuery(params: NoInfer<z.input<TSchema>>): PermissionCriteria<TQuery>;
toQuery(
params: NoInfer<z.input<PermissionRuleSchema<TParams>>>,
): PermissionCriteria<TQuery>;
};