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

Issue 32: Add a pre-commit hook for lint checking and styling #264

Merged
merged 15 commits into from
Oct 31, 2022
Merged
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
"browser": true,
"es2021": true
},
"extends": "standard-with-typescript",
"extends": ["prettier"],
"plugins": ["prettier"],
"overrides": [
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"prettier/prettier": ["error"]
}
}
32 changes: 16 additions & 16 deletions middleware/rate-limiter/RateLimiter.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { rateLimit } from 'express-rate-limit';
import { Request,Response } from 'express';
import { Request, Response } from 'express';
import rateLimitResponse from './consts/RateLimitResponse';

// WINDOW_SIZE and WINDOW_TIME can be configured in .env.example file
const RATELIMIT_WINDOWSIZE=parseInt(process.env.RATELIMIT_WINDOWSIZE);
const RATELIMIT_WINDOWTIME=parseInt(process.env.RATELIMIT_WINDOWTIME);
const RATELIMIT_WINDOWSIZE = parseInt(process.env.RATELIMIT_WINDOWSIZE);
const RATELIMIT_WINDOWTIME = parseInt(process.env.RATELIMIT_WINDOWTIME);

// middleware for rate limiting application usage.
export const applicationRateLimiter=rateLimit({
max:RATELIMIT_WINDOWSIZE,
windowMs:RATELIMIT_WINDOWTIME,
legacyHeaders:false,
standardHeaders:true, // sends standard headers after the limit get over
message:(req:Request,res:Response)=>{
const ratelimitObject:rateLimitResponse={
id: "too_many_requests",
generatedAt:Date.now(),
message: "API Rate limit exceeded."
}
export const applicationRateLimiter = rateLimit({
max: RATELIMIT_WINDOWSIZE,
windowMs: RATELIMIT_WINDOWTIME,
legacyHeaders: false,
standardHeaders: true, // sends standard headers after the limit get over
message: (req: Request, res: Response) => {
const ratelimitObject: rateLimitResponse = {
id: 'too_many_requests',
generatedAt: Date.now(),
message: 'API Rate limit exceeded.',
};
res.json(ratelimitObject).status(429);
}
});
},
});
2 changes: 1 addition & 1 deletion middleware/rate-limiter/consts/RateLimitResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ type rateLimitResponse = {
message: String;
};

export default rateLimitResponse;
export default rateLimitResponse;
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate",
"contributors:check": "all-contributors check",
"chuck": "ts-node modules\\chuck-norris\\utils\\scrape-facts.ts modules\\chuck-norris\\data\\chuckfacts.json"
"chuck": "ts-node modules\\chuck-norris\\utils\\scrape-facts.ts modules\\chuck-norris\\data\\chuckfacts.json",
"lint": "eslint . --ext .ts",
"lint-fix": "eslint . --fix --ext .ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,16 +43,19 @@
"@typescript-eslint/eslint-plugin": "5.41.0",
"all-contributors-cli": "6.24.0",
"eslint": "8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard-with-typescript": "23.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "15.3.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "6.1.1",
"nodemon": "2.0.20",
"prettier": "2.7.1",
"ts-jest": "29.0.3",
"ts-node": "10.9.1",
"typescript": "4.8.4",
"jest": "29.2.2"
"jest": "29.2.2",
"typescript-eslint": "^0.0.1-alpha.0"
},
"dependencies": {
"chucknorris-io": "^1.0.5",
Expand Down
14 changes: 14 additions & 0 deletions pre-commit.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit" and add it under `.git/hooks/`.

# Fixes and throws error if cannot
npm run lint-fix

# Redirect output to stderr.
exec 1>&2
3 changes: 1 addition & 2 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import app from './app'
import app from './app';

const port = 3000;

app.listen(port, () => {
console.log(`Mock API is running on port ${port}.`);
console.log(`Vist Mock API in the browser http://localhost:${port}.`);
});

32 changes: 16 additions & 16 deletions src/modules/address/consts/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@
* MockAddress:
* type: objcts
* properties:
* houseNumber:
* houseNumber:
* type: string
* example: 10
* addressLine1:
* addressLine1:
* type: string
* example: Flat 22b
* addressLine2:
* addressLine2:
* type: string
* example: Gladstone Road
* city:
* city:
* type: string
* example: London
* postcode:
* postcode:
* type: string
* example: TN22 7HL
* zipcode:
* zipcode:
* type: string
* example: 10234
* country:
* country:
* type: string
* example: uk
*/
type Address = {
houseNumber: string,
addressLine1: string,
addressLine2: string,
city: string,
postcode: string,
zipcode: string,
country: string
}
houseNumber: string;
addressLine1: string;
addressLine2: string;
city: string;
postcode: string;
zipcode: string;
country: string;
};

