-
Notifications
You must be signed in to change notification settings - Fork 419
chore(nextjs): Improve type safety of #safe-node-apis #6597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/nextjs': patch | ||
| --- | ||
|
|
||
| Add types to safe-node-apis modules. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,31 +2,27 @@ | |
| * Attention: Only import this module when the node runtime is used. | ||
| * We are using conditional imports to mitigate bundling issues with Next.js server actions on version prior to 14.1.0. | ||
| */ | ||
| // @ts-ignore | ||
| import nodeRuntime from '#safe-node-apis'; | ||
|
|
||
| const throwMissingFsModule = (module: string) => { | ||
| throw new Error(`Clerk: ${module} is missing. This is an internal error. Please contact Clerk's support.`); | ||
| }; | ||
|
|
||
| const nodeFsOrThrow = () => { | ||
| if (!nodeRuntime.fs) { | ||
| throwMissingFsModule('fs'); | ||
| // Generic assertion function that acts as a proper type guard | ||
| function assertNotNullable<T>(value: T, moduleName: string): asserts value is NonNullable<T> { | ||
| if (!value) { | ||
| throw new Error(`Clerk: ${moduleName} is missing. This is an internal error. Please contact Clerk's support.`); | ||
| } | ||
| } | ||
|
|
||
| const nodeFsOrThrow = (): NonNullable<typeof nodeRuntime.fs> => { | ||
| assertNotNullable(nodeRuntime.fs, 'fs'); | ||
| return nodeRuntime.fs; | ||
| }; | ||
|
|
||
| const nodePathOrThrow = () => { | ||
| if (!nodeRuntime.path) { | ||
| throwMissingFsModule('path'); | ||
| } | ||
| const nodePathOrThrow = (): NonNullable<typeof nodeRuntime.path> => { | ||
| assertNotNullable(nodeRuntime.path, 'path'); | ||
| return nodeRuntime.path; | ||
| }; | ||
|
|
||
| const nodeCwdOrThrow = () => { | ||
| if (!nodeRuntime.cwd) { | ||
| throwMissingFsModule('cwd'); | ||
| } | ||
| const nodeCwdOrThrow = (): NonNullable<typeof nodeRuntime.cwd> => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we have the assertion function, what happens if we remove the explicit return type here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When building
|
||
| assertNotNullable(nodeRuntime.cwd, 'cwd'); | ||
| return nodeRuntime.cwd; | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Global type declarations for #safe-node-apis conditional import | ||
| */ | ||
|
|
||
| declare module '#safe-node-apis' { | ||
| import type { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import type * as nodePath from 'node:path'; | ||
|
|
||
| interface FileSystem { | ||
| existsSync: typeof existsSync; | ||
| writeFileSync: typeof writeFileSync; | ||
| readFileSync: typeof readFileSync; | ||
| appendFileSync: typeof appendFileSync; | ||
| mkdirSync: typeof mkdirSync; | ||
| rmSync: typeof rmSync; | ||
| } | ||
|
|
||
| interface SafeNodeApis { | ||
| fs: FileSystem | undefined; | ||
| path: typeof nodePath | undefined; | ||
| cwd: (() => string) | undefined; | ||
| } | ||
|
|
||
| const safeNodeApis: SafeNodeApis; | ||
| export = safeNodeApis; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.