Skip to content

Commit

Permalink
fix: merge (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook authored Mar 25, 2023
1 parent 88084d6 commit a453a3e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/framework/awsLambda/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
Handler,
} from 'aws-lambda'
import { HTTPMethod } from '../../types'
import { normaliseHttpMethod } from '../../utils'
import { getFromObjectCaseInsensitive, normaliseHttpMethod } from '../../utils'
import { BaseRequest } from '../request'
import { BaseResponse } from '../response'
import { getCookieValueFromHeaders, normalizeHeaderValue, serializeCookieValue } from '../utils'
Expand Down Expand Up @@ -116,7 +116,7 @@ export class AWSRequest extends BaseRequest {
if (this.event.headers === undefined || this.event.headers === null)
return undefined

return normalizeHeaderValue(this.event.headers[key])
return normalizeHeaderValue(getFromObjectCaseInsensitive(key, this.event.headers))
}

getOriginalURL = (): string => {
Expand Down
4 changes: 2 additions & 2 deletions src/framework/fastify/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
FastifyRequest as OriginalFastifyRequest,
} from 'fastify'
import type { HTTPMethod } from '../../types'
import { normaliseHttpMethod } from '../../utils'
import { getFromObjectCaseInsensitive, normaliseHttpMethod } from '../../utils'
import { BaseRequest } from '../request'
import { BaseResponse } from '../response'
import { getCookieValueFromHeaders, normalizeHeaderValue, serializeCookieValue } from '../utils'
Expand Down Expand Up @@ -66,7 +66,7 @@ export class FastifyRequest extends BaseRequest {
}

getHeaderValue = (key: string): string | undefined => {
return normalizeHeaderValue(this.request.headers[key])
return normalizeHeaderValue(getFromObjectCaseInsensitive(key, this.request.headers))
}

getOriginalURL = (): string => {
Expand Down
3 changes: 2 additions & 1 deletion src/framework/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { json, urlencoded } from 'body-parser'
import { NextApiRequest } from 'next'
import STError from '../error'
import type { HTTPMethod } from '../types'
import { getFromObjectCaseInsensitive } from '../utils'
import { COOKIE_HEADER } from './constants'

export function getCookieValueFromHeaders(headers: any, key: string): string | undefined {
Expand Down Expand Up @@ -48,7 +49,7 @@ export function getCookieValueFromIncomingMessage(request: IncomingMessage, key:
}

export function getHeaderValueFromIncomingMessage(request: IncomingMessage, key: string): string | undefined {
return normalizeHeaderValue(request.headers[key])
return normalizeHeaderValue(getFromObjectCaseInsensitive(key, request.headers))
}

export function normalizeHeaderValue(value: string | string[] | undefined): string | undefined {
Expand Down
124 changes: 115 additions & 9 deletions src/recipe/dashboard/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import error from '../../error'
import { APIFunction, APIInterface, APIOptions, RecipeInterface, TypeInput, TypeNormalisedInput } from './types'
import RecipeImplementation from './recipeImplementation'
import APIImplementation from './api/implementation'
import { getApiIdIfMatched, isApiPath, validateAndNormaliseUserInput } from './utils'
import { getApiIdIfMatched, getApiPathWithDashboardBase, isApiPath, validateAndNormaliseUserInput } from './utils'
import {
DASHBOARD_API,
SIGN_IN_API,
Expand Down Expand Up @@ -115,14 +115,120 @@ export default class Recipe extends RecipeModule {

getAPIsHandled = (): APIHandled[] => {
/**
* Normally this array is used by the SDK to decide whether or not the recipe
* handles a specific API path and method and then returns the ID.
*
* For the dashboard recipe this logic is fully custom and handled inside the
* `returnAPIIdIfCanHandleRequest` method of this class. Since this array is never
* used for this recipe, we simply return an empty array.
*/
return []
* Normally this array is used by the SDK to decide whether or not the recipe
* handles a specific API path and method and then returns the ID.
*
* For the dashboard recipe this logic is fully custom and handled inside the
* `returnAPIIdIfCanHandleRequest` method of this class.
*
* For most frameworks this array is redundant because the `returnAPIIdIfCanHandleRequest` is used.
* But for frameworks such as Hapi that require all APIs to be declared up front, this array is used
* to make sure that the framework does not return a 404
*/
return [
{
id: DASHBOARD_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(DASHBOARD_API)),
disabled: false,
method: 'get',
},
{
id: SIGN_IN_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(SIGN_IN_API)),
disabled: false,
method: 'post',
},
{
id: VALIDATE_KEY_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(VALIDATE_KEY_API)),
disabled: false,
method: 'post',
},
{
id: SIGN_OUT_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(SIGN_OUT_API)),
disabled: false,
method: 'post',
},
{
id: USERS_LIST_GET_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USERS_LIST_GET_API)),
disabled: false,
method: 'get',
},
{
id: USERS_COUNT_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USERS_COUNT_API)),
disabled: false,
method: 'get',
},
{
id: USER_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_API)),
disabled: false,
method: 'get',
},
{
id: USER_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_API)),
disabled: false,
method: 'post',
},
{
id: USER_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_API)),
disabled: false,
method: 'delete',
},
{
id: USER_EMAIL_VERIFY_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_EMAIL_VERIFY_API)),
disabled: false,
method: 'get',
},
{
id: USER_EMAIL_VERIFY_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_EMAIL_VERIFY_API)),
disabled: false,
method: 'put',
},
{
id: USER_METADATA_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_METADATA_API)),
disabled: false,
method: 'get',
},
{
id: USER_METADATA_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_METADATA_API)),
disabled: false,
method: 'put',
},
{
id: USER_SESSIONS_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_SESSIONS_API)),
disabled: false,
method: 'get',
},
{
id: USER_SESSIONS_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_SESSIONS_API)),
disabled: false,
method: 'post',
},
{
id: USER_PASSWORD_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_PASSWORD_API)),
disabled: false,
method: 'put',
},
{
id: USER_EMAIL_VERIFY_TOKEN_API,
pathWithoutApiBasePath: new NormalisedURLPath(getApiPathWithDashboardBase(USER_EMAIL_VERIFY_TOKEN_API)),
disabled: false,
method: 'post',
},
]
}

