|
| 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 | + /** @deprecated Use the instance `.code` property. */ |
| 15 | + errorCode: string; |
| 16 | +}; |
| 17 | + |
| 18 | +function sub(code: OAuthErrorCode, name: string): OAuthErrorSubclass { |
| 19 | + const Sub = class extends OAuthError { |
| 20 | + static errorCode = code; |
| 21 | + constructor(message: string, errorUri?: string) { |
| 22 | + super(code, message, errorUri); |
| 23 | + this.name = name; |
| 24 | + } |
| 25 | + }; |
| 26 | + Object.defineProperty(Sub, 'name', { value: name, configurable: true }); |
| 27 | + return Sub; |
| 28 | +} |
| 29 | + |
| 30 | +/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/no-redeclare */ |
| 31 | + |
| 32 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidRequest`. */ |
| 33 | +export const InvalidRequestError = sub(OAuthErrorCode.InvalidRequest, 'InvalidRequestError'); |
| 34 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidRequest`. */ |
| 35 | +export type InvalidRequestError = InstanceType<typeof InvalidRequestError>; |
| 36 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidClient`. */ |
| 37 | +export const InvalidClientError = sub(OAuthErrorCode.InvalidClient, 'InvalidClientError'); |
| 38 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidClient`. */ |
| 39 | +export type InvalidClientError = InstanceType<typeof InvalidClientError>; |
| 40 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidGrant`. */ |
| 41 | +export const InvalidGrantError = sub(OAuthErrorCode.InvalidGrant, 'InvalidGrantError'); |
| 42 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidGrant`. */ |
| 43 | +export type InvalidGrantError = InstanceType<typeof InvalidGrantError>; |
| 44 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnauthorizedClient`. */ |
| 45 | +export const UnauthorizedClientError = sub(OAuthErrorCode.UnauthorizedClient, 'UnauthorizedClientError'); |
| 46 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnauthorizedClient`. */ |
| 47 | +export type UnauthorizedClientError = InstanceType<typeof UnauthorizedClientError>; |
| 48 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnsupportedGrantType`. */ |
| 49 | +export const UnsupportedGrantTypeError = sub(OAuthErrorCode.UnsupportedGrantType, 'UnsupportedGrantTypeError'); |
| 50 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnsupportedGrantType`. */ |
| 51 | +export type UnsupportedGrantTypeError = InstanceType<typeof UnsupportedGrantTypeError>; |
| 52 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidScope`. */ |
| 53 | +export const InvalidScopeError = sub(OAuthErrorCode.InvalidScope, 'InvalidScopeError'); |
| 54 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidScope`. */ |
| 55 | +export type InvalidScopeError = InstanceType<typeof InvalidScopeError>; |
| 56 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.AccessDenied`. */ |
| 57 | +export const AccessDeniedError = sub(OAuthErrorCode.AccessDenied, 'AccessDeniedError'); |
| 58 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.AccessDenied`. */ |
| 59 | +export type AccessDeniedError = InstanceType<typeof AccessDeniedError>; |
| 60 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.ServerError`. */ |
| 61 | +export const ServerError = sub(OAuthErrorCode.ServerError, 'ServerError'); |
| 62 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.ServerError`. */ |
| 63 | +export type ServerError = InstanceType<typeof ServerError>; |
| 64 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.TemporarilyUnavailable`. */ |
| 65 | +export const TemporarilyUnavailableError = sub(OAuthErrorCode.TemporarilyUnavailable, 'TemporarilyUnavailableError'); |
| 66 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.TemporarilyUnavailable`. */ |
| 67 | +export type TemporarilyUnavailableError = InstanceType<typeof TemporarilyUnavailableError>; |
| 68 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnsupportedResponseType`. */ |
| 69 | +export const UnsupportedResponseTypeError = sub(OAuthErrorCode.UnsupportedResponseType, 'UnsupportedResponseTypeError'); |
| 70 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnsupportedResponseType`. */ |
| 71 | +export type UnsupportedResponseTypeError = InstanceType<typeof UnsupportedResponseTypeError>; |
| 72 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnsupportedTokenType`. */ |
| 73 | +export const UnsupportedTokenTypeError = sub(OAuthErrorCode.UnsupportedTokenType, 'UnsupportedTokenTypeError'); |
| 74 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.UnsupportedTokenType`. */ |
| 75 | +export type UnsupportedTokenTypeError = InstanceType<typeof UnsupportedTokenTypeError>; |
| 76 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidToken`. */ |
| 77 | +export const InvalidTokenError = sub(OAuthErrorCode.InvalidToken, 'InvalidTokenError'); |
| 78 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidToken`. */ |
| 79 | +export type InvalidTokenError = InstanceType<typeof InvalidTokenError>; |
| 80 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.MethodNotAllowed`. */ |
| 81 | +export const MethodNotAllowedError = sub(OAuthErrorCode.MethodNotAllowed, 'MethodNotAllowedError'); |
| 82 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.MethodNotAllowed`. */ |
| 83 | +export type MethodNotAllowedError = InstanceType<typeof MethodNotAllowedError>; |
| 84 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.TooManyRequests`. */ |
| 85 | +export const TooManyRequestsError = sub(OAuthErrorCode.TooManyRequests, 'TooManyRequestsError'); |
| 86 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.TooManyRequests`. */ |
| 87 | +export type TooManyRequestsError = InstanceType<typeof TooManyRequestsError>; |
| 88 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidClientMetadata`. */ |
| 89 | +export const InvalidClientMetadataError = sub(OAuthErrorCode.InvalidClientMetadata, 'InvalidClientMetadataError'); |
| 90 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidClientMetadata`. */ |
| 91 | +export type InvalidClientMetadataError = InstanceType<typeof InvalidClientMetadataError>; |
| 92 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InsufficientScope`. */ |
| 93 | +export const InsufficientScopeError = sub(OAuthErrorCode.InsufficientScope, 'InsufficientScopeError'); |
| 94 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InsufficientScope`. */ |
| 95 | +export type InsufficientScopeError = InstanceType<typeof InsufficientScopeError>; |
| 96 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidTarget`. */ |
| 97 | +export const InvalidTargetError = sub(OAuthErrorCode.InvalidTarget, 'InvalidTargetError'); |
| 98 | +/** @deprecated Use `OAuthError` with `OAuthErrorCode.InvalidTarget`. */ |
| 99 | +export type InvalidTargetError = InstanceType<typeof InvalidTargetError>; |
| 100 | + |
| 101 | +/** |
| 102 | + * @deprecated Construct {@link OAuthError} directly with a custom code string. |
| 103 | + * |
| 104 | + * v1 pattern was `class MyErr extends CustomOAuthError { static errorCode = 'my_code' }`; |
| 105 | + * this preserves that by reading `static errorCode` from the concrete subclass. |
| 106 | + */ |
| 107 | +export class CustomOAuthError extends OAuthError { |
| 108 | + static errorCode: string; |
| 109 | + constructor(message: string, errorUri?: string) { |
| 110 | + super((new.target as typeof CustomOAuthError).errorCode, message, errorUri); |
| 111 | + } |
| 112 | +} |
0 commit comments