-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: autoformat * feat: display fetch error * feat: add statusText property
- Loading branch information
Showing
3 changed files
with
80 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
const base_url = 'https://did.dyne.org'; | ||
const base_url = "https://did.dyne.org"; | ||
|
||
export const getData = async (endpoint: string) => { | ||
const res = await fetch(`${base_url}/${endpoint}`); | ||
if (!res.ok) { | ||
throw new Error(`Failed to fetch data from ${endpoint}`); | ||
} | ||
return res.json(); | ||
const res = await fetch(`${base_url}/${endpoint}`); | ||
if (!res.ok) { | ||
throw new FetchError( | ||
res.status, | ||
`Failed to fetch data from ${endpoint}`, | ||
res.statusText | ||
); | ||
} | ||
return res.json(); | ||
}; | ||
|
||
export class FetchError extends Error { | ||
status: number; | ||
message: string; | ||
statusText: string; | ||
|
||
constructor(status: number, message: string, statusText: string) { | ||
super(message); | ||
this.name = "FetchError"; | ||
this.status = status; | ||
this.message = message; | ||
this.statusText = statusText; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import { DIDResolutionOptions, DIDResolutionResult, ParsedDID, Resolvable, Resolver } from "did-resolver"; | ||
import { | ||
DIDResolutionOptions, | ||
DIDResolutionResult, | ||
ParsedDID, | ||
Resolvable, | ||
Resolver, | ||
} from "did-resolver"; | ||
import { getData } from "./fetcher"; | ||
|
||
const resolver = new Resolver({ | ||
dyne: async function resolve( | ||
did: string, | ||
parsed: ParsedDID, | ||
didResolver: Resolvable, | ||
options: DIDResolutionOptions | ||
): Promise<DIDResolutionResult> { | ||
// {method: 'mymethod', id: 'abcdefg', did: 'did:mymethod:abcdefg/some/path#fragment=123', path: '/some/path', fragment: 'fragment=123'} | ||
const didDoc = await getData(`dids/${did}`); | ||
// If you need to lookup another did as part of resolving this did document, the primary DIDResolver object is passed in as well | ||
// const parentDID = await didResolver.resolve(...) | ||
// Return the DIDResolutionResult object | ||
return didDoc | ||
} | ||
}) | ||
dyne: async function resolve( | ||
did: string, | ||
parsed: ParsedDID, | ||
didResolver: Resolvable, | ||
options: DIDResolutionOptions | ||
): Promise<DIDResolutionResult> { | ||
// {method: 'mymethod', id: 'abcdefg', did: 'did:mymethod:abcdefg/some/path#fragment=123', path: '/some/path', fragment: 'fragment=123'} | ||
const didDoc = await getData(`dids/${did}`); | ||
// If you need to lookup another did as part of resolving this did document, the primary DIDResolver object is passed in as well | ||
// const parentDID = await didResolver.resolve(...) | ||
// Return the DIDResolutionResult object | ||
return didDoc; | ||
}, | ||
}); | ||
|
||
export const resolve = async (didUrl: string): Promise<DIDResolutionResult> => { | ||
return await resolver.resolve(didUrl) | ||
} | ||
return await resolver.resolve(didUrl); | ||
}; |