SHARE
TWEET

Untitled

a guest Jul 20th, 2019 60 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
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top