Guest User

Untitled

a guest
Jul 20th, 2019
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
  2. import { Observable } from 'rxjs';
  3. import { GqlExecutionContext } from '@nestjs/graphql';
  4. import { default as slugify } from 'slugify';
  5.  
  6. @Injectable()
  7. export class SlugInterceptor implements NestInterceptor {
  8. intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
  9. const ctx = GqlExecutionContext.create(context);
  10. const input = ctx.getArgs().input;
  11. if (input.title || input.slug) {
  12. if (!input.slug || input.slug === '') {
  13. input.slug = slugify(input.title, {
  14. lower: true,
  15. });
  16. } else {
  17. input.slug = slugify(input.slug, {
  18. replacement: '-',
  19. lower: true,
  20. });
  21. }
  22. }
  23. return next.handle();
  24. }
  25. }
RAW Paste Data