Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: .provide inference regression #242

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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