|
| 1 | +/** |
| 2 | + * v1-compat: OAuth error subclasses. |
| 3 | + * |
| 4 | + * v1 shipped one `Error` subclass per OAuth error code (e.g. `InvalidTokenError`). |
| 5 | + * v2 also exposes the consolidated {@link OAuthError} + {@link OAuthErrorCode} enum. |
| 6 | + * These thin wrappers preserve `throw new InvalidTokenError(msg)` and `instanceof` |
| 7 | + * patterns from v1 and set `.code` to the matching enum value. |
| 8 | + */ |
| 9 | + |
| 10 | +import { OAuthError, OAuthErrorCode } from '../auth/errors.js'; |
| 11 | + |
| 12 | +type OAuthErrorSubclass = { |
| 13 | + new (message: string, errorUri?: string): OAuthError; |
| 14 | + /** v1 static field. v2-preferred is the instance `.code` property. */ |
| 15 | + errorCode: string; |
| 16 | +}; |
| 17 | + |
| 18 | +function sub(code: OAuthErrorCode, name: string): OAuthErrorSubclass { |
| 19 | + return class extends OAuthError { |
| 20 | + static errorCode = code as string; |
| 21 | + constructor(message: string, errorUri?: string) { |
| 22 | + super(code, message, errorUri); |
| 23 | + this.name = name; |
| 24 | + } |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 29 | + |
| 30 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidRequest, ...)`. */ |
| 31 | +export const InvalidRequestError = sub(OAuthErrorCode.InvalidRequest, 'InvalidRequestError'); |
| 32 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidClient, ...)`. */ |
| 33 | +export const InvalidClientError = sub(OAuthErrorCode.InvalidClient, 'InvalidClientError'); |
| 34 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidGrant, ...)`. */ |
| 35 | +export const InvalidGrantError = sub(OAuthErrorCode.InvalidGrant, 'InvalidGrantError'); |
| 36 | +/** Equivalent to `new OAuthError(OAuthErrorCode.UnauthorizedClient, ...)`. */ |
| 37 | +export const UnauthorizedClientError = sub(OAuthErrorCode.UnauthorizedClient, 'UnauthorizedClientError'); |
| 38 | +/** Equivalent to `new OAuthError(OAuthErrorCode.UnsupportedGrantType, ...)`. */ |
| 39 | +export const UnsupportedGrantTypeError = sub(OAuthErrorCode.UnsupportedGrantType, 'UnsupportedGrantTypeError'); |
| 40 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidScope, ...)`. */ |
| 41 | +export const InvalidScopeError = sub(OAuthErrorCode.InvalidScope, 'InvalidScopeError'); |
| 42 | +/** Equivalent to `new OAuthError(OAuthErrorCode.AccessDenied, ...)`. */ |
| 43 | +export const AccessDeniedError = sub(OAuthErrorCode.AccessDenied, 'AccessDeniedError'); |
| 44 | +/** Equivalent to `new OAuthError(OAuthErrorCode.ServerError, ...)`. */ |
| 45 | +export const ServerError = sub(OAuthErrorCode.ServerError, 'ServerError'); |
| 46 | +/** Equivalent to `new OAuthError(OAuthErrorCode.TemporarilyUnavailable, ...)`. */ |
| 47 | +export const TemporarilyUnavailableError = sub(OAuthErrorCode.TemporarilyUnavailable, 'TemporarilyUnavailableError'); |
| 48 | +/** Equivalent to `new OAuthError(OAuthErrorCode.UnsupportedResponseType, ...)`. */ |
| 49 | +export const UnsupportedResponseTypeError = sub(OAuthErrorCode.UnsupportedResponseType, 'UnsupportedResponseTypeError'); |
| 50 | +/** Equivalent to `new OAuthError(OAuthErrorCode.UnsupportedTokenType, ...)`. */ |
| 51 | +export const UnsupportedTokenTypeError = sub(OAuthErrorCode.UnsupportedTokenType, 'UnsupportedTokenTypeError'); |
| 52 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidToken, ...)`. */ |
| 53 | +export const InvalidTokenError = sub(OAuthErrorCode.InvalidToken, 'InvalidTokenError'); |
| 54 | +/** Equivalent to `new OAuthError(OAuthErrorCode.MethodNotAllowed, ...)`. */ |
| 55 | +export const MethodNotAllowedError = sub(OAuthErrorCode.MethodNotAllowed, 'MethodNotAllowedError'); |
| 56 | +/** Equivalent to `new OAuthError(OAuthErrorCode.TooManyRequests, ...)`. */ |
| 57 | +export const TooManyRequestsError = sub(OAuthErrorCode.TooManyRequests, 'TooManyRequestsError'); |
| 58 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidClientMetadata, ...)`. */ |
| 59 | +export const InvalidClientMetadataError = sub(OAuthErrorCode.InvalidClientMetadata, 'InvalidClientMetadataError'); |
| 60 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InsufficientScope, ...)`. */ |
| 61 | +export const InsufficientScopeError = sub(OAuthErrorCode.InsufficientScope, 'InsufficientScopeError'); |
| 62 | +/** Equivalent to `new OAuthError(OAuthErrorCode.InvalidTarget, ...)`. */ |
| 63 | +export const InvalidTargetError = sub(OAuthErrorCode.InvalidTarget, 'InvalidTargetError'); |
| 64 | + |
| 65 | +/** |
| 66 | + * v1 base class for custom OAuth error codes. |
| 67 | + * |
| 68 | + * v1 pattern was `class MyErr extends CustomOAuthError { static errorCode = 'my_code' }`; |
| 69 | + * this preserves that by reading `static errorCode` from the concrete subclass. |
| 70 | + * v2-preferred is to construct {@link OAuthError} directly with a custom code string. |
| 71 | + */ |
| 72 | +export class CustomOAuthError extends OAuthError { |
| 73 | + static errorCode: string; |
| 74 | + constructor(message: string, errorUri?: string) { |
| 75 | + super((new.target as typeof CustomOAuthError).errorCode, message, errorUri); |
| 76 | + } |
| 77 | +} |
0 commit comments