Skip to content

Commit 9fdb90e

Browse files
author
Wes Biggs
committed
Clean up syntax
1 parent 3fa3780 commit 9fdb90e

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ This resolver should be used with one or more plugins for specific DSNP systems
66
# Usage
77

88
This package follows the conventions described in the [`did-resolver`](https://github.com/decentralized-identity/did-resolver) package and exposes a `getResolver()` function which returns a mapping from the `dsnp` DID method to a resolver function.
9+
Pass an array of plugins to the `getResolver()` method.
910

1011
## Usage for DSNP DID method only
1112

1213
```
1314
import { Resolver } from "did-resolver";
14-
import dsnp from "@dsnp/did-resolver";
15-
import "dsnp-did-resolver-plugin-foo"; // See below for known plugins
15+
import dsnpResolver from "@dsnp/did-resolver";
16+
import { FooResolver} from "dsnp-did-resolver-plugin-foo"; // See below for known plugins
1617
// ... additional dsnp-did-resolver plugins if needed
1718
18-
const resolver = new Resolver(dsnp.getResolver());
19+
const resolver = new Resolver(dsnpResolver.getResolver(
20+
[ new FooResolver() ]
21+
));
1922
const myDid = "did:dsnp:123456";
2023
const result = await resolver.resolve(myDid);
2124
console.log(JSON.stringify(result, null, 2));
@@ -27,36 +30,46 @@ The Resolver constructor can combine resolvers for multiple DID methods as descr
2730
For example, if you also want to be able to resolve `did:ethr:*` DIDs, you would import and include the `ethr` resolver and pass its destructured resolver alongside the DSNP one to the constructor as follows:
2831

2932
```
30-
import ethr from "ethr-did-resolver";
33+
import ethrResolver from "ethr-did-resolver";
3134
// ... additional did-resolver packages;
3235
3336
const resolver = new Resolver({
34-
...dsnp.getResolver(),
35-
...ethr.getResolver()
37+
...dsnpResolver.getResolver([plugin]),
38+
...ethrResolver.getResolver()
3639
});
3740
// usage is same as in previous example
3841
```
3942

4043
# Plugins
4144

42-
To resolve a DSNP User Id to a specific DSNP system, one or more system-specific plugins must be imported.
43-
Plugin import order defines the order in which this resolver will attempt to resolve a DSNP DID.
45+
To resolve a DSNP User Id to a specific DSNP system, one or more instances of system-specific plugins are required.
46+
Array ordering defines the order in which this resolver will attempt to resolve a DSNP DID.
4447
It will return the result from the first plugin that returns a DID document.
4548

4649
## Plugin development
4750

48-
Plugins should implement a function with the `DSNPResolver` type (see `index.ts`):
51+
Plugins should implement the `DSNPResolver` interface, which includes a `resolve(bigint)` function (see `index.ts`):
4952

5053
```
51-
export type DSNPResolver = (dsnpUserId: BigInt) => Promise<DIDDocument | null>;
54+
export interface DSNPResolver {
55+
resolve(dsnpUserId: bigint): Promise<DIDDocument | null>;
56+
}
5257
```
5358

5459
The `DIDDocument` type is defined in the `did-resolver` package.
5560

56-
The DSNP resolver function should return `null` if the DSNP User Id cannot be resolved to a DID document for any reason.
61+
The `resolve()` function should return `null` if the DSNP User Id cannot be resolved to a DID document for any reason.
5762
Plugins should avoid throwing errors except in dire circumstances, as errors from one plugin will cause any further plugins that have been registered to *not* be called.
5863

59-
Register the function by calling `registerDSNPResolver(myResolver)`.
64+
`DSNPResolver` instances are passed to the `getResolver()` function.
65+
66+
## Known plugins
67+
68+
| System | Plugin package
69+
|---|---|
70+
| Frequency | [`@dsnp/did-resolver-plugin-frequency`](https://github.com/AmplicaLabs/dsnp-did-resolver-plugin-frequency) |
71+
72+
Please submit a pull request with any additional plugins.
6073

6174
# Development
6275

index.test.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@ import {
66
DIDResolutionOptions,
77
} from "did-resolver";
88
import { expect, jest, test } from "@jest/globals";
9-
import { DSNPResolver, getResolver, registerDSNPResolver } from "./index.js";
9+
import { DSNPResolver, getResolver } from "./index.js";
1010

1111
const someDocument: DIDDocument = {
1212
id: "",
1313
};
1414

15-
const plugin1: DSNPResolver = async (dsnpUserId: bigint) => {
16-
if (dsnpUserId === 123456n) {
17-
return someDocument;
18-
} else {
19-
return null;
15+
class Plugin1 implements DSNPResolver {
16+
async resolve(dsnpUserId: bigint): Promise<DIDDocument | null> {
17+
if (dsnpUserId === 123456n) {
18+
return someDocument;
19+
} else {
20+
return null;
21+
}
2022
}
21-
};
23+
}
2224

23-
const plugin2: DSNPResolver = async (dsnpUserId: bigint) => {
24-
if (dsnpUserId === 234567n) {
25-
return someDocument;
26-
} else {
27-
return null;
25+
class Plugin2 implements DSNPResolver {
26+
async resolve(dsnpUserId: bigint): Promise<DIDDocument | null> {
27+
if (dsnpUserId === 234567n) {
28+
return someDocument;
29+
} else {
30+
return null;
31+
}
2832
}
29-
};
30-
31-
registerDSNPResolver(plugin1);
32-
registerDSNPResolver(plugin2);
33+
}
3334

3435
function errorResult(error: string): DIDResolutionResult {
3536
return {
@@ -61,7 +62,7 @@ describe("dsnp-did-resolver", () => {
6162
let resolver: GetResolverOutput;
6263

6364
beforeAll(() => {
64-
resolver = getResolver();
65+
resolver = getResolver([new Plugin1(), new Plugin2()]);
6566
});
6667

6768
async function doResolve(id: string) {

index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import {
77
DIDDocument,
88
} from "did-resolver";
99

10-
const dsnpResolvers: DSNPResolver[] = [];
11-
12-
export type DSNPResolver = (dsnpUserId: bigint) => Promise<DIDDocument | null>;
13-
14-
export function registerDSNPResolver(resolver: DSNPResolver) {
15-
dsnpResolvers.push(resolver);
10+
export interface DSNPResolver {
11+
resolve(dsnpUserId: bigint): Promise<DIDDocument | null>;
1612
}
1713

1814
const notFoundResult: DIDResolutionResult = {
@@ -21,7 +17,7 @@ const notFoundResult: DIDResolutionResult = {
2117
didResolutionMetadata: { error: "notFound" },
2218
};
2319

24-
export function getResolver() {
20+
export function getResolver(dsnpResolvers: DSNPResolver[]) {
2521
async function resolve(
2622
did: string,
2723
parsed: ParsedDID,
@@ -54,7 +50,7 @@ export function getResolver() {
5450

5551
for (const dsnpResolver of dsnpResolvers) {
5652
try {
57-
const output = await dsnpResolver(dsnpUserId);
53+
const output = await dsnpResolver.resolve(dsnpUserId);
5854
if (output)
5955
return {
6056
didResolutionMetadata: { contentType: "application/did+ld+json" },

0 commit comments

Comments
 (0)