Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquecarv committed Jun 3, 2020
0 parents commit c2538aa
Show file tree
Hide file tree
Showing 21 changed files with 7,295 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
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
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/out/*
**/.next/*
**/dist/*
**/installs/*
38 changes: 38 additions & 0 deletions .eslintrc.json
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"]
}
]
}
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Dependency directories
node_modules/

# production server output
dist

.env
6 changes: 6 additions & 0 deletions .huskyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "yarn type-check"
}
}
3 changes: 3 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"*.@(ts|tsx)": ["yarn lint", "yarn format"]
}
11 changes: 11 additions & 0 deletions .npmignore
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
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn.lock
dist
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
21 changes: 21 additions & 0 deletions LICENSE
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.
81 changes: 81 additions & 0 deletions README.md
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
10 changes: 10 additions & 0 deletions lib/config/variables.ts
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 }
9 changes: 9 additions & 0 deletions lib/index.ts
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 }
6 changes: 6 additions & 0 deletions lib/middlewares/defaultReturn.ts
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()
}
11 changes: 11 additions & 0 deletions lib/middlewares/handleRoutes.ts
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
}
}
14 changes: 14 additions & 0 deletions lib/middlewares/init.ts
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
}
12 changes: 12 additions & 0 deletions lib/middlewares/render.ts
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
}
}
2 changes: 2 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
53 changes: 53 additions & 0 deletions package.json
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"
}
}
28 changes: 28 additions & 0 deletions tsconfig.json
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"]
}
Loading

0 comments on commit c2538aa

Please sign in to comment.