Skip to content

Commit

Permalink
feat: allow binding any object to globalThis variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Jan 31, 2024
1 parent e01dc59 commit 5a53e74
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 51 deletions.
12 changes: 10 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ module.exports = {
plugins: ["@typescript-eslint", "prettier"],
root: true,
rules: {
"prettier/prettier": "error",
"prettier/prettier": [
"error",
{
tabWidth: 2,
singleQuote: false,
trailingComma: "all",
printWidth: 120,
bracketSpacing: true,
},
],
"no-unused-vars": "off",
"no-var": "off",
"@typescript-eslint/no-unused-vars": [
"error", // or "error"
{
Expand Down
7 changes: 0 additions & 7 deletions .prettierrc

This file was deleted.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@
"prettier": "^3.2.4",
"typescript": "^5.3.3",
"vitest": "^1.2.2"
},
"prettier": {
"tabWidth": 2
}
}
45 changes: 45 additions & 0 deletions src/binder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Safely binds functions to globalThis
*
* @example
* ```ts
* type bindings = { test: Function };
*
* set<bindings>("wallet", bindings);
* ```
*
* @export
* @template T
* @param {string} name
* @param {T} bindings
*/
export function set<T = object>(name: string, bindings: T) {
(
globalThis as typeof globalThis & {
[key in typeof name]: T;
}
)[name] = bindings;
}

/**
* Returns bindings assigned to globalThis
*
* @example
* ```
* type bindings = { test: Function };
*
* const wallet = get<bindings>("wallet");
* ```
*
* @export
* @template T
* @param {string} name
* @return {T}
*/
export function get<T = object>(name: string): T {
return (
globalThis as typeof globalThis & {
[key in typeof name]: T;
}
)[name];
}
20 changes: 0 additions & 20 deletions src/extension/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/extension/proxyBindings.ts

This file was deleted.

26 changes: 18 additions & 8 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { ProxyAPI, bindToWindow } from "./extension";
import * as bindings from "./binder";
import { expect, test } from "vitest";

declare global {
var wallet: ProxyAPI;
// eslint-disable-next-line no-var
var wallet: typeof testBindings;
}

const testBindings = {
test1() {},
test2() {},
test3() {},
};

test("test binding to 'window' or 'global' variables", () => {
const bindingName = "wallet";

const target = typeof window !== "undefined" ? window : global;

expect(typeof target).not.toBe("undefined");
expect(Object.hasOwn(target, "wallet")).toBe(false);
expect(Object.hasOwn(target, bindingName)).toBe(false);

bindToWindow(target, "wallet");
bindings.set(bindingName, testBindings);

expect(typeof target.wallet).not.toBe("undefined");
expect(typeof target[bindingName]).not.toBe("undefined");

expect(typeof target.wallet?.test1).toBe("function");
expect(typeof target.wallet?.test2).toBe("function");
expect(typeof target.wallet?.test3).toBe("function");
const result = bindings.get<typeof testBindings>(bindingName);
expect(typeof result.test1).toBe("function");
expect(typeof result.test2).toBe("function");
expect(typeof result.test3).toBe("function");
});

0 comments on commit 5a53e74

Please sign in to comment.