Skip to content

Commit

Permalink
Add client with workspaces.get
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Sep 21, 2023
1 parent 5beaaf9 commit b1323b3
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 14 deletions.
99 changes: 95 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"npm": ">= 8.1.0"
},
"dependencies": {
"@seamapi/types": "^1.12.0"
"@seamapi/types": "^1.13.0",
"axios": "^1.5.0"
},
"devDependencies": {
"@types/node": "^18.11.18",
Expand Down
68 changes: 68 additions & 0 deletions src/lib/seam/connect/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type {
SeamHttpOptionsWithApiKey,
SeamHttpOptionsWithClientSessionToken,
} from './client-options.js'

type Headers = Record<string, string>

export const getAuthHeadersForApiKey = ({
apiKey,
}: SeamHttpOptionsWithApiKey): Headers => {
if (isClientSessionToken(apiKey)) {
throw new InvalidSeamTokenError(
'A Client Session Token cannot be used as an apiKey',
)
}

if (isAccessToken(apiKey)) {
throw new InvalidSeamTokenError(
'An access token cannot be used as an apiKey without specifying a workspaceId',
)
}

if (isJwt(apiKey) || !isSeamToken(apiKey)) {
throw new InvalidSeamTokenError(
`Unknown or Invalid apiKey format, expected token to start with ${tokenPrefix}`,
)
}

return {
authorization: `Bearer ${apiKey}`,
}
}

export const getAuthHeadersForClientSessionToken = ({
clientSessionToken,
}: SeamHttpOptionsWithClientSessionToken): Headers => {
if (!isClientSessionToken(clientSessionToken)) {
throw new InvalidSeamTokenError(
`Unknown or invalid clientSessionToken format, expected token to start with ${clientSessionTokenPrefix}`,
)
}

return {
authorization: `Bearer ${clientSessionToken}`,
'client-session-token': clientSessionToken,
}
}

export class InvalidSeamTokenError extends Error {
constructor(message: string) {
super(`SeamHttp received an authorization invalid token: ${message}`)
this.name = this.constructor.name
Error.captureStackTrace(this, this.constructor)
}
}

const tokenPrefix = 'seam_'

const clientSessionTokenPrefix = 'seam_cst'

const isClientSessionToken = (token: string): boolean =>
token.startsWith(clientSessionTokenPrefix)

const isAccessToken = (token: string): boolean => token.startsWith('seam_at')

const isJwt = (token: string): boolean => token.startsWith('ey')

const isSeamToken = (token: string): boolean => token.startsWith(tokenPrefix)
55 changes: 55 additions & 0 deletions src/lib/seam/connect/client-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { AxiosRequestConfig } from 'axios'

export type SeamHttpOptions =
| SeamHttpOptionsWithApiKey
| SeamHttpOptionsWithClientSessionToken

interface SeamHttpCommonOptions {
endpoint?: string
axiosOptions?: AxiosRequestConfig
}

export interface SeamHttpOptionsWithApiKey extends SeamHttpCommonOptions {
apiKey: string
}

export const isSeamHttpOptionsWithApiKey = (
options: SeamHttpOptions,
): options is SeamHttpOptionsWithApiKey => {
if (!('apiKey' in options)) return false

if ('clientSessionToken' in options && options.clientSessionToken != null) {
throw new InvalidSeamHttpOptionsError(
'The clientSessionToken option cannot be used with the apiKey option.',
)
}

return true
}

export interface SeamHttpOptionsWithClientSessionToken
extends SeamHttpCommonOptions {
clientSessionToken: string
}

export const isSeamHttpOptionsWithClientSessionToken = (
options: SeamHttpOptions,
): options is SeamHttpOptionsWithClientSessionToken => {
if (!('clientSessionToken' in options)) return false

if ('apiKey' in options && options.apiKey != null) {
throw new InvalidSeamHttpOptionsError(
'The clientSessionToken option cannot be used with the apiKey option.',
)
}

return true
}

export class InvalidSeamHttpOptionsError extends Error {
constructor(message: string) {
super(`SeamHttp received invalid options: ${message}`)
this.name = this.constructor.name
Error.captureStackTrace(this, this.constructor)
}
}
Loading

0 comments on commit b1323b3

Please sign in to comment.