-
-
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.
feat(basics): eslint, prettier, husky, commitlint, lint-staged, eslin…
…t plugins, etc
- Loading branch information
0 parents
commit 694d952
Showing
13 changed files
with
388 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,7 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
indent_size = 2 | ||
end_of_line = lf | ||
insert_final_newline = 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,17 @@ | ||
{ | ||
"extends": [ | ||
"eslint:recommended", | ||
"prettier", | ||
"plugin:@typescript-eslint/recommended", | ||
"plugin:prettier/recommended" | ||
], | ||
"plugins": ["@typescript-eslint"], | ||
"parser": "@typescript-eslint/parser", | ||
"env": { | ||
"node": true | ||
}, | ||
"rules": { | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/no-non-null-assertion": "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
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 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npx --no -- commitlint --edit "" |
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 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npx lint-staged |
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 @@ | ||
{ | ||
"*.{ts,js}": [ | ||
"yarn lint:fix" | ||
] | ||
} |
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 @@ | ||
# vercel-cors |
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 @@ | ||
module.exports = { | ||
extends: ['@commitlint/config-conventional'], | ||
} |
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,37 @@ | ||
{ | ||
"name": "vercel-cors", | ||
"version": "1.0.0", | ||
"description": "Simple CORS module for vercel serverless functions", | ||
"main": "src/main.js", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"lint": "eslint . --cache", | ||
"lint:fix": "eslint . --cache --fix", | ||
"prepare": "husky install" | ||
}, | ||
"keywords": [ | ||
"cors", | ||
"vercel", | ||
"simple", | ||
"serverless", | ||
"functions" | ||
], | ||
"author": "diegoulloao", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"husky": "^8.0.1", | ||
"typescript": "^4.8.3", | ||
"@commitlint/cli": "^17.1.2", | ||
"@commitlint/config-conventional": "^17.1.0", | ||
"@typescript-eslint/eslint-plugin": "^5.37.0", | ||
"@typescript-eslint/parser": "^5.37.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint": "^8.23.1", | ||
"lint-staged": "^13.0.3", | ||
"prettier": "^2.7.1" | ||
}, | ||
"dependencies": { | ||
"@vercel/node": "^2.5.14" | ||
} | ||
} |
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 @@ | ||
module.exports = { | ||
singleQuote: true, | ||
semi: false, | ||
printWidth: 120, | ||
} |
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,67 @@ | ||
import type { VercelRequest, VercelResponse, VercelApiHandler } from '@vercel/node' | ||
import type { CorsOptions } from 'types' | ||
|
||
export const defaults: CorsOptions = { | ||
origin: '*', | ||
headers: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'], | ||
methods: ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'UPDATE', 'OPTIONS', 'DELETE'], | ||
expose: [], | ||
maxAge: undefined, | ||
credentials: false, | ||
} | ||
|
||
export default (options: Partial<CorsOptions>) => { | ||
options = { ...defaults, ...options } | ||
|
||
return (handler: VercelApiHandler): VercelApiHandler => { | ||
return function (req: VercelRequest, res: VercelResponse) { | ||
if (typeof options.origin === 'boolean' && options.origin === false) { | ||
return res.status(403).end('not allowed') | ||
} | ||
|
||
if (options.origin !== '*' && req.headers.origin === undefined) { | ||
return res.status(403).end('unknown origin') | ||
} | ||
|
||
if (typeof options.origin === 'string') { | ||
if (options.origin === '*' || options.origin === req.headers.origin) { | ||
res.setHeader('Access-Control-Allow-Origin', options.origin) | ||
} | ||
} else if (options.origin instanceof RegExp) { | ||
if (options.origin.test(req.headers.origin as string)) { | ||
res.setHeader('Access-Control-Allow-Origin', req.headers.origin as string) | ||
} | ||
} else { | ||
const allowedOrigin: boolean = (options.origin as Array<string | RegExp>).some((h: RegExp | string) => | ||
typeof h === 'string' ? h === req.headers.origin : h.test(req.headers.origin as string) | ||
) | ||
|
||
if (allowedOrigin) { | ||
res.setHeader('Access-Control-Allow-Origin', req.headers.origin as string) | ||
} | ||
} | ||
|
||
res.setHeader('Access-Control-Allow-Headers', options.headers!.join(', ')) | ||
res.setHeader('Access-Control-Allow-Credentials', options.credentials!.toString()) | ||
res.setHeader('Access-Control-Allow-Methods', options.methods!.join(', ')) | ||
|
||
if (options.expose!.length) { | ||
res.setHeader('Access-Control-Expose-Methods', options.methods!.join(', ')) | ||
} | ||
|
||
if (!isNaN(options.maxAge!)) { | ||
res.setHeader('Access-Control-Max-Age', options.maxAge!) | ||
} | ||
|
||
if (options.origin === '*' || (Array.isArray(options.origin) && options.origin.length > 1)) { | ||
res.setHeader('Vary', 'Origin') | ||
} | ||
|
||
if (req.method === 'OPTIONS') { | ||
return res.status(200).json({ body: 'OK' }) | ||
} | ||
|
||
return handler.apply(this, [req, res]) | ||
} | ||
} | ||
} |
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 @@ | ||
type HttpMethods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'UPDATE' | 'DELETE' | 'OPTIONS' | ||
|
||
export interface CorsOptions { | ||
origin: string | RegExp | boolean | Array<string | RegExp> | ||
headers: string[] | ||
methods: HttpMethods[] | ||
expose: string[] | ||
maxAge: number | undefined | ||
credentials: boolean | ||
} |
Oops, something went wrong.