Skip to content

Commit 2978e46

Browse files
authored
🤖 Merge PR DefinitelyTyped#74283 [Gimloader] Update rewriter types by @TheLazySquid
1 parent 6ac7ae0 commit 2978e46

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

types/gimloader/gimloader-tests.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
interface TestPlugin {
2+
one: number;
3+
}
4+
5+
interface TestLib {
6+
two: string;
7+
}
8+
9+
declare namespace Gimloader {
10+
interface Plugins {
11+
testPlugin: TestPlugin;
12+
}
13+
14+
interface Libraries {
15+
testLib: TestLib;
16+
}
17+
}
18+
119
GL; // $ExpectType typeof Api
220
api; // $ExpectType Api
321

@@ -25,6 +43,10 @@ GL.rewriter; // $ExpectType Readonly<RewriterApi>
2543
GL.storage; // $ExpectType Readonly<StorageApi>
2644
GL.commands; // $ExpectType Readonly<CommandsApi>
2745

46+
api.plugin("testPlugin").one; // $ExpectType number
47+
api.lib("testLib").two; // $ExpectType string
48+
api.plugin("somethingElse"); // $ExpectType any
49+
2850
// @ts-expect-error
2951
GL.onStop;
3052
// @ts-expect-error
@@ -49,6 +71,13 @@ api.net.onLoad((type, gamemode) => {});
4971
api.net.modifyFetchRequest("/path/*/thing", (options) => null);
5072
api.net.modifyFetchRequest("/path/*/thing", (options) => options);
5173
api.net.modifyFetchResponse("/path/*/thing", (response) => response);
74+
api.rewriter.runInScope("App", (code, evalCode) => evalCode("something"));
75+
api.rewriter.exposeVar("App", {
76+
check: "someString",
77+
find: /,(.+)=>{/,
78+
callback: (theVar) => {},
79+
multiple: false,
80+
});
5281

5382
api.commands.addCommand({
5483
text: "test",

types/gimloader/index.d.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,7 @@ declare global {
23052305
/** Gets the headers of a plugin, such as version, author, and description */
23062306
getHeaders(name: string): ScriptHeaders;
23072307
/** Gets the exported values of a plugin, if it has been enabled */
2308-
get(name: string): any;
2308+
get<T extends keyof Gimloader.Plugins>(name: T): Gimloader.Plugins[T];
23092309
/**
23102310
* @deprecated Use {@link get} instead
23112311
* @hidden
@@ -2342,7 +2342,7 @@ declare global {
23422342
/** Gets the headers of a library, such as version, author, and description */
23432343
getHeaders(name: string): ScriptHeaders;
23442344
/** Gets the exported values of a library */
2345-
get(name: string): any;
2345+
get<T extends keyof Gimloader.Libraries>(name: T): Gimloader.Libraries[T];
23462346
}
23472347

23482348
class ScopedCommandsApi {
@@ -2415,6 +2415,23 @@ declare global {
24152415
createShared(id: string, value: any): string;
24162416
/** Removes the shared value with a certain id created by {@link createShared} */
24172417
removeSharedById(id: string): void;
2418+
/**
2419+
* Runs code in the scope of modules when they are loaded, or when runInScope is called with them already loaded.
2420+
* Returning true from the callback will remove the hook.
2421+
*/
2422+
runInScope(prefix: string | boolean, callback: RunInScopeCallback): () => void;
2423+
/** A utility function that exposes a variable based on regex to get its name. */
2424+
exposeVar(prefix: string | boolean, exposer: Exposer): () => void;
2425+
}
2426+
2427+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
2428+
type RunInScopeCallback = (code: string, run: (evalCode: string) => void) => true | void;
2429+
2430+
interface Exposer {
2431+
check?: string;
2432+
find: RegExp;
2433+
callback: (val: any) => void;
2434+
multiple?: boolean;
24182435
}
24192436

24202437
class RewriterApi {
@@ -2425,10 +2442,10 @@ declare global {
24252442
* @param pluginName The name of the plugin creating the hook.
24262443
* @param prefix Limits the hook to only running on scripts beginning with this prefix.
24272444
* Passing `true` will only run on the index script, and passing `false` will run on all scripts.
2428-
* @param callback The function that will modify the code. Should return the modified code. Cannot have side effects.
2445+
* @param callback A function that will modify the code, which should return the modified code.
24292446
*/
2430-
addParseHook(pluginName: string, prefix: string | boolean, callback: (code: string) => string): () => void;
2431-
/** Removes all hooks created by a certain plugin */
2447+
addParseHook(pluginName: string, prefix: string | boolean, modifier: (code: string) => string): () => void;
2448+
/** Removes all parse hooks created by a certain plugin */
24322449
removeParseHooks(pluginName: string): void;
24332450
/**
24342451
* Creates a shared value that can be accessed from any script.
@@ -2442,6 +2459,15 @@ declare global {
24422459
removeShared(pluginName: string): void;
24432460
/** Removes the shared value with a certain id created by {@link createShared} */
24442461
removeSharedById(pluginName: string, id: string): void;
2462+
/**
2463+
* Runs code in the scope of modules when they are loaded, or when runInScope is called with them already loaded.
2464+
* Returning true from the callback will remove the hook.
2465+
*/
2466+
runInScope(pluginName: string, prefix: string | boolean, callback: RunInScopeCallback): () => void;
2467+
/** Stops all hooks created by {@link runInScope} */
2468+
removeRunInScope(pluginName: string): void;
2469+
/** A utility function that exposes a variable based on regex to get its name. */
2470+
exposeVar(pluginName: string, prefix: string | boolean, exposer: Exposer): () => void;
24452471
}
24462472

24472473
class ScopedPatcherApi {
@@ -2836,11 +2862,11 @@ declare global {
28362862
/** Methods for getting info on libraries */
28372863
static libs: Readonly<LibsApi>;
28382864
/** Gets the exported values of a library */
2839-
static lib: (name: string) => any;
2865+
static lib: <T extends keyof Gimloader.Libraries>(name: T) => Gimloader.Libraries[T];
28402866
/** Methods for getting info on plugins */
28412867
static plugins: Readonly<PluginsApi>;
28422868
/** Gets the exported values of a plugin, if it has been enabled */
2843-
static plugin: (name: string) => any;
2869+
static plugin: <T extends keyof Gimloader.Plugins>(name: T) => Gimloader.Plugins[T];
28442870
/** Gimkit's internal react instance */
28452871
static get React(): typeof import("react");
28462872
/** Gimkit's internal reactDom instance */
@@ -2909,11 +2935,11 @@ declare global {
29092935
/** Methods for getting info on libraries */
29102936
libs: Readonly<LibsApi>;
29112937
/** Gets the exported values of a library */
2912-
lib: (name: string) => any;
2938+
lib: <T extends keyof Gimloader.Libraries>(name: T) => Gimloader.Libraries[T];
29132939
/** Methods for getting info on plugins */
29142940
plugins: Readonly<PluginsApi>;
29152941
/** Gets the exported values of a plugin, if it has been enabled */
2916-
plugin: (name: string) => any;
2942+
plugin: <T extends keyof Gimloader.Plugins>(name: T) => Gimloader.Plugins[T];
29172943
/** Gimkit's internal react instance */
29182944
get React(): typeof import("react");
29192945
/** Gimkit's internal reactDom instance */
@@ -2935,6 +2961,14 @@ declare global {
29352961
*/
29362962
openSettingsMenu: (callback: () => void) => void;
29372963
}
2964+
2965+
interface Plugins {
2966+
[name: string]: any;
2967+
}
2968+
2969+
interface Libraries {
2970+
[name: string]: any;
2971+
}
29382972
}
29392973
const api: Gimloader.Api;
29402974
const GL: typeof Gimloader.Api;

0 commit comments

Comments
 (0)