Skip to content

Commit

Permalink
fix: .provide inference regression (#242)
Browse files Browse the repository at this point in the history
* chore: create a failing test

* fix: type misalignment
  • Loading branch information
Gozala committed Mar 1, 2023
1 parent 8344791 commit ab155b7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
8 changes: 3 additions & 5 deletions packages/interface/src/capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Failure,
PrincipalParser,
PrincipalResolver,
Signer,
URI,
UCANLink,
Await,
Expand Down Expand Up @@ -182,10 +181,9 @@ export interface TheCapabilityParser<M extends Match<ParsedCapability>>
* 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'] }
export type InferCapability<T extends Capability> = 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
Expand Down
5 changes: 4 additions & 1 deletion packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DIDKeyResolutionError,
ParsedCapability,
CapabilityParser,
InferCapability,
} from './capability.js'
import type * as Transport from './transport.js'
import type { Tuple, Block } from './transport.js'
Expand Down Expand Up @@ -746,7 +747,9 @@ export type KeyArchive<Alg extends SigAlg = SigAlg> =

export type InferInvokedCapability<
C extends CapabilityParser<Match<ParsedCapability>>
> = C extends CapabilityParser<Match<infer T>> ? T : never
> = C extends CapabilityParser<Match<infer T>>
? InferCapability<T & Capability>
: never

export type Intersection<T> = (T extends any ? (i: T) => void : never) extends (
i: infer I
Expand Down
32 changes: 32 additions & 0 deletions packages/server/test/handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,35 @@ test('checks for single capability invocation', async () => {
],
})
})

test('test access/claim provider', async () => {
const server = Server.create({
id: w3,
service: { access: Access },
decoder: CAR,
encoder: CBOR,
})

/**
* @type {Client.ConnectionView<{
* access: {
* claim: API.ServiceMethod<API.InferInvokedCapability<typeof Access.claimCapability>, never[], never>
* }
* }>}
*/
const client = Client.connect({
id: w3,
encoder: CAR,
decoder: CBOR,
channel: server,
})

const claim = Access.claimCapability.invoke({
issuer: alice,
audience: w3,
with: alice.did(),
})

const result = await claim.execute(client)
assert.deepEqual(result, [])
})
13 changes: 13 additions & 0 deletions packages/server/test/service/access.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Server from '../../src/server.js'
import { provide } from '../../src/handler.js'
import { DID } from '@ucanto/validator'
import * as API from './api.js'
import { service as w3 } from '../fixtures.js'
export const id = w3
Expand Down Expand Up @@ -33,6 +34,11 @@ const identifyCapability = Server.capability({
new Server.Failure(`Can not derive ${claimed.with} from ${claimed.with}`),
})

export const claimCapability = Server.capability({
can: 'access/claim',
with: DID.match({ method: 'key' }).or(DID.match({ method: 'mailto' })),
})

/**
* @typedef {Map<API.DID|API.URI<"mailto:">, {account:API.DID, proof:API.Link}>} Model
* @type {Model}
Expand Down Expand Up @@ -74,6 +80,13 @@ export const identify = provide(
}
)

export const claim = provide(
claimCapability,
async function claim({ capability }) {
return []
}
)

/**
* @param {Model} accounts
* @param {API.DID} from
Expand Down

0 comments on commit ab155b7

Please sign in to comment.