feat!: @rudder/sdk 0.1.0: createClient, idempotent modules.call retries, storage save(type, data), drop resources and projectStorage
This commit is contained in:
+28
-29
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* RudderClient — the main entry point for the LiveOps Web SDK.
|
||||
* RudderClient — the main entry point of `@rudder/sdk`; create it with
|
||||
* {@link createClient}.
|
||||
*
|
||||
* Composes the kernel domains (auth, player, catalog, storage, modules,
|
||||
* resources) and the HTTP transport every service uses to reach the API.
|
||||
* Composes the kernel domains (auth, player, catalog, storage, modules) and the HTTP transport every service uses to reach the API.
|
||||
*
|
||||
* The client builds a {@link RudderContext} first and injects it into every
|
||||
* domain, so they depend on that narrow surface rather than on the client
|
||||
@@ -15,16 +15,16 @@ import type {
|
||||
} from './RudderClientOptions.js';
|
||||
import { RudderError, SDK_ERROR_INVALID_OPTIONS } from './RudderError.js';
|
||||
import type { RudderContext } from '../core/context.js';
|
||||
import { request, type RequestContext } from '../transport/request.js';
|
||||
import { request, type RequestContext, type RequestOptions } from '../transport/request.js';
|
||||
import { createDefaultTokenStore } from '../token/TokenStore.js';
|
||||
import { AuthService } from '../auth/AuthService.js';
|
||||
import { SyncEngine } from '../state/SyncEngine.js';
|
||||
import { PlayerDomain } from '../domains/PlayerDomain.js';
|
||||
import { CatalogDomain } from '../domains/CatalogDomain.js';
|
||||
import { StorageDomain } from '../domains/StorageDomain.js';
|
||||
import { ProjectStorageDomain } from '../domains/ProjectStorageDomain.js';
|
||||
import { ModulesDomain } from '../domains/ModulesDomain.js';
|
||||
import { ResourcesDomain, type ResourceHandle } from '../domains/ResourcesDomain.js';
|
||||
|
||||
export const DEFAULT_BASE_URL = 'https://api.rudder.build';
|
||||
|
||||
type ManagedDomain = { invalidate(): void; reset(): void };
|
||||
|
||||
@@ -36,9 +36,7 @@ export class RudderClient {
|
||||
public readonly player: PlayerDomain;
|
||||
public readonly catalog: CatalogDomain;
|
||||
public readonly storage: StorageDomain;
|
||||
public readonly projectStorage: ProjectStorageDomain;
|
||||
public readonly modules: ModulesDomain;
|
||||
private readonly resourcesDomain: ResourcesDomain;
|
||||
|
||||
private readonly syncEngine: SyncEngine;
|
||||
private readonly ctx: RudderContext;
|
||||
@@ -46,18 +44,17 @@ export class RudderClient {
|
||||
private runtimeStarted = false;
|
||||
|
||||
constructor(options: RudderClientOptions) {
|
||||
if (!options.baseUrl) {
|
||||
throw new RudderError('RudderClient: baseUrl is required', {
|
||||
code: SDK_ERROR_INVALID_OPTIONS,
|
||||
});
|
||||
}
|
||||
if (!options.projectKey) {
|
||||
throw new RudderError('RudderClient: projectKey is required', {
|
||||
code: SDK_ERROR_INVALID_OPTIONS,
|
||||
});
|
||||
}
|
||||
|
||||
this.options = { ...options, tokenStore: options.tokenStore ?? createDefaultTokenStore() };
|
||||
this.options = {
|
||||
...options,
|
||||
baseUrl: options.baseUrl || DEFAULT_BASE_URL,
|
||||
tokenStore: options.tokenStore ?? createDefaultTokenStore(),
|
||||
};
|
||||
|
||||
this.transportContext = {
|
||||
baseUrl: this.options.baseUrl,
|
||||
@@ -73,24 +70,25 @@ export class RudderClient {
|
||||
|
||||
this.ctx = {
|
||||
options: this.options,
|
||||
request: <TResponse>(method: string, path: string, body?: unknown) =>
|
||||
request<TResponse>(method, path, body, this.transportContext),
|
||||
request: <TResponse>(
|
||||
method: string,
|
||||
path: string,
|
||||
body?: unknown,
|
||||
options?: RequestOptions,
|
||||
) => request<TResponse>(method, path, body, this.transportContext, options),
|
||||
};
|
||||
const ctx = this.ctx;
|
||||
|
||||
this.player = new PlayerDomain(ctx);
|
||||
this.catalog = new CatalogDomain(ctx);
|
||||
this.storage = new StorageDomain(ctx);
|
||||
this.projectStorage = new ProjectStorageDomain(ctx);
|
||||
this.modules = new ModulesDomain(ctx);
|
||||
this.resourcesDomain = new ResourcesDomain(ctx);
|
||||
|
||||
this.syncEngine = new SyncEngine(ctx, [
|
||||
this.player,
|
||||
this.catalog,
|
||||
this.storage,
|
||||
this.projectStorage,
|
||||
]);
|
||||
this.syncEngine = new SyncEngine(
|
||||
ctx,
|
||||
[this.player, this.catalog, this.storage],
|
||||
this.options.syncIntervalMs,
|
||||
);
|
||||
this.auth = new AuthService(
|
||||
ctx,
|
||||
() => this.startRuntimeAfterLogin(),
|
||||
@@ -104,10 +102,6 @@ export class RudderClient {
|
||||
this.runtimeStarted = false;
|
||||
}
|
||||
|
||||
resources(slug: string): ResourceHandle {
|
||||
return this.resourcesDomain.of(slug);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
async startRuntimeAfterLogin(): Promise<void> {
|
||||
if (this.runtimeStarted) {
|
||||
@@ -126,7 +120,7 @@ export class RudderClient {
|
||||
}
|
||||
|
||||
private get managedDomains(): ManagedDomain[] {
|
||||
return [this.player, this.catalog, this.storage, this.projectStorage];
|
||||
return [this.player, this.catalog, this.storage];
|
||||
}
|
||||
|
||||
private invalidateAll(): void {
|
||||
@@ -137,3 +131,8 @@ export class RudderClient {
|
||||
for (const domain of this.managedDomains) domain.reset();
|
||||
}
|
||||
}
|
||||
|
||||
/** Creates a client. Only `projectKey` is required. */
|
||||
export function createClient(options: RudderClientOptions): RudderClient {
|
||||
return new RudderClient(options);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user