Giter VIP home page Giter VIP logo

Comments (13)

musti2 avatar musti2 commented on May 30, 2024 1

Hey there,

Firstly, appreciate your prompt response!

The Logging aspect works as expected. My IDE shows no errors either, however, in the browser is where i see the error.

Here is the full implementation (without @Injectable :)) -

import { AdvicePool, adviceMetadata, IMetadata } from 'kaop-ts'

export class LoggingAspect extends AdvicePool {
    static log( @adviceMetadata meta: IMetadata) {
        console.log('Called: ', meta.target)
        console.log('Args: ', meta.args)
    }
}

And the service method i am injecting it to is:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable'

// kaop imports
import { beforeMethod } from 'kaop-ts';
import { LogAspect } from '../sample-advice';

import 'rxjs/add/operator/toPromise';

@Injectable
export class MyService {

    constructor(private http: Http) { }

    @beforeMethod(LoggingAspect.log)
    httpGetRequest(url: string): Promise<Response> {
        return this.http.get(url)
            .toPromise().then(...)
    }
}

I also tried the example on the readme, with the same error in the browser console:

    static log( @adviceMetadata metadata: IMetadata) {
        console.log(`${metadata.target.name}::${metadata.propertyKey}()`) // class and method name
        console.log(`called with arguments: `)
        console.log(metadata.args) // function arguments
        console.log(`output a result of: `)
        console.log(metadata.result) // returned value
    }

(edit .. place ```javascript before a block of code to highlight :))

from kaop-ts.

alexjoverm avatar alexjoverm commented on May 30, 2024 1

Hi @musti2!

in the browser is where i see the error

How are you running it? Can you provide the exact steps in order to reproduce it? That'd help a lot

from kaop-ts.

alexjoverm avatar alexjoverm commented on May 30, 2024 1

@musti2 @k1r0s I found the issue, we can fix by changing IMetadata to an empty class, instead of an interface.

The problem is in TypeScript + Webpack side, not in ours, but apparently it's a real issue. When exporting code + interfaces in the same file, Webpack treats it as CJS modules instead of ESM, causing compile-time code (interfaces) to be looked up. Of course this is removed after all by TypeScript, so that's why it's a warning and things keep running.

There are 2 solutions: either split exports in separate files (one for code and another for interfaces) or rewrite the exported interfaces (IMetadata) as an empty class. Keep in mind empty classes, used as types (no new invoked) in TypeScript are removed when compiled, just as interfaces, so no junk code is generated.

More info on angular/angular-cli#2034 (comment)

from kaop-ts.

alexjoverm avatar alexjoverm commented on May 30, 2024 1

@k1r0s I think it was too early to close, since we didn't provide a final solution.

I've been investigating. As you could have seen in angular/angular-cli#2034 (comment), the issue have implications on Webpack, TypeScript and Angular, so prob the issue will remain for a time, and is not in our side. Is not too important anyway since is a warning.

Anyway, I've tried several things from kaop-ts side, and there is nothing we can do. The best workaround I found is in the angular app re-exporting the interface from an inteface-only file (very important, don't mix interfaces with variables, const, classes or functions in that file):

// app/interfaces.ts
...
export { IMetadata } from 'kaop-ts';
...
// app/logging-aspect.ts

import { adviceMetadata, AdvicePool } from 'kaop-ts'
import { IMetadata } from './interfaces' // Import from here instead

class LoggingAspect extends AdvicePool {
    static log( @adviceMetadata meta: IMetadata) {
        console.log('Called: ', meta.target);
        console.log('Args: ', meta.args);
    }
}

That will remove the warnings.

@k1r0s I'd suggest to add this case to a Troubleshooting section in the Readme. Then is more visible for the angular users

from kaop-ts.

alexjoverm avatar alexjoverm commented on May 30, 2024 1

Thanks to @musti2! We learnt something new today

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

I think you're trying to inject an instance of LogAspect somewhere. You should import LogAspect and use LogAspect.log directly placing it on any decorator this lib provides.

log method is 'static' so there is no need to apply 'Injectable()' :)

by the way can you please provide a gist or paste your implementation?

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

export 'IMetadata' was not found in 'kaop-ts' error is due Angular is trying to inject meta as an instance of IMetadata which is not declared as a dependency.

to retrieve method metadata you don't need @Injectable()

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

So, advice is working but u got an error anyway? :\ seems weird

Hold on, I'm gonna test this

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

I think Angular nor this library is the cause of this :\ gonna seek a solution

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

Enough arguments by @alexjoverm to close this, thanks mate

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

Agree. Best contributor ever

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

Another painless workarround

export not found

More info
angular/angular-cli#2034 (comment)

from kaop-ts.

musti2 avatar musti2 commented on May 30, 2024

Thanks guys! Appreciate it.

The workaround does the trick.

from kaop-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.