Skip to content

Commit 1b3c200

Browse files
prettier
1 parent e73b98f commit 1b3c200

File tree

5 files changed

+176
-142
lines changed

5 files changed

+176
-142
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"jasmine": "jasmine",
1010
"build": "rm -rf ./build/ && npx tsc",
1111
"server": "nodemon src/server.ts",
12-
"prettier": "prettier --config ./.prettierrc --write ./src/**.ts",
12+
"prettier": "prettier --config ./.prettierrc --write ./src/*/**.ts",
1313
"lint": "eslint ./src/"
1414
},
1515
"author": "AhmedZein",
@@ -19,7 +19,6 @@
1919
"sharp": "^0.30.7"
2020
},
2121
"devDependencies": {
22-
"supertest": "^6.2.4",
2322
"@types/express": "^4.17.13",
2423
"@types/jasmine": "^4.0.3",
2524
"@types/node": "^18.0.6",
@@ -28,13 +27,14 @@
2827
"@types/supertest": "^2.0.12",
2928
"@typescript-eslint/eslint-plugin": "^5.30.7",
3029
"@typescript-eslint/parser": "^5.30.7",
31-
"eslint": "^8.20.0",
32-
"nodemon": "^2.0.19",
33-
"ts-node": "^10.9.1",
34-
"typescript": "^4.7.4",
3530
"dotenv": "^16.0.1",
31+
"eslint": "^8.20.0",
3632
"jasmine": "^4.2.1",
3733
"jasmine-spec-reporter": "^7.0.0",
38-
"prettier": "^2.7.1"
34+
"nodemon": "^2.0.19",
35+
"prettier": "^2.7.1",
36+
"supertest": "^6.2.4",
37+
"ts-node": "^10.9.1",
38+
"typescript": "^4.7.4"
3939
}
4040
}

src/routes/image.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import express from 'express';
22
import validate from '../middlewares/api.image.Middleware/validate';
3-
import isExist from '../middlewares/api.image.Middleware/isExist' ;
3+
import isExist from '../middlewares/api.image.Middleware/isExist';
44
import createImage from '../middlewares/api.image.Middleware/createImage';
55

66
const route = express.Router();
77

8+
route.get(
9+
'/',
10+
validate,
11+
isExist,
12+
createImage,
13+
(req: express.Request, res: express.Response) => {
14+
// The requested data will come as a req.query object
15+
// first check if the params is valid by the middlware validate
16+
// if the request is validate 'next()'
17+
// check the chashed folder for existing photo
18+
}
19+
);
820

9-
route.get ( '/' , validate, isExist , createImage ,(req : express.Request ,res : express.Response)=>{
10-
11-
// The requested data will come as a req.query object
12-
// first check if the params is valid by the middlware validate
13-
// if the request is validate 'next()'
14-
// check the chashed folder for existing photo
15-
});
16-
17-
18-
export default route ;
21+
export default route;

src/server.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
import express from "express";
2-
import imageApi from "./routes/image";
3-
import dotenv from "dotenv";
1+
import express from 'express';
2+
import imageApi from './routes/image';
3+
import dotenv from 'dotenv';
44

55
dotenv.config();
66

7-
const port = process.env.PORT || 8000 ;
7+
const port = process.env.PORT || 8000;
88
const server = express();
99

10+
server.use('/api/image', imageApi);
1011

