diff --git a/.prettierrc b/.prettierrc index e3c4b95..5e60fe1 100644 --- a/.prettierrc +++ b/.prettierrc @@ -3,5 +3,6 @@ "tabWidth": 2, "printWidth": 80, "singleQuote": true, - "semi": false + "semi": false, + "trailingComma": "none" } diff --git a/package-lock.json b/package-lock.json index 5923c67..2d9db3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2059,10 +2059,114 @@ } }, "@types/jest": { - "version": "23.3.10", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-23.3.10.tgz", - "integrity": "sha512-DC8xTuW/6TYgvEg3HEXS7cu9OijFqprVDXXiOcdOKZCU/5PJNLZU37VVvmZHdtMiGOa8wAA/We+JzbdxFzQTRQ==", - "dev": true + "version": "26.0.5", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.5.tgz", + "integrity": "sha512-heU+7w8snfwfjtcj2H458aTx3m5unIToOJhx75ebHilBiiQ39OIdA18WkG4LP08YKeAoWAGvWg8s+22w/PeJ6w==", + "dev": true, + "requires": { + "jest-diff": "^25.2.1", + "pretty-format": "^25.2.1" + }, + "dependencies": { + "@jest/types": { + "version": "25.5.0", + "resolved": "https://registry.npmjs.org/@jest/types/-/types-25.5.0.tgz", + "integrity": "sha512-OXD0RgQ86Tu3MazKo8bnrkDRaDXXMGUqd+kTtLtK1Zb7CRzQcaSRPPPV37SvYTdevXEBVxe0HXylEjs8ibkmCw==", + "dev": true, + "requires": { + "@types/istanbul-lib-coverage": "^2.0.0", + "@types/istanbul-reports": "^1.1.1", + "@types/yargs": "^15.0.0", + "chalk": "^3.0.0" + } + }, + "ansi-styles": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", + "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", + "dev": true, + "requires": { + "@types/color-name": "^1.1.1", + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-3.0.0.tgz", + "integrity": "sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "diff-sequences": { + "version": "25.2.6", + "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-25.2.6.tgz", + "integrity": "sha512-Hq8o7+6GaZeoFjtpgvRBUknSXNeJiCx7V9Fr94ZMljNiCr9n9L8H8aJqgWOQiDDGdyn29fRNcDdRVJ5fdyihfg==", + "dev": true + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true + }, + "jest-diff": { + "version": "25.5.0", + "resolved": "https://registry.npmjs.org/jest-diff/-/jest-diff-25.5.0.tgz", + "integrity": "sha512-z1kygetuPiREYdNIumRpAHY6RXiGmp70YHptjdaxTWGmA085W3iCnXNx0DhflK3vwrKmrRWyY1wUpkPMVxMK7A==", + "dev": true, + "requires": { + "chalk": "^3.0.0", + "diff-sequences": "^25.2.6", + "jest-get-type": "^25.2.6", + "pretty-format": "^25.5.0" + } + }, + "jest-get-type": { + "version": "25.2.6", + "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-25.2.6.tgz", + "integrity": "sha512-DxjtyzOHjObRM+sM1knti6or+eOgcGU4xVSb2HNP1TqO4ahsT+rqZg+nyqHWJSvWgKC5cG3QjGFBqxLghiF/Ig==", + "dev": true + }, + "pretty-format": { + "version": "25.5.0", + "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-25.5.0.tgz", + "integrity": "sha512-kbo/kq2LQ/A/is0PQwsEHM7Ca6//bGPPvU6UnsdDRSKTWxT/ru/xb88v4BJf6a69H+uTytOEsTusT9ksd/1iWQ==", + "dev": true, + "requires": { + "@jest/types": "^25.5.0", + "ansi-regex": "^5.0.0", + "ansi-styles": "^4.0.0", + "react-is": "^16.12.0" + } + }, + "supports-color": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", + "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + } + } }, "@types/minimist": { "version": "1.2.0", diff --git a/package.json b/package.json index 925e377..7dee04e 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test": "jest", "test-with-coverage": "jest --coverage && codecov", "dev": "tsc --watch", - "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"", + "format": "prettier --write \"src/**/*.ts\"", "lint": "tslint -p tsconfig.json", "prepare": "npm run build", "prepublishOnly": "npm test && npm run lint", @@ -27,7 +27,7 @@ "@commitlint/config-conventional": "9.1.1", "@semantic-release/changelog": "5.0.1", "@semantic-release/git": "9.0.0", - "@types/jest": "23.3.10", + "@types/jest": "26.0.5", "codecov": "3.7.1", "jest": "26.1.0", "prettier": "2.0.5", diff --git a/src/__tests__/resolver.test.ts b/src/__tests__/resolver.test.ts index b57985b..0929c38 100644 --- a/src/__tests__/resolver.test.ts +++ b/src/__tests__/resolver.test.ts @@ -2,7 +2,8 @@ import { Resolver, DIDResolver, DIDDocument } from 'did-resolver' import { getResolver } from '../resolver' import fetch from 'cross-fetch' jest.mock('cross-fetch') -const mockedFetch = fetch as jest.Mock +import { mocked } from 'ts-jest/utils' +const mockedFetch = mocked(fetch, true) describe('web did resolver', () => { const did: string = 'did:web:example.com' @@ -27,7 +28,9 @@ describe('web did resolver', () => { ] } - const validResponseLong: DIDDocument = JSON.parse(JSON.stringify(validResponse).replace(did, didLong)) + const validResponseLong: DIDDocument = JSON.parse( + JSON.stringify(validResponse).replace(did, didLong) + ) const noContextResponse: object = { id: validResponse.id, publicKey: validResponse.publicKey, @@ -55,19 +58,20 @@ describe('web did resolver', () => { it('resolves document', () => { mockedFetch.mockResolvedValueOnce({ - json: () => validResponse - }) + json: () => Promise.resolve(validResponse) + } as Response) return expect(didResolver.resolve(did)).resolves.toEqual(validResponse) }) it('resolves document with long did', () => { mockedFetch.mockResolvedValueOnce({ - json: () => validResponseLong - }) - return expect(didResolver.resolve(didLong)).resolves.toEqual(validResponseLong) + json: () => Promise.resolve(validResponseLong) + } as Response) + return expect(didResolver.resolve(didLong)).resolves.toEqual( + validResponseLong + ) }) - it('fails if the did is not a valid https url', () => { mockedFetch.mockRejectedValueOnce({ status: 404 }) return expect(didResolver.resolve(did)).rejects.toThrow() @@ -75,10 +79,8 @@ describe('web did resolver', () => { it('fails if the did document is not valid json', () => { mockedFetch.mockResolvedValueOnce({ - json: () => { - throw new Error('unable to parse json') - } - }) + json: () => Promise.reject(new Error('unable to parse json')) + } as Response) return expect(didResolver.resolve(did)).rejects.toThrowError( /unable to parse json/ ) @@ -86,8 +88,8 @@ describe('web did resolver', () => { it('fails if the did document id does not match', () => { mockedFetch.mockResolvedValueOnce({ - json: () => wrongIdResponse - }) + json: () => Promise.resolve(wrongIdResponse) + } as Response) return expect(didResolver.resolve(did)).rejects.toThrowError( 'DID document id does not match requested did' ) @@ -95,8 +97,8 @@ describe('web did resolver', () => { it('fails if the did document has no public keys', () => { mockedFetch.mockResolvedValueOnce({ - json: () => noPublicKeyResponse - }) + json: () => Promise.resolve(noPublicKeyResponse) + } as Response) return expect(didResolver.resolve(did)).rejects.toThrowError( 'DID document has no public keys' ) diff --git a/src/resolver.ts b/src/resolver.ts index 582b64b..107c0c2 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -20,10 +20,9 @@ export function getResolver() { did: string, parsed: ParsedDID ): Promise { - let path = parsed.id + DOC_PATH - const id = parsed.id.split(':'); - if (id.length > 1) path = id.join('/') + '/did.json'; + const id = parsed.id.split(':') + if (id.length > 1) path = id.join('/') + '/did.json' const url: string = `https://${path}` let data: any = null