Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prettier for code formatting #7

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
pull_request:
push:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Install dependencies
run: yarn install
- name: Run prettier
run: yarn format:check
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.17.1
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yarn.lock
.gitignore
node_modules/*
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"use strict";
'use strict';

module.exports = require("./lib/msw-config");
module.exports = require('./lib/msw-config');
75 changes: 39 additions & 36 deletions lib/msw-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultRouteOptions = {
* @private
*/
function isOption(option) {
if (!option || typeof option !== "object") {
if (!option || typeof option !== 'object') {
return false;
}

Expand Down Expand Up @@ -86,27 +86,30 @@ export default class MswConfig {
].forEach(([verb, alias]) => {
this[verb] = (path, ...args) => {
let [rawHandler, customizedCode, options] = extractRouteArguments(args);
let handler = mirageServer.registerRouteHandler(verb, path, rawHandler, customizedCode, options);
let handler = mirageServer.registerRouteHandler(
verb,
path,
rawHandler,
customizedCode,
options
);
let fullPath = this._getFullPath(path);
let mswHandler = rest[verb](fullPath, async (req, res, ctx) => {
let queryParams = {};
req.url.searchParams.forEach((value, key) => {
if (key.includes("[]")) {
key = key.replace("[]", "");
value = [...(queryParams[key] || []), value]
if (key.includes('[]')) {
key = key.replace('[]', '');
value = [...(queryParams[key] || []), value];
}
queryParams[key] = value;
});
let request = Object.assign(
req,
{
requestBody:
typeof req.body === 'string'
? req.body
: JSON.stringify(req.body),
queryParams: queryParams,
}
);
let request = Object.assign(req, {
requestBody:
typeof req.body === 'string'
? req.body
: JSON.stringify(req.body),
queryParams: queryParams,
});
let [code, headers, response] = await handler(request);
if (code === 204) {
// MirageJS Incorrectly sets the body to "" on a 204.
Expand Down Expand Up @@ -210,31 +213,31 @@ export default class MswConfig {
// if there is a urlPrefix and a namespace
if (this.urlPrefix && this.namespace) {
if (
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] === '/'
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] === '/'
) {
namespace = this.namespace
.substring(0, this.namespace.length - 1)
.substring(1);
.substring(0, this.namespace.length - 1)
.substring(1);
}

if (
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] !== '/'
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] !== '/'
) {
namespace = this.namespace.substring(1);
}

if (
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] === '/'
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] === '/'
) {
namespace = this.namespace.substring(0, this.namespace.length - 1);
}

if (
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] !== '/'
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] !== '/'
) {
namespace = this.namespace;
}
Expand All @@ -243,33 +246,33 @@ export default class MswConfig {
// if there is a namespace and no urlPrefix
if (this.namespace && !this.urlPrefix) {
if (
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] === '/'
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] === '/'
) {
namespace = this.namespace.substring(0, this.namespace.length - 1);
}

if (
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] !== '/'
this.namespace[0] === '/' &&
this.namespace[this.namespace.length - 1] !== '/'
) {
namespace = this.namespace;
}

if (
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] === '/'
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] === '/'
) {
let namespaceSub = this.namespace.substring(
0,
this.namespace.length - 1
0,
this.namespace.length - 1
);
namespace = `/${namespaceSub}`;
}

if (
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] !== '/'
this.namespace[0] !== '/' &&
this.namespace[this.namespace.length - 1] !== '/'
) {
namespace = `/${this.namespace}`;
}
Expand All @@ -287,7 +290,7 @@ export default class MswConfig {
// otherwise, if there is a urlPrefix, use that as the beginning of the path
if (urlPrefix.length) {
fullPath +=
urlPrefix[urlPrefix.length - 1] === '/' ? urlPrefix : `${urlPrefix}/`;
urlPrefix[urlPrefix.length - 1] === '/' ? urlPrefix : `${urlPrefix}/`;
}

// add the namespace to the path
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"testing"
],
"scripts": {
"test": "yarn"
"test": "yarn",
"format": "yarn prettier --write .",
"format:check": "yarn prettier --check ."
},
"devDependencies": {
"miragejs": "~0.1.43",
"msw": "^0.39.2"
"msw": "^0.39.2",
"prettier": "^3.0.1"
},
"peerDependancies": {
"miragejs": "~0.1.43",
Expand Down
8 changes: 8 additions & 0 deletions prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-check

/** @type {import("prettier").Config} */
export default {
printWidth: 80,
singleQuote: true,
trailingComma: 'es5',
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ pretender@^3.4.7:
fake-xml-http-request "^2.1.2"
route-recognizer "^0.3.3"

prettier@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.1.tgz#65271fc9320ce4913c57747a70ce635b30beaa40"
integrity sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==

readable-stream@^3.4.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.0.tgz#337bbda3adc0706bd3e024426a286d4b4b2c9198"
Expand Down