From 8d5f0ba5399a9f9ed909d36a407fb142c5d1d2b4 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 18 Mar 2024 09:36:22 -0400 Subject: [PATCH] feat: implement protocol support, starting with s5 --- src/index.ts | 1 + src/protocol/impl/protocol.ts | 24 ++++++++++++++++++++++++ src/protocol/impl/s5.ts | 18 ++++++++++++++++++ src/protocol/index.ts | 14 ++++++++++++++ src/protocol/registry.ts | 34 ++++++++++++++++++++++++++++++++++ src/sdk.ts | 31 ++++++++++++++++++++++++++++--- 6 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 src/protocol/impl/protocol.ts create mode 100644 src/protocol/impl/s5.ts create mode 100644 src/protocol/index.ts create mode 100644 src/protocol/registry.ts diff --git a/src/index.ts b/src/index.ts index 60431d5..4d95634 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from "./sdk.js"; export * from "./account/generated/openapi.schemas.js"; +export * from "./protocol/index.js"; diff --git a/src/protocol/impl/protocol.ts b/src/protocol/impl/protocol.ts new file mode 100644 index 0000000..f437809 --- /dev/null +++ b/src/protocol/impl/protocol.ts @@ -0,0 +1,24 @@ +export abstract class Protocol { + private sdk: T; + + constructor(sdk: T) { + this.sdk = sdk; + } + + public getSdk(): T { + return this.sdk; + } + + public abstract setAuthToken(token: string): Promise; +} + +export interface ProtocolConstructor { + new (apiDomain: string): Protocol; +} + +export function createProtocol( + implementation: ProtocolConstructor, + apiDomain: string, +): Protocol { + return new implementation(apiDomain); +} diff --git a/src/protocol/impl/s5.ts b/src/protocol/impl/s5.ts new file mode 100644 index 0000000..efe4345 --- /dev/null +++ b/src/protocol/impl/s5.ts @@ -0,0 +1,18 @@ +import { Protocol } from "./protocol"; +import { S5Client } from "@lumeweb/s5-js"; + +export const PROTOCOL_S5 = "s5"; + +export class S5 extends Protocol { + constructor(apiDomain: string) { + const sdk = new S5Client(`s5.${apiDomain}`); + + super(sdk); + } + + async setAuthToken(token: string): Promise { + const options = this.getSdk().clientOptions; + options.apiKey = token; + this.getSdk().clientOptions = options; + } +} diff --git a/src/protocol/index.ts b/src/protocol/index.ts new file mode 100644 index 0000000..c6f532b --- /dev/null +++ b/src/protocol/index.ts @@ -0,0 +1,14 @@ +import { Registry } from "./registry"; +import { createProtocol, Protocol } from "src/protocol/impl/protocol.js"; +import { S5Client } from "@lumeweb/s5-js"; +import { PROTOCOL_S5, S5 } from "src/protocol/impl/s5.js"; + +export function registerDefaults(registry: Registry) { + registry.register( + PROTOCOL_S5, + createProtocol(S5, registry.getApiDomain()), + ); +} + +export { Protocol, Registry }; +export { PROTOCOL_S5 }; diff --git a/src/protocol/registry.ts b/src/protocol/registry.ts new file mode 100644 index 0000000..7fe771c --- /dev/null +++ b/src/protocol/registry.ts @@ -0,0 +1,34 @@ +import { Sdk } from "src/sdk.js"; +import { Protocol } from "src/protocol/impl/protocol.js"; + +export class Registry { + private store: Map>; + private sdk: Sdk; + private _apiDomain?: string; + + constructor(sdk: Sdk) { + this.store = new Map(); + this.sdk = sdk; + } + + public register(name: string, value: Protocol) { + this.store.set(name, value); + } + + public get(name: string): Protocol { + return this.store.get(name) as Protocol; + } + + [Symbol.iterator]() { + return this.store[Symbol.iterator](); + } + + public getApiDomain(): string { + if (!this._apiDomain) { + const urlObject = new URL(this.sdk.apiUrl); + this._apiDomain = urlObject.hostname; + } + + return this._apiDomain; + } +} diff --git a/src/sdk.ts b/src/sdk.ts index 7888c43..4cb3a1f 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -1,11 +1,22 @@ import { AccountApi } from "./account.js"; +import { registerDefaults, Registry } from "./protocol/index.js"; export class Sdk { - private apiUrl: string; private accountApi?: AccountApi; + private registry?: Registry; constructor(apiUrl: string) { - this.apiUrl = apiUrl; + this._apiUrl = apiUrl; + } + + private _apiUrl: string; + + get apiUrl(): string { + return this._apiUrl; + } + + set apiUrl(value: string) { + this._apiUrl = value; } public static create(apiUrl: string): Sdk { @@ -14,12 +25,26 @@ export class Sdk { public account(): AccountApi { if (!this.accountApi) { - this.accountApi = AccountApi.create(this.apiUrl); + this.accountApi = AccountApi.create(this._apiUrl); } return this.accountApi!; } + public protocols(): Registry { + if (!this.registry) { + this.registry = new Registry(this); + } + + registerDefaults(this.registry!); + + return this.registry!; + } + public setAuthToken(token: string) { this.account().jwtToken = token; + + for (const [, protocol] of this.protocols()) { + protocol.setAuthToken(token); + } } }