-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Introduce
createIsomorphicRequest
(#1393)
* feat(backend): Introduce createIsomorphicRequest Introducing the new utility `createIsomorphicRequest` for the `@clerk/backend` package, so that the `authenticateRequest` signature will be more simplified, and it will be easier to integrate with more frameworks. * fix(backend): Align build scripts * fix(backend): Correctly export fetch apis using subpath imports * feat(backend): Expose callback with Request and Headers as a param of createIsomorphicRequest chore(backend): Add changeset for `createIsomorphicRequest` refactor(clerk-sdk-node,backend): Remove sdk-node cookie dependency Also refactor authenticateRequest options fix(backend): Refactor the new IsomorphicRequest utilities Also, manipulate headers objects to be compatible with Headers constructor chore(repo): Revert `package-lock.json` changes --------- Co-authored-by: Nikos Douvlis <nikosdouvlis@gmail.com>
- Loading branch information
1 parent
949959f
commit fd692af
Showing
26 changed files
with
350 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
'gatsby-plugin-clerk': minor | ||
'@clerk/clerk-sdk-node': minor | ||
'@clerk/backend': minor | ||
'@clerk/fastify': minor | ||
'@clerk/nextjs': minor | ||
'@clerk/remix': minor | ||
--- | ||
|
||
Introduce `createIsomorphicRequest` in `@clerk/backend` | ||
|
||
This utility simplifies the `authenticateRequest` signature, and it makes it easier to integrate with more frameworks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
export default fetch; | ||
export const RuntimeBlob = Blob; | ||
export const RuntimeFormData = FormData; | ||
export const RuntimeHeaders = Headers; | ||
export const RuntimeRequest = Request; | ||
export const RuntimeResponse = Response; | ||
export const RuntimeAbortController = AbortController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
const fetch = require('node-fetch-native'); | ||
const { fetch, Blob, FormData, Headers, Request, Response, AbortController } = require('node-fetch-native'); | ||
|
||
module.exports = fetch; | ||
module.exports.RuntimeBlob = Blob; | ||
module.exports.RuntimeFormData = FormData; | ||
module.exports.RuntimeHeaders = Headers; | ||
module.exports.RuntimeRequest = Request; | ||
module.exports.RuntimeResponse = Response; | ||
module.exports.RuntimeAbortController = AbortController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
export * from './authObjects'; | ||
export * from './factory'; | ||
export { RequestState, AuthStatus } from './authStatus'; | ||
export type { RequestState } from './authStatus'; | ||
export { AuthStatus } from './authStatus'; | ||
export { loadInterstitialFromLocal } from './interstitial'; | ||
export { | ||
debugRequestState, | ||
AuthenticateRequestOptions, | ||
OptionalVerifyTokenOptions, | ||
RequiredVerifyTokenOptions, | ||
} from './request'; | ||
export type { AuthenticateRequestOptions, OptionalVerifyTokenOptions, RequiredVerifyTokenOptions } from './request'; | ||
export { debugRequestState } from './request'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { parse } from 'cookie'; | ||
|
||
import runtime from '../runtime'; | ||
|
||
type IsomorphicRequestOptions = (Request: typeof runtime.Request, Headers: typeof runtime.Headers) => Request; | ||
export const createIsomorphicRequest = (cb: IsomorphicRequestOptions): Request => { | ||
return cb(runtime.Request, runtime.Headers); | ||
}; | ||
|
||
export const buildRequest = (req?: Request) => { | ||
if (!req) { | ||
return {}; | ||
} | ||
const cookies = parseIsomorphicRequestCookies(req); | ||
const headers = getHeaderFromIsomorphicRequest(req); | ||
const searchParams = getSearchParamsFromIsomorphicRequest(req); | ||
|
||
return { | ||
cookies, | ||
headers, | ||
searchParams, | ||
}; | ||
}; | ||
|
||
const decode = (str: string): string => { | ||
if (!str) { | ||
return str; | ||
} | ||
return str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent); | ||
}; | ||
|
||
const parseIsomorphicRequestCookies = (req: Request) => { | ||
const cookies = req.headers && req.headers?.get('cookie') ? parse(req.headers.get('cookie') as string) : {}; | ||
return (key: string): string | undefined => { | ||
const value = cookies?.[key]; | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
return decode(value); | ||
}; | ||
}; | ||
|
||
const getHeaderFromIsomorphicRequest = (req: Request) => (key: string) => req?.headers?.get(key) || undefined; | ||
|
||
const getSearchParamsFromIsomorphicRequest = (req: Request) => (req?.url ? new URL(req.url)?.searchParams : undefined); | ||
|
||
export const stripAuthorizationHeader = (authValue: string | undefined | null): string | undefined => { | ||
return authValue?.replace('Bearer ', ''); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"skipLibCheck": true, | ||
"noEmit": false, | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"declarationMap": true, | ||
"sourceMap": false, | ||
"declarationDir": "./dist/types" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"baseUrl": "src", | ||
"baseUrl": ".", | ||
"declaration": true, | ||
"declarationDir": "dist/types", | ||
"declarationMap": true, | ||
"emitDeclarationOnly": true, | ||
"declarationMap": false, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"importHelpers": true, | ||
"moduleResolution": "node", | ||
"noUnusedLocals": true, | ||
"noImplicitReturns": true, | ||
"noUnusedLocals": false, | ||
"noUnusedParameters": true, | ||
"removeComments": true, | ||
"outDir": "dist", | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"sourceMap": false, | ||
"strict": true, | ||
"target": "ES2020" | ||
"target": "ES2020", | ||
"isolatedModules": true | ||
}, | ||
"include": ["src", "global.d.ts"], | ||
"exclude": ["node_modules", "dist", "/src/runtime/*", "src/**/*.spec.ts", "src/**/*.test.ts", "src/__tests__"] | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist", "/src/runtime/*", "src/**/*.spec.ts", "src/**/*.test.ts", "src/tests"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,45 @@ | ||
import type { Options } from 'tsup'; | ||
import { defineConfig } from 'tsup'; | ||
|
||
import { runAfterLast } from '../../scripts/utils'; | ||
// @ts-ignore | ||
import { name, version } from './package.json'; | ||
|
||
export default defineConfig(overrideOptions => { | ||
const isProd = overrideOptions.env?.NODE_ENV === 'production'; | ||
const isWatch = !!overrideOptions.watch; | ||
const shouldPublish = !!overrideOptions.env?.publish; | ||
|
||
return { | ||
// const onSuccess = (format: 'esm' | 'cjs') => { | ||
// const rsync = `rsync -r --include '*/' --include '*.js' --include '*.mjs' --include '*.cjs' --exclude='*' ./src/runtime ./dist/${format}/`; | ||
// // return `cp ./package.${format}.json ./dist/${format}/package.json && ${rsync}`; | ||
// return rsync; | ||
// }; | ||
|
||
const common: Options = { | ||
entry: ['src/index.ts'], | ||
onSuccess: 'tsc && npm run build:runtime', | ||
minify: isProd, | ||
onSuccess: `rsync -r --include '*/' --include '*.js' --include '*.mjs' --include '*.cjs' --exclude='*' ./src/runtime ./dist/`, | ||
sourcemap: true, | ||
format: ['cjs', 'esm'], | ||
define: { | ||
PACKAGE_NAME: `"${name}"`, | ||
PACKAGE_VERSION: `"${version}"`, | ||
__DEV__: `${!isProd}`, | ||
__DEV__: `${isWatch}`, | ||
}, | ||
external: ['#crypto', '#fetch'], | ||
legacyOutput: true, | ||
bundle: true, | ||
clean: true, | ||
minify: false, | ||
}; | ||
|
||
const esm: Options = { | ||
...common, | ||
format: 'esm', | ||
}; | ||
|
||
const cjs: Options = { | ||
...common, | ||
format: 'cjs', | ||
}; | ||
|
||
return runAfterLast(['npm run build:declarations', shouldPublish && 'npm run publish:local'])(esm, cjs); | ||
}); |
Oops, something went wrong.