-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add index.d.ts
#343
base: master
Are you sure you want to change the base?
Add index.d.ts
#343
Conversation
thanks to DefinitelyTyped/DefinitelyTyped#65416 for the initial version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments, everything else looks good
STANDARD: string; | ||
}; | ||
|
||
export const RuleTypes: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it should be an enum or union type. I do not know which will be compatible with the Rust library. At least it is enum here: https://docs.rs/adblock/latest/adblock/lists/enum.RuleTypes.html
For now, it is an object with all required fields and it can not be used to parse only some types of rules
Line 103 in dc2a4fa
addFilters(rules: string[], opts?: ParseOptions): FilterListMetadata; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, after some reading (1, 2), my understanding is that enum
s shouldn't be declared in a .d.ts
file.
This seems correct to me because it's describing the shape of (await import('adblock-rs')).RuleTypes
, which basically behaves like a struct with those 3 fields.
Perhaps there is an issue with using rule_types: typeof RuleTypes
in ParseOptions
instead? I think it needs to be "field of" rather than typeof
, but I don't know how to express that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can use keyof typeof RyleTypes
or union type like here:
#343 (comment)
export class Engine { | ||
constructor(rules: FilterSet, debug: boolean); | ||
addResource(resource: Resource): boolean; | ||
check(url: string, source_url: string, request_type: string, debug?: false): boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, request_type
is enum in Rust. So, I think it should be enum here too.
https://docs.rs/adblock/latest/adblock/request/enum.RequestType.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically, although the enum isn't currently used in any public-facing Rust API. Those functions just accept string arguments. I do want to change that but it'll have to be left to a breaking change in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can deal with union type here:
type RequestType =
| 'beacon'
| 'csp'
| 'document'
| 'dtd'
| 'fetch'
| 'font'
| 'image'
| 'media'
| 'object'
| 'other'
| 'ping'
| 'script'
| 'stylesheet'
| 'subdocument'
| 'websocket'
| 'xlst'
| 'xmlhttprequest';
For reference see different implementations from other adblockers:
constructor(rules: FilterSet, debug: boolean); | ||
addResource(resource: Resource): boolean; | ||
check(url: string, source_url: string, request_type: string, debug?: false): boolean; | ||
check(url: string, source_url: string, request_type: string, debug: true): BlockerResult; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: we can pass an object (aka named parameters) instead of passing 4 parameters separately, and then we will have a better user experience.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds like that's a breaking change as well, so I'll leave it as-is for now
@antonok-edm If you need some help to finish this PR you can ping me here |
closes #340
@darahe does this look good to you?