Skip to content

Commit

Permalink
fix: actually really make the config singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsbenz committed May 6, 2024
1 parent 0d59964 commit 02e2743
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
7 changes: 4 additions & 3 deletions lib/components/ssi-transfer-proof.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { config } from "../main";
import { GLOBAL_CSS } from "../styles/styles";
import type {
ConnectionInfoResponse,
StartIssuingRequest,
StartIssuingResponse,
} from "../types";
import { useConfig } from "../utils";

import "./ssi-card";
import "./ssi-qrcode";
Expand All @@ -24,6 +24,7 @@ export class SsiTransferProof extends LitElement {
@state() isActive?: boolean;

timer?: number;
config = useConfig();

async firstUpdated(changedProperties: Map<string, unknown>) {
if (changedProperties.has("token")) {
Expand Down Expand Up @@ -142,7 +143,7 @@ export class SsiTransferProof extends LitElement {
const { active } = await fetch(
new URL(
`/v1/connectionInfo?transactionId=${this.currentConnection.transactionId}`,
config.baseUrl
this.config.baseUrl
)
).then(async (res) =>
res.ok
Expand All @@ -163,7 +164,7 @@ export class SsiTransferProof extends LitElement {
} else {
const body: StartIssuingRequest = { token: this.token };
const connection = (await fetch(
new URL("/v1/startIssuing?credentialType=ACAPY", config.baseUrl),
new URL("/v1/startIssuing?credentialType=ACAPY", this.config.baseUrl),
{
method: "POST",
body: JSON.stringify(body),
Expand Down
37 changes: 9 additions & 28 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
class Config {
static instance: Config;

private _baseUrl = "";

constructor() {
if (Config.instance) {
return Config.instance;
}

Config.instance = this;
}

get baseUrl() {
if (!this._baseUrl) {
throw new Error(
"The base url has not been initialized. Execute `config.init()` before reading any config properties."
);
}
return this._baseUrl;
}

init({ baseUrl }: { baseUrl: string }) {
this._baseUrl = baseUrl;
}
}

export const config = new Config();
import type { Config } from "./types";

export const config: Config = {
baseUrl: "",
init({ baseUrl }) {
this.baseUrl = baseUrl;
window.ssiElementsConfig = this;
},
};
5 changes: 5 additions & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export type Config = {
baseUrl: string;
init: (props: { baseUrl: string }) => void;
};

export type StartIssuingRequest = { token: string };
export type StartIssuingResponse = {
transactionId: string;
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Config } from "../types";

export function useConfig() {
if (!window.ssiElementsConfig) {
throw new Error(
"The config has not been initialized. Execute `config.init()` before any properties are read."
);
}
return window.ssiElementsConfig;
}

declare global {
interface Window {
ssiElementsConfig: Config | undefined;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ubique-innovation/ssi-issuer-elements",
"version": "0.2.1",
"version": "0.2.2",
"repository": {
"type": "git",
"url": "https://github.com/UbiqueInnovation/ssi-issuer-elements.git"
Expand Down

0 comments on commit 02e2743

Please sign in to comment.