11-
server.use ('/api/image' , imageApi );
12-
13-
server.get('/' , (req : express.Request , res : express.Response )=>{
14-
15-
res.status(200).send("The root of this server nothing will be returned")
16-
12+
server.get('/', (req: express.Request, res: express.Response) => {
13+
res.status(200).send('The root of this server nothing will be returned');
1714
});
1815

19-
server.listen (port , ()=>{
20-
console.log ( `Server is now online at port ${port}` );
21-
})
16+
server.listen(port, () => {
17+
console.log(`Server is now online at port ${port}`);
18+
});
2219

23-
export default server ;
20+
export default server;

src/test/imageSpec.ts

Lines changed: 119 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,139 @@
1-
import server from '../server';
1+
import server from '../server';
22
import supertest from 'supertest';
33
import resizeImage from '../utilize/sharp';
44
import { getImageMetaData } from '../utilize/sharp';
55

6-
const response = supertest ( server ) ;
6+
const response = supertest(server);
77

8-
9-
describe('these tests are focused at the validate middleware a failure tests at /api/image/?' , ()=>{
8+
describe('these tests are focused at the validate middleware a failure tests at /api/image/?', () => {
109
const queriesSchema = {
11-
allNull : '',
12-
onlyNameAndWrong : '?name=ahmedzein',
10+
allNull: '',
11+
onlyNameAndWrong: '?name=ahmedzein',
1312
onlyNameAndCorrect: '?name=smtp1',
14-
onlywidthAndWrong : '?width=aasd',
15-
onlywidthAndCorrect : '?width=407',
16-
onlyHeightAndWrong : '?height=qwe',
17-
onlyHeightAndCorrect : '?height=365',
18-
nameAndWidthWrong : '?name=ahmedzein&width=aasd',
19-
nameAndWidthCorrect : '?name=smtp1&width=407',
20-
nameAndHeightWrong :'?name=ahmedzein&height=qwe',
21-
nameAndHeightCorrect :'?name=smtp1&height=365',
22-
WidthAndHeightWrong : '?width=aasd&height=qwe',
23-
WidthAndHeightCorrect : '?width=407&height=365',
13+
onlywidthAndWrong: '?width=aasd',
14+
onlywidthAndCorrect: '?width=407',
15+
onlyHeightAndWrong: '?height=qwe',
16+
onlyHeightAndCorrect: '?height=365',
17+
nameAndWidthWrong: '?name=ahmedzein&width=aasd',
18+
nameAndWidthCorrect: '?name=smtp1&width=407',
19+
nameAndHeightWrong: '?name=ahmedzein&height=qwe',
20+
nameAndHeightCorrect: '?name=smtp1&height=365',
21+
WidthAndHeightWrong: '?width=aasd&height=qwe',
22+
WidthAndHeightCorrect: '?width=407&height=365',
2423
};
25-
26-
it( '1.1 Query values is null or NaN or undefined ' , async () => {
27-
const responseObject = await response.get (`/api/image${queriesSchema.allNull}`);
28-
expect( responseObject.status ).toEqual (400);
29-
});
30-
it( '1.2 Query values only have a name value and its Wrong ' , async () => {
31-
const responseObject = await response.get (`/api/image?${queriesSchema.onlyNameAndWrong}`);
32-
expect( responseObject.status ).toEqual (400);
33-
});
34-
it( '1.3 Query values only have a name and its Correct ' , async () => {
35-
const responseObject = await response.get (`/api/image${queriesSchema.onlyNameAndCorrect}`);
36-
expect( responseObject.status ).toEqual (400);
37-
});
38-
it( '1.4 Query values only have a width and its Wrong' , async () => {
39-
const responseObject = await response.get (`/api/image${queriesSchema.onlywidthAndWrong}`);
40-
expect( responseObject.status ).toEqual (400);
41-
});
42-
it( '1.5 Query values only have a width and its Correct ' , async () => {
43-
const responseObject = await response.get (`/api/image${queriesSchema.onlywidthAndCorrect}`);
44-
expect( responseObject.status ).toEqual (400);
45-
});
46-
it( '1.6 Query values only have a height and its Wrong ' , async () => {
47-
const responseObject = await response.get (`/api/image${queriesSchema.onlyHeightAndWrong}`);
48-
expect( responseObject.status ).toEqual (400);
49-
});
50-
it( '1.7 Query values only have a height and its Correct ' , async () => {
51-
const responseObject = await response.get (`/api/image${queriesSchema.onlyHeightAndCorrect}`);
52-
expect( responseObject.status ).toEqual (400);
53-
});
54-
it( '1.7 Query values only have a name and width and its Wrong ' , async () => {
55-
const responseObject = await response.get (`/api/image${queriesSchema.nameAndWidthWrong}`);
56-
expect( responseObject.status ).toEqual (400);
57-
});
58-
it( '1.8 Query values only have a name and width and its Correct ' , async () => {
59-
const responseObject = await response.get (`/api/image${queriesSchema.nameAndWidthCorrect}`);
60-
expect( responseObject.status ).toEqual (400);
61-
});
62-
it( '1.9 Query values only have a name and height and its Wrong ' , async () => {
63-
const responseObject = await response.get (`/api/image${queriesSchema.nameAndHeightWrong}`);
64-
expect( responseObject.status ).toEqual (400);
65-
});
66-
it( '2.0 Query values only have a name and height and its Correct ' , async () => {
67-
const responseObject = await response.get (`/api/image${queriesSchema.nameAndHeightCorrect}`);
68-
expect( responseObject.status ).toEqual (400);
69-
});
70-
it( '2.1 Query values only have a width and height and its Wrong ' , async () => {
71-
const responseObject = await response.get (`/api/image${queriesSchema.WidthAndHeightWrong}`);
72-
expect( responseObject.status ).toEqual (400);
73-
});
74-
it( '2.2 Query values only have a width and height and its Correct ' , async () => {
75-
const responseObject = await response.get (`/api/image${queriesSchema.WidthAndHeightCorrect}`);
76-
expect( responseObject.status ).toEqual (400);
77-
});
7824

25+
it('1.1 Query values is null or NaN or undefined ', async () => {
26+
const responseObject = await response.get(
27+
`/api/image${queriesSchema.allNull}`
28+
);
29+
expect(responseObject.status).toEqual(400);
30+
});
31+
it('1.2 Query values only have a name value and its Wrong ', async () => {
32+
const responseObject = await response.get(
33+
`/api/image?${queriesSchema.onlyNameAndWrong}`
34+
);
35+
expect(responseObject.status).toEqual(400);
36+
});
37+
it('1.3 Query values only have a name and its Correct ', async () => {
38+
const responseObject = await response.get(
39+
`/api/image${queriesSchema.onlyNameAndCorrect}`
40+
);
41+
expect(responseObject.status).toEqual(400);
42+
});
43+
it('1.4 Query values only have a width and its Wrong', async () => {
44+
const responseObject = await response.get(
45+
`/api/image${queriesSchema.onlywidthAndWrong}`
46+
);
47+
expect(responseObject.status).toEqual(400);
48+
});
49+
it('1.5 Query values only have a width and its Correct ', async () => {
50+
const responseObject = await response.get(
51+
`/api/image${queriesSchema.onlywidthAndCorrect}`
52+
);
53+
expect(responseObject.status).toEqual(400);
54+
});
55+
it('1.6 Query values only have a height and its Wrong ', async () => {
56+
const responseObject = await response.get(
57+
`/api/image${queriesSchema.onlyHeightAndWrong}`
58+
);
59+
expect(responseObject.status).toEqual(400);
60+
});
61+
it('1.7 Query values only have a height and its Correct ', async () => {
62+
const responseObject = await response.get(
63+
`/api/image${queriesSchema.onlyHeightAndCorrect}`
64+
);
65+
expect(responseObject.status).toEqual(400);
66+
});
67+
it('1.7 Query values only have a name and width and its Wrong ', async () => {
68+
const responseObject = await response.get(
69+
`/api/image${queriesSchema.nameAndWidthWrong}`
70+
);
71+
expect(responseObject.status).toEqual(400);
72+
});
73+
it('1.8 Query values only have a name and width and its Correct ', async () => {
74+
const responseObject = await response.get(
75+
`/api/image${queriesSchema.nameAndWidthCorrect}`
76+
);
77+
expect(responseObject.status).toEqual(400);
78+
});
79+
it('1.9 Query values only have a name and height and its Wrong ', async () => {
80+
const responseObject = await response.get(
81+
`/api/image${queriesSchema.nameAndHeightWrong}`
82+
);
83+
expect(responseObject.status).toEqual(400);
84+
});
85+
it('2.0 Query values only have a name and height and its Correct ', async () => {
86+
const responseObject = await response.get(
87+
`/api/image${queriesSchema.nameAndHeightCorrect}`
88+
);
89+
expect(responseObject.status).toEqual(400);
90+
});
91+
it('2.1 Query values only have a width and height and its Wrong ', async () => {
92+
const responseObject = await response.get(
93+
`/api/image${queriesSchema.WidthAndHeightWrong}`
94+
);
95+
expect(responseObject.status).toEqual(400);
96+
});
97+
it('2.2 Query values only have a width and height and its Correct ', async () => {
98+
const responseObject = await response.get(
99+
`/api/image${queriesSchema.WidthAndHeightCorrect}`
100+
);
101+
expect(responseObject.status).toEqual(400);
102+
});
79103
});
80104

81105
describe('these tests are focused at the utilize/sharp file ', () => {
82-
const testImageNameFailure = 'fjod' ;
83-
const testImageWidth = 1500 ;
84-
const testImageHeight = 1500 ;
85-
it ('1.1 The resizeImage function,Wrong image name .toBeRejected ',async ()=>{
86-
87-
await expectAsync( resizeImage (testImageNameFailure , testImageWidth , testImageHeight ) ).toBeRejected();
88-
89-
});
90-
it ('1.2 The getImageMetaData function,Wrong image name .toBeRejected ',async ()=>{
91-
92-
await expectAsync( getImageMetaData ( testImageNameFailure ) ).toBeRejected();
93-
106+
const testImageNameFailure = 'fjod';
107+
const testImageWidth = 1500;
108+
const testImageHeight = 1500;
109+
it('1.1 The resizeImage function,Wrong image name .toBeRejected ', async () => {
110+
await expectAsync(
111+
resizeImage(testImageNameFailure, testImageWidth, testImageHeight)
112+
).toBeRejected();
113+
});
114+
it('1.2 The getImageMetaData function,Wrong image name .toBeRejected ', async () => {
115+
await expectAsync(
116+
getImageMetaData(testImageNameFailure)
117+
).toBeRejected();
94118
});
95119
});
96120

97-
describe('these tests are focused at the createImage middleware at the middleware/api.image.Middleware/createImage.ts ' , ()=>{
98-
99-
const queriesSchema = {
100-
AllCorrect : '?name=encenadaport&width=407&height=365',
101-
nameAndWidthAndHeightWrong : '?name=ahmedzein&width=500&height=1000'
121+
describe('these tests are focused at the createImage middleware at the middleware/api.image.Middleware/createImage.ts ', () => {
122+
const queriesSchema = {
123+
AllCorrect: '?name=encenadaport&width=407&height=365',
124+
nameAndWidthAndHeightWrong: '?name=ahmedzein&width=500&height=1000',
102125
};
103126

104-
it( '1.1 All query values are Correct status code Equal to 200' , async () => {
105-
const responseObject = await response.get (`/api/image${queriesSchema.AllCorrect}`);
106-
expect( responseObject.status ).toEqual (200);
127+
it('1.1 All query values are Correct status code Equal to 200', async () => {
128+
const responseObject = await response.get(
129+
`/api/image${queriesSchema.AllCorrect}`
130+
);
131+
expect(responseObject.status).toEqual(200);
107132
});
108-
it( '1.2 All query values are Wrong Equal 404 resource was not found' , async () => {
109-
const responseObject = await response.get (`/api/image${queriesSchema.nameAndWidthAndHeightWrong}`);
110-
expect( responseObject.status ).toEqual (404);
133+
it('1.2 All query values are Wrong Equal 404 resource was not found', async () => {
134+
const responseObject = await response.get(
135+
`/api/image${queriesSchema.nameAndWidthAndHeightWrong}`
136+
);
137+
expect(responseObject.status).toEqual(404);
111138
});
112139
});

src/utilize/sharp.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
import sharp from 'sharp';
22
import path from 'path';
3-
const fullLocation = path.join (__dirname , '../../full');
4-
const thumbLocation = path.join (__dirname ,'../../thumb' ) ;
3+
const fullLocation = path.join(__dirname, '../../full');
4+
const thumbLocation = path.join(__dirname, '../../thumb');
55

6-
export default async function resizeImage( requiredImage : string , width : number , height : number) : Promise<void>{
7-
const imageLocation = path.join (fullLocation ,requiredImage );
8-
const saveLocation = path.join (thumbLocation , requiredImage);
6+
export default async function resizeImage(
7+
requiredImage: string,
8+
width: number,
9+
height: number
10+
): Promise<void> {
11+
const imageLocation = path.join(fullLocation, requiredImage);
12+
const saveLocation = path.join(thumbLocation, requiredImage);
913
try {
10-
await sharp (imageLocation).resize( width , height ,{
11-
fit : 'contain'
12-
}).toFile(saveLocation);
13-
}
14-
catch (error) {
14+
await sharp(imageLocation)
15+
.resize(width, height, {
16+
fit: 'contain',
17+
})
18+
.toFile(saveLocation);
19+
} catch (error) {
1520
throw error;
1621
}
1722
}
1823

19-
export async function getImageMetaData (imageName : string) : Promise<sharp.Metadata> {
20-
24+
export async function getImageMetaData(
25+
imageName: string
26+
): Promise<sharp.Metadata> {
2127
try {
22-
const imageMetaData = await sharp (`${thumbLocation}\\${imageName}`).metadata();
23-
return imageMetaData ;
24-
}
25-
catch (error){
26-
throw error ;
28+
const imageMetaData = await sharp(
29+
`${thumbLocation}\\${imageName}`
30+
).metadata();
31+
return imageMetaData;
32+
} catch (error) {
33+
throw error;
2734
}
28-
}
35+
}

0 commit comments

Comments
 (0)