Skip to content

Commit d808a01

Browse files
committed
first-commit
0 parents  commit d808a01

File tree

12 files changed

+2421
-0
lines changed

12 files changed

+2421
-0
lines changed

.eslintrc.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"plugin:@typescript-eslint/recommended",
5+
"prettier"
6+
],
7+
"parser": "@typescript-eslint/parser",
8+
"plugins": ["@typescript-eslint", "import"],
9+
"ignorePatterns": ["generated/*"],
10+
"rules": {
11+
"no-empty": 0,
12+
"@typescript-eslint/no-unused-vars": 0,
13+
"@typescript-eslint/no-namespace": 0,
14+
"@typescript-eslint/explicit-module-boundary-types": 0,
15+
"@typescript-eslint/no-non-null-assertion": 0,
16+
"@typescript-eslint/no-var-requires": 0,
17+
"@typescript-eslint/ban-types": 0,
18+
"@next/next/no-html-link-for-pages": 0,
19+
"import/order": [
20+
"warn",
21+
{
22+
"groups": [
23+
"builtin",
24+
"external",
25+
"internal",
26+
["parent", "sibling"],
27+
"object",
28+
"type",
29+
"index"
30+
],
31+
"pathGroupsExcludedImportTypes": ["builtin"],
32+
"alphabetize": {
33+
"order": "asc",
34+
"caseInsensitive": true
35+
}
36+
}
37+
]
38+
}
39+
}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
*.log
3+
/dist
4+
coverage
5+
/test/bin/test.ts

.npmignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
node_modules
2+
/src
3+
/test
4+
/.github
5+
/dist/package.json
6+
.gitignore
7+
coverage
8+
yarn.lock
9+
tsconfig.json
10+
tsconfig.esm.json
11+
jest.config.ts
12+
.eslintrc.json
13+
.prettierrc
14+
.env.test
15+
/esm
16+
/documents

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 SoraKumo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# next-server-context
2+
3+
Server component of Next.js' AppRouter, which provides a ContextAPI-like mechanism.
4+
5+
Context instances are created for each component tree.
6+
Contexts cannot be retrieved from ServerActions functions.
7+
8+
Normally `page.tsx` -> `layout.tsx` is executed in this order, but with this library `layout.tsx` -> `page.tsx` will be executed in this order.
9+
10+
- app/context.tsx
11+
12+
```tsx
13+
import { createContext } from "next-server-context";
14+
15+
export const context = createContext<{ text: string; color: string }>();
16+
```
17+
18+
- app/layout.tsx
19+
20+
```tsx
21+
import React from "react";
22+
import { context } from "context";
23+
24+
export default function RootLayout({
25+
children,
26+
}: {
27+
children: React.ReactNode;
28+
}) {
29+
// Without using Provider, values can also be set in the following ways
30+
// context().set(VALUE)
31+
32+
return (
33+
<html lang="en">
34+
<body>
35+
<context.Provider
36+
value={{ text: "Send colors and text from Layout", color: "red" }}
37+
>
38+
{children}
39+
</context.Provider>
40+
</body>
41+
</html>
42+
);
43+
}
44+
```
45+
46+
- app/pages.tsx
47+
48+
```tsx
49+
import { context } from "context";
50+
import { useContext } from "next-server-context";
51+
52+
const Page = () => {
53+
const { text, color } = useContext(context);
54+
// If the component is async, it should be written as follows
55+
// const { text, color } = async getContext(context);
56+
return <div style={{ color }}>Page: {text}</div>;
57+
};
58+
59+
export default Page;
60+
```

esm/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

next-env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "next-server-context",
3+
"version": "0.0.1",
4+
"main": "./dist/cjs/index.js",
5+
"types": "./dist/cjs/index.d.ts",
6+
"exports": {
7+
"require": "./dist/cjs/index.js",
8+
"import": "./dist/esm/index.js"
9+
},
10+
"author": "SoraKumo <[email protected]>",
11+
"license": "MIT",
12+
"scripts": {
13+
"build": "rimraf dist && tsc && tsc -p ./tsconfig.esm.json && cpy esm dist",
14+
"watch": "tsc -b -w",
15+
"lint": "eslint ./src",
16+
"lint:fix": "eslint --fix ./src"
17+
},
18+
"devDependencies": {
19+
"@types/node": "20.9.4",
20+
"@types/react": "^18.2.38",
21+
"@typescript-eslint/eslint-plugin": "^6.12.0",
22+
"@typescript-eslint/parser": "^6.12.0",
23+
"cpy-cli": "^5.0.0",
24+
"eslint": "8.54.0",
25+
"eslint-config-prettier": "^9.0.0",
26+
"eslint-import-resolver-typescript": "^3.6.1",
27+
"eslint-plugin-import": "^2.29.0",
28+
"next": "^14.0.3",
29+
"rimraf": "^5.0.5",
30+
"typescript": "^5.3.2"
31+
},
32+
"repository": "https://github.com/ReactLibraries/next-server-context"
33+
}

src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ReactNode, cache, use } from "react";
2+
3+
export const createContext = <T>() => {
4+
const context = cache(() => {
5+
let resolve: (value: T) => void;
6+
const promise = new Promise<T>((r) => (resolve = r));
7+
return {
8+
set: (value: T) => resolve(value),
9+
get: () => promise,
10+
};
11+
});
12+
const Provider = ({ children, value }: { children: ReactNode; value: T }) => {
13+
context().set(value);
14+
return children;
15+
};
16+
return Object.assign(context, { Provider });
17+
};
18+
19+
export const getContext = <T>(context: ReturnType<typeof createContext<T>>) =>
20+
context().get();
21+
export const useContext = <T>(context: ReturnType<typeof createContext<T>>) =>
22+
use(context().get());

tsconfig.esm.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/esm",
5+
"module": "ESNext"
6+
}
7+
}

0 commit comments

Comments
 (0)