Skip to content

Commit

Permalink
refactor: add explicit return types everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jun 3, 2024
1 parent 89a2604 commit 5601591
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/jwe/compact/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class CompactEncrypt {
*
* @param cek JWE Content Encryption Key.
*/
setContentEncryptionKey(cek: Uint8Array) {
setContentEncryptionKey(cek: Uint8Array): this {
this._flattened.setContentEncryptionKey(cek)
return this
}
Expand All @@ -52,7 +52,7 @@ export class CompactEncrypt {
*
* @param iv JWE Initialization Vector.
*/
setInitializationVector(iv: Uint8Array) {
setInitializationVector(iv: Uint8Array): this {
this._flattened.setInitializationVector(iv)
return this
}
Expand All @@ -62,7 +62,7 @@ export class CompactEncrypt {
*
* @param protectedHeader JWE Protected Header object.
*/
setProtectedHeader(protectedHeader: CompactJWEHeaderParameters) {
setProtectedHeader(protectedHeader: CompactJWEHeaderParameters): this {
this._flattened.setProtectedHeader(protectedHeader)
return this
}
Expand All @@ -74,7 +74,7 @@ export class CompactEncrypt {
*
* @param parameters JWE Key Management parameters.
*/
setKeyManagementParameters(parameters: JWEKeyManagementHeaderParameters) {
setKeyManagementParameters(parameters: JWEKeyManagementHeaderParameters): this {
this._flattened.setKeyManagementParameters(parameters)
return this
}
Expand Down
14 changes: 7 additions & 7 deletions src/jwe/flattened/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class FlattenedEncrypt {
*
* @param parameters JWE Key Management parameters.
*/
setKeyManagementParameters(parameters: JWEKeyManagementHeaderParameters) {
setKeyManagementParameters(parameters: JWEKeyManagementHeaderParameters): this {
if (this._keyManagementParameters) {
throw new TypeError('setKeyManagementParameters can only be called once')
}
Expand All @@ -79,7 +79,7 @@ export class FlattenedEncrypt {
*
* @param protectedHeader JWE Protected Header.
*/
setProtectedHeader(protectedHeader: JWEHeaderParameters) {
setProtectedHeader(protectedHeader: JWEHeaderParameters): this {
if (this._protectedHeader) {
throw new TypeError('setProtectedHeader can only be called once')
}
Expand All @@ -92,7 +92,7 @@ export class FlattenedEncrypt {
*
* @param sharedUnprotectedHeader JWE Shared Unprotected Header.
*/
setSharedUnprotectedHeader(sharedUnprotectedHeader: JWEHeaderParameters) {
setSharedUnprotectedHeader(sharedUnprotectedHeader: JWEHeaderParameters): this {
if (this._sharedUnprotectedHeader) {
throw new TypeError('setSharedUnprotectedHeader can only be called once')
}
Expand All @@ -105,7 +105,7 @@ export class FlattenedEncrypt {
*
* @param unprotectedHeader JWE Per-Recipient Unprotected Header.
*/
setUnprotectedHeader(unprotectedHeader: JWEHeaderParameters) {
setUnprotectedHeader(unprotectedHeader: JWEHeaderParameters): this {
if (this._unprotectedHeader) {
throw new TypeError('setUnprotectedHeader can only be called once')
}
Expand All @@ -118,7 +118,7 @@ export class FlattenedEncrypt {
*
* @param aad Additional Authenticated Data.
*/
setAdditionalAuthenticatedData(aad: Uint8Array) {
setAdditionalAuthenticatedData(aad: Uint8Array): this {
this._aad = aad
return this
}
Expand All @@ -132,7 +132,7 @@ export class FlattenedEncrypt {
*
* @param cek JWE Content Encryption Key.
*/
setContentEncryptionKey(cek: Uint8Array) {
setContentEncryptionKey(cek: Uint8Array): this {
if (this._cek) {
throw new TypeError('setContentEncryptionKey can only be called once')
}
Expand All @@ -149,7 +149,7 @@ export class FlattenedEncrypt {
*
* @param iv JWE Initialization Vector.
*/
setInitializationVector(iv: Uint8Array) {
setInitializationVector(iv: Uint8Array): this {
if (this._iv) {
throw new TypeError('setInitializationVector can only be called once')
}
Expand Down
2 changes: 1 addition & 1 deletion src/jwe/general/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class GeneralEncrypt {
*
* @param aad Additional Authenticated Data.
*/
setAdditionalAuthenticatedData(aad: Uint8Array) {
setAdditionalAuthenticatedData(aad: Uint8Array): this {
this._aad = aad
return this
}
Expand Down
4 changes: 3 additions & 1 deletion src/jwks/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ async function importWithAlgCache<KeyLikeType extends KeyLike = KeyLike>(
*
* @param jwks JSON Web Key Set formatted object.
*/
export function createLocalJWKSet<KeyLikeType extends KeyLike = KeyLike>(jwks: JSONWebKeySet) {
export function createLocalJWKSet<KeyLikeType extends KeyLike = KeyLike>(
jwks: JSONWebKeySet,
): (protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput) => Promise<KeyLikeType> {
const set = new LocalJWKSet<KeyLikeType>(jwks)

const localJWKSet = async (
Expand Down
2 changes: 1 addition & 1 deletion src/jws/compact/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CompactSign {
*
* @param protectedHeader JWS Protected Header.
*/
setProtectedHeader(protectedHeader: CompactJWSHeaderParameters) {
setProtectedHeader(protectedHeader: CompactJWSHeaderParameters): this {
this._flattened.setProtectedHeader(protectedHeader)
return this
}
Expand Down
4 changes: 2 additions & 2 deletions src/jws/flattened/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class FlattenedSign {
*
* @param protectedHeader JWS Protected Header.
*/
setProtectedHeader(protectedHeader: JWSHeaderParameters) {
setProtectedHeader(protectedHeader: JWSHeaderParameters): this {
if (this._protectedHeader) {
throw new TypeError('setProtectedHeader can only be called once')
}
Expand All @@ -56,7 +56,7 @@ export class FlattenedSign {
*
* @param unprotectedHeader JWS Unprotected Header.
*/
setUnprotectedHeader(unprotectedHeader: JWSHeaderParameters) {
setUnprotectedHeader(unprotectedHeader: JWSHeaderParameters): this {
if (this._unprotectedHeader) {
throw new TypeError('setUnprotectedHeader can only be called once')
}
Expand Down
14 changes: 7 additions & 7 deletions src/jwt/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class EncryptJWT extends ProduceJWT {
* @param protectedHeader JWE Protected Header. Must contain an "alg" (JWE Algorithm) and "enc"
* (JWE Encryption Algorithm) properties.
*/
setProtectedHeader(protectedHeader: CompactJWEHeaderParameters) {
setProtectedHeader(protectedHeader: CompactJWEHeaderParameters): this {
if (this._protectedHeader) {
throw new TypeError('setProtectedHeader can only be called once')
}
Expand All @@ -63,7 +63,7 @@ export class EncryptJWT extends ProduceJWT {
*
* @param parameters JWE Key Management parameters.
*/
setKeyManagementParameters(parameters: JWEKeyManagementHeaderParameters) {
setKeyManagementParameters(parameters: JWEKeyManagementHeaderParameters): this {
if (this._keyManagementParameters) {
throw new TypeError('setKeyManagementParameters can only be called once')
}
Expand All @@ -80,7 +80,7 @@ export class EncryptJWT extends ProduceJWT {
*
* @param cek JWE Content Encryption Key.
*/
setContentEncryptionKey(cek: Uint8Array) {
setContentEncryptionKey(cek: Uint8Array): this {
if (this._cek) {
throw new TypeError('setContentEncryptionKey can only be called once')
}
Expand All @@ -97,7 +97,7 @@ export class EncryptJWT extends ProduceJWT {
*
* @param iv JWE Initialization Vector.
*/
setInitializationVector(iv: Uint8Array) {
setInitializationVector(iv: Uint8Array): this {
if (this._iv) {
throw new TypeError('setInitializationVector can only be called once')
}
Expand All @@ -110,7 +110,7 @@ export class EncryptJWT extends ProduceJWT {
*
* @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-5.3 RFC7519#section-5.3}
*/
replicateIssuerAsHeader() {
replicateIssuerAsHeader(): this {
this._replicateIssuerAsHeader = true
return this
}
Expand All @@ -120,7 +120,7 @@ export class EncryptJWT extends ProduceJWT {
*
* @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-5.3 RFC7519#section-5.3}
*/
replicateSubjectAsHeader() {
replicateSubjectAsHeader(): this {
this._replicateSubjectAsHeader = true
return this
}
Expand All @@ -130,7 +130,7 @@ export class EncryptJWT extends ProduceJWT {
*
* @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-5.3 RFC7519#section-5.3}
*/
replicateAudienceAsHeader() {
replicateAudienceAsHeader(): this {
this._replicateAudienceAsHeader = true
return this
}
Expand Down
14 changes: 7 additions & 7 deletions src/jwt/produce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ProduceJWT {
*
* @param issuer "Issuer" Claim value to set on the JWT Claims Set.
*/
setIssuer(issuer: string) {
setIssuer(issuer: string): this {
this._payload = { ...this._payload, iss: issuer }
return this
}
Expand All @@ -38,7 +38,7 @@ export class ProduceJWT {
*
* @param subject "sub" (Subject) Claim value to set on the JWT Claims Set.
*/
setSubject(subject: string) {
setSubject(subject: string): this {
this._payload = { ...this._payload, sub: subject }
return this
}
Expand All @@ -48,7 +48,7 @@ export class ProduceJWT {
*
* @param audience "aud" (Audience) Claim value to set on the JWT Claims Set.
*/
setAudience(audience: string | string[]) {
setAudience(audience: string | string[]): this {
this._payload = { ...this._payload, aud: audience }
return this
}
Expand All @@ -58,7 +58,7 @@ export class ProduceJWT {
*
* @param jwtId "jti" (JWT ID) Claim value to set on the JWT Claims Set.
*/
setJti(jwtId: string) {
setJti(jwtId: string): this {
this._payload = { ...this._payload, jti: jwtId }
return this
}
Expand Down Expand Up @@ -86,7 +86,7 @@ export class ProduceJWT {
*
* @param input "nbf" (Not Before) Claim value to set on the JWT Claims Set.
*/
setNotBefore(input: number | string | Date) {
setNotBefore(input: number | string | Date): this {
if (typeof input === 'number') {
this._payload = { ...this._payload, nbf: validateInput('setNotBefore', input) }
} else if (input instanceof Date) {
Expand Down Expand Up @@ -120,7 +120,7 @@ export class ProduceJWT {
*
* @param input "exp" (Expiration Time) Claim value to set on the JWT Claims Set.
*/
setExpirationTime(input: number | string | Date) {
setExpirationTime(input: number | string | Date): this {
if (typeof input === 'number') {
this._payload = { ...this._payload, exp: validateInput('setExpirationTime', input) }
} else if (input instanceof Date) {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class ProduceJWT {
*
* @param input "iat" (Expiration Time) Claim value to set on the JWT Claims Set.
*/
setIssuedAt(input?: number | string | Date) {
setIssuedAt(input?: number | string | Date): this {
if (typeof input === 'undefined') {
this._payload = { ...this._payload, iat: epoch(new Date()) }
} else if (input instanceof Date) {
Expand Down
2 changes: 1 addition & 1 deletion src/jwt/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class SignJWT extends ProduceJWT {
*
* @param protectedHeader JWS Protected Header. Must contain an "alg" (JWS Algorithm) property.
*/
setProtectedHeader(protectedHeader: JWTHeaderParameters) {
setProtectedHeader(protectedHeader: JWTHeaderParameters): this {
this._protectedHeader = protectedHeader
return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/decode_protected_header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ProtectedHeaderParameters = JWSHeaderParameters & JWEHeaderParameter
*
* @param token JWE/JWS/JWT token in any JOSE serialization.
*/
export function decodeProtectedHeader(token: string | object) {
export function decodeProtectedHeader(token: string | object): ProtectedHeaderParameters {
let protectedB64u!: unknown

if (typeof token === 'string') {
Expand Down

0 comments on commit 5601591

Please sign in to comment.