Skip to content

Commit

Permalink
Merge pull request #47 from dxfrontier/feature/various
Browse files Browse the repository at this point in the history
  • Loading branch information
dragolea authored Apr 17, 2024
2 parents f4d23da + d17c95e commit e6c570e
Show file tree
Hide file tree
Showing 28 changed files with 110 additions and 89 deletions.
7 changes: 6 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Ignore artifacts
dist
test

test/e2e
test/integration
test/unit
test/util
test/bookshop/@cds-models
2 changes: 1 addition & 1 deletion lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type NextMiddleware = () => Promise<unknown>;
* Use `NextEvent` type to annotate the `next` parameter of the implementation of the `ON` events.
* @example "@Next() next: NextEvent"
*/
export type NextEvent = (req?: Request) => void;
export type NextEvent = (req?: Request) => Function;

export type MiddlewareImpl = {
use: (req: Request, next: NextMiddleware) => Promise<unknown>;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dxfrontier/cds-ts-dispatcher",
"version": "2.0.8",
"version": "2.0.9",
"description": "The goal of CDS-TS-Dispatcher is to significantly reduce the boilerplate code required to implement TS handlers provided by the SAP CAP framework.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ import {
Inject,
Req,
Result,
Service,
SRV,
TypedRequest,
} from '../../../../../../lib';
import { Promotion } from '../../../../@cds-models/AdminService';

import type { TypedRequest, Service } from '../../../../../../lib';

