@eskais/app-sdk 0.0.0-stage → 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +74 -2
- package/dist/action/action.d.ts +133 -0
- package/dist/action/action.js +224 -0
- package/dist/action/bulk.d.ts +24 -0
- package/dist/action/bulk.js +24 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.js +51 -0
- package/dist/cli/routes.d.ts +17 -0
- package/dist/cli/routes.js +36 -0
- package/dist/core/assume.d.ts +1 -0
- package/dist/core/assume.js +8 -0
- package/dist/core/constants.d.ts +359 -0
- package/dist/core/constants.js +187 -0
- package/dist/core/contracts.d.ts +107 -0
- package/dist/core/contracts.js +3 -0
- package/dist/core/data-labels.d.ts +210 -0
- package/dist/core/data-labels.js +385 -0
- package/dist/core/errors.d.ts +52 -0
- package/dist/core/errors.js +209 -0
- package/dist/core/guards.d.ts +9 -0
- package/dist/core/guards.js +14 -0
- package/dist/core/refusal.d.ts +30 -0
- package/dist/core/refusal.js +53 -0
- package/dist/dialogs/dialogs.d.ts +75 -0
- package/dist/dialogs/dialogs.js +66 -0
- package/dist/draft/draft.d.ts +36 -0
- package/dist/draft/draft.js +87 -0
- package/dist/errors/adapters.d.ts +21 -0
- package/dist/errors/adapters.js +81 -0
- package/dist/errors/define-errors.d.ts +88 -0
- package/dist/errors/define-errors.js +168 -0
- package/dist/errors/idempotency.d.ts +2 -0
- package/dist/errors/idempotency.js +13 -0
- package/dist/export/csv.d.ts +25 -0
- package/dist/export/csv.js +59 -0
- package/dist/i18n/adapters.d.ts +53 -0
- package/dist/i18n/adapters.js +56 -0
- package/dist/i18n/format.d.ts +10 -0
- package/dist/i18n/format.js +273 -0
- package/dist/i18n/labels.d.ts +10 -0
- package/dist/i18n/labels.js +227 -0
- package/dist/i18n/locale.d.ts +73 -0
- package/dist/i18n/locale.js +74 -0
- package/dist/i18n/messages.d.ts +93 -0
- package/dist/i18n/messages.js +16 -0
- package/dist/import/csv-parse.d.ts +11 -0
- package/dist/import/csv-parse.js +70 -0
- package/dist/import/import.d.ts +104 -0
- package/dist/import/import.js +266 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +45 -0
- package/dist/navigation/nav.d.ts +128 -0
- package/dist/navigation/nav.js +301 -0
- package/dist/navigation/router.d.ts +81 -0
- package/dist/navigation/router.js +249 -0
- package/dist/navigation/shortcuts.d.ts +37 -0
- package/dist/navigation/shortcuts.js +93 -0
- package/dist/next/index.d.ts +3 -0
- package/dist/next/index.js +3 -0
- package/dist/next/screen-hydration.d.ts +12 -0
- package/dist/next/screen-hydration.js +26 -0
- package/dist/next/screen.d.ts +25 -0
- package/dist/next/screen.js +44 -0
- package/dist/next/server/handler.d.ts +39 -0
- package/dist/next/server/handler.js +190 -0
- package/dist/next/server/index.d.ts +6 -0
- package/dist/next/server/index.js +6 -0
- package/dist/next/server/page.d.ts +63 -0
- package/dist/next/server/page.js +110 -0
- package/dist/next/server/prefetch.d.ts +29 -0
- package/dist/next/server/prefetch.js +97 -0
- package/dist/next/server/session.d.ts +25 -0
- package/dist/next/server/session.js +23 -0
- package/dist/next/use-next-router.d.ts +21 -0
- package/dist/next/use-next-router.js +104 -0
- package/dist/permissions/permissions.d.ts +134 -0
- package/dist/permissions/permissions.js +121 -0
- package/dist/react/data-layer.d.ts +68 -0
- package/dist/react/data-layer.js +76 -0
- package/dist/react/hooks/use-action-dialog.d.ts +25 -0
- package/dist/react/hooks/use-action-dialog.js +31 -0
- package/dist/react/hooks/use-action.d.ts +37 -0
- package/dist/react/hooks/use-action.js +125 -0
- package/dist/react/hooks/use-better-auth.d.ts +12 -0
- package/dist/react/hooks/use-better-auth.js +11 -0
- package/dist/react/hooks/use-bulk-action.d.ts +20 -0
- package/dist/react/hooks/use-bulk-action.js +24 -0
- package/dist/react/hooks/use-dialogs.d.ts +14 -0
- package/dist/react/hooks/use-dialogs.js +20 -0
- package/dist/react/hooks/use-draft.d.ts +5 -0
- package/dist/react/hooks/use-draft.js +17 -0
- package/dist/react/hooks/use-nav.d.ts +17 -0
- package/dist/react/hooks/use-nav.js +35 -0
- package/dist/react/hooks/use-new-rows.d.ts +22 -0
- package/dist/react/hooks/use-new-rows.js +24 -0
- package/dist/react/hooks/use-permissions.d.ts +13 -0
- package/dist/react/hooks/use-permissions.js +19 -0
- package/dist/react/hooks/use-reauthenticate.d.ts +6 -0
- package/dist/react/hooks/use-reauthenticate.js +11 -0
- package/dist/react/hooks/use-resource.d.ts +92 -0
- package/dist/react/hooks/use-resource.js +255 -0
- package/dist/react/hooks/use-view.d.ts +6 -0
- package/dist/react/hooks/use-view.js +24 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.js +15 -0
- package/dist/react/keys.d.ts +60 -0
- package/dist/react/keys.js +71 -0
- package/dist/react/new-rows.d.ts +22 -0
- package/dist/react/new-rows.js +72 -0
- package/dist/refresh/new-rows.d.ts +13 -0
- package/dist/refresh/new-rows.js +18 -0
- package/dist/resource/form-schema.d.ts +11 -0
- package/dist/resource/form-schema.js +51 -0
- package/dist/resource/relations.d.ts +99 -0
- package/dist/resource/relations.js +194 -0
- package/dist/resource/resource-relations.d.ts +47 -0
- package/dist/resource/resource-relations.js +154 -0
- package/dist/resource/resource-schema.d.ts +9 -0
- package/dist/resource/resource-schema.js +73 -0
- package/dist/resource/resource-texts.d.ts +18 -0
- package/dist/resource/resource-texts.js +13 -0
- package/dist/resource/resource-types.d.ts +420 -0
- package/dist/resource/resource-types.js +1 -0
- package/dist/resource/resource-words.d.ts +25 -0
- package/dist/resource/resource-words.js +221 -0
- package/dist/resource/resource.d.ts +15 -0
- package/dist/resource/resource.js +280 -0
- package/dist/resource/schema.d.ts +57 -0
- package/dist/resource/schema.js +115 -0
- package/dist/services/from-page-source.d.ts +15 -0
- package/dist/services/from-page-source.js +27 -0
- package/dist/services/memory-query.d.ts +8 -0
- package/dist/services/memory-query.js +165 -0
- package/dist/services/memory-service.d.ts +33 -0
- package/dist/services/memory-service.js +291 -0
- package/dist/services/read-only-service.d.ts +18 -0
- package/dist/services/read-only-service.js +25 -0
- package/dist/services/rest-service.d.ts +65 -0
- package/dist/services/rest-service.js +222 -0
- package/dist/services/rng.d.ts +9 -0
- package/dist/services/rng.js +22 -0
- package/dist/services/spring-service.d.ts +58 -0
- package/dist/services/spring-service.js +132 -0
- package/dist/session/better-auth.d.ts +83 -0
- package/dist/session/better-auth.js +86 -0
- package/dist/session/session.d.ts +45 -0
- package/dist/session/session.js +36 -0
- package/dist/view/preferences.d.ts +35 -0
- package/dist/view/preferences.js +93 -0
- package/dist/view/view-types.d.ts +124 -0
- package/dist/view/view-types.js +1 -0
- package/dist/view/view-url.d.ts +7 -0
- package/dist/view/view-url.js +54 -0
- package/dist/view/view.d.ts +4 -0
- package/dist/view/view.js +392 -0
- package/locales/en.json +193 -0
- package/locales/es.json +193 -0
- package/package.json +88 -4
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isObject } from "../core/guards.js";
|
|
2
|
+
const allowAll = () => true;
|
|
3
|
+
export function createSession(options) {
|
|
4
|
+
return {
|
|
5
|
+
user: options.user,
|
|
6
|
+
organization: options.organization,
|
|
7
|
+
can: options.can ?? allowAll,
|
|
8
|
+
...(options.token ? { token: options.token } : {}),
|
|
9
|
+
...(options.subscribe ? { subscribe: options.subscribe } : {}),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function caslSubject(resource, row) {
|
|
13
|
+
if (row === undefined || row === null)
|
|
14
|
+
return resource;
|
|
15
|
+
return isObject(row) ? { ...row, __caslSubjectType__: resource } : row;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A session over a CASL ability: `can(action, { resource, row })` asks `ability.can(action, subject(resource, row))`.
|
|
19
|
+
* A rule's `because(...)` reason becomes the refusal's reason.
|
|
20
|
+
*/
|
|
21
|
+
export function fromCasl(ability, options) {
|
|
22
|
+
const subjectOf = options.subject ?? caslSubject;
|
|
23
|
+
return {
|
|
24
|
+
user: options.user,
|
|
25
|
+
organization: options.organization,
|
|
26
|
+
...(options.token ? { token: options.token } : {}),
|
|
27
|
+
can(action, subject) {
|
|
28
|
+
const target = subject ? subjectOf(subject.resource, subject.row) : "all";
|
|
29
|
+
if (ability.can(action, target))
|
|
30
|
+
return true;
|
|
31
|
+
const reason = ability.relevantRuleFor?.(action, target)?.reason;
|
|
32
|
+
return reason === undefined ? false : { allowed: false, reason };
|
|
33
|
+
},
|
|
34
|
+
...(ability.on ? { subscribe: (listener) => ability.on?.("updated", listener) ?? (() => { }) } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CallContext } from "../core/contracts.js";
|
|
2
|
+
import type { ViewState } from "./view.js";
|
|
3
|
+
export interface PreferencesStore {
|
|
4
|
+
get(key: string): Promise<unknown> | unknown;
|
|
5
|
+
set(key: string, value: unknown): Promise<void> | void;
|
|
6
|
+
}
|
|
7
|
+
type StorageLike = Pick<Storage, "getItem" | "setItem">;
|
|
8
|
+
/**
|
|
9
|
+
* Preferences in this browser's `localStorage`, under `prefix:key`. Reads and writes never throw: a private window
|
|
10
|
+
* or blocked storage reads nothing and forgets the write. Storage is only touched on a read or write, never when the
|
|
11
|
+
* store is made, so a module or a server render can create one.
|
|
12
|
+
*/
|
|
13
|
+
export declare function localPreferences(prefix?: string, storage?: StorageLike): PreferencesStore;
|
|
14
|
+
export declare function memoryPreferences(initial?: Record<string, unknown>): PreferencesStore;
|
|
15
|
+
export declare function remotePreferences(service: {
|
|
16
|
+
get(key: string): Promise<unknown>;
|
|
17
|
+
set(key: string, value: unknown): Promise<void>;
|
|
18
|
+
}): PreferencesStore;
|
|
19
|
+
export type SavedView<F> = {
|
|
20
|
+
id: string;
|
|
21
|
+
label: string;
|
|
22
|
+
resource: string;
|
|
23
|
+
state: Partial<ViewState<F>>;
|
|
24
|
+
shared?: boolean;
|
|
25
|
+
isDefault?: boolean;
|
|
26
|
+
/** Kept in this browser only: no other device or colleague sees it. */
|
|
27
|
+
local?: boolean;
|
|
28
|
+
};
|
|
29
|
+
export interface ViewStore<F> {
|
|
30
|
+
list(resource: string, context: CallContext): Promise<SavedView<F>[]>;
|
|
31
|
+
save(view: SavedView<F>, context: CallContext): Promise<SavedView<F>>;
|
|
32
|
+
remove(id: string, context: CallContext): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export declare function localViews<F>(store?: PreferencesStore): ViewStore<F>;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isRecord, isString } from "../core/guards.js";
|
|
2
|
+
function defaultStorage() {
|
|
3
|
+
try {
|
|
4
|
+
return globalThis.localStorage;
|
|
5
|
+
}
|
|
6
|
+
catch {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function parse(raw) {
|
|
11
|
+
if (raw === null)
|
|
12
|
+
return undefined;
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(raw);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Preferences in this browser's `localStorage`, under `prefix:key`. Reads and writes never throw: a private window
|
|
22
|
+
* or blocked storage reads nothing and forgets the write. Storage is only touched on a read or write, never when the
|
|
23
|
+
* store is made, so a module or a server render can create one.
|
|
24
|
+
*/
|
|
25
|
+
export function localPreferences(prefix = "app-sdk", storage) {
|
|
26
|
+
const store = () => storage ?? defaultStorage();
|
|
27
|
+
return {
|
|
28
|
+
get(key) {
|
|
29
|
+
try {
|
|
30
|
+
return parse(store()?.getItem(`${prefix}:${key}`) ?? null);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
set(key, value) {
|
|
37
|
+
try {
|
|
38
|
+
store()?.setItem(`${prefix}:${key}`, JSON.stringify(value));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Security: storage that refuses a write (quota, a private window) loses a preference, never the screen.
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function memoryPreferences(initial = {}) {
|
|
47
|
+
const values = new Map(Object.entries(initial));
|
|
48
|
+
return {
|
|
49
|
+
get: (key) => values.get(key),
|
|
50
|
+
set(key, value) {
|
|
51
|
+
values.set(key, value);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export function remotePreferences(service) {
|
|
56
|
+
return { get: (key) => service.get(key), set: (key, value) => service.set(key, value) };
|
|
57
|
+
}
|
|
58
|
+
function isSavedView(value) {
|
|
59
|
+
return isRecord(value) && isString(value.id) && isString(value.label) && isString(value.resource);
|
|
60
|
+
}
|
|
61
|
+
const viewsKey = (resource) => `views:${resource}`;
|
|
62
|
+
export function localViews(store = localPreferences()) {
|
|
63
|
+
const read = async (resource) => {
|
|
64
|
+
const stored = await store.get(viewsKey(resource));
|
|
65
|
+
return Array.isArray(stored) ? stored.filter(isSavedView).map((view) => ({ ...view, local: true })) : [];
|
|
66
|
+
};
|
|
67
|
+
const owners = new Map();
|
|
68
|
+
return {
|
|
69
|
+
async list(resource) {
|
|
70
|
+
const views = await read(resource);
|
|
71
|
+
for (const view of views)
|
|
72
|
+
owners.set(view.id, resource);
|
|
73
|
+
return views;
|
|
74
|
+
},
|
|
75
|
+
async save(view) {
|
|
76
|
+
const views = await read(view.resource);
|
|
77
|
+
const saved = { ...view, local: true };
|
|
78
|
+
const next = views.some((entry) => entry.id === view.id)
|
|
79
|
+
? views.map((entry) => (entry.id === view.id ? saved : entry))
|
|
80
|
+
: [...views, saved];
|
|
81
|
+
owners.set(view.id, view.resource);
|
|
82
|
+
await store.set(viewsKey(view.resource), next);
|
|
83
|
+
return saved;
|
|
84
|
+
},
|
|
85
|
+
async remove(id) {
|
|
86
|
+
const resource = owners.get(id);
|
|
87
|
+
if (resource === undefined)
|
|
88
|
+
return;
|
|
89
|
+
const views = await read(resource);
|
|
90
|
+
await store.set(viewsKey(resource), views.filter((view) => view.id !== id));
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { ActionTarget } from "../action/action.js";
|
|
2
|
+
import type { SelectionKind, SelectionMode } from "../core/constants.js";
|
|
3
|
+
import type { CallContext, Query, SortSpec, Target } from "../core/contracts.js";
|
|
4
|
+
import type { PreferencesStore, SavedView, ViewStore } from "./preferences.js";
|
|
5
|
+
import type { Allowed } from "../core/refusal.js";
|
|
6
|
+
import type { Resource } from "../resource/resource.js";
|
|
7
|
+
import type { RouterAdapter } from "../navigation/router.js";
|
|
8
|
+
import type { Text } from "../i18n/locale.js";
|
|
9
|
+
export type ViewState<F> = {
|
|
10
|
+
filter: Partial<F>;
|
|
11
|
+
search: string;
|
|
12
|
+
sort: readonly SortSpec[];
|
|
13
|
+
page: number;
|
|
14
|
+
size: number;
|
|
15
|
+
/** Visible columns, in order; every column that isn't hidden by default when unset. */
|
|
16
|
+
columns?: readonly string[] | undefined;
|
|
17
|
+
scope?: string | undefined;
|
|
18
|
+
/** The saved view or preset applied; it stays when the state moves away from it, which `views.modified()` tells. */
|
|
19
|
+
view?: string | undefined;
|
|
20
|
+
};
|
|
21
|
+
/** A tab over the list; its label shows with `resolveText`, so it may be a message. */
|
|
22
|
+
export type ViewScope<F> = {
|
|
23
|
+
id: string;
|
|
24
|
+
label: Text;
|
|
25
|
+
filter: Partial<F>;
|
|
26
|
+
count?: boolean;
|
|
27
|
+
};
|
|
28
|
+
export type ViewPreset<F> = {
|
|
29
|
+
id: string;
|
|
30
|
+
label: Text;
|
|
31
|
+
state: Partial<ViewState<F>>;
|
|
32
|
+
};
|
|
33
|
+
export type ViewOptions<F> = {
|
|
34
|
+
/** Keeps the state in the URL through the `RouterAdapter`. Default true. */
|
|
35
|
+
url?: boolean;
|
|
36
|
+
urlPrefix?: string;
|
|
37
|
+
scopes?: readonly ViewScope<F>[];
|
|
38
|
+
presets?: readonly ViewPreset<F>[];
|
|
39
|
+
/** Where saved views live. Default: this browser (`localViews`), marked "only in this browser". */
|
|
40
|
+
store?: ViewStore<F>;
|
|
41
|
+
preferences?: PreferencesStore;
|
|
42
|
+
/** Rows per page. Default 25. */
|
|
43
|
+
size?: number;
|
|
44
|
+
selection?: SelectionMode;
|
|
45
|
+
/** A filter that always applies and never shows, like a tenant. */
|
|
46
|
+
baseFilter?: Partial<F>;
|
|
47
|
+
/**
|
|
48
|
+
* The URL's query when the view is created (`?status=blocked&page=2`), so the first state is the URL's, not the
|
|
49
|
+
* default: a server render, the prefetch and the first client render then ask for the same page.
|
|
50
|
+
*/
|
|
51
|
+
search?: string;
|
|
52
|
+
};
|
|
53
|
+
export type Selection = {
|
|
54
|
+
kind: typeof SelectionKind.ids;
|
|
55
|
+
ids: readonly string[];
|
|
56
|
+
} | {
|
|
57
|
+
kind: typeof SelectionKind.query;
|
|
58
|
+
except: readonly string[];
|
|
59
|
+
};
|
|
60
|
+
export type ViewSnapshot<F> = {
|
|
61
|
+
state: ViewState<F>;
|
|
62
|
+
selection: Selection;
|
|
63
|
+
views: readonly SavedView<F>[];
|
|
64
|
+
};
|
|
65
|
+
export type View<T, F, Id = unknown> = {
|
|
66
|
+
readonly resource: Resource<T, Id, F>;
|
|
67
|
+
readonly options: ViewOptions<F>;
|
|
68
|
+
readonly scopes: readonly ViewScope<F>[];
|
|
69
|
+
readonly presets: readonly ViewPreset<F>[];
|
|
70
|
+
readonly selectionMode: SelectionMode;
|
|
71
|
+
readonly local: boolean;
|
|
72
|
+
/** The same object until something changes. */
|
|
73
|
+
getSnapshot(): ViewSnapshot<F>;
|
|
74
|
+
getState(): ViewState<F>;
|
|
75
|
+
subscribe(listener: () => void): () => void;
|
|
76
|
+
/** Changes the state; a new filter, search, sort, size or scope goes back to page 1 and clears a query selection. */
|
|
77
|
+
setState(patch: Partial<ViewState<F>>): void;
|
|
78
|
+
setFilter(key: string, value: unknown): void;
|
|
79
|
+
clearFilters(): void;
|
|
80
|
+
setSearch(text: string): void;
|
|
81
|
+
setSort(sort: readonly SortSpec[]): void;
|
|
82
|
+
setPage(page: number): void;
|
|
83
|
+
setSize(size: number): void;
|
|
84
|
+
setScope(id: string | undefined): void;
|
|
85
|
+
setColumns(columns: readonly string[] | undefined): void;
|
|
86
|
+
filter(): Partial<F>;
|
|
87
|
+
query(): Query<Partial<F>>;
|
|
88
|
+
checkFilter(key: string): Allowed;
|
|
89
|
+
checkPage(page: number, pageCount?: number): Allowed;
|
|
90
|
+
readonly selection: {
|
|
91
|
+
get(): Selection;
|
|
92
|
+
select(ids: readonly string[]): void;
|
|
93
|
+
toggle(id: string, selected?: boolean): void;
|
|
94
|
+
selectAllMatching(): void;
|
|
95
|
+
clear(): void;
|
|
96
|
+
count(total?: number): number | undefined;
|
|
97
|
+
has(id: string): boolean;
|
|
98
|
+
target(): Target<string, Partial<F>>;
|
|
99
|
+
actionTarget(rows: readonly T[]): ActionTarget<T>;
|
|
100
|
+
};
|
|
101
|
+
readonly views: {
|
|
102
|
+
list(): readonly SavedView<F>[];
|
|
103
|
+
/** Reads the store; when the state is still the initial one, applies the default view. */
|
|
104
|
+
load(context: CallContext): Promise<readonly SavedView<F>[]>;
|
|
105
|
+
/** Saves the current state as a new view and applies it. */
|
|
106
|
+
save(label: string, context: CallContext, extra?: Pick<SavedView<F>, "shared" | "isDefault">): Promise<SavedView<F>>;
|
|
107
|
+
apply(id: string): Allowed;
|
|
108
|
+
/** The applied view's id, saved view or preset. */
|
|
109
|
+
current(): string | undefined;
|
|
110
|
+
/** Whether the state moved away from the applied view: another filter, search, sort, scope or columns. */
|
|
111
|
+
modified(): boolean;
|
|
112
|
+
/** A saved view (not a preset) by id, or why there's none to change. */
|
|
113
|
+
checkSaved(id: string): Allowed;
|
|
114
|
+
/** Writes the current state into a saved view. */
|
|
115
|
+
update(id: string, context: CallContext): Promise<SavedView<F>>;
|
|
116
|
+
rename(id: string, label: string, context: CallContext): Promise<SavedView<F>>;
|
|
117
|
+
/** Makes a saved view the one applied on load, or none with `undefined`; only one is the default. */
|
|
118
|
+
setDefault(id: string | undefined, context: CallContext): Promise<void>;
|
|
119
|
+
defaultView(): SavedView<F> | undefined;
|
|
120
|
+
remove(id: string, context: CallContext): Promise<void>;
|
|
121
|
+
};
|
|
122
|
+
/** Follows and writes the URL. Returns the cleanup; call it from an effect. */
|
|
123
|
+
attach(router: RouterAdapter): () => void;
|
|
124
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SortSpec } from "../core/contracts.js";
|
|
2
|
+
export declare function same(a: unknown, b: unknown): boolean;
|
|
3
|
+
export declare function compact<F>(filter: Partial<F>): Partial<F>;
|
|
4
|
+
export declare function encodeSort(sort: readonly SortSpec[]): string;
|
|
5
|
+
export declare function decodeSort(raw: string | null): SortSpec[] | undefined;
|
|
6
|
+
export declare function encodeValue(value: unknown): string[];
|
|
7
|
+
export declare function parseJson(raw: string): unknown;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { assumeType } from "../core/assume.js";
|
|
2
|
+
import { SortDirection } from "../core/constants.js";
|
|
3
|
+
import { isBoolean, isNumber, isOneOf, isRecord, isString, valuesOf } from "../core/guards.js";
|
|
4
|
+
import { isActive } from "../services/memory-query.js";
|
|
5
|
+
function canonical(value) {
|
|
6
|
+
if (Array.isArray(value))
|
|
7
|
+
return value.map(canonical);
|
|
8
|
+
if (!isRecord(value))
|
|
9
|
+
return value;
|
|
10
|
+
return Object.fromEntries(Object.keys(value)
|
|
11
|
+
.toSorted()
|
|
12
|
+
.map((key) => [key, canonical(value[key])]));
|
|
13
|
+
}
|
|
14
|
+
export function same(a, b) {
|
|
15
|
+
return a === b || JSON.stringify(canonical(a)) === JSON.stringify(canonical(b));
|
|
16
|
+
}
|
|
17
|
+
export function compact(filter) {
|
|
18
|
+
const entries = Object.entries(filter).filter(([, value]) => isActive(value));
|
|
19
|
+
// Workaround: dropping unset keys from a `Partial<F>` leaves a `Partial<F>`, which `Object.fromEntries` can't type.
|
|
20
|
+
return assumeType(Object.fromEntries(entries));
|
|
21
|
+
}
|
|
22
|
+
const sortDirections = valuesOf(SortDirection);
|
|
23
|
+
export function encodeSort(sort) {
|
|
24
|
+
return sort.map((spec) => `${spec.field}.${spec.direction}`).join(",");
|
|
25
|
+
}
|
|
26
|
+
export function decodeSort(raw) {
|
|
27
|
+
if (raw === null)
|
|
28
|
+
return undefined;
|
|
29
|
+
return raw
|
|
30
|
+
.split(",")
|
|
31
|
+
.map((part) => {
|
|
32
|
+
const at = part.lastIndexOf(".");
|
|
33
|
+
const direction = part.slice(at + 1);
|
|
34
|
+
return at > 0 && isOneOf(sortDirections, direction) ? { field: part.slice(0, at), direction } : undefined;
|
|
35
|
+
})
|
|
36
|
+
.filter((spec) => spec !== undefined);
|
|
37
|
+
}
|
|
38
|
+
export function encodeValue(value) {
|
|
39
|
+
if (Array.isArray(value))
|
|
40
|
+
return value.map((entry) => (isString(entry) ? entry : JSON.stringify(entry)));
|
|
41
|
+
if (isString(value))
|
|
42
|
+
return [value];
|
|
43
|
+
if (isNumber(value) || isBoolean(value))
|
|
44
|
+
return [String(value)];
|
|
45
|
+
return [JSON.stringify(value)];
|
|
46
|
+
}
|
|
47
|
+
export function parseJson(raw) {
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(raw);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Resource } from "../resource/resource.js";
|
|
2
|
+
import type { View, ViewOptions } from "./view-types.js";
|
|
3
|
+
export type * from "./view-types.js";
|
|
4
|
+
export declare function defineView<T, Id, F>(resource: Resource<T, Id, F>, options?: ViewOptions<NoInfer<F>>): View<T, F, Id>;
|