Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions types/ee-first/ee-first-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const thunk = first<Foo | Bar>(
(err, ee, event, args) => {
err; // $ExpectType any
ee; // $ExpectType Foo | Bar
event; // $ExpectType string[]
event; // $ExpectType string
args; // $ExpectType any[]
},
);

thunk((err, ee, event, args) => {
err; // $ExpectType any
ee; // $ExpectType Foo | Bar
event; // $ExpectType string[]
event; // $ExpectType string
args; // $ExpectType any[]
});

Expand Down
2 changes: 1 addition & 1 deletion types/ee-first/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare namespace first {
type Listener<TEmitter extends EventEmitter> = (
err: any,
ee: TEmitter,
event: string[],
event: string,
args: any[],
) => void;

Expand Down
122 changes: 76 additions & 46 deletions types/hooker/hooker-tests.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,96 @@
import hooker = require("hooker");

function tests() {
var objectToHook: any = {
hello: "world",
};
hooker.hook(objectToHook, "hello", () => {});
hooker.hook(objectToHook, "hello", (): any => {
return null;
});
hooker.hook(objectToHook, ["hello", "foo"], () => {});
hooker.hook(objectToHook, ["hello", "bar"], (): any => {
return null;
});
hooker.hook(objectToHook, "bar", () => {
return hooker.filter(this, ["foo", "bar"]);
});
hooker.hook(objectToHook, "bar", () => {
return hooker.override("good");
hooker.hook(Math, "max", function(
...args // $ExpectType number[]
) {});
hooker.unhook(Math, "max");

hooker.hook(Math, "max", function() {
if (arguments.length === 0) {
return hooker.override(9000);
}
});
hooker.hook(objectToHook, "bar", () => {
return hooker.preempt("good");
// @ts-expect-error
hooker.hook(Math, "max", function() {
if (arguments.length === 0) {
return hooker.override("not a number");
}
});
hooker.orig(objectToHook, "hello");
hooker.orig(objectToHook, ["hello", "foo"]);
hooker.hook(objectToHook, "foo", {
pre: () => {},

hooker.hook(Math, "max", {
once: true,
pre: function() {},
});
hooker.hook(objectToHook, "foo", {
pre: () => {
return hooker.preempt(1);

hooker.hook(Math, "max", {
pre: function(...args) {
return hooker.filter(this, args.map(num => num * 2));
},
});
hooker.hook(objectToHook, "foo", {
pre: () => {
return hooker.override(1);

// @ts-expect-error
hooker.hook(Math, "max", {
pre: function(...args) {
return hooker.filter(this, args.map(num => num.toString()));
},
});
hooker.hook(objectToHook, "foo", {
pre: () => {
return hooker.filter(1, ["abc"]);

hooker.hook(Math, "max", {
post: function(result) {
return hooker.override(result * 1000);
},
});
hooker.hook(objectToHook, "foo", {
post: () => {},

hooker.orig(Math, "max"); // $ExpectType (...values: number[]) => number

hooker.hook(Math, Object.getOwnPropertyNames(Math), {
passName: true,
pre: function(name) {},
post: function(result, name) {},
});
hooker.hook(objectToHook, "foo", {
post: () => {
return hooker.filter(1, ["abc"]);

hooker.hook(Number, "parseInt", function(
string, // $ExpectType string
radix, // $ExpectType number | undefined
) {});

hooker.hook(Number, "parseInt", {
pre(
string, // $ExpectType string
radix, // $ExpectType number | undefined
) {},
post(
result, // $ExpectType number
string, // $ExpectType string
radix, // $ExpectType number | undefined
) {},
});

hooker.hook(Number, "parseInt", {
pre(string, radix) {
return hooker.preempt(0);
},
});
hooker.hook(objectToHook, "foo", {
once: false,

// @ts-expect-error
hooker.hook(Number, "parseInt", {
pre(string, radix) {
return hooker.preempt("not a number");
},
});
hooker.hook(objectToHook, "foo", {
passName: true,

hooker.hook(Number, "parseInt", {
pre(string, radix) {
return hooker.filter(this, [string, radix] as [string, number | undefined]);
},
});
hooker.hook(objectToHook, "foo", {
pre: () => {},
post: () => {
return hooker.filter(this, []);
// @ts-expect-error
hooker.hook(Number, "parseInt", {
pre(string, radix) {
return hooker.filter(this, [0, "not a number"]);
},
once: true,
passName: false,
});

hooker.orig(Number, "parseInt"); // $ExpectType (string: string, radix?: number | undefined) => number
}
130 changes: 105 additions & 25 deletions types/hooker/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,118 @@
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
declare type HookerPostHookFunction = (result: any, ...args: any[]) => IHookerPostHookResult | void;
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
declare type HookerPreHookFunction = (...args: any[]) => IHookerPreHookResult | void;
/* eslint-disable @typescript-eslint/no-invalid-void-type */
declare type HookerPreHookFunction<F extends (...args: any[]) => any = (...args: any[]) => any> = (
...args: Parameters<F>
) => HookerOverride<ReturnType<F>> | HookerPreempt<ReturnType<F>> | HookerFilter<Parameters<F>> | Promise<void> | void;
declare type HookerPostHookFunction<F extends (...args: any[]) => any = (...args: any[]) => any> = (
result: ReturnType<F>,
...args: Parameters<F>
) => HookerOverride<ReturnType<F>> | Promise<void> | void;
/* eslint-enable @typescript-eslint/no-invalid-void-type */

declare module "hooker" {
function hook(object: any, props: string | string[], options: IHookerOptions): void;
function hook(object: any, props: string | string[], prehookFunction: HookerPreHookFunction): void;
/**
* Monkey-patch (hook) one or more methods of an object. Returns an array of hooked method names.
*
* @param object
* @param props Can be a method name, array of method names or null. If null (or omitted), all enumerable methods of `object` will be hooked.
* @param options
*/
function hook<T, K extends keyof T>(
object: T,
props: K,
options: IHookerOptions<T[K] extends (...args: any[]) => any ? T[K] : never>,
): string[];
/**
* Monkey-patch (hook) one or more methods of an object. Returns an array of hooked method names.
*
* @param object
* @param props Can be a method name, array of method names or null. If null (or omitted), all enumerable methods of `object` will be hooked.
* @param prehookFunction A pre-hook function to be executed before the original function. Arguments passed into the method will be passed into the pre-hook function as well.
*/
function hook<T, K extends keyof T>(
object: T,
props: K,
prehookFunction: HookerPreHookFunction<T[K] extends (...args: any[]) => any ? T[K] : never>,
): string[];
/**
* Monkey-patch (hook) one or more methods of an object. Returns an array of hooked method names.
*
* @param object
* @param props Can be a method name, array of method names or null. If null (or omitted), all enumerable methods of `object` will be hooked.
* @param options
*/
function hook(object: any, props: string[], options: IHookerOptions): string[];
/**
* Monkey-patch (hook) one or more methods of an object. Returns an array of hooked method names.
*
* @param object
* @param props Can be a method name, array of method names or null. If null (or omitted), all enumerable methods of `object` will be hooked.
* @param prehookFunction A pre-hook function to be executed before the original function. Arguments passed into the method will be passed into the pre-hook function as well.
*/
function hook(object: any, props: string[], prehookFunction: HookerPreHookFunction): string[];
/**
* Un-monkey-patch (unhook) one or more methods of an object.
*
* @param object
* @param props Can be a method name, array of method names or null. If null (or omitted), all methods of object will be unhooked.
*/
function unhook(object: any, props?: string | string[]): string[];
function orig(object: any, props: string | string[]): Function;
function override(value: any): HookerOverride;
function preempt(value: any): HookerPreempt;
function filter(context: any, args: any[]): HookerFilter;
/**
* Get a reference to the original method from a hooked function.
*
* @param object
* @param prop
*/
function orig<T, K extends keyof T>(object: T, prop: K): T[K] extends (...args: any[]) => any ? T[K] : never;
function orig(object: any, prop: string): Function;
/**
* When a pre- or post-hook returns the result of this function, the value passed will be used in place of the original function's return value. Any post-hook override value will take precedence over a pre-hook override value.
*
* @param value
*/
function override<T>(value: T): HookerOverride<T>;
/**
* When a pre-hook returns the result of this function, the value passed will be used in place of the original function's return value, and the original function will **NOT** be executed.
*
* @param value
*/
function preempt<T>(value: T): HookerPreempt<T>;
/**
* When a pre-hook returns the result of this function, the context and arguments passed will be applied into the original function.
*
* @param context
* @param args
*/
function filter<T>(context: any, args: T): HookerFilter<T>;
}

declare class HookerOverride implements IHookerPostHookResult, IHookerPreHookResult {
value: any;
declare class HookerOverride<T> {
value: T;
}

declare class HookerPreempt implements IHookerPreHookResult {
value: any;
declare class HookerPreempt<T> {
value: T;
}

declare class HookerFilter implements IHookerPreHookResult {
declare class HookerFilter<T> {
context: any;
args: any[];
args: T;
}

interface IHookerPostHookResult {}

interface IHookerPreHookResult {}

interface IHookerOptions {
pre?: HookerPreHookFunction | undefined;
post?: HookerPostHookFunction | undefined;
once?: boolean | undefined;
passName?: boolean | undefined;
interface IHookerOptions<F extends (...args: any[]) => any = (...args: any[]) => any> {
/**
* A pre-hook function to be executed before the original function. Arguments passed into the method will be passed into the pre-hook function as well.
*/
pre?: HookerPreHookFunction<F>;
/**
* A post-hook function to be executed after the original function. The original function's result is passed into the post-hook function as its first argument, followed by the method arguments.
*/
post?: HookerPostHookFunction<F>;
/**
* If true, auto-unhook the function after the first execution.
*/
once?: boolean;
/**
* If true, pass the name of the method into the pre-hook function as its first arg (preceding all other arguments), and into the post-hook function as the second arg (after result but preceding all other arguments).
*/
passName?: boolean;
}
22 changes: 0 additions & 22 deletions types/picomatch/lib/picomatch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ declare namespace picomatch {
* Allows glob to match any part of the given string(s).
*/
contains?: boolean | undefined;
/**
* Current working directory. Used by `picomatch.split()`
*/
cwd?: string | undefined;
/**
* Debug regular expressions when an error is thrown.
*/
Expand All @@ -87,10 +83,6 @@ declare namespace picomatch {
*/
expandRange?(from: string, to: string, options: PicomatchOptions): string;
expandRange?(from: string, to: string, step: string, options: PicomatchOptions): string;
/**
* Throws an error if no matches are found. Based on the bash option of the same name.
*/
failglob?: boolean | undefined;
/**
* To speed up processing, full parsing is skipped for a handful common glob patterns. Disable this behavior by setting this option to `false`.
*/
Expand Down Expand Up @@ -135,12 +127,6 @@ declare namespace picomatch {
* Make matching case-insensitive. Equivalent to the regex `i` flag. Note that this option is overridden by the `flags` option.
*/
nocase?: boolean | undefined;
/**
* @deprecated use `nounique` instead.
* This option will be removed in a future major release. By default duplicates are removed.
* Disable uniquification by setting this option to false.
*/
nodupes?: boolean | undefined;
/**
* Alias for `noextglob`
*/
Expand All @@ -157,10 +143,6 @@ declare namespace picomatch {
* Disable support for negating with leading `!`
*/
nonegate?: boolean | undefined;
/**
* Disable support for regex quantifiers (like `a{1,2}`) and treat them as brace patterns to be expanded.
*/
noquantifiers?: boolean | undefined;
/**
* Function to be called on ignored items.
*/
Expand All @@ -177,10 +159,6 @@ declare namespace picomatch {
* Support POSIX character classes ("posix brackets").
*/
posix?: boolean | undefined;
/**
* Convert all slashes in file paths to forward slashes. This does not convert slashes in the glob pattern itself.
*/
posixSlashes?: boolean | undefined;
/**
* String to prepend to the generated regex used for matching.
*/
Expand Down
18 changes: 17 additions & 1 deletion types/recurly__recurly-js/lib/paypal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ export type DirectConfig = {
};
};

export type UsagePattern =
| 'SUBSCRIPTION_PREPAID'
| 'SUBSCRIPTION_POSTPAID'
| 'RECURRING_PREPAID'
| 'RECURRING_POSTPAID'
| 'UNSCHEDULED_POSTPAID'
| 'UNSCHEDULED_PREPAID'
| 'INSTALLMENT_POSTPAID'
| 'INSTALLMENT_PREPAID';

export type BillingPlan = {
[key: string]: any;
};

export type PayPalCompleteConfig = {
payPalComplete?: boolean;
payPalComplete?: boolean | { target: string; buttonOptions?: object };
display?: PayPalDisplayConfig;
gatewayCode?: string;
usagePattern?: UsagePattern;
billingPlan?: BillingPlan;
};

export type PayPalConfig = BraintreeConfig | DirectConfig | PayPalCompleteConfig;
Expand Down
Loading