Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnapo committed Jan 31, 2020
1 parent 86a96d6 commit 188580b
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 174 deletions.
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
5 changes: 3 additions & 2 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"MD013": false,
"MD014": false
"MD010": false,
"MD013": false,
"MD014": false
}
2 changes: 1 addition & 1 deletion .ncurc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"upgrade": true
"upgrade": true
}
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ const getWithCors = (path, handler) => get(path, cors(handler));

const hello = cors((req, res) => send(res, 200, { message: "Hello 1!" }));
const hello2 = (req, res) => {
cors(req, res);
return send(res, 200, { message: "Hello 2!" });
cors(req, res);
return send(res, 200, { message: "Hello 2!" });
};
const hello3 = (req, res) => send(res, 200, { message: "Hello 3!" });

module.exports = router(
get("/hello/1", hello),
get("/hello/2", hello2),
get("/hello/3", cors(hello3)),
getWithCors("/*", (req, res) => send(res, 200, { message: "Hello in general!" })),
get("/hello/1", hello),
get("/hello/2", hello2),
get("/hello/3", cors(hello3)),
getWithCors("/*", (req, res) =>
send(res, 200, { message: "Hello in general!" })
)
);
```

Expand All @@ -42,19 +44,19 @@ module.exports = router(
const { router, get } = require("microrouter");
const cors = require("just-the-cors");

const hello1 = (req) => {
cors(req); // Does nothing!
return "Hello 1";
const hello1 = req => {
cors(req); // Does nothing!
return "Hello 1";
};

const hello2 = (req, res) => {
cors(req, res);
return "Hello 2";
cors(req, res);
return "Hello 2";
};

module.exports = router(
get("/hello1", hello1), // "Access-Control-Allow-Origin": ❌
get("/hello2", hello2), // "Access-Control-Allow-Origin": ✅
get("/hello1", hello1), // "Access-Control-Allow-Origin": ❌
get("/hello2", hello2) // "Access-Control-Allow-Origin": ✅
);
```

Expand Down
60 changes: 30 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
module.exports = (...args) => {
if (args.length === 1 && typeof args[0] === "function") {
const cb = args[0];
return (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
if (args.length === 1 && typeof args[0] === "function") {
const cb = args[0];
return (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");

if (req.method.toUpperCase() === "OPTIONS") {
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,PUT,PATCH,POST,DELETE");
res.setHeader("Access-Control-Request-Headers", "Vary");
const reqHeaders = req.headers["access-control-request-headers"];
if (reqHeaders && reqHeaders.length) res.setHeader("Access-Control-Allow-Headers", reqHeaders);
res.setHeader("Content-Length", "0");
res.statusCode = 204;
return res.end();
}
if (req.method.toUpperCase() === "OPTIONS") {
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,PUT,PATCH,POST,DELETE");
res.setHeader("Access-Control-Request-Headers", "Vary");
const reqHeaders = req.headers["access-control-request-headers"];
if (reqHeaders && reqHeaders.length) res.setHeader("Access-Control-Allow-Headers", reqHeaders);
res.setHeader("Content-Length", "0");
res.statusCode = 204;
return res.end();
}

return cb(req, res);
};
}
return cb(req, res);
};
}

const [req, res = { setHeader: () => {}, end: () => {} }] = args;
const [req, res = { setHeader: () => {}, end: () => {} }] = args;

res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");

if (req.method.toUpperCase() === "OPTIONS") {
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,PUT,PATCH,POST,DELETE");
res.setHeader("Access-Control-Request-Headers", "Vary");
const reqHeaders = req.headers["access-control-request-headers"];
if (reqHeaders && reqHeaders.length) res.setHeader("Access-Control-Allow-Headers", reqHeaders);
res.setHeader("Content-Length", "0");
res.statusCode = 204;
return res.end();
}
if (req.method.toUpperCase() === "OPTIONS") {
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,PUT,PATCH,POST,DELETE");
res.setHeader("Access-Control-Request-Headers", "Vary");
const reqHeaders = req.headers["access-control-request-headers"];
if (reqHeaders && reqHeaders.length) res.setHeader("Access-Control-Allow-Headers", reqHeaders);
res.setHeader("Content-Length", "0");
res.statusCode = 204;
return res.end();
}

return args;
return args;
};
116 changes: 58 additions & 58 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
{
"name": "just-the-cors",
"version": "0.2.1",
"description": "Tiny middleware to add cors support when using zeit's micro",
"keywords": [
"cors",
"micro",
"microservice",
"router",
"routing",
"zeit"
],
"homepage": "https://github.com/iamnapo/just-the-cors#readme",
"bugs": {
"url": "https://github.com/iamnapo/just-the-cors/issues"
},
"repository": "github:iamnapo/just-the-cors",
"license": "MIT",
"author": {
"name": "Napoleon-Christos Oikonomou",
"email": "Napoleonoikon@gmail.com",
"url": "https://iamnapo.me"
},
"files": [
"index.js"
],
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "npm run lint && ava --verbose"
},
"husky": {
"hooks": {
"pre-commit": "npm test"
}
},
"dependencies": {},
"devDependencies": {
"ava": "^2.3.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.4.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-iamnapo": "^1.3.3",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^2.0.1",
"got": "^9.6.0",
"husky": "^3.0.5",
"micro": "^9.3.4",
"microrouter": "^3.1.3",
"test-listen": "^1.1.0"
},
"peerDependencies": {
"micro": ">=9"
},
"engines": {
"node": ">=10"
}
"name": "just-the-cors",
"version": "0.2.1",
"description": "Tiny middleware to add cors support when using zeit's micro",
"keywords": [
"cors",
"micro",
"microservice",
"router",
"routing",
"zeit"
],
"homepage": "https://github.com/iamnapo/just-the-cors#readme",
"bugs": {
"url": "https://github.com/iamnapo/just-the-cors/issues"
},
"repository": "github:iamnapo/just-the-cors",
"license": "MIT",
"author": {
"name": "Napoleon-Christos Oikonomou",
"email": "Napoleonoikon@gmail.com",
"url": "https://iamnapo.me"
},
"files": [
"index.js"
],
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "npm run lint && ava --verbose"
},
"husky": {
"hooks": {
"pre-commit": "npm test"
}
},
"dependencies": {},
"devDependencies": {
"ava": "^3.1.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-iamnapo": "^2.1.1",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.18.0",
"eslint-plugin-react-hooks": "^2.3.0",
"got": "^10.4.0",
"husky": "^4.2.1",
"micro": "^9.3.4",
"microrouter": "^3.1.3",
"test-listen": "^1.1.0"
},
"peerDependencies": {
"micro": ">=9"
},
"engines": {
"node": ">=10"
}
}
Loading

0 comments on commit 188580b

Please sign in to comment.