feat: implement protocol support, starting with s5
This commit is contained in:
parent
cfcedc3670
commit
8d5f0ba539
|
@ -1,2 +1,3 @@
|
|||
export * from "./sdk.js";
|
||||
export * from "./account/generated/openapi.schemas.js";
|
||||
export * from "./protocol/index.js";
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
export abstract class Protocol<T> {
|
||||
private sdk: T;
|
||||
|
||||
constructor(sdk: T) {
|
||||
this.sdk = sdk;
|
||||
}
|
||||
|
||||
public getSdk(): T {
|
||||
return this.sdk;
|
||||
}
|
||||
|
||||
public abstract setAuthToken(token: string): Promise<void>;
|
||||
}
|
||||
|
||||
export interface ProtocolConstructor<T> {
|
||||
new (apiDomain: string): Protocol<T>;
|
||||
}
|
||||
|
||||
export function createProtocol<T>(
|
||||
implementation: ProtocolConstructor<T>,
|
||||
apiDomain: string,
|
||||
): Protocol<T> {
|
||||
return new implementation(apiDomain);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
import { Protocol } from "./protocol";
|
||||
import { S5Client } from "@lumeweb/s5-js";
|
||||
|
||||
export const PROTOCOL_S5 = "s5";
|
||||
|
||||
export class S5 extends Protocol<S5Client> {
|
||||
constructor(apiDomain: string) {
|
||||
const sdk = new S5Client(`s5.${apiDomain}`);
|
||||
|
||||
super(sdk);
|
||||
}
|
||||
|
||||
async setAuthToken(token: string): Promise<void> {
|
||||
const options = this.getSdk().clientOptions;
|
||||
options.apiKey = token;
|
||||
this.getSdk().clientOptions = options;
|
||||
}
|
||||
}
|
|
@ -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<S5Client>(
|
||||
PROTOCOL_S5,
|
||||
createProtocol<S5Client>(S5, registry.getApiDomain()),
|
||||
);
|
||||
}
|
||||
|
||||
export { Protocol, Registry };
|
||||
export { PROTOCOL_S5 };
|
|
@ -0,0 +1,34 @@
|
|||
import { Sdk } from "src/sdk.js";
|
||||
import { Protocol } from "src/protocol/impl/protocol.js";
|
||||
|
||||
export class Registry {
|
||||
private store: Map<string, Protocol<any>>;
|
||||
private sdk: Sdk;
|
||||
private _apiDomain?: string;
|
||||
|
||||
constructor(sdk: Sdk) {
|
||||
this.store = new Map();
|
||||
this.sdk = sdk;
|
||||
}
|
||||
|
||||
public register<T>(name: string, value: Protocol<T>) {
|
||||
this.store.set(name, value);
|
||||
}
|
||||
|
||||
public get<T>(name: string): Protocol<T> {
|
||||
return this.store.get(name) as Protocol<T>;
|
||||
}
|
||||
|
||||
[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;
|
||||
}
|
||||
}
|
31
src/sdk.ts
31
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue