-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathget.js
46 lines (41 loc) · 1.22 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as API from '../types.js'
import * as Provider from '@ucanto/server'
import { Customer } from '@web3-storage/capabilities'
/**
* @param {API.CustomerServiceContext} context
*/
export const provide = (context) =>
Provider.provide(Customer.get, (input) => get(input, context))
/**
* @param {API.Input<Customer.get>} input
* @param {API.CustomerServiceContext} context
* @returns {Promise<API.CustomerGetResult>}
*/
const get = async ({ capability }, context) => {
/**
* Ensure that resource is the service DID, which implies it's either
* invoked by service itself or an authorized delegate (like admin).
* In other words no user will be able to invoke this unless service
* explicitly delegated capability to them to do so.
*/
if (capability.with !== context.signer.did()) {
return { error: new UnknownProvider(capability.with) }
}
return await context.provisionsStorage.getCustomer(
capability.with,
capability.nb.customer
)
}
class UnknownProvider extends Provider.Failure {
/**
* @param {API.DID} did
*/
constructor(did) {
super()
this.did = did
this.name = /** @type {const} */ ('UnknownProvider')
}
describe() {
return `Provider ${this.did} not found`
}
}