Skip to content

Commit

Permalink
Setup ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
amattu2 committed Oct 13, 2024
1 parent 41ab063 commit 383ce02
Show file tree
Hide file tree
Showing 8 changed files with 11,270 additions and 10,642 deletions.
21,739 changes: 11,158 additions & 10,581 deletions package-lock.json

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions packages/backend/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @type {import("eslint").Linter.Config}
*/
const config = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 11,
project: './tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'prettier'],
root: true,
rules: {
/* base prettier rule */
'prettier/prettier': 'error',

'max-len': 'off',
'no-console': 'warn',
'no-param-reassign': 'off',
'object-curly-newline': 'off',
'no-underscore-dangle': ['off'],
'arrow-body-style': ['warn'],
'eol-last': ['warn'],
'no-unreachable': ['warn'],
'no-continue': 'off',

/* typescript-eslint overwritten rules */
'no-use-before-define': 'off',
'no-unused-vars': 'off',
'no-loss-of-precision': 'off',
'no-shadow': 'off',
'no-empty-function': 'off',

/* typescript-eslint rules */
'@typescript-eslint/no-empty-function': 'error',
'@typescript-eslint/no-use-before-define': 'error',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-loss-of-precision': 'error',
'@typescript-eslint/no-redundant-type-constituents': 'error',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'error',
'@typescript-eslint/no-shadow': 'off',
'@typescript-eslint/dot-notation': 'off',
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
},
};