handleAPIRequest = async (
Expand Down
4 changes: 4 additions & 0 deletions src/recipe/dashboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,7 @@ export async function validateApiKey(input: { req: BaseRequest; config: TypeNorm

return apiKeyHeaderValue === input.config.apiKey
}

export function getApiPathWithDashboardBase(path: string): string {
return DASHBOARD_API + path
}
8 changes: 0 additions & 8 deletions src/supertokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import { TypeFramework } from './framework/types'
import STError from './error'
import { logDebugMessage } from './logger'
import { PostSuperTokensInitCallbacks } from './postSuperTokensInitCallbacks'
import DashboardIndex from './recipe/dashboard'
import DashboardRecipe from './recipe/dashboard/recipe'
import { BaseRequest } from './framework/request'
import { BaseResponse } from './framework/response'

Expand Down Expand Up @@ -86,11 +84,6 @@ export default class SuperTokens {
return func(this.appInfo, this.isInServerlessEnv)
})

if (this.recipeModules.filter(i => i.getRecipeId() === DashboardRecipe.RECIPE_ID).length === 0) {
// This means that the user has not initialised the dashboard recipe
this.recipeModules.push(DashboardIndex.init()(this.appInfo, this.isInServerlessEnv))
}

const telemetry = config.telemetry === undefined ? process.env.TEST_MODE !== 'testing' : config.telemetry

if (telemetry) {
Expand Down Expand Up @@ -142,7 +135,6 @@ export default class SuperTokens {
throw new Error('calling testing function in non testing env')

Querier.reset()
DashboardRecipe.reset()
SuperTokens.instance = undefined
}

Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,12 @@ export function getTopLevelDomainForSameSiteResolution(url: string): string {

return parsedURL.domain
}

export function getFromObjectCaseInsensitive<T>(key: string, object: Record<string, T>): T | undefined {
const matchedKeys = Object.keys(object).filter(i => i.toLocaleLowerCase() === key.toLocaleLowerCase())

if (matchedKeys.length === 0)
return undefined

return object[matchedKeys[0]]
}

0 comments on commit a453a3e

Please sign in to comment.