@EntityHandler(Promotion)
class PromotionHandler {
@Inject(SRV) private readonly srv: Service;

@AfterNewDraft()
public async afterNewDraft(@Result() result: Promotion, @Req() req: TypedRequest<Promotion>) {
public async afterNewDraft(@Result() result: Promotion, @Req() req: TypedRequest<Promotion>): Promise<void> {
req.notify(201, 'After new draft executed');
}

@AfterSaveDraft()
public async afterSaveDraft(@Result() result: Promotion, @Req() req: TypedRequest<Promotion>) {
public async afterSaveDraft(@Result() result: Promotion, @Req() req: TypedRequest<Promotion>): Promise<void> {
req.notify(201, 'After save draft executed');
}

@AfterEditDraft()
public async afterEditDraft(@Result() result: Promotion, @Req() req: TypedRequest<Promotion>) {
public async afterEditDraft(@Result() result: Promotion, @Req() req: TypedRequest<Promotion>): Promise<void> {
req.notify(201, 'After edit draft executed');
}

@AfterDeleteDraft()
public async afterDeleteDraft(@Result() deleted: boolean, @Req() req: TypedRequest<Promotion>) {
public async afterDeleteDraft(@Result() deleted: boolean, @Req() req: TypedRequest<Promotion>): Promise<void> {
// ...
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import {
EntityHandler,
Inject,
Req,
Service,
SRV,
TypedRequest,
} from '../../../../../../lib';
import { UserActivityLog } from '../../../../@cds-models/AdminService';

import type { Service, TypedRequest } from '../../../../../../lib';

@EntityHandler(UserActivityLog)
class UserActivityLogHandler {
@Inject(SRV) private readonly srv: Service;

@BeforeNewDraft()
public async beforeNewDraft(@Req() req: TypedRequest<UserActivityLog>) {
public async beforeNewDraft(@Req() req: TypedRequest<UserActivityLog>): Promise<void> {
req.notify(201, 'Before new draft executed');
}

@BeforeSaveDraft()
public async beforeSaveDraft(@Req() req: TypedRequest<UserActivityLog>) {
public async beforeSaveDraft(@Req() req: TypedRequest<UserActivityLog>): Promise<void> {
req.notify(201, 'Before save draft executed');
}

@BeforeEditDraft()
public async beforeEditDraft(@Req() req: TypedRequest<UserActivityLog>) {
public async beforeEditDraft(@Req() req: TypedRequest<UserActivityLog>): Promise<void> {
req.notify(201, 'Before edit draft executed');
}

@BeforeDeleteDraft()
public async beforeDeleteDraft(@Req() req: TypedRequest<UserActivityLog>) {
public async beforeDeleteDraft(@Req() req: TypedRequest<UserActivityLog>): Promise<void> {
// ...
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-confusing-void-expression */
import {
AfterRead,
AfterReadDraft,
Expand Down Expand Up @@ -54,7 +56,7 @@ class BookEventsHandler {
@Results() results: BookEvent[],
@Req() req: Request,
@SingleInstanceSwitch() isSingleInstance: boolean,
) {
): Promise<void> {
this.bookEventsService.handleSingleInstance(req, results, isSingleInstance);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-confusing-void-expression */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import {
AfterRead,
BeforeCreate,
Expand All @@ -24,13 +26,13 @@ class BookFormatsHandler {

@BeforeCreate()
@FieldsFormatter<BookFormat>({ action: 'blacklist', charsToRemove: 'le' }, 'format')
public async beforeCreate(@Req() req: TypedRequest<BookFormat>) {
public async beforeCreate(@Req() req: TypedRequest<BookFormat>): Promise<void> {
// ...
}

@BeforeUpdate()
@FieldsFormatter<BookFormat>({ action: 'truncate', options: { length: 7 } }, 'format')
public async beforeUpdate(@Req() req: TypedRequest<BookFormat>) {
public async beforeUpdate(@Req() req: TypedRequest<BookFormat>): Promise<void> {
// ...
}

Expand Down
14 changes: 7 additions & 7 deletions test/bookshop/srv/controller/cat-service/handler/BookHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class BookHandler {
@Inject(BookService) private readonly bookService: BookService;

@AfterCreate()
private async afterCreate(@Result() result: Book, @Req() req: Request) {
private async afterCreate(@Result() result: Book, @Req() req: Request): Promise<void> {
this.bookService.validateData(result, req);
}

@BeforeRead()
@Use(MiddlewareMethodBeforeRead)
private async beforeRead(@Req() req: TypedRequest<Book>) {
private async beforeRead(@Req() req: TypedRequest<Book>): Promise<void> {
this.bookService.showConsoleLog();
}

Expand All @@ -55,17 +55,17 @@ class BookHandler {
@IsPresent('SELECT', 'columns') hasColumns: boolean,
@IsRole('Developer', 'AnotherRole') role: boolean,
@GetRequest('locale') locale: Request['locale'],
) {
this.bookService.manageAfterReadMethods({ req, results, singleInstance });
): Promise<void> {
await this.bookService.manageAfterReadMethods({ req, results, singleInstance });
}

@AfterUpdate()
private async afterUpdate(@Result() result: Book, @Req() req: TypedRequest<Book>) {
this.bookService.addDefaultTitleText(result, req);
private async afterUpdate(@Result() result: Book, @Req() req: TypedRequest<Book>): Promise<void> {
await this.bookService.addDefaultTitleText(result, req);
}

@AfterDelete()
private async afterDelete(@Result() deleted: boolean, @Req() req: Request) {
private async afterDelete(@Result() deleted: boolean, @Req() req: Request): Promise<void> {
this.bookService.notifyItemDeleted(req, deleted);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class BookOrdersHandler {
@Inject(BookOrdersService) private readonly bookOrdersService: BookOrdersService;

@AfterCreate()
private async afterCreate(@Results() result: BookOrder, @Req() req: Request) {
private async afterCreate(@Results() result: BookOrder, @Req() req: Request): Promise<void> {
this.bookService.validateData(result, req);
}

@BeforeRead()
@Use(MiddlewareMethodBeforeRead) // THIS IS OK
private async beforeRead(req: Request) {
private async beforeRead(req: Request): Promise<void> {
this.bookOrdersService.showBeforeReadNotify();
}

Expand All @@ -46,7 +46,7 @@ class BookOrdersHandler {
@Results() results: BookOrder[],
@Req() req: Request,
@SingleInstanceSwitch() isSingleInstance: boolean,
) {
): Promise<void> {
// Method implementation
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-confusing-void-expression */
import {
BeforeCreate,
BeforeUpdate,
Expand Down Expand Up @@ -35,13 +37,13 @@ class BookRecommendationsHandler {

@OnCreate()
@Validate<BookRecommendation>({ action: 'isAlphanumeric' }, 'book_ID')
public async create(@Req() req: TypedRequest<BookRecommendation>, @Next() next: NextEvent) {
public async create(@Req() req: TypedRequest<BookRecommendation>, @Next() next: NextEvent): Promise<Function> {
return next();
}

@OnUpdate()
@Validate<BookRecommendation>({ action: 'isLength', options: { min: 5 } }, 'comment')
public async update(@Req() req: TypedRequest<BookRecommendation>, @Next() next: NextEvent) {
public async update(@Req() req: TypedRequest<BookRecommendation>, @Next() next: NextEvent): Promise<Function> {
return next();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
EntityHandler,
ExecutionAllowedForRole,
GetQuery,
GetQueryType,
GetRequest,
Inject,
IsColumnSupplied,
Expand All @@ -21,6 +20,8 @@ import {
} from '../../../../../../lib';
import BookSalesService from '../../../service/BookSalesService';

import type { GetQueryType } from '../../../../../../lib';

@EntityHandler(BookSale)
class BookSalesHandler {
@Inject(SRV) private readonly srv: Service;
Expand Down Expand Up @@ -49,7 +50,7 @@ class BookSalesHandler {
@SingleInstanceSwitch() isSingleInstance: boolean,

@Jwt() token: string | undefined,
) {
): Promise<void> {
this.bookSalesService.showAfterReadNotifies({
req,
hasRoles,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-confusing-void-expression */
import {
AfterRead,
ActionRequest,
ActionReturn,
EntityHandler,
Inject,
Next,
NextEvent,
OnBoundAction,
OnBoundFunction,
OnCreate,
OnDelete,
OnRead,
OnUpdate,
Req,
Results,
Request,
Service,
SingleInstanceSwitch,
SRV,
TypedRequest,
} from '../../../../../../lib';
import { BookStat } from '../../../../@cds-models/CatalogService';
import AuthorService from '../../../service/AuthorService';
import BookStatsService from '../../../service/BookStatsService';

import type { TypedRequest, Service, Request, ActionRequest, ActionReturn, NextEvent } from '../../../../../../lib';

@EntityHandler(BookStat)
class BookStatsHandler {
@Inject(SRV) private readonly srv: Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { Publisher } from '#cds-models/CatalogService';

import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import {
BeforeCreate,
BeforeDelete,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
Use,
Validate,
} from '../../../../../../lib';
import { ExposeFields } from '../../../../../../lib/types/validator';
import {
changeBookProperties,
OrderedBook,
Expand All @@ -29,6 +28,8 @@ import {
import { MiddlewareEntity1 } from '../../../middleware/MiddlewareEntity1';
import { MiddlewareEntity2 } from '../../../middleware/MiddlewareEntity2';

import type { ExposeFields } from '../../../../../../lib/types/validator';

@UnboundActions()
@Use(MiddlewareEntity1, MiddlewareEntity2)
class UnboundActionsHandler {
Expand Down Expand Up @@ -69,6 +70,7 @@ class UnboundActionsHandler {
}

@OnEvent(OrderedBook)
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
public async orderedBook(req: TypedRequest<OrderedBook>) {
if (req.event !== 'OrderedBook') {
req.reject(400, 'Not OrderedBook: check @OnEvent decorator');
Expand Down
8 changes: 4 additions & 4 deletions test/bookshop/srv/middleware/MiddlewareAfterRead1.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Book } from '#cds-models/CatalogService';
import type { Book } from '#cds-models/CatalogService';

import { TypedRequest } from '@sap/cds';
import type { TypedRequest } from '@sap/cds';

import { MiddlewareImpl, NextMiddleware } from '../../../../lib/types/types';
import type { MiddlewareImpl, NextMiddleware } from '../../../../lib/types/types';

export class MiddlewareMethodAfterRead1 implements MiddlewareImpl {
public async use(req: TypedRequest<Book>, next: NextMiddleware) {
public async use(req: TypedRequest<Book>, next: NextMiddleware): Promise<void> {
console.log('Middleware 1: @AfterRead');

await next();
Expand Down
8 changes: 4 additions & 4 deletions test/bookshop/srv/middleware/MiddlewareAfterRead2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Book } from '#cds-models/CatalogService';
import type { Book } from '#cds-models/CatalogService';

import { TypedRequest } from '@sap/cds';
import type { TypedRequest } from '@sap/cds';

import { MiddlewareImpl, NextMiddleware } from '../../../../lib/types/types';
import type { MiddlewareImpl, NextMiddleware } from '../../../../lib/types/types';

export class MiddlewareMethodAfterRead2 implements MiddlewareImpl {
public async use(req: TypedRequest<Book>, next: NextMiddleware) {
public async use(req: TypedRequest<Book>, next: NextMiddleware): Promise<void> {
console.log('Middleware 2: @AfterRead');

req.notify('MiddlewareAfterRead2');
Expand Down
8 changes: 4 additions & 4 deletions test/bookshop/srv/middleware/MiddlewareBeforeRead.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Book } from '#cds-models/CatalogService';
import type { Book } from '#cds-models/CatalogService';

import { TypedRequest } from '@sap/cds';
import type { TypedRequest } from '@sap/cds';

import { MiddlewareImpl, NextMiddleware } from '../../../../lib/types/types';
import type { MiddlewareImpl, NextMiddleware } from '../../../../lib/types/types';

export class MiddlewareMethodBeforeRead implements MiddlewareImpl {
public async use(req: TypedRequest<Book>, next: NextMiddleware) {
public async use(req: TypedRequest<Book>, next: NextMiddleware): Promise<void> {
console.log('Middleware 1: @BeforeRead');
await next();
}
Expand Down
Loading

0 comments on commit e6c570e

Please sign in to comment.