module.exports = config;
11 changes: 9 additions & 2 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "@better-vinwiki/backend",
"version": "1.0.3",
"private": true,
"private": false,
"scripts": {
"deploy": "wrangler deploy",
"start": "wrangler dev",
"test": "vitest",
"test:ci": "vitest --coverage",
"lint": "eslint --ext .ts,.js src",
"lint:ci": "eslint --ext .ts,.js src --max-warnings 0",
"lint:fix": "eslint --ext .ts,.js src --fix",
"cf-typegen": "wrangler types",
"typecheck": "tsc --noEmit"
},
Expand All @@ -18,7 +19,13 @@
"@vitest/coverage-istanbul": "^2.0.5",
"typescript": "^5.5.2",
"vitest": "2.0.5",
"wrangler": "^3.60.3"
"wrangler": "^3.60.3",
"eslint": "^8.55.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.3.3"
},
"dependencies": {
"itty-router": "^5.0.18"
Expand Down
64 changes: 30 additions & 34 deletions packages/backend/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { describe, expect, it, vi } from "vitest";
import router from "./index";
import { describe, expect, it } from 'vitest';
import router from './index';

const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;

const env: Env = {};

const ctx: ExecutionContext = {
waitUntil: (promise: Promise<any>): void => {
throw new Error("Function not implemented.");
waitUntil: (): void => {
throw new Error('Function not implemented.');
},
passThroughOnException: (): void => {
throw new Error("Function not implemented.");
}
throw new Error('Function not implemented.');
},
};

describe("Basic Functionality", () => {
it("should return a 404 error if the route is not found", async () => {
const request = new IncomingRequest("https://mock-test-url/this-route-does-not-exist", {
method: "GET",
describe('Basic Functionality', () => {
it('should return a 404 error if the route is not found', async () => {
const request = new IncomingRequest('https://mock-test-url/this-route-does-not-exist', {
method: 'GET',
});

const response = await router.fetch(request, env, ctx);

expect(response).toMatchObject({ status: 404 });
});

it("should return a 405 error if the method is not allowed for the route", async () => {
const request = new IncomingRequest("https://mock-test-url/this-route-does-not-exist", {
method: "DELETE",
it('should return a 405 error if the method is not allowed for the route', async () => {
const request = new IncomingRequest('https://mock-test-url/this-route-does-not-exist', {
method: 'DELETE',
});

const response = await router.fetch(request, env, ctx);
Expand All @@ -36,10 +36,10 @@ describe("Basic Functionality", () => {
});
});

describe("Golo365 Route", () => {
it("should handle a valid request to the route", async () => {
const request = new IncomingRequest("https://mock-test-url/api/v1/vin/ZARFANBN2R7681933/golo365", {
method: "GET",
describe('Golo365 Route', () => {
it('should handle a valid request to the route', async () => {
const request = new IncomingRequest('https://mock-test-url/api/v1/vin/ZARFANBN2R7681933/golo365', {
method: 'GET',
});

const response = await router.fetch(request, env, ctx);
Expand All @@ -49,30 +49,26 @@ describe("Golo365 Route", () => {
// TODO: Mock handler to avoid actually calling external service
});

it("should return a 400 error if the VIN is invalid", async () => {
const request = new IncomingRequest("https://mock-test-url/api/v1/vin/INVALIDVIN/golo365", {
method: "GET",
it('should return a 400 error if the VIN is invalid', async () => {
const request = new IncomingRequest('https://mock-test-url/api/v1/vin/INVALIDVIN/golo365', {
method: 'GET',
});

const response = await router.fetch(request, env, ctx);

expect(response).toMatchObject({ status: 400 });
});

it.each<string>([
"POST",
"DELETE",
"HEAD",
"PUT",
"PATCH",
"OPTIONS",
])("should return a 405 error if the method is not allowed for the route", async (method) => {
const request = new IncomingRequest("https://mock-test-url/api/v1/vin/ZARFANBN2R7681933/golo365", {
method,
});
it.each<string>(['POST', 'DELETE', 'HEAD', 'PUT', 'PATCH', 'OPTIONS'])(
'should return a 405 error if the method is not allowed for the route',
async (method) => {
const request = new IncomingRequest('https://mock-test-url/api/v1/vin/ZARFANBN2R7681933/golo365', {
method,
});

const response = await router.fetch(request, env, ctx);
const response = await router.fetch(request, env, ctx);

expect(response).toMatchObject({ status: 405 });
});
expect(response).toMatchObject({ status: 405 });
},
);
});
13 changes: 7 additions & 6 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { error, json } from "itty-router";
import router from "./router";
import { error, json } from 'itty-router';
import router from './router';

export default {
fetch: (request, ...args) => router
.fetch(request, ...args)
.then(json)
.catch(error),
fetch: (request, ...args) =>
router
.fetch(request, ...args)
.then(json)
.catch(error),
} satisfies ExportedHandler<Env>;
27 changes: 11 additions & 16 deletions packages/backend/src/middleware/withVinValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import withVinValidation from './withVinValidation';
import { Router } from 'itty-router';

describe('withVinValidation Middleware', () => {
it.each<string>([
"ZHWGC7AJ3ELA13895",
"2T2HA31U74C006100",
"5YJSA1E5XNF479116",
])("should forward the request if the VIN is valid", async (vin) => {
const router = Router();
const request = { method: 'GET', url: `https://mock-url-test.com/${vin}` };
const handler = vi.fn(({ vin }) => ({ vin }));
it.each<string>(['ZHWGC7AJ3ELA13895', '2T2HA31U74C006100', '5YJSA1E5XNF479116'])(
'should forward the request if the VIN is valid',
async (vin) => {
const router = Router();
const request = { method: 'GET', url: `https://mock-url-test.com/${vin}` };
const handler = vi.fn(({ vin }) => ({ vin }));

await router.get('/:vin', withVinValidation, handler).fetch(request);
await router.get('/:vin', withVinValidation, handler).fetch(request);

expect(handler).toHaveBeenCalledWith(expect.objectContaining({ params: { vin } }));
});
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ params: { vin } }));
},
);

it("should transform the VIN to uppercase if it's valid", async () => {
const router = Router();
Expand All @@ -27,11 +26,7 @@ describe('withVinValidation Middleware', () => {
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ params: { vin: '5YJSA1E5XNF479116' } }));
});

it.each<string>([
"not a vin",
"ZHWGC7AJ3ELA1389",
"-",
])("should return a 400 error if the VIN is not 17 characters long", async (vin) => {
it.each<string>(['not a vin', 'ZHWGC7AJ3ELA1389', '-'])('should return a 400 error if the VIN is not 17 characters long', async (vin) => {
const router = Router();
const request = { method: 'GET', url: `https://mock-url-test.com/${vin}` };
const handler = vi.fn(({ vin }) => ({ vin }));
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/middleware/withVinValidation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error, type RequestHandler } from "itty-router";
import { error, type RequestHandler } from 'itty-router';

/**
* Middleware to perform VIN validation on the request
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IttyRouter, IttyRouterType, error, withParams } from 'itty-router'
import { IttyRouter, IttyRouterType, error, withParams } from 'itty-router';
import withVinValidation from './middleware/withVinValidation';

/**
Expand All @@ -7,7 +7,7 @@ import withVinValidation from './middleware/withVinValidation';
const router: IttyRouterType = IttyRouter();

router
.get('/api/v1/vin/:vin/golo365', withParams, withVinValidation, ({ vin }) => ({ message: `${vin}`}))
.get('/api/v1/vin/:vin/golo365', withParams, withVinValidation, ({ vin }) => ({ message: `${vin}` }))
.get('*', () => error(404, 'Not found'))
.all('*', () => error(405, 'Method not allowed'));

Expand Down

0 comments on commit 383ce02

Please sign in to comment.