-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from vordgi/get-47
get-47 format package files
- Loading branch information
Showing
12 changed files
with
1,174 additions
and
114 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,22 @@ | ||
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"; | ||
import tseslint from "typescript-eslint"; | ||
|
||
const ignores = ["**/node_modules/**", "**/dist/**", "**/*.js", "**/*.d.ts"]; | ||
|
||
export default [ | ||
{ | ||
rules: { | ||
"prettier/prettier": [ | ||
"error", | ||
{ | ||
endOfLine: "auto", | ||
tabWidth: 4, | ||
printWidth: 120, | ||
arrowParens: "always", | ||
}, | ||
], | ||
}, | ||
}, | ||
...tseslint.configs.recommended, | ||
eslintPluginPrettierRecommended, | ||
].map((r) => Object.assign(r, { ignores })); |
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,23 +1,23 @@ | ||
import { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage.external' | ||
import { serverGetterInClientComponentError } from './server-getter-in-client-component-error' | ||
import { staticGenerationAsyncStorage } from "next/dist/client/components/static-generation-async-storage.external"; | ||
import { serverGetterInClientComponentError } from "./server-getter-in-client-component-error"; | ||
|
||
export const getPageConfig = () => { | ||
serverGetterInClientComponentError('getPageConfig'); | ||
serverGetterInClientComponentError("getPageConfig"); | ||
|
||
const store = staticGenerationAsyncStorage.getStore(); | ||
const store = staticGenerationAsyncStorage.getStore(); | ||
|
||
if (!store) return {}; | ||
if (!store) return {}; | ||
|
||
const basePath = process.env.__NEXT_ROUTER_BASEPATH || ''; | ||
const { pagePath, forceDynamic, forceStatic, dynamicShouldError, revalidate } = store || {}; | ||
let dynamic = 'auto'; | ||
if (forceDynamic) { | ||
dynamic = 'force-dynamic'; | ||
} else if (forceStatic) { | ||
dynamic = 'force-static'; | ||
} else if (dynamicShouldError) { | ||
dynamic = 'error'; | ||
} | ||
const basePath = process.env.__NEXT_ROUTER_BASEPATH || ""; | ||
const { pagePath, forceDynamic, forceStatic, dynamicShouldError, revalidate } = store || {}; | ||
let dynamic = "auto"; | ||
if (forceDynamic) { | ||
dynamic = "force-dynamic"; | ||
} else if (forceStatic) { | ||
dynamic = "force-static"; | ||
} else if (dynamicShouldError) { | ||
dynamic = "error"; | ||
} | ||
|
||
return { pagePath, dynamic, revalidate, basePath }; | ||
} | ||
return { pagePath, dynamic, revalidate, basePath }; | ||
}; |
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,36 +1,46 @@ | ||
import { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage.external' | ||
import { normalizeInterceptingRoutes, normalizePagePath, normalizePathname, parseSegments } from './utils'; | ||
import { serverGetterInClientComponentError } from './server-getter-in-client-component-error'; | ||
import { staticGenerationAsyncStorage } from "next/dist/client/components/static-generation-async-storage.external"; | ||
import { normalizeInterceptingRoutes, normalizePagePath, normalizePathname, parseSegments } from "./utils"; | ||
import { serverGetterInClientComponentError } from "./server-getter-in-client-component-error"; | ||
|
||
export const getParams = () => { | ||
serverGetterInClientComponentError('getParams'); | ||
serverGetterInClientComponentError("getParams"); | ||
|
||
const store = staticGenerationAsyncStorage.getStore(); | ||
|
||
if (!store) return {}; | ||
|
||
const { urlPathname, pagePath = '/' } = store; | ||
const { urlPathname, pagePath = "/" } = store; | ||
|
||
const cleanUrlPathname = normalizePathname(urlPathname); | ||
const cleanPagePath = normalizePagePath(pagePath); | ||
const pagePathParts = cleanPagePath.split('/').slice(1).filter(part => !part.match(/^(\([^)]+\)|\@.+)$/)); | ||
const pagePathParts = cleanPagePath | ||
.split("/") | ||
.slice(1) | ||
.filter((part) => !part.match(/^(\([^)]+\)|\@.+)$/)); | ||
const pagePathInterceptedParts = normalizeInterceptingRoutes(pagePathParts); | ||
const pathnameParts = cleanUrlPathname.split('/').slice(1); | ||
const pathnameParts = cleanUrlPathname.split("/").slice(1); | ||
|
||
const isRootPage = cleanUrlPathname === '' && cleanPagePath === ''; | ||
const isRootPage = cleanUrlPathname === "" && cleanPagePath === ""; | ||
const isNotFoundPage = pagePath.match(/\/_not-found\/?$/); | ||
const isValidCatchALl = cleanPagePath.match(/\/\[\.\.\.[^\]]+\]/) && pathnameParts.length >= pagePathInterceptedParts.length; | ||
const isValidOptionalCatchALl = cleanPagePath.match(/\/\[\[\.\.\.[^\]]+\]\]/) && pathnameParts.length >= pagePathInterceptedParts.length - 1; | ||
const isCorrectMatched = isRootPage || isNotFoundPage || pagePathInterceptedParts.length === pathnameParts.length || isValidCatchALl || isValidOptionalCatchALl; | ||
const isValidCatchALl = | ||
cleanPagePath.match(/\/\[\.\.\.[^\]]+\]/) && pathnameParts.length >= pagePathInterceptedParts.length; | ||
const isValidOptionalCatchALl = | ||
cleanPagePath.match(/\/\[\[\.\.\.[^\]]+\]\]/) && pathnameParts.length >= pagePathInterceptedParts.length - 1; | ||
const isCorrectMatched = | ||
isRootPage || | ||
isNotFoundPage || | ||
pagePathInterceptedParts.length === pathnameParts.length || | ||
isValidCatchALl || | ||
isValidOptionalCatchALl; | ||
|
||
if (!isCorrectMatched) { | ||
const createIssueUrl = new URL('https://github.com/vordgi/nimpl-getters/issues/new') | ||
createIssueUrl.searchParams.set('title', 'Error parsing segments in get-params'); | ||
createIssueUrl.searchParams.set('body', `urlPathname: \`${urlPathname}\`;\n\npagePath: \`${pagePath}\`;`); | ||
createIssueUrl.searchParams.append('labels', 'bug'); | ||
const createIssueUrl = new URL("https://github.com/vordgi/nimpl-getters/issues/new"); | ||
createIssueUrl.searchParams.set("title", "Error parsing segments in get-params"); | ||
createIssueUrl.searchParams.set("body", `urlPathname: \`${urlPathname}\`;\n\npagePath: \`${pagePath}\`;`); | ||
createIssueUrl.searchParams.append("labels", "bug"); | ||
throw new Error(`Something went wrong. Please create an issue on Github: ${createIssueUrl}`); | ||
} | ||
|
||
const query = parseSegments(pagePathInterceptedParts, pathnameParts); | ||
return query; | ||
} | ||
}; |
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,21 +1,19 @@ | ||
import { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage.external' | ||
import { hasBasePath } from 'next/dist/client/has-base-path' | ||
import { removeBasePath } from 'next/dist/client/remove-base-path' | ||
import { serverGetterInClientComponentError } from './server-getter-in-client-component-error' | ||
import { staticGenerationAsyncStorage } from "next/dist/client/components/static-generation-async-storage.external"; | ||
import { hasBasePath } from "next/dist/client/has-base-path"; | ||
import { removeBasePath } from "next/dist/client/remove-base-path"; | ||
import { serverGetterInClientComponentError } from "./server-getter-in-client-component-error"; | ||
|
||
export function getPathname() { | ||
serverGetterInClientComponentError('getPathname') | ||
serverGetterInClientComponentError("getPathname"); | ||
|
||
const store = staticGenerationAsyncStorage.getStore() | ||
const store = staticGenerationAsyncStorage.getStore(); | ||
|
||
if (!store) return null | ||
if (!store) return null; | ||
|
||
const { urlPathname } = store | ||
const url = new URL(urlPathname, 'http://n') | ||
const { urlPathname } = store; | ||
const url = new URL(urlPathname, "http://n"); | ||
|
||
const pathname = hasBasePath(url.pathname) | ||
? removeBasePath(url.pathname) | ||
: url.pathname | ||
const pathname = hasBasePath(url.pathname) ? removeBasePath(url.pathname) : url.pathname; | ||
|
||
return pathname | ||
} | ||
return pathname; | ||
} |
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,32 +1,34 @@ | ||
import { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage.external' | ||
import { serverGetterInClientComponentError } from './server-getter-in-client-component-error' | ||
import { ReadonlyURLSearchParams } from 'next/navigation' | ||
import { stripInternalSearchParams } from 'next/dist/server/internal-utils' | ||
import { staticGenerationAsyncStorage } from "next/dist/client/components/static-generation-async-storage.external"; | ||
import { serverGetterInClientComponentError } from "./server-getter-in-client-component-error"; | ||
import { ReadonlyURLSearchParams } from "next/navigation"; | ||
import { stripInternalSearchParams } from "next/dist/server/internal-utils"; | ||
|
||
/** @deprecated getSearchParams is deprecated. [Read more](https://nimpl.tech/getters/current-getters/get-search-params) */ | ||
export function getSearchParams(opts?: { ignoreDynamicOptionErrors?: boolean }) { | ||
serverGetterInClientComponentError('getSearchParams') | ||
console.error('getSearchParams is deprecated. Read more - https://nimpl.tech/getters/current-getters/get-search-params') | ||
serverGetterInClientComponentError("getSearchParams"); | ||
console.error( | ||
"getSearchParams is deprecated. Read more - https://nimpl.tech/getters/current-getters/get-search-params", | ||
); | ||
|
||
const store = staticGenerationAsyncStorage.getStore() | ||
const store = staticGenerationAsyncStorage.getStore(); | ||
|
||
if (!store) return new URLSearchParams() | ||
if (!store) return new URLSearchParams(); | ||
|
||
const { urlPathname, forceStatic, forceDynamic, dynamicShouldError } = store | ||
const { urlPathname, forceStatic, forceDynamic, dynamicShouldError } = store; | ||
|
||
if (!opts?.ignoreDynamicOptionErrors) { | ||
if (forceStatic) { | ||
throw new Error('Сannot get client search parameters with dynamic=force-static setting') | ||
} else if (dynamicShouldError) { | ||
throw new Error('Сannot get client search parameters with dynamic=error setting') | ||
} else if (!forceDynamic) { | ||
console.warn('Do not use getSearchParams with unselected dynamic setting, use force-dynamic instead') | ||
if (!opts?.ignoreDynamicOptionErrors) { | ||
if (forceStatic) { | ||
throw new Error("Сannot get client search parameters with dynamic=force-static setting"); | ||
} else if (dynamicShouldError) { | ||
throw new Error("Сannot get client search parameters with dynamic=error setting"); | ||
} else if (!forceDynamic) { | ||
console.warn("Do not use getSearchParams with unselected dynamic setting, use force-dynamic instead"); | ||
} | ||
} | ||
} | ||
|
||
const url = new URL(urlPathname, 'http://n') | ||
const strippedUrl = stripInternalSearchParams(url, true) | ||
const readonlySearchParams = new ReadonlyURLSearchParams(strippedUrl.searchParams) | ||
const url = new URL(urlPathname, "http://n"); | ||
const strippedUrl = stripInternalSearchParams(url, true); | ||
const readonlySearchParams = new ReadonlyURLSearchParams(strippedUrl.searchParams); | ||
|
||
return readonlySearchParams | ||
return readonlySearchParams; | ||
} |
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,12 +1,10 @@ | ||
import React from 'react' | ||
import React from "react"; | ||
|
||
export function serverGetterInClientComponentError( | ||
getterName: string | ||
): void | never { | ||
if (process.env.NODE_ENV !== 'production') { | ||
// If useState is defined we're in a client component | ||
if (Boolean(React.useState)) { | ||
throw new Error(`${getterName} only works in Server Components`) | ||
export function serverGetterInClientComponentError(getterName: string): void | never { | ||
if (process.env.NODE_ENV !== "production") { | ||
// If useState is defined we're in a client component | ||
if (Boolean(React.useState)) { | ||
throw new Error(`${getterName} only works in Server Components`); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,8 +1,8 @@ | ||
import type { AsyncLocalStorage } from "async_hooks"; | ||
|
||
export type ServerContext<T> = { | ||
Provider: ({ children, value }: { children: React.ReactNode; value: T; }) => React.ReactNode; | ||
Consumer: ({ children }: { children: (context: T | undefined) => React.ReactNode; }) => React.ReactNode; | ||
Provider: ({ children, value }: { children: React.ReactNode; value: T }) => React.ReactNode; | ||
Consumer: ({ children }: { children: (context: T | undefined) => React.ReactNode }) => React.ReactNode; | ||
_storage: AsyncLocalStorage<{ value: T }>; | ||
_defaultValue: T | undefined; | ||
}; | ||
}; |
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
Oops, something went wrong.