export default Address;
export default Address;
106 changes: 53 additions & 53 deletions src/modules/address/tests/utils/getRandomAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,64 @@ import getRandomAddresses from '../../utils/getRandomAddress';
import { faker } from '@faker-js/faker';

jest.mock('@faker-js/faker', () => ({
faker: {
address: {
buildingNumber: jest.fn().mockReturnValue('10'),
secondaryAddress: jest.fn().mockReturnValue('Flat 1'),
street: jest.fn().mockReturnValue('Something Road'),
cityName: jest.fn().mockReturnValue('Lahore'),
zipCode: jest.fn().mockReturnValue('2344'),
}
}
faker: {
address: {
buildingNumber: jest.fn().mockReturnValue('10'),
secondaryAddress: jest.fn().mockReturnValue('Flat 1'),
street: jest.fn().mockReturnValue('Something Road'),
cityName: jest.fn().mockReturnValue('Lahore'),
zipCode: jest.fn().mockReturnValue('2344'),
},
},
}));

describe('getRandomAddresses', () => {
afterEach(() => {
jest.clearAllMocks();
})
afterEach(() => {
jest.clearAllMocks();
});

it('should return a random USA address by default', () => {
const address = getRandomAddresses();
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(1);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(1);
expect(faker.address.street).toHaveBeenCalledTimes(1);
expect(faker.address.cityName).toHaveBeenCalledTimes(1);
expect(faker.address.zipCode).toHaveBeenCalledTimes(1);
})
it('should return a random USA address by default', () => {
const address = getRandomAddresses();
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(1);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(1);
expect(faker.address.street).toHaveBeenCalledTimes(1);
expect(faker.address.cityName).toHaveBeenCalledTimes(1);
expect(faker.address.zipCode).toHaveBeenCalledTimes(1);
});

it('should return a random UK address when passed the "uk" argument', () => {
const address = getRandomAddresses(1, 'uk');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(1);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(1);
expect(faker.address.street).toHaveBeenCalledTimes(1);
expect(faker.address.cityName).toHaveBeenCalledTimes(1);
expect(faker.address.zipCode).toHaveBeenCalledTimes(1);
})
it('should return a random UK address when passed the "uk" argument', () => {
const address = getRandomAddresses(1, 'uk');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(1);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(1);
expect(faker.address.street).toHaveBeenCalledTimes(1);
expect(faker.address.cityName).toHaveBeenCalledTimes(1);
expect(faker.address.zipCode).toHaveBeenCalledTimes(1);
});

it('should return a random USA address when passed the "usa" argument', () => {
const address = getRandomAddresses(1, 'usa');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(1);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(1);
expect(faker.address.street).toHaveBeenCalledTimes(1);
expect(faker.address.cityName).toHaveBeenCalledTimes(1);
expect(faker.address.zipCode).toHaveBeenCalledTimes(1);
})
it('should return a random USA address when passed the "usa" argument', () => {
const address = getRandomAddresses(1, 'usa');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(1);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(1);
expect(faker.address.street).toHaveBeenCalledTimes(1);
expect(faker.address.cityName).toHaveBeenCalledTimes(1);
expect(faker.address.zipCode).toHaveBeenCalledTimes(1);
});

it('should return USA format addresses equal to the number provided as "addressCount" parameter', () => {
const address = getRandomAddresses(5, 'usa');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(5);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(5);
expect(faker.address.street).toHaveBeenCalledTimes(5);
expect(faker.address.cityName).toHaveBeenCalledTimes(5);
expect(faker.address.zipCode).toHaveBeenCalledTimes(5);
})
it('should return USA format addresses equal to the number provided as "addressCount" parameter', () => {
const address = getRandomAddresses(5, 'usa');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(5);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(5);
expect(faker.address.street).toHaveBeenCalledTimes(5);
expect(faker.address.cityName).toHaveBeenCalledTimes(5);
expect(faker.address.zipCode).toHaveBeenCalledTimes(5);
});

it('should return UK format addresses equal to the number provided as "addressCount" parameter', () => {
const address = getRandomAddresses(3, 'uk');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(3);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(3);
expect(faker.address.street).toHaveBeenCalledTimes(3);
expect(faker.address.cityName).toHaveBeenCalledTimes(3);
expect(faker.address.zipCode).toHaveBeenCalledTimes(3);
})
})
it('should return UK format addresses equal to the number provided as "addressCount" parameter', () => {
const address = getRandomAddresses(3, 'uk');
expect(faker.address.buildingNumber).toHaveBeenCalledTimes(3);
expect(faker.address.secondaryAddress).toHaveBeenCalledTimes(3);
expect(faker.address.street).toHaveBeenCalledTimes(3);
expect(faker.address.cityName).toHaveBeenCalledTimes(3);
expect(faker.address.zipCode).toHaveBeenCalledTimes(3);
});
});
34 changes: 17 additions & 17 deletions src/modules/address/utils/getRandomAddress.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { faker } from '@faker-js/faker';
import Address from '../consts/Address';

