Skip to content

Commit

Permalink
Added type definitions and tests for types (#115)
Browse files Browse the repository at this point in the history
* added type defs

* Fix merge conflict

* Fixed merge conflict

Co-authored-by: Nate Totten <nate@zuplo.com>
  • Loading branch information
radomird and ntotten authored Feb 24, 2022
1 parent 90dd538 commit e26bb57
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"version": "5.0.0",
"description": "Fetch utils for JWKS keys",
"main": "src/get-jwks.js",
"types": "./src/get-jwks.d.ts",
"engines": {
"node": ">=12"
},
"tsd": {
"directory": "./test/types"
},
"scripts": {
"test": "tap test/*.spec.js",
"test": "tap test/*.spec.js && tsd",
"lint": "eslint ."
},
"repository": {
Expand All @@ -30,14 +34,17 @@
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/lru-cache": "^5.1.1",
"eslint": "^8.6.0",
"fast-jwt": "^1.1.2",
"fastify": "^3.12.0",
"fastify-jwt": "^4.0.0",
"jsonwebtoken": "^8.5.1",
"nock": "^13.0.7",
"prettier": "^2.2.1",
"sinon": "^13.0.0",
"tap": "^15.0.2"
"sinon": "^12.0.0",
"tap": "^15.0.2",
"tsd": "^0.19.1",
"typescript": "^4.5.5"
}
}
29 changes: 29 additions & 0 deletions src/get-jwks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import LRU from 'lru-cache'

type JWKSignature = { domain: string; alg: string; kid: string }
type JWK = { [key: string]: any; domain: string; alg: string; kid: string }

type GetPublicKeyOptions = {
domain?: string
alg?: string
kid?: string
}

type GetJwksOptions = {
max?: number
maxAge?: number
allowedDomains?: string[]
providerDiscovery?: boolean
jwksPath?: string
agent?: string
}

type GetJwks = {
getPublicKey: (options?: GetPublicKeyOptions) => Promise<string>
getJwk: (signature: JWKSignature) => Promise<JWK>
getJwksUri: (normalizedDomain: string) => Promise<string>
cache: LRU<string, JWK>
staleCache: LRU<string, JWK>
}

export default function buildGetJwks(options?: GetJwksOptions): GetJwks
11 changes: 11 additions & 0 deletions test/types/get-jwks.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expectAssignable } from 'tsd'
import LRU from 'lru-cache'
import buildGetJwks, { GetPublicKeyOptions, JWK, JWKSignature } from '../../src/get-jwks'

const { getPublicKey, getJwk, getJwksUri, cache, staleCache } = buildGetJwks()

expectAssignable<(signature: JWKSignature) => Promise<JWK>>(getJwk)
expectAssignable<(normalizedDomain: string) => Promise<string>>(getJwksUri)
expectAssignable<(options?: GetPublicKeyOptions) => Promise<string>>(getPublicKey)
expectAssignable<LRU<string, JWK>>(cache)
expectAssignable<LRU<string, JWK>>(staleCache)
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"moduleResolution": "node",
"jsx": "preserve",
"outDir": "lib",
"declarationDir": "types",
"allowJs": false,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true,
"declaration": true
},
"files": ["./test/types/get-jwks.test-d.ts"],
"exclude": ["node_modules"]
}

0 comments on commit e26bb57

Please sign in to comment.