This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
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.
- Loading branch information
0 parents
commit c2538aa
Showing
21 changed files
with
7,295 additions
and
0 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,4 @@ | ||
NODE_ENV=development | ||
|
||
NEXT_CLIENT_DIR=/ | ||
NEXT_CONFIG_PATH=/next.config.js |
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,5 @@ | ||
**/node_modules/* | ||
**/out/* | ||
**/.next/* | ||
**/dist/* | ||
**/installs/* |
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,38 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint"], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier", | ||
"prettier/@typescript-eslint" | ||
], | ||
"env": { | ||
"es6": true, | ||
"browser": true, | ||
"jest": true, | ||
"node": true | ||
}, | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": 0, | ||
"@typescript-eslint/explicit-module-boundary-types": 0, | ||
"@typescript-eslint/explicit-member-accessibility": 0, | ||
"@typescript-eslint/indent": 0, | ||
"@typescript-eslint/member-delimiter-style": 0, | ||
"@typescript-eslint/no-explicit-any": 0, | ||
"@typescript-eslint/no-var-requires": 0, | ||
"@typescript-eslint/no-use-before-define": 0, | ||
"@typescript-eslint/no-unused-vars": [ | ||
2, | ||
{ | ||
"argsIgnorePattern": "^_" | ||
} | ||
], | ||
"no-console": [ | ||
2, | ||
{ | ||
"allow": ["warn", "error"] | ||
} | ||
] | ||
} | ||
} |
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,7 @@ | ||
# Dependency directories | ||
node_modules/ | ||
|
||
# production server output | ||
dist | ||
|
||
.env |
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,6 @@ | ||
{ | ||
"hooks": { | ||
"pre-commit": "lint-staged", | ||
"pre-push": "yarn type-check" | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"*.@(ts|tsx)": ["yarn lint", "yarn format"] | ||
} |
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,11 @@ | ||
node_modules/ | ||
.env | ||
.env.example | ||
.eslintignore | ||
.eslintrc.json | ||
.gitignore | ||
.huskyrc | ||
.prettierignore | ||
.prettierrc | ||
README.md | ||
tsconfig.json |
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,3 @@ | ||
node_modules | ||
yarn.lock | ||
dist |
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,4 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Henrique Carvalho da Cruz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,81 @@ | ||
# nextjs-koa-middleware | ||
|
||
NextJS koa middleware | ||
|
||
[![npm](https://img.shields.io/npm/v/nextjs-koa-middleware.svg)](https://www.npmjs.com/package/nextjs-koa-middleware) | ||
[![npm](https://img.shields.io/npm/dt/nextjs-koa-middleware.svg)](https://www.npmjs.com/package/nextjs-koa-middleware) | ||
[![LICENSE](https://img.shields.io/github/license/henriquecarv/nextjs-koa-middleware.svg)](./LICENSE) | ||
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=henriquecarv/nextjs-koa-middleware)](https://dependabot.com) | ||
|
||
## System Requirements | ||
|
||
- **[NodeJS](https://nodejs.org/en/)** (version >= 12). | ||
|
||
## Installation | ||
|
||
```sh | ||
# npm | ||
npm i nextjs-koa-middleware | ||
# yarn | ||
yarn add nextjs-koa-middleware | ||
``` | ||
|
||
## Peer Dependency | ||
|
||
- **[koa-router](https://github.com/koajs/router)** (version <= 8.0.8) | ||
|
||
## Usage | ||
|
||
- Define environment variables in .env file: | ||
|
||
```sh | ||
NODE_ENV=development | ||
|
||
# Relative path to .next directory | ||
NEXT_CLIENT_DIR=/ | ||
NEXT_CONFIG_PATH=/next.config.js | ||
``` | ||
|
||
- Use the [deafultReturn()](./lib/middlewares/defaultReturn.ts) as early as possible after starting a koa instance: | ||
|
||
```typescript | ||
import koa from 'koa' | ||
import { defaultReturn } from 'nextjs-koa-middleware' | ||
|
||
const app = new koa() | ||
|
||
app.use(defaultReturn()) | ||
``` | ||
|
||
- Default NextJS Routes: | ||
|
||
```typescript | ||
import Router from 'koa-router' | ||
import { handleRoutes } from 'nextjs-koa-middleware' | ||
|
||
const nextRouter = new Router() | ||
|
||
nextRouter.all('*', handleRoutes()) | ||
|
||
export default nextRouter | ||
``` | ||
|
||
- Custom NextJS Routes: | ||
|
||
```typescript | ||
import Router from 'koa-router' | ||
import { render } from 'nextjs-koa-middleware' | ||
|
||
const customRoute = new Router() | ||
|
||
customRoute.get('/custom-path', render('pageComponentName')) | ||
|
||
export default customRoute | ||
``` | ||
|
||
### License | ||
|
||
Copylefted (c) 2020 [Henrique Carvalho da Cruz][1] Licensed under the [MIT license][2]. | ||
|
||
[1]: https://henriquecarv.com | ||
[2]: ./LICENSE |
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,10 @@ | ||
const { NODE_ENV: nodeEnv, NEXT_CLIENT_DIR, NEXT_CONFIG_PATH } = process.env | ||
|
||
import { cwd } from 'process' | ||
import { join } from 'path' | ||
const dev = nodeEnv !== 'production' | ||
const nextClientDir = join(cwd(), NEXT_CLIENT_DIR ?? '/') | ||
const nextConfigPath = join(cwd(), NEXT_CONFIG_PATH ?? '/next.config.js') | ||
const nextConfig = require(nextConfigPath) | ||
|
||
export { nextClientDir, dev, nextConfig } |
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,9 @@ | ||
import { config as dotenvConfig } from 'dotenv' | ||
dotenvConfig() | ||
|
||
import init from './middlewares/init' | ||
import defaultReturn from './middlewares/defaultReturn' | ||
import handleRoutes from './middlewares/handleRoutes' | ||
import render from './middlewares/render' | ||
|
||
export { init, defaultReturn, handleRoutes, render } |
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,6 @@ | ||
import { Context, Next } from 'koa' | ||
|
||
export default () => async (ctx: Context, next: Next) => { | ||
ctx.res.statusCode = 200 | ||
await next() | ||
} |
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,11 @@ | ||
import app from './init' | ||
import { Context } from 'koa' | ||
|
||
export default () => { | ||
const handle = app().getRequestHandler() | ||
|
||
return async (ctx: Context) => { | ||
await handle(ctx.req, ctx.res) | ||
ctx.respond = false | ||
} | ||
} |
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,14 @@ | ||
import next from 'next' | ||
import { dev, nextClientDir, nextConfig } from '../config/variables' | ||
|
||
export default () => { | ||
const app = next({ dev, dir: nextClientDir, conf: nextConfig }) | ||
|
||
const init = async () => { | ||
await app.prepare() | ||
} | ||
|
||
init() | ||
|
||
return app | ||
} |
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 @@ | ||
import app from './init' | ||
import { Context } from 'koa' | ||
|
||
export default (handleComponent: string) => { | ||
return async (ctx: Context) => { | ||
await app().render(ctx.req, ctx.res, `/${handleComponent}`, { | ||
...ctx.query, | ||
...ctx.params, | ||
}) | ||
ctx.respond = false | ||
} | ||
} |
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,2 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/types/global" /> |
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,53 @@ | ||
{ | ||
"name": "nextjs-koa-middleware", | ||
"version": "1.0.0", | ||
"description": "NextJS koa middleware", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"private": false, | ||
"scripts": { | ||
"prebuild": "rm -rf dist/", | ||
"build": "tsc --project tsconfig.json", | ||
"type-check": "tsc --pretty --noEmit", | ||
"format": "prettier --write **/*.{js,ts}", | ||
"lint": "eslint . --ext ts --ext tsx --ext js", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://[email protected]/henriquecarv/nextjs-koa-middleware.git" | ||
}, | ||
"author": "Henrique Carvalho da Cruz <[email protected]> (https://henriquecarv.com)", | ||
"license": "MIT", | ||
"homepage": "https://github.com/henriquecarv/nextjs-koa-middleware#readme", | ||
"bugs": "https://github.com/henriquecarv/nextjs-koa-middleware/issues", | ||
"dependencies": { | ||
"dotenv": "^8.2.0", | ||
"koa": "^2.12.0", | ||
"next": "^9.4.4" | ||
}, | ||
"peerDependencies": { | ||
"koa-router": "^8.0.8", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1" | ||
}, | ||
"devDependencies": { | ||
"@types/koa": "^2.11.3", | ||
"@types/node": "^14.0.9", | ||
"@types/react": "^16.9.35", | ||
"@types/react-dom": "16.9.8", | ||
"@typescript-eslint/eslint-plugin": "^3.1.0", | ||
"@typescript-eslint/parser": "^3.1.0", | ||
"eslint": "^7.1.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"husky": "^4.2.5", | ||
"lint-staged": "^10.2.7", | ||
"prettier": "^2.0.5", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"typescript": "^3.9.3" | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
} | ||
} |
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,28 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2018", | ||
"module": "commonjs", | ||
"jsx": "preserve", | ||
"lib": ["dom", "es2018"], | ||
"baseUrl": ".", | ||
"outDir": "dist", | ||
"moduleResolution": "node", | ||
"strict": true, | ||
"allowJs": true, | ||
"noEmit": false, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"isolatedModules": true, | ||
"removeComments": false, | ||
"preserveConstEnums": true, | ||
"sourceMap": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"resolveJsonModule": true, | ||
"declaration": true | ||
}, | ||
"exclude": ["dist", ".next", "out", "next.config.js"], | ||
"include": ["next-env.d.ts", "lib/**/*.ts", "lib/config/next.config.js"] | ||
} |
Oops, something went wrong.