const getRandomAddresses = (addressCount : number = 1, country : string = 'uk') : Address[] => {
const addresses : Address[] = [];

for (let index = 0; index < addressCount; index++) {
addresses.push({
houseNumber: faker.address.buildingNumber(),
addressLine1: faker.address.secondaryAddress(),
addressLine2: faker.address.street(),
city: faker.address.cityName(),
postcode: country !== "usa" ? faker.address.zipCode() : null,
zipcode: country === "usa" ? faker.address.zipCode() : null,
country
})
}
return addresses;
}
const getRandomAddresses = (addressCount: number = 1, country: string = 'uk'): Address[] => {
const addresses: Address[] = [];

export default getRandomAddresses;
for (let index = 0; index < addressCount; index++) {
addresses.push({
houseNumber: faker.address.buildingNumber(),
addressLine1: faker.address.secondaryAddress(),
addressLine2: faker.address.street(),
city: faker.address.cityName(),
postcode: country !== 'usa' ? faker.address.zipCode() : null,
zipcode: country === 'usa' ? faker.address.zipCode() : null,
country,
});
}
return addresses;
};

export default getRandomAddresses;
1 change: 0 additions & 1 deletion src/modules/animal/api/animal-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getQtyFromRequest } from '../../../utils/route-utils';
import AnimalType from '../consts/AnimalEnum';
import getSpeciesOfAnimal from '../utils/getSpeciesOfAnimal';


module.exports = function (app: core.Express) {
/**
* @openapi
Expand Down
4 changes: 2 additions & 2 deletions src/modules/animal/consts/AnimalEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum AnimalType {
Rabbit,
Rodent,
Snake,
Type
Type,
}

export default AnimalType
export default AnimalType;
1 change: 0 additions & 1 deletion src/modules/animal/tests/Api/animal-routes.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import request from 'supertest';
import app from '../../../../../app';


//These routes dont seem to provide consistent responses
// Its not consistent because the data is randomly generated. You will need to mock the data to get consistent responses.
describe.skip('animal api endpoints', () => {
Expand Down