add generics to sign
This commit is contained in:
@@ -14,6 +14,11 @@ type Data = {
|
|||||||
[key in JwtAlgorithm]: Dataset
|
[key in JwtAlgorithm]: Dataset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Payload = {
|
||||||
|
sub: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
const data: Data = {
|
const data: Data = {
|
||||||
'ES256': {
|
'ES256': {
|
||||||
public: '-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9\nq9UU8I5mEovUf86QZ7kOBIjJwqnzD1omageEHWwHdBO6B+dFabmdT9POxg==\n-----END PUBLIC KEY-----',
|
public: '-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9\nq9UU8I5mEovUf86QZ7kOBIjJwqnzD1omageEHWwHdBO6B+dFabmdT9POxg==\n-----END PUBLIC KEY-----',
|
||||||
@@ -63,7 +68,7 @@ const data: Data = {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = {
|
const payload: Payload = {
|
||||||
sub: "1234567890",
|
sub: "1234567890",
|
||||||
name: "John Doe",
|
name: "John Doe",
|
||||||
}
|
}
|
||||||
@@ -77,7 +82,7 @@ describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorith
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('decode external', async () => {
|
test('decode external', async () => {
|
||||||
const decoded = jwt.decode(data.token)
|
const decoded = jwt.decode<Payload>(data.token)
|
||||||
expect({
|
expect({
|
||||||
sub: payload.sub,
|
sub: payload.sub,
|
||||||
name: payload.name
|
name: payload.name
|
||||||
@@ -88,7 +93,7 @@ describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorith
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('sign internal', async () => {
|
test('sign internal', async () => {
|
||||||
token = await jwt.sign(payload, data.private, algorithm)
|
token = await jwt.sign<Payload>(payload, data.private, algorithm)
|
||||||
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
|
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ function decodePayload<T = any>(raw: string): T | undefined {
|
|||||||
* @throws {Error} If there's a validation issue.
|
* @throws {Error} If there's a validation issue.
|
||||||
* @returns {Promise<string>} Returns token as a `string`.
|
* @returns {Promise<string>} Returns token as a `string`.
|
||||||
*/
|
*/
|
||||||
export async function sign(payload: JwtPayload, secret: string | JsonWebKey, options: JwtSignOptions | JwtAlgorithm = 'HS256'): Promise<string> {
|
export async function sign<Payload = {}>(payload: JwtPayload<Payload>, secret: string | JsonWebKey, options: JwtSignOptions | JwtAlgorithm = 'HS256'): Promise<string> {
|
||||||
if (typeof options === 'string')
|
if (typeof options === 'string')
|
||||||
options = { algorithm: options }
|
options = { algorithm: options }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user