-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic module options Add defu dependency explicitly Create npm-publish.yml
- Loading branch information
Lennart Zellmer
committed
Dec 7, 2022
0 parents
commit d563f90
Showing
16 changed files
with
5,943 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,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = 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 @@ | ||
dist | ||
node_modules |
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 @@ | ||
{ | ||
"extends": [ | ||
"@nuxtjs/eslint-config-typescript" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-unused-vars": [ | ||
"off" | ||
] | ||
} | ||
} |
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,25 @@ | ||
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created | ||
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages | ||
|
||
name: Node.js Package | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
publish-npm: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '16.x' | ||
registry-url: 'https://registry.npmjs.org' | ||
scope: '@zellart' | ||
- run: yarn install --frozen-lockfile | ||
- run: yarn dev:prepare | ||
- run: yarn prepack | ||
- run: yarn publish --access public | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}} |
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,56 @@ | ||
# Dependencies | ||
node_modules | ||
|
||
# Logs | ||
*.log* | ||
|
||
# Temp directories | ||
.temp | ||
.tmp | ||
.cache | ||
|
||
# Yarn | ||
**/.yarn/cache | ||
**/.yarn/*state* | ||
|
||
# Generated dirs | ||
dist | ||
|
||
# Nuxt | ||
.nuxt | ||
.output | ||
.vercel_build_output | ||
.build-* | ||
.env | ||
.netlify | ||
|
||
# Env | ||
.env | ||
|
||
# Testing | ||
reports | ||
coverage | ||
*.lcov | ||
.nyc_output | ||
|
||
# VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
!.vscode/*.code-snippets | ||
|
||
# Intellij idea | ||
*.iml | ||
.idea | ||
|
||
# OSX | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk |
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 @@ | ||
imports.autoImport=false | ||
typescript.includeWorkspace=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,6 @@ | ||
# Nuxt Module | ||
|
||
## Development | ||
|
||
- Run `npm run dev:prepare` to generate type stubs. | ||
- Use `npm run dev` to start [playground](./playground) in development mode. |
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 @@ | ||
{ | ||
"name": "@zellart/nuxt-basic-auth", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lennartzellmer/nuxt-basic-auth.git" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/module.mjs", | ||
"require": "./dist/module.cjs" | ||
} | ||
}, | ||
"main": "./dist/module.cjs", | ||
"types": "./dist/types.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"prepack": "nuxt-module-build", | ||
"dev": "nuxi dev playground", | ||
"dev:build": "nuxi build playground", | ||
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground" | ||
}, | ||
"dependencies": { | ||
"@nuxt/kit": "^3.0.0", | ||
"defu": "^6.1.1" | ||
}, | ||
"devDependencies": { | ||
"@nuxt/module-builder": "^0.2.1", | ||
"@nuxt/schema": "^3.0.0", | ||
"@nuxtjs/eslint-config-typescript": "^12.0.0", | ||
"eslint": "^8.29.0", | ||
"nuxt": "^3.0.0" | ||
} | ||
} |
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,8 @@ | ||
<template> | ||
<div> | ||
Nuxt module playground! | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
</script> |
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,13 @@ | ||
import { defineNuxtConfig } from 'nuxt/config' | ||
import MyModule from '..' | ||
|
||
export default defineNuxtConfig({ | ||
modules: [MyModule], | ||
basicAuth: { | ||
password: 'password', | ||
username: 'username', | ||
realm: 'defaultRealm', | ||
excludedRoutes: ['/health'], | ||
enableOnDev: 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,4 @@ | ||
{ | ||
"private": true, | ||
"name": "playground" | ||
} |
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 @@ | ||
import { defineEventHandler } from 'h3' | ||
|
||
export default defineEventHandler(() => { | ||
return { | ||
state: 'healthy' | ||
} | ||
}) |
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,57 @@ | ||
import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit' | ||
import { defu } from 'defu' | ||
|
||
export interface ModuleOptions { | ||
username?: string | ||
password?: string | ||
realm: string | ||
whitelistedRoutes: string[] | ||
enabled: boolean | ||
} | ||
|
||
export default defineNuxtModule<ModuleOptions>({ | ||
meta: { | ||
name: 'nuxt-basic-auth', | ||
configKey: 'basicAuth' | ||
}, | ||
defaults: { | ||
username: process.env.BASIC_AUTH_USERNAME, | ||
password: process.env.BASIC_AUTH_PASSWORD, | ||
realm: process.env.BASIC_AUTH_REALM || 'default', | ||
whitelistedRoutes: [], | ||
enabled: true | ||
}, | ||
setup (options, nuxt) { | ||
// Check if enabled | ||
if (!options.enabled) { | ||
return | ||
} | ||
// Check if config is present | ||
if (!options.username) { | ||
throw new Error('BasicAuth username not set. Either use runtimeConfig or \'.env\' with BASIC_AUTH_USERNAME') | ||
} | ||
if (!options.password) { | ||
throw new Error('BasicAuth password not set. Either use runtimeConfig or \'.env\' with BASIC_AUTH_PASSWORD') | ||
} | ||
|
||
// Copy module config in server runtimeConfig | ||
nuxt.options.runtimeConfig.basicAuth = defu(nuxt.options.runtimeConfig.basicAuth, options) | ||
|
||
// Transpile runtime | ||
const { resolve } = createResolver(import.meta.url) | ||
const runtimeDir = resolve('./runtime') | ||
nuxt.options.build.transpile.push(runtimeDir) | ||
|
||
addServerHandler({ | ||
middleware: true, | ||
handler: resolve(runtimeDir, 'server/middleware/serverHandler') | ||
}) | ||
} | ||
}) | ||
|
||
declare module '@nuxt/schema' { | ||
interface RuntimeConfig | ||
{ | ||
basicAuth: ModuleOptions | ||
} | ||
} |
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,32 @@ | ||
import { defineEventHandler } from 'h3' | ||
import { useRuntimeConfig } from '#imports' | ||
|
||
export default defineEventHandler((event) => { | ||
const { | ||
username, | ||
password, | ||
realm, | ||
whitelistedRoutes | ||
} = useRuntimeConfig().basicAuth | ||
|
||
// Allow access on whitelisted routes | ||
if (whitelistedRoutes.includes(event.node.req.url || '')) { | ||
return | ||
} | ||
|
||
// Check authentication | ||
let authenticated = false | ||
const base64Credentials = event.node.req.headers?.authorization?.split(' ')?.[1] | ||
if (base64Credentials) { | ||
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii') | ||
const [credentialsUsername, credentialsPassword] = credentials.split(':') | ||
authenticated = credentialsUsername === username && credentialsPassword === password | ||
} | ||
|
||
// Reject authentication | ||
if (!authenticated) { | ||
event.node.res.statusCode = 401 | ||
event.node.res.setHeader('WWW-Authenticate', `Basic realm="${realm}"`) | ||
event.node.res.end('Unauthorized') | ||
} | ||
}) |
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 @@ | ||
{ | ||
"extends": "./playground/.nuxt/tsconfig.json", | ||
"include": ["./src"] | ||
} |
Oops, something went wrong.