@hasna/shortlinks 0.1.24 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ import { CloudShortlinksStore } from "./cloud-store.js";
2
+ import type { Env } from "@hasna/contracts/mode";
3
+ import type { ListLinksOptions, Store, TotalStats } from "./store-interface.js";
4
+ import type { AddDomainInput, Click, ClickInput, CreateLinkInput, Domain, Link, LinkStats } from "./types.js";
5
+ /** The self_hosted/cloud HTTP transport. Same client code for both tiers. */
6
+ export { CloudShortlinksStore as ApiStore } from "./cloud-store.js";
7
+ export type { Store } from "./store-interface.js";
8
+ /**
9
+ * On-box SQLite store. An async adapter over the synchronous sqlite engine
10
+ * (`ShortlinksStore`) so it satisfies the shared async {@link Store} interface.
11
+ */
12
+ export declare class LocalStore implements Store {
13
+ readonly kind: "local";
14
+ private readonly inner;
15
+ constructor(dbPath?: string);
16
+ addDomain(input: AddDomainInput): Promise<Domain>;
17
+ listDomains(): Promise<Domain[]>;
18
+ getDomain(hostnameOrId: string): Promise<Domain | null>;
19
+ getDefaultDomain(): Promise<Domain | null>;
20
+ deleteDomain(hostnameOrId: string): Promise<Domain>;
21
+ createLink(input: CreateLinkInput): Promise<Link>;
22
+ listLinks(options?: ListLinksOptions): Promise<Link[]>;
23
+ getLink(domainOrSlug: string, maybeSlug?: string): Promise<Link | null>;
24
+ resolve(hostname: string, slug: string): Promise<Link | null>;
25
+ setLinkActive(domainOrSlug: string, slugOrActive: string | boolean, active?: boolean): Promise<Link>;
26
+ deleteLink(domainOrSlug: string, maybeSlug?: string): Promise<Link>;
27
+ recordClick(link: Link, input?: ClickInput): Promise<Click>;
28
+ getStats(domainOrSlug: string, maybeSlug?: string): Promise<LinkStats>;
29
+ totalStats(): Promise<TotalStats>;
30
+ close(): Promise<void>;
31
+ }
32
+ export interface ResolveStoreOptions {
33
+ /** Explicit local SQLite path (CLI `--db`); ignored in cloud mode. */
34
+ dbPath?: string;
35
+ /** Transport overrides for the cloud client (test injection: fetchImpl, ...). */
36
+ cloudOverrides?: Parameters<typeof CloudShortlinksStore.fromEnv>[1];
37
+ }
38
+ /**
39
+ * Resolve the active {@link Store} for the current environment. Returns the
40
+ * cloud {@link ApiStore} when the client flip is on, otherwise a
41
+ * {@link LocalStore}. Throws when cloud was requested but is misconfigured.
42
+ */
43
+ export declare function resolveStore(env?: Env, options?: ResolveStoreOptions): Store;
44
+ /**
45
+ * Run `fn` with a resolved {@link Store}, always closing it afterward. The
46
+ * canonical helper for one-shot CLI/MCP operations.
47
+ */
48
+ export declare function withStore<T>(fn: (store: Store) => T | Promise<T>, env?: Env, options?: ResolveStoreOptions): Promise<T>;
@@ -0,0 +1,47 @@
1
+ import { resolveStorageClient, type HasnaStorageClient } from "@hasna/contracts/client/storage";
2
+ import type { Env } from "@hasna/contracts/mode";
3
+ import type { Store } from "./store-interface.js";
4
+ import type { AddDomainInput, Click, ClickInput, CreateLinkInput, Domain, Link, LinkStats } from "./types.js";
5
+ /**
6
+ * Cloud-backed shortlinks store. All methods hit `/v1` over HTTPS with the
7
+ * bearer key. Mirrors the async `PgShortlinksStore` method signatures so it is a
8
+ * drop-in `RuntimeStore` for the CLI.
9
+ */
10
+ export declare class CloudShortlinksStore implements Store {
11
+ private readonly client;
12
+ readonly kind: "cloud-http";
13
+ readonly transport: HasnaStorageClient["transport"];
14
+ private constructor();
15
+ /** Base `<origin>/v1` URL this store targets (for status/diagnostics). */
16
+ get baseUrl(): string;
17
+ /**
18
+ * Resolve a cloud store from the environment. Returns the store when the
19
+ * client-flip resolves to `cloud-http` (self_hosted + API_URL + API_KEY), else
20
+ * null so the caller falls back to the local store. Throws only when cloud was
21
+ * explicitly requested but is misconfigured (never silent local drift).
22
+ */
23
+ static fromEnv(env?: Env, overrides?: Parameters<typeof resolveStorageClient>[2]): CloudShortlinksStore | null;
24
+ close(): Promise<void>;
25
+ addDomain(input: AddDomainInput): Promise<Domain>;
26
+ listDomains(): Promise<Domain[]>;
27
+ getDomain(hostnameOrId: string): Promise<Domain | null>;
28
+ getDefaultDomain(): Promise<Domain | null>;
29
+ deleteDomain(hostnameOrId: string): Promise<Domain>;
30
+ createLink(input: CreateLinkInput): Promise<Link>;
31
+ listLinks(options?: {
32
+ domain?: string;
33
+ activeOnly?: boolean;
34
+ limit?: number;
35
+ }): Promise<Link[]>;
36
+ getLink(domainOrSlug: string, maybeSlug?: string): Promise<Link | null>;
37
+ resolve(hostname: string, slug: string): Promise<Link | null>;
38
+ setLinkActive(domainOrSlug: string, maybeSlugOrActive: string | boolean, maybeActive?: boolean): Promise<Link>;
39
+ deleteLink(domainOrSlug: string, maybeSlug?: string): Promise<Link>;
40
+ recordClick(_link: Link, _input?: ClickInput): Promise<Click>;
41
+ getStats(domainOrSlug: string, maybeSlug?: string): Promise<LinkStats>;
42
+ totalStats(): Promise<{
43
+ domains: number;
44
+ links: number;
45
+ clicks: number;
46
+ }>;
47
+ }
@@ -5,8 +5,8 @@
5
5
  * vendored storage kit's MigrationLedger. The service reads and writes the same
