Skip to content

Commit

Permalink
refactor: reorganize exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The "oauth1" and "oauth2" clients are exported from
their own sub-modules
  • Loading branch information
thetutlage committed Sep 23, 2023
1 parent 0b4bc30 commit 6f7826b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 35 deletions.
5 changes: 1 addition & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
* file that was distributed with this source code.
*/

export * as errors from './src/errors.js'
export { HttpClient } from './src/http_client.js'
export { UrlBuilder } from './src/url_builder.js'
export { Oauth1Client } from './src/clients/oauth1/main.js'
export { Oauth2Client } from './src/clients/oauth2/main.js'
export { MissingTokenException } from './src/exceptions/missing_token.js'
export { StateMisMatchException } from './src/exceptions/state_mismatch.js'
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
],
"exports": {
".": "./build/index.js",
"./oauth1": "./build/src/clients/oauth1/main.js",
"./oauth2": "./build/src/clients/oauth2/main.js",
"./types": "./build/src/types.js"
},
"engines": {
Expand Down
9 changes: 4 additions & 5 deletions src/clients/oauth1/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import debug from '../../debug.js'
import { Oauth1Signature } from './signature.js'
import { HttpClient } from '../../http_client.js'
import { UrlBuilder } from '../../url_builder.js'
import { MissingTokenException } from '../../exceptions/missing_token.js'
import { StateMisMatchException } from '../../exceptions/state_mismatch.js'
import { E_OAUTH_MISSING_TOKEN, E_OAUTH_STATE_MISMATCH } from '../../errors.js'

/**
* Generic implementation of Oauth1 three leged authorization flow.
Expand Down Expand Up @@ -181,7 +180,7 @@ export class Oauth1Client<Token extends Oauth1AccessToken> {
*/
verifyState(state: string, inputValue?: string) {
if (!state || state !== inputValue) {
throw new StateMisMatchException()
throw new E_OAUTH_STATE_MISMATCH()
}
}

Expand Down Expand Up @@ -226,7 +225,7 @@ export class Oauth1Client<Token extends Oauth1AccessToken> {
* We expect the response to have "oauth_token" and "oauth_token_secret"
*/
if (!oauthToken || !oauthTokenSecret) {
throw new MissingTokenException(MissingTokenException.oauth1Message, { cause: parsed })
throw new E_OAUTH_MISSING_TOKEN(E_OAUTH_MISSING_TOKEN.oauth1Message, { cause: parsed })
}

return {
Expand Down Expand Up @@ -343,7 +342,7 @@ export class Oauth1Client<Token extends Oauth1AccessToken> {
* We expect the response to have "oauth_token" and "oauth_token_secret"
*/
if (!accessOauthToken || !accessOauthTokenSecret) {
throw new MissingTokenException(MissingTokenException.oauth1Message, { cause: parsed })
throw new E_OAUTH_MISSING_TOKEN(E_OAUTH_MISSING_TOKEN.oauth1Message, { cause: parsed })
}

return {
Expand Down
7 changes: 3 additions & 4 deletions src/clients/oauth2/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
import debug from '../../debug.js'
import { HttpClient } from '../../http_client.js'
import { UrlBuilder } from '../../url_builder.js'
import { MissingTokenException } from '../../exceptions/missing_token.js'
import { StateMisMatchException } from '../../exceptions/state_mismatch.js'
import { E_OAUTH_MISSING_TOKEN, E_OAUTH_STATE_MISMATCH } from '../../errors.js'

/**
* Generic implementation of OAuth2.
Expand Down Expand Up @@ -140,7 +139,7 @@ export class Oauth2Client<Token extends Oauth2AccessToken> {
*/
verifyState(state: string, inputValue?: string) {
if (!state || state !== inputValue) {
throw new StateMisMatchException()
throw new E_OAUTH_STATE_MISMATCH()
}
}

Expand Down Expand Up @@ -216,7 +215,7 @@ export class Oauth2Client<Token extends Oauth2AccessToken> {
* We expect the response to have "access_token"
*/
if (!accessToken) {
throw new MissingTokenException(MissingTokenException.oauth2Message, { cause: parsed })
throw new E_OAUTH_MISSING_TOKEN(E_OAUTH_MISSING_TOKEN.oauth2Message, { cause: parsed })
}

return {
Expand Down
13 changes: 11 additions & 2 deletions src/exceptions/missing_token.ts → src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import { createError, Exception } from '@poppinss/utils'

/**
* Raised when unable to get access token for oauth2 or oauth token and secret
* for oauth1
*/
export class MissingTokenException extends Exception {
export const E_OAUTH_MISSING_TOKEN = class MissingTokenException extends Exception {
static status = 400
static code = 'E_OAUTH_MISSING_TOKEN'
static oauth2Message = 'Invalid oauth2 response. Missing "access_token"'
static oauth1Message = 'Invalid oauth1 response. Missing "oauth_token" and "oauth_token_secret"'
}

/**
* Raised when unable to verify the CSRF state post redirect
*/
export const E_OAUTH_STATE_MISMATCH = createError(
'Unable to verify re-redirect state',
'E_OAUTH_STATE_MISMATCH',
400
)
19 changes: 0 additions & 19 deletions src/exceptions/state_mismatch.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import got, { type CancelableRequest, type Response } from 'got'

import type { ApiRequestContract } from './types.js'
import debug from './debug.js'
import type { ApiRequestContract } from './types.js'

/**
* An HTTP client abstraction we need for making OAuth requests
Expand Down

0 comments on commit 6f7826b

Please sign in to comment.