Skip to content

Commit

Permalink
Merge pull request #87 from DigiChanges/feat/LC/transformer-async
Browse files Browse the repository at this point in the history
feat: transformer async
  • Loading branch information
SmartNetAR authored Feb 17, 2022
2 parents de76fae + cdc78db commit f74763d
Show file tree
Hide file tree
Showing 30 changed files with 816 additions and 2,115 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"author": "Digichanges",
"license": "MIT",
"dependencies": {
"@digichanges/shared-experience": "^0.7.1",
"@digichanges/shared-experience": "^0.8.0",
"@koa/multer": "^3.0.0",
"@mikro-orm/core": "^5.0.0-dev.636",
"@mikro-orm/postgresql": "^5.0.0-dev.636",
Expand Down Expand Up @@ -114,7 +114,7 @@
"devDependencies": {
"@commitlint/cli": "^13.2.1",
"@commitlint/config-conventional": "^13.2.0",
"@parcel/transformer-typescript-tsc": "^2.0.0",
"@parcel/transformer-typescript-tsc": "^2.3.1",
"@shelf/jest-mongodb": "^2.1.0",
"@types/bcrypt": "^3.0.1",
"@types/bcryptjs": "^2.4.2",
Expand Down Expand Up @@ -166,7 +166,7 @@
"lint-staged": "^11.2.6",
"nodemon": "^2.0.7",
"npm-run-all": "^4.1.5",
"parcel": "^2.0.1",
"parcel": "^2.3.1",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"supertest": "^6.1.3",
Expand Down
4 changes: 2 additions & 2 deletions src/App/Presentation/Handlers/Express/IndexHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class IndexHandler extends BaseHttpController
private responder: Responder;

@httpGet('/')
public index()
public async index()
{
const locales = Locales.getInstance().getLocales();
return this.responder.send({ message: locales.__('greetings') }, this.httpContext.request, this.httpContext.response, StatusCode.HTTP_OK, null);
return await this.responder.send({ message: locales.__('greetings') }, this.httpContext.request, this.httpContext.response, StatusCode.HTTP_OK, null);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/App/Presentation/Handlers/Koa/IndexHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const responder: Responder = new Responder();

IndexHandler.get('/', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send('Welcome to Node Experience', ctx, StatusCode.HTTP_OK);
void await responder.send('Welcome to Node Experience', ctx, StatusCode.HTTP_OK);
});

export default IndexHandler;
8 changes: 4 additions & 4 deletions src/App/Presentation/Shared/Express/Responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Responder
@inject(TYPES.IFormatResponder)
private formatResponder: IFormatResponder;

public send(data: any, request: Request | any, response: Response, status: IHttpStatusCode, transformer: Transformer = null)
public async send(data: any, request: Request | any, response: Response, status: IHttpStatusCode, transformer: Transformer = null)
{
let metadata = null;

Expand All @@ -32,7 +32,7 @@ class Responder
return response.status(status.code).send({ data: { ...data, metadata } });
}

data = transformer.handle(data);
data = await transformer.handle(data);

response.status(status.code).send(this.formatResponder.getFormatData(data, status, metadata));
}
Expand All @@ -53,12 +53,12 @@ class Responder
});
}

result.data = transformer.handle(data);
result.data = await transformer.handle(data);

if (paginator.getExist())
{
const paginatorTransformer = new PaginatorTransformer();
paginator = paginatorTransformer.handle(paginator);
paginator = await paginatorTransformer.handle(paginator);

const pagination = { pagination: paginator };

Expand Down
8 changes: 4 additions & 4 deletions src/App/Presentation/Shared/Koa/Responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Responder
this.formatError = new FormatError();
}

public send(data: any, ctx: Koa.ParameterizedContext, status: IHttpStatusCode, transformer: Transformer = null)
public async send(data: any, ctx: Koa.ParameterizedContext, status: IHttpStatusCode, transformer: Transformer = null)
{
if (!transformer)
{
Expand All @@ -28,7 +28,7 @@ class Responder
};
}

data = transformer.handle(data);
data = await transformer.handle(data);

ctx.status = status.code;
return ctx.body = this.formatResponder.getFormatData(data, status, null);
Expand All @@ -49,12 +49,12 @@ class Responder
};
}

result.data = transformer.handle(data);
result.data = await transformer.handle(data);

if (paginator.getExist())
{
const paginatorTransformer = new PaginatorTransformer();
paginator = paginatorTransformer.handle(paginator);
paginator = await paginatorTransformer.handle(paginator);

const pagination = { pagination: paginator };

Expand Down
2 changes: 1 addition & 1 deletion src/App/Presentation/Transformers/DefaultTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Transformer } from '@digichanges/shared-experience';