6
6
  * cloud database — there is no local mirror, cache, or sync engine here.
7
7
  *
8
- * The domains/links/clicks schema mirrors src/pg-migrations.ts (SQLite-parity
9
- * TEXT/INTEGER shapes) so the existing SaaS-remnant tables are matched exactly
8
+ * The domains/links/clicks schema uses SQLite-parity TEXT/INTEGER shapes so the
9
+ * existing SaaS-remnant tables are matched exactly
10
10
  * (every statement is `IF NOT EXISTS` — applying the ledger never clobbers data).
11
11
  * The api_keys table migrations come from @hasna/contracts/auth so the API-key
12
12
  * middleware and the `contracts issue-key` issuer share one schema.
@@ -1,4 +1,4 @@
1
- export declare const KIT_VERSION = "0.4.1";
1
+ export declare const KIT_VERSION = "0.4.2";
2
2
  export * from "./mode.js";
3
3
  export * from "./tls.js";
4
4
  export * from "./query.js";
package/dist/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  export { ShortlinksDatabase, SQLITE_MIGRATIONS, makeId, now } from "./database.js";
2
2
  export { ShortlinksStore } from "./store.js";
3
- export { PgShortlinksStore, applyPostgresMigrations, createKitPgAdapter } from "./pg-store.js";
3
+ export { LocalStore, ApiStore, resolveStore, withStore } from "./client-store.js";
4
+ export { CloudShortlinksStore } from "./cloud-store.js";
5
+ export type { Store, ListLinksOptions, TotalStats } from "./store-interface.js";
6
+ export { PgShortlinksStore, createKitPgAdapter } from "./pg-store.js";
4
7
  export { SHORTLINKS_MIGRATIONS } from "./db/migrations.js";
5
8
  export { createServeApp } from "./serve/app.js";
6
9
  export { buildOpenApiDocument } from "./serve/openapi.js";
7
10
  export { createShortlinksHandler, serveShortlinks } from "./server.js";
8
11
  export { createCloudflarePlan, generateWorkerScript, writeWorkerFiles, upsertCloudflareDnsRecord } from "./cloudflare.js";
9
12
  export { createLocalSetupPlan, registerMachinesDns } from "./local.js";
10
- export { PG_MIGRATIONS } from "./pg-migrations.js";
11
- export { CANONICAL_SHORTLINKS_POSTGRES_CLUSTER, CANONICAL_SHORTLINKS_POSTGRES_DATABASE, CANONICAL_SHORTLINKS_RUNTIME_SECRET_PATH, SHORTLINKS_RUNTIME_ENV, SHORTLINKS_RUNTIME_FALLBACK_ENV, assertShortlinksPostgresConfig, getCanonicalShortlinksPostgresConfig, getShortlinksDatabaseSsl, getShortlinksDatabaseUrl, getShortlinksRuntimeEnvName, getShortlinksRuntimeStatus, getShortlinksStoreMode, loadShortlinksRuntimeConfig, parseShortlinksStoreMode, redactDatabaseUrl, } from "./runtime.js";
12
13
  export { formatShortUrl, getConfigPath, getDataDir, getDatabasePath, loadConfig, normalizeHostname, saveConfig } from "./config.js";
13
14
  export { normalizeSlug, randomToken } from "./slug.js";
14
15
  export type { AddDomainInput, Click, ClickInput, CreateLinkInput, Domain, Link, LinkStats } from "./types.js";
15
- export type { CanonicalShortlinksPostgresConfig, RuntimeEnvStatus, ShortlinksPostgresConfig, ShortlinksRuntimeConfig, ShortlinksRuntimeEnv, ShortlinksRuntimeStatus, ShortlinksStoreMode, } from "./runtime.js";