question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documention Update: Pipes with config AND dependency

See original GitHub issue

Bug Report

Current behavior

Your docs give great examples of how to configure a Pipe for DI or an options object but not both. The use case below would allow me to create one pipe that I could configure for any request object by supplying the property name containing the file path value. As it stands, I would basically need to create the same pipe over and over again for requests with different request signatures. This example is contrived but hopefully you could see how it could be beneficial.

Input Code

pipe.ts

@Injectable()
export class FileExistsPipe implements PipeTransform {

  constructor(private filePath: string, db: DatabaseService) { }

  async transform(value: any, metadata: ArgumentMetadata) {
    const path = value[this.filePath];
    const doesExist =  await this.db.file(path).exists()
    if(!doesExist)  throw new BadRequestException();
    return value;
  }
}

controller.ts

interface JobRequest {
   input: 'path/to/file/on/server'
}

@Controller('transcode')
export class TranscodeController {

  @Post()
  async transcode ( 
    @Body( new FileExistsPipe('input')) transcodeRequest: JobRequest) {
    return await this.videoProducer.addJob(transcodeRequest);
  }

Expected behavior

The above solution doesn’t work because the call to new FileExistsPipe('input') needs two arguments, the second being the DatabaseService dependency. The other option would be to do @Body(FileExistsPipe) but then I wouldn’t be able to pass my property name to my pipe to look up in the request object.

Possible Solution

I have posted this question on stackoverflow, here, in case you would prefer to leave the documentation as is.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
nartccommented, Jul 9, 2020

@tommyc38 I just checked your repo (sorry for missing it before). Your DatabaseService is undefined in the FIleExistPipe because you use the pipe in AppController. AppController will be resolved before the DatabaseModule gets resolved. You can use forwardRef() to inject the DatabaseService in your pipe if you are going to use the pipe in AppController. The good practice here is to have feature controllers provided in feature modules.

export const FileExistPipe: (filePath: string) => PipeTransform = memoize(
  createFileExistPipe
);

function createFileExistPipe(filePath: string): Type<PipeTransform> {
  class MixinFileExistPipe implements PipeTransform {
    constructor(
      // use forwardRef here
      @Inject(forwardRef(() => DatabaseService)) private db: DatabaseService 
    ) {
      console.log(db);
    }

    async transform(value: ITranscodeRequest, metadata: ArgumentMetadata) {
      console.log(filePath, this.db);
      const doesExist = await this.db.checkFileExists(filePath);
      if (!doesExist) throw new BadRequestException();
      return value;
    }
  }

  return mixin(MixinFileExistPipe);
}

this is the fix btw

1reaction
tommyc38commented, Jun 23, 2020

https://github.com/tommyc38/nest-pipe-repro

@nartc , I included instructions in the README. Should be pretty straightforward. Note, I am also creating another Nest repro and am using this for that as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

update-pipe — AWS CLI 1.27.32 Command Reference
Update an existing pipe. When you call UpdatePipe , only the fields that are included in the request are changed, the rest are...
Read more >
Transforming Data Using Pipes - Angular
For a complete list of built-in pipes, see the pipes API documentation. ... To mark a class as a pipe and supply configuration...
Read more >
Language Processing Pipelines · spaCy Usage Documentation
spaCy is a free open-source library for Natural Language Processing in Python. It features NER, POS tagging, dependency parsing, word vectors and more....
Read more >
Write a pipe for Bitbucket Pipelines - Atlassian Support
(Optional) metadata and readme docs, to make your pipe easy to understand. (Optional) some CI/CD configuration so that you can easily update the...
Read more >
Pipes | NestJS - A progressive Node.js framework
A pipe is a class annotated with the @Injectable() decorator, which implements the PipeTransform interface. Pipes have two typical use cases: transformation: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found