@ucanto/interface
Advanced tools
Comparing version 4.2.3 to 4.3.4
import { Ability, Capability, DID, Link, Resource } from '@ipld/dag-ucan'; | ||
import * as UCAN from '@ipld/dag-ucan'; | ||
import { Delegation, Result, Failure, PrincipalParser, PrincipalResolver, Signer, URI, UCANLink, Await, IssuedInvocationView, UCANOptions, Verifier } from './lib.js'; | ||
import { Delegation, Result, Failure, PrincipalParser, PrincipalResolver, URI, UCANLink, Await, IssuedInvocationView, UCANOptions, Verifier } from './lib.js'; | ||
export interface Source { | ||
capability: Capability; | ||
capability: { | ||
can: Ability; | ||
with: URI; | ||
nb?: Caveats; | ||
}; | ||
delegation: Delegation; | ||
index: number; | ||
} | ||
@@ -40,3 +43,3 @@ export interface Match<T = unknown, M extends Match = UnknownMatch> extends Selector<M> { | ||
export interface Caveats { | ||
[key: string]: Reader<any, unknown>; | ||
[key: string]: unknown; | ||
} | ||
@@ -50,12 +53,8 @@ export type MatchResult<M extends Match> = Result<M, InvalidCapability>; | ||
} | ||
export interface DeriveSelector<M extends Match, T extends ParsedCapability> { | ||
to: TheCapabilityParser<DirectMatch<T>>; | ||
derives: Derives<ToDeriveClaim<T>, ToDeriveProof<M['value']>>; | ||
} | ||
/** | ||
* Utility type is used to infer the type of the capability passed into | ||
* `derives` handler. It simply makes all `nb` fileds optional because | ||
* `derives` handler. It simply makes all `nb` fields optional because | ||
* in delegation all `nb` fields could be left out implying no restrictions. | ||
*/ | ||
export type ToDeriveClaim<T extends ParsedCapability> = T | ParsedCapability<T['can'], T['with'], Partial<T['nb']>>; | ||
type ToDeriveClaim<T extends ParsedCapability> = T | ParsedCapability<T['can'], T['with'], Partial<T['nb']>>; | ||
/** | ||
@@ -68,3 +67,3 @@ * Utility type is used to infer type of the second argument of `derives` | ||
*/ | ||
export type ToDeriveProof<T> = T extends ParsedCapability ? ToDeriveClaim<T> : ToDeriveProofs<T>; | ||
export type InferDeriveProof<T> = T extends ParsedCapability ? InferDelegatedCapability<T> : InferDeriveProofs<T>; | ||
/** | ||
@@ -74,3 +73,3 @@ * Another helper type which is equivalent of `ToDeriveClaim` except it works | ||
*/ | ||
type ToDeriveProofs<T> = T extends [infer U, ...infer E] ? [ToDeriveClaim<U & ParsedCapability>, ...ToDeriveProofs<E>] : T extends never[] ? [] : never; | ||
type InferDeriveProofs<T> = T extends [infer U, ...infer E] ? [ToDeriveClaim<U & ParsedCapability>, ...InferDeriveProofs<E>] : T extends never[] ? [] : never; | ||
export interface Derives<T extends ParsedCapability, U = T> { | ||
@@ -112,12 +111,10 @@ (claim: T, proof: U): Result<true, Failure>; | ||
*/ | ||
derive<T extends ParsedCapability>(options: DeriveSelector<M, T>): TheCapabilityParser<DerivedMatch<T, M>>; | ||
derive<T extends ParsedCapability>(options: { | ||
to: TheCapabilityParser<DirectMatch<T>>; | ||
derives: Derives<T, InferDeriveProof<M['value']>>; | ||
}): TheCapabilityParser<DerivedMatch<T, M>>; | ||
} | ||
export type InferCaveatParams<T> = keyof T extends never ? never | undefined : { | ||
[K in keyof T]: T[K] extends { | ||
toJSON(): infer U; | ||
} ? U : T[K]; | ||
}; | ||
export interface TheCapabilityParser<M extends Match<ParsedCapability>> extends CapabilityParser<M> { | ||
readonly can: M['value']['can']; | ||
create(input: InferCreateOptions<M['value']['with'], M['value']['nb']>): M['value']; | ||
create(input: InferCreateOptions<M['value']['with'], M['value']['nb']>): InferCapability<M['value']>; | ||
/** | ||
@@ -127,3 +124,3 @@ * Creates an invocation of this capability. Function throws exception if | ||
*/ | ||
invoke(options: InferInvokeOptions<M['value']['with'], M['value']['nb']>): IssuedInvocationView<M['value']>; | ||
invoke(options: InferInvokeOptions<M['value']['with'], M['value']['nb']>): IssuedInvocationView<InferCapability<M['value']>>; | ||
/** | ||
@@ -134,4 +131,29 @@ * Creates a delegation of this capability. Please note that all the | ||
*/ | ||
delegate(options: InferDelegationOptions<M['value']['with'], M['value']['nb']>): Promise<Delegation<[ToDeriveClaim<M['value']>]>>; | ||
delegate(options: InferDelegationOptions<M['value']['with'], M['value']['nb']>): Promise<Delegation<[InferDelegatedCapability<M['value']>]>>; | ||
} | ||
/** | ||
* When normalize capabilities by removing `nb` if it is a `{}`. This type | ||
* does that normalization at the type level. | ||
*/ | ||
export type InferCapability<T extends ParsedCapability> = keyof T['nb'] extends never ? { | ||
can: T['can']; | ||
with: T['with']; | ||
} : { | ||
can: T['can']; | ||
with: T['with']; | ||
nb: T['nb']; | ||
}; | ||
/** | ||
* In delegation capability all the `nb` fields are optional. This type maps | ||
* capability type (as it would be in the invocation) to the form it will be | ||
* in the delegation. | ||
*/ | ||
export type InferDelegatedCapability<T extends ParsedCapability> = keyof T['nb'] extends never ? { | ||
can: T['can']; | ||
with: T['with']; | ||
} : { | ||
can: T['can']; | ||
with: T['with']; | ||
nb: Partial<T['nb']>; | ||
}; | ||
export type InferCreateOptions<R extends Resource, C extends {} | undefined> = keyof C extends never ? { | ||
@@ -145,6 +167,6 @@ with: R; | ||
export type InferInvokeOptions<R extends Resource, C extends {} | undefined> = UCANOptions & { | ||
issuer: Signer; | ||
issuer: UCAN.Signer; | ||
} & InferCreateOptions<R, C>; | ||
export type InferDelegationOptions<R extends Resource, C extends {} | undefined> = UCANOptions & { | ||
issuer: Signer; | ||
issuer: UCAN.Signer; | ||
with: R; | ||
@@ -156,9 +178,2 @@ nb?: Partial<InferCreateOptions<R, C>['nb']>; | ||
}; | ||
type Optionalize<T> = InferRequried<T> & InferOptional<T>; | ||
type InferOptional<T> = { | ||
[K in keyof T as T[K] | undefined extends T[K] ? K : never]?: T[K]; | ||
}; | ||
type InferRequried<T> = { | ||
[K in keyof T as T[K] | undefined extends T[K] ? never : K]: T[K]; | ||
}; | ||
export interface CapabilityParser<M extends Match = Match> extends View<M> { | ||
@@ -169,3 +184,3 @@ /** | ||
* validate any of one of them without having to maintain list of supported | ||
* capabilities. It is especially useful when dealiving with derived | ||
* capabilities. It is especially useful when dealing with derived | ||
* capability chains when you might derive capability from either one or the | ||
@@ -222,3 +237,3 @@ * other. | ||
* Creates new capability group containing capabilities from this group and | ||
* provedid `other` capability. This method complements `and` method on | ||
* provided `other` capability. This method complements `and` method on | ||
* `Capability` to allow chaining e.g. `read.and(write).and(modify)`. | ||
@@ -236,21 +251,8 @@ */ | ||
export type InferMatch<Members extends unknown[]> = Members extends [] ? [] : Members extends [Match<unknown, infer M>, ...infer Rest] ? [M, ...InferMatch<Rest>] : never; | ||
export type ParsedCapability<Can extends Ability = Ability, Resource extends URI = URI, C extends object = {}> = keyof C extends never ? { | ||
export interface ParsedCapability<Can extends Ability = Ability, Resource extends URI = URI, C extends Caveats = {}> { | ||
can: Can; | ||
with: Resource; | ||
nb?: C; | ||
} : { | ||
can: Can; | ||
with: Resource; | ||
nb: C; | ||
}; | ||
export type InferCaveats<C> = Optionalize<{ | ||
[K in keyof C]: C[K] extends Reader<infer T, unknown, infer _> ? T : never; | ||
}>; | ||
export interface Descriptor<A extends Ability, R extends URI, C extends Caveats> { | ||
can: A; | ||
with: Reader<R, Resource, Failure>; | ||
nb?: C; | ||
derives?: Derives<ToDeriveClaim<ParsedCapability<A, R, InferCaveats<C>>>, ToDeriveClaim<ParsedCapability<A, R, InferCaveats<C>>>>; | ||
} | ||
export interface CapabilityMatch<A extends Ability, R extends URI, C extends Caveats> extends DirectMatch<ParsedCapability<A, R, InferCaveats<C>>> { | ||
export interface CapabilityMatch<A extends Ability, R extends URI, C extends Caveats> extends DirectMatch<ParsedCapability<A, R, C>> { | ||
} | ||
@@ -322,3 +324,3 @@ export interface CanIssue { | ||
readonly did: UCAN.DID; | ||
readonly cause?: Unauthorized; | ||
readonly cause?: Failure; | ||
} | ||
@@ -341,6 +343,11 @@ export interface Expired extends Failure { | ||
} | ||
export interface SessionEscalation extends Failure { | ||
readonly name: 'SessionEscalation'; | ||
readonly delegation: Delegation; | ||
readonly cause: Failure; | ||
} | ||
/** | ||
* Error produces by invalid proof | ||
*/ | ||
export type InvalidProof = Expired | NotValidBefore | InvalidSignature | InvalidAudience | DIDKeyResolutionError | UnavailableProof; | ||
export type InvalidProof = Expired | NotValidBefore | InvalidSignature | InvalidAudience | SessionEscalation | DIDKeyResolutionError | UnavailableProof; | ||
export interface Unauthorized extends Failure { | ||
@@ -347,0 +354,0 @@ name: 'Unauthorized'; |
@@ -41,3 +41,3 @@ import { Ability, Block as UCANBlock, ByteView, Capabilities, Capability, DID, Fact, Link as UCANLink, MultihashHasher, MultihashDigest, MultibaseDecoder, MultibaseEncoder, Phantom, Resource, Signature, Principal, MulticodecCode, SigAlg, ToJSON, SignatureJSON, JSONUnknown, JSONObject } from '@ipld/dag-ucan'; | ||
*/ | ||
issuer: Signer; | ||
issuer: UCAN.Signer; | ||
/** | ||
@@ -137,3 +137,3 @@ * The `audience` for a {@link Delegation} is the party being delegated to, or the | ||
/** The `issuer` of an invocation is the "caller" of the RPC method and the party that signs the invocation UCAN token. */ | ||
issuer: Signer; | ||
issuer: UCAN.Signer; | ||
/** The {@link Capability} that is being invoked. */ | ||
@@ -140,0 +140,0 @@ capability: C; |
{ | ||
"name": "@ucanto/interface", | ||
"description": "interface definitions for ucanto", | ||
"version": "4.2.3", | ||
"version": "4.3.4", | ||
"types": "./dist/src/lib.d.ts", | ||
@@ -6,0 +6,0 @@ "main": "./src/lib.js", |
@@ -15,11 +15,8 @@ import { Ability, Capability, DID, Link, Resource } from '@ipld/dag-ucan' | ||
UCANOptions, | ||
DIDKey, | ||
Verifier, | ||
API, | ||
} from './lib.js' | ||
export interface Source { | ||
capability: Capability | ||
capability: { can: Ability; with: URI; nb?: Caveats } | ||
delegation: Delegation | ||
index: number | ||
} | ||
@@ -71,3 +68,3 @@ | ||
export interface Caveats { | ||
[key: string]: Reader<any, unknown> | ||
[key: string]: unknown | ||
} | ||
@@ -85,13 +82,8 @@ | ||
export interface DeriveSelector<M extends Match, T extends ParsedCapability> { | ||
to: TheCapabilityParser<DirectMatch<T>> | ||
derives: Derives<ToDeriveClaim<T>, ToDeriveProof<M['value']>> | ||
} | ||
/** | ||
* Utility type is used to infer the type of the capability passed into | ||
* `derives` handler. It simply makes all `nb` fileds optional because | ||
* `derives` handler. It simply makes all `nb` fields optional because | ||
* in delegation all `nb` fields could be left out implying no restrictions. | ||
*/ | ||
export type ToDeriveClaim<T extends ParsedCapability> = | ||
type ToDeriveClaim<T extends ParsedCapability> = | ||
| T | ||
@@ -107,7 +99,7 @@ | ParsedCapability<T['can'], T['with'], Partial<T['nb']>> | ||
*/ | ||
export type ToDeriveProof<T> = T extends ParsedCapability | ||
export type InferDeriveProof<T> = T extends ParsedCapability | ||
? // If it a capability we just make `nb` partial | ||
ToDeriveClaim<T> | ||
InferDelegatedCapability<T> | ||
: // otherwise we need to map tuple | ||
ToDeriveProofs<T> | ||
InferDeriveProofs<T> | ||
@@ -118,4 +110,4 @@ /** | ||
*/ | ||
type ToDeriveProofs<T> = T extends [infer U, ...infer E] | ||
? [ToDeriveClaim<U & ParsedCapability>, ...ToDeriveProofs<E>] | ||
type InferDeriveProofs<T> = T extends [infer U, ...infer E] | ||
? [ToDeriveClaim<U & ParsedCapability>, ...InferDeriveProofs<E>] | ||
: T extends never[] | ||
@@ -162,13 +154,8 @@ ? [] | ||
*/ | ||
derive<T extends ParsedCapability>( | ||
options: DeriveSelector<M, T> | ||
): TheCapabilityParser<DerivedMatch<T, M>> | ||
derive<T extends ParsedCapability>(options: { | ||
to: TheCapabilityParser<DirectMatch<T>> | ||
derives: Derives<T, InferDeriveProof<M['value']>> | ||
}): TheCapabilityParser<DerivedMatch<T, M>> | ||
} | ||
export type InferCaveatParams<T> = keyof T extends never | ||
? never | undefined | ||
: { | ||
[K in keyof T]: T[K] extends { toJSON(): infer U } ? U : T[K] | ||
} | ||
export interface TheCapabilityParser<M extends Match<ParsedCapability>> | ||
@@ -180,3 +167,3 @@ extends CapabilityParser<M> { | ||
input: InferCreateOptions<M['value']['with'], M['value']['nb']> | ||
): M['value'] | ||
): InferCapability<M['value']> | ||
@@ -187,6 +174,5 @@ /** | ||
*/ | ||
invoke( | ||
options: InferInvokeOptions<M['value']['with'], M['value']['nb']> | ||
): IssuedInvocationView<M['value']> | ||
): IssuedInvocationView<InferCapability<M['value']>> | ||
@@ -200,8 +186,27 @@ /** | ||
options: InferDelegationOptions<M['value']['with'], M['value']['nb']> | ||
): Promise<Delegation<[ToDeriveClaim<M['value']>]>> | ||
): Promise<Delegation<[InferDelegatedCapability<M['value']>]>> | ||
} | ||
/** | ||
* When normalize capabilities by removing `nb` if it is a `{}`. This type | ||
* does that normalization at the type level. | ||
*/ | ||
export type InferCapability<T extends ParsedCapability> = | ||
keyof T['nb'] extends never | ||
? { can: T['can']; with: T['with'] } | ||
: { can: T['can']; with: T['with']; nb: T['nb'] } | ||
/** | ||
* In delegation capability all the `nb` fields are optional. This type maps | ||
* capability type (as it would be in the invocation) to the form it will be | ||
* in the delegation. | ||
*/ | ||
export type InferDelegatedCapability<T extends ParsedCapability> = | ||
keyof T['nb'] extends never | ||
? { can: T['can']; with: T['with'] } | ||
: { can: T['can']; with: T['with']; nb: Partial<T['nb']> } | ||
export type InferCreateOptions<R extends Resource, C extends {} | undefined> = | ||
// If capability has no NB we want to prevent passing it into | ||
// .create funciton so we make `nb` as optional `never` type so | ||
// .create function so we make `nb` as optional `never` type so | ||
// it can not be satisfied | ||
@@ -213,3 +218,3 @@ keyof C extends never ? { with: R; nb?: never } : { with: R; nb: C } | ||
C extends {} | undefined | ||
> = UCANOptions & { issuer: Signer } & InferCreateOptions<R, C> | ||
> = UCANOptions & { issuer: UCAN.Signer } & InferCreateOptions<R, C> | ||
@@ -220,3 +225,3 @@ export type InferDelegationOptions< | ||
> = UCANOptions & { | ||
issuer: Signer | ||
issuer: UCAN.Signer | ||
with: R | ||
@@ -227,12 +232,3 @@ nb?: Partial<InferCreateOptions<R, C>['nb']> | ||
export type EmptyObject = { [key: string | number | symbol]: never } | ||
type Optionalize<T> = InferRequried<T> & InferOptional<T> | ||
type InferOptional<T> = { | ||
[K in keyof T as T[K] | undefined extends T[K] ? K : never]?: T[K] | ||
} | ||
type InferRequried<T> = { | ||
[K in keyof T as T[K] | undefined extends T[K] ? never : K]: T[K] | ||
} | ||
export interface CapabilityParser<M extends Match = Match> extends View<M> { | ||
@@ -243,3 +239,3 @@ /** | ||
* validate any of one of them without having to maintain list of supported | ||
* capabilities. It is especially useful when dealiving with derived | ||
* capabilities. It is especially useful when dealing with derived | ||
* capability chains when you might derive capability from either one or the | ||
@@ -298,3 +294,3 @@ * other. | ||
* Creates new capability group containing capabilities from this group and | ||
* provedid `other` capability. This method complements `and` method on | ||
* provided `other` capability. This method complements `and` method on | ||
* `Capability` to allow chaining e.g. `read.and(write).and(modify)`. | ||
@@ -329,28 +325,10 @@ */ | ||
export type ParsedCapability< | ||
export interface ParsedCapability< | ||
Can extends Ability = Ability, | ||
Resource extends URI = URI, | ||
C extends object = {} | ||
> = keyof C extends never | ||
? { can: Can; with: Resource; nb?: C } | ||
: { can: Can; with: Resource; nb: C } | ||
export type InferCaveats<C> = Optionalize<{ | ||
[K in keyof C]: C[K] extends Reader<infer T, unknown, infer _> ? T : never | ||
}> | ||
export interface Descriptor< | ||
A extends Ability, | ||
R extends URI, | ||
C extends Caveats | ||
C extends Caveats = {} | ||
> { | ||
can: A | ||
with: Reader<R, Resource, Failure> | ||
nb?: C | ||
derives?: Derives< | ||
ToDeriveClaim<ParsedCapability<A, R, InferCaveats<C>>>, | ||
ToDeriveClaim<ParsedCapability<A, R, InferCaveats<C>>> | ||
> | ||
can: Can | ||
with: Resource | ||
nb: C | ||
} | ||
@@ -362,3 +340,3 @@ | ||
C extends Caveats | ||
> extends DirectMatch<ParsedCapability<A, R, InferCaveats<C>>> {} | ||
> extends DirectMatch<ParsedCapability<A, R, C>> {} | ||
@@ -454,3 +432,3 @@ export interface CanIssue { | ||
readonly cause?: Unauthorized | ||
readonly cause?: Failure | ||
} | ||
@@ -477,2 +455,8 @@ | ||
export interface SessionEscalation extends Failure { | ||
readonly name: 'SessionEscalation' | ||
readonly delegation: Delegation | ||
readonly cause: Failure | ||
} | ||
/** | ||
@@ -486,2 +470,3 @@ * Error produces by invalid proof | ||
| InvalidAudience | ||
| SessionEscalation | ||
| DIDKeyResolutionError | ||
@@ -488,0 +473,0 @@ | UnavailableProof |
@@ -110,3 +110,3 @@ import { | ||
*/ | ||
issuer: Signer | ||
issuer: UCAN.Signer | ||
@@ -228,3 +228,3 @@ /** | ||
/** The `issuer` of an invocation is the "caller" of the RPC method and the party that signs the invocation UCAN token. */ | ||
issuer: Signer | ||
issuer: UCAN.Signer | ||
@@ -231,0 +231,0 @@ /** The {@link Capability} that is being invoked. */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16
111722
2268
1