class DefaultTransformer extends Transformer
{
public transform(data: any)
public async transform(data: any)
{
return data;
}
Expand Down
21 changes: 11 additions & 10 deletions src/App/Tests/Express/WhiteListHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,60 @@ class WhiteListHandler
@httpGet('/countries')
public async testEqualOne(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpGet('/all/:id')
public async testAllGet(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpPost('/all/hello/world')
public async testAllPost(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpPut('/all/numeric/123')
public async testAllPut(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpDelete('/all/:id/delete')
public async testAllDelete(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpGet('/countries/:id')
public async testDynamicEqual(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpGet('/countries/:id/states')
public async testDynamicOne(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpGet('/countries/:id/states/:stateId/cities')
public async testDynamicTwo(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpGet('/cities/:id/countries/:stateId/states')
public async testDynamicUntidy(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}

@httpGet('/:id/hello/all')
public async testAllUntidy(@request() req: Request, @response() res: Response, @next() nex: NextFunction)
{
this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
void await this.responder.send({ message: 'hello world' }, null, res, StatusCode.HTTP_OK);
}
}
20 changes: 10 additions & 10 deletions src/App/Tests/Koa/WhiteListHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,52 @@ const responder: Responder = new Responder();

WhiteListHandler.get('/countries', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.get('/all/:id', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.post('/all/hello/world', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.put('/all/numeric/123', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.delete('/all/:id/delete', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.get('/countries/:id', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.get('/countries/:id/states', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.get('/countries/:id/states/:stateId/cities', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.get('/cities/:id/countries/:stateId/states', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

WhiteListHandler.get('/:id/hello/all', async(ctx: Koa.ParameterizedContext & any) =>
{
responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
void await responder.send({ message: 'hello world' }, ctx, StatusCode.HTTP_OK);
});

export default WhiteListHandler;
26 changes: 13 additions & 13 deletions src/Auth/Presentation/Handlers/Express/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AuthHandler
@httpGet('/me')
public async me(@request() req: Request, @response() res: Response): Promise<void>
{
this.responder.send(AuthUser(req), null, res, StatusCode.HTTP_OK, new UserTransformer());
void await this.responder.send(AuthUser(req), null, res, StatusCode.HTTP_OK, new UserTransformer());
}

@httpPut('/me')
Expand All @@ -51,7 +51,7 @@ class AuthHandler
const _request = new UpdateMeRequest(req.body);
const payload = await this.controller.updateMe(_request, AuthUser(req));

this.responder.send(payload, req, res, StatusCode.HTTP_OK, new UserTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_OK, new UserTransformer());
}

@httpPost('/login')
Expand All @@ -71,7 +71,7 @@ class AuthHandler
httpOnly: true
});

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new AuthTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new AuthTransformer());
}

@httpPost('/signup')
Expand All @@ -81,7 +81,7 @@ class AuthHandler

const payload = await this.controller.register(_request);

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new DefaultTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new DefaultTransformer());
}

@httpPost('/logout')
Expand All @@ -91,7 +91,7 @@ class AuthHandler

res.cookie('refreshToken', null);

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new DefaultTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new DefaultTransformer());
}

@httpPost('/refresh-token', RefreshTokenMiddleware)
Expand All @@ -111,7 +111,7 @@ class AuthHandler
httpOnly: true
});

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new AuthTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new AuthTransformer());
}

@httpPost('/forgot-password')
Expand All @@ -121,7 +121,7 @@ class AuthHandler

const payload = await this.controller.forgotPassword(_request);

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, null);
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, null);
}

@httpPost('/change-forgot-password')
Expand All @@ -131,7 +131,7 @@ class AuthHandler

const payload = await this.controller.changeForgotPassword(_request);

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, null);
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, null);
}

@httpPut('/verify-your-account/:confirmationToken')
Expand All @@ -141,22 +141,22 @@ class AuthHandler

const payload = await this.controller.verifyYourAccount(_request);

this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new DefaultTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_CREATED, new DefaultTransformer());
}

@httpGet('/permissions', AuthorizeMiddleware(Permissions.GET_PERMISSIONS))
public permissions(@request() req: Request, @response() res: Response)
public async permissions(@request() req: Request, @response() res: Response)
{
const payload = this.controller.permissions();

this.responder.send(payload, req, res, StatusCode.HTTP_OK, new PermissionsTransformer());
void await this.responder.send(payload, req, res, StatusCode.HTTP_OK, new PermissionsTransformer());
}

@httpPost('/sync-roles-permissions', AuthorizeMiddleware(Permissions.AUTH_SYNC_PERMISSIONS))
public syncRolesPermissions(@request() req: Request, @response() res: Response)
public async syncRolesPermissions(@request() req: Request, @response() res: Response)
{
this.controller.syncRolesPermissions();

this.responder.send({ message: 'Sync Successfully' }, req, res, StatusCode.HTTP_CREATED, null);
void await this.responder.send({ message: 'Sync Successfully' }, req, res, StatusCode.HTTP_CREATED, null);
}
}
Loading

0 comments on commit f74763d

Please sign in to comment.