@eskais/app-sdk 0.0.0-stage → 0.1.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 +68 -2
- package/dist/action/action.d.ts +74 -0
- package/dist/action/action.js +106 -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 +302 -0
- package/dist/core/constants.js +156 -0
- package/dist/core/contracts.d.ts +100 -0
- package/dist/core/contracts.js +3 -0
- package/dist/core/data-labels.d.ts +196 -0
- package/dist/core/data-labels.js +345 -0
- package/dist/core/errors.d.ts +46 -0
- package/dist/core/errors.js +166 -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/draft/draft.d.ts +36 -0
- package/dist/draft/draft.js +87 -0
- package/dist/export/csv.d.ts +25 -0
- package/dist/export/csv.js +59 -0
- package/dist/import/csv-parse.d.ts +11 -0
- package/dist/import/csv-parse.js +70 -0
- package/dist/import/import.d.ts +102 -0
- package/dist/import/import.js +263 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +30 -0
- package/dist/navigation/nav.d.ts +119 -0
- package/dist/navigation/nav.js +278 -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 +5 -0
- package/dist/next/server/index.js +5 -0
- package/dist/next/server/page.d.ts +57 -0
- package/dist/next/server/page.js +103 -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 +12 -0
- package/dist/next/server/session.js +10 -0
- package/dist/next/use-next-router.d.ts +21 -0
- package/dist/next/use-next-router.js +104 -0
- package/dist/react/data-layer.d.ts +31 -0
- package/dist/react/data-layer.js +31 -0
- package/dist/react/hooks/use-action.d.ts +12 -0
- package/dist/react/hooks/use-action.js +67 -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-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 +34 -0
- package/dist/react/hooks/use-resource.d.ts +69 -0
- package/dist/react/hooks/use-resource.js +218 -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 +8 -0
- package/dist/react/index.js +8 -0
- package/dist/react/keys.d.ts +51 -0
- package/dist/react/keys.js +67 -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 +44 -0
- package/dist/resource/resource-relations.js +141 -0
- package/dist/resource/resource-schema.d.ts +9 -0
- package/dist/resource/resource-schema.js +73 -0
- package/dist/resource/resource-types.d.ts +281 -0
- package/dist/resource/resource-types.js +1 -0
- package/dist/resource/resource.d.ts +11 -0
- package/dist/resource/resource.js +354 -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/rest-service.d.ts +62 -0
- package/dist/services/rest-service.js +220 -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 +122 -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/package.json +85 -4
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { JsonType } from "../core/constants.js";
|
|
2
|
+
export type StandardSchema<Input = unknown, Output = Input> = {
|
|
3
|
+
readonly "~standard": {
|
|
4
|
+
readonly version: 1;
|
|
5
|
+
readonly vendor: string;
|
|
6
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
7
|
+
readonly types?: {
|
|
8
|
+
readonly input: Input;
|
|
9
|
+
readonly output: Output;
|
|
10
|
+
} | undefined;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export type StandardIssue = {
|
|
14
|
+
readonly message: string;
|
|
15
|
+
readonly path?: readonly (PropertyKey | {
|
|
16
|
+
readonly key: PropertyKey;
|
|
17
|
+
})[] | undefined;
|
|
18
|
+
};
|
|
19
|
+
export type StandardResult<Output> = {
|
|
20
|
+
readonly value: Output;
|
|
21
|
+
readonly issues?: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
readonly issues: readonly StandardIssue[];
|
|
24
|
+
};
|
|
25
|
+
export type InferOutput<Schema> = Schema extends StandardSchema<unknown, infer Output> ? Output : never;
|
|
26
|
+
export type SchemaProperty = {
|
|
27
|
+
key: string;
|
|
28
|
+
type?: JsonType;
|
|
29
|
+
format?: string;
|
|
30
|
+
enum?: readonly string[];
|
|
31
|
+
title?: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
required: boolean;
|
|
34
|
+
readOnly: boolean;
|
|
35
|
+
minLength?: number;
|
|
36
|
+
maxLength?: number;
|
|
37
|
+
minimum?: number;
|
|
38
|
+
maximum?: number;
|
|
39
|
+
pattern?: string;
|
|
40
|
+
items?: SchemaProperty;
|
|
41
|
+
properties?: readonly SchemaProperty[];
|
|
42
|
+
};
|
|
43
|
+
export type Introspect = (schema: unknown) => readonly SchemaProperty[] | undefined;
|
|
44
|
+
export declare function jsonSchemaProperties(jsonSchema: unknown): SchemaProperty[] | undefined;
|
|
45
|
+
/** Whether a Zod schema (or one with Standard JSON Schema) is a list: `z.array(line)`. */
|
|
46
|
+
export declare function isListSchema(schema: unknown): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Reads a Zod 4 schema through its own JSON Schema (`schema.toJSONSchema()`), or any schema that implements Standard
|
|
49
|
+
* JSON Schema. A schema it can't read still validates; only the deduced columns and fields need declaring by hand.
|
|
50
|
+
*/
|
|
51
|
+
export declare const zodIntrospect: Introspect;
|
|
52
|
+
export declare function isStandardSchema(value: unknown): value is StandardSchema;
|
|
53
|
+
export declare function issuePath(issue: StandardIssue): string;
|
|
54
|
+
export declare function validate<Output>(schema: StandardSchema<unknown, Output>, value: unknown): Promise<StandardResult<Output>>;
|
|
55
|
+
export declare function isValidationFailure<Output>(result: StandardResult<Output>): result is {
|
|
56
|
+
readonly issues: readonly StandardIssue[];
|
|
57
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { JsonType } from "../core/constants.js";
|
|
2
|
+
import { isFunction, isNumber, isRecord, isString, isText, valuesOf } from "../core/guards.js";
|
|
3
|
+
const jsonTypes = valuesOf(JsonType);
|
|
4
|
+
function typeOf(node) {
|
|
5
|
+
const raw = node.type;
|
|
6
|
+
const list = Array.isArray(raw) ? raw : [raw];
|
|
7
|
+
return jsonTypes.find((type) => type !== JsonType.null && list.includes(type));
|
|
8
|
+
}
|
|
9
|
+
function unwrap(node) {
|
|
10
|
+
if (!isRecord(node))
|
|
11
|
+
return undefined;
|
|
12
|
+
for (const key of ["anyOf", "oneOf", "allOf"]) {
|
|
13
|
+
const list = node[key];
|
|
14
|
+
if (!Array.isArray(list))
|
|
15
|
+
continue;
|
|
16
|
+
const meaningful = list.filter((entry) => !(isRecord(entry) && entry.type === JsonType.null));
|
|
17
|
+
if (meaningful.length === 1)
|
|
18
|
+
return { ...unwrap(meaningful[0]), ...withoutCombinators(node) };
|
|
19
|
+
}
|
|
20
|
+
return node;
|
|
21
|
+
}
|
|
22
|
+
function withoutCombinators(node) {
|
|
23
|
+
const { anyOf: _anyOf, oneOf: _oneOf, allOf: _allOf, ...rest } = node;
|
|
24
|
+
return rest;
|
|
25
|
+
}
|
|
26
|
+
function propertyOf(key, raw, required) {
|
|
27
|
+
const node = unwrap(raw) ?? {};
|
|
28
|
+
const type = typeOf(node);
|
|
29
|
+
const enumValues = Array.isArray(node.enum) ? node.enum.filter(isString) : undefined;
|
|
30
|
+
const items = node.items === undefined ? undefined : propertyOf("", node.items, true);
|
|
31
|
+
const properties = type === JsonType.object ? propertiesOf(node) : undefined;
|
|
32
|
+
return {
|
|
33
|
+
key,
|
|
34
|
+
required,
|
|
35
|
+
readOnly: node.readOnly === true,
|
|
36
|
+
...(type ? { type } : {}),
|
|
37
|
+
...(isText(node.format) ? { format: node.format } : {}),
|
|
38
|
+
...(enumValues && enumValues.length > 0 ? { enum: enumValues } : {}),
|
|
39
|
+
...(isText(node.title) ? { title: node.title } : {}),
|
|
40
|
+
...(isText(node.description) ? { description: node.description } : {}),
|
|
41
|
+
...(isNumber(node.minLength) ? { minLength: node.minLength } : {}),
|
|
42
|
+
...(isNumber(node.maxLength) ? { maxLength: node.maxLength } : {}),
|
|
43
|
+
...(isNumber(node.minimum) ? { minimum: node.minimum } : {}),
|
|
44
|
+
...(isNumber(node.maximum) ? { maximum: node.maximum } : {}),
|
|
45
|
+
...(isText(node.pattern) ? { pattern: node.pattern } : {}),
|
|
46
|
+
...(items ? { items } : {}),
|
|
47
|
+
...(properties ? { properties } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function propertiesOf(node) {
|
|
51
|
+
if (!isRecord(node.properties))
|
|
52
|
+
return undefined;
|
|
53
|
+
const required = new Set(Array.isArray(node.required) ? node.required.filter(isString) : []);
|
|
54
|
+
return Object.entries(node.properties).map(([key, value]) => propertyOf(key, value, required.has(key)));
|
|
55
|
+
}
|
|
56
|
+
export function jsonSchemaProperties(jsonSchema) {
|
|
57
|
+
const node = unwrap(jsonSchema);
|
|
58
|
+
if (node && typeOf(node) === JsonType.array) {
|
|
59
|
+
const items = unwrap(node.items);
|
|
60
|
+
return items ? propertiesOf(items) : undefined;
|
|
61
|
+
}
|
|
62
|
+
return node ? propertiesOf(node) : undefined;
|
|
63
|
+
}
|
|
64
|
+
/** Whether a Zod schema (or one with Standard JSON Schema) is a list: `z.array(line)`. */
|
|
65
|
+
export function isListSchema(schema) {
|
|
66
|
+
try {
|
|
67
|
+
const node = unwrap(zodJsonSchema(schema));
|
|
68
|
+
return node !== undefined && typeOf(node) === JsonType.array;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function zodJsonSchema(schema) {
|
|
75
|
+
if (!isRecord(schema))
|
|
76
|
+
return undefined;
|
|
77
|
+
const method = schema.toJSONSchema;
|
|
78
|
+
if (isFunction(method)) {
|
|
79
|
+
const call = Reflect.apply(method, schema, [{ io: "input", unrepresentable: "any" }]);
|
|
80
|
+
return call;
|
|
81
|
+
}
|
|
82
|
+
const standard = schema["~standard"];
|
|
83
|
+
if (isRecord(standard) && isRecord(standard.jsonSchema) && isFunction(standard.jsonSchema.input)) {
|
|
84
|
+
const call = Reflect.apply(standard.jsonSchema.input, standard.jsonSchema, [{ target: "draft-2020-12" }]);
|
|
85
|
+
return call;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Reads a Zod 4 schema through its own JSON Schema (`schema.toJSONSchema()`), or any schema that implements Standard
|
|
91
|
+
* JSON Schema. A schema it can't read still validates; only the deduced columns and fields need declaring by hand.
|
|
92
|
+
*/
|
|
93
|
+
export const zodIntrospect = (schema) => {
|
|
94
|
+
try {
|
|
95
|
+
return jsonSchemaProperties(zodJsonSchema(schema));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export function isStandardSchema(value) {
|
|
102
|
+
return isRecord(value) && isRecord(value["~standard"]) && isFunction(value["~standard"].validate);
|
|
103
|
+
}
|
|
104
|
+
function pathKey(segment) {
|
|
105
|
+
return String(isRecord(segment) ? segment.key : segment);
|
|
106
|
+
}
|
|
107
|
+
export function issuePath(issue) {
|
|
108
|
+
return (issue.path ?? []).map(pathKey).join(".");
|
|
109
|
+
}
|
|
110
|
+
export async function validate(schema, value) {
|
|
111
|
+
return schema["~standard"].validate(value);
|
|
112
|
+
}
|
|
113
|
+
export function isValidationFailure(result) {
|
|
114
|
+
return result.issues !== undefined;
|
|
115
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CallContext, PageSourceLike, ResourceService, Target, Version } from "../core/contracts.js";
|
|
2
|
+
export type PageSourceCrud<T, Id, F> = {
|
|
3
|
+
get(id: Id, context: CallContext): Promise<T>;
|
|
4
|
+
create?(input: Partial<T>, context: CallContext): Promise<T>;
|
|
5
|
+
update?(id: Id, patch: Partial<T>, context: CallContext & {
|
|
6
|
+
version?: Version | undefined;
|
|
7
|
+
}): Promise<T>;
|
|
8
|
+
remove?(target: Target<Id, F>, context: CallContext): Promise<void>;
|
|
9
|
+
restore?(target: Target<Id, F>, context: CallContext): Promise<void>;
|
|
10
|
+
getMany?(ids: readonly Id[], context: CallContext): Promise<T[]>;
|
|
11
|
+
versionOf?(row: T): Version | undefined;
|
|
12
|
+
};
|
|
13
|
+
export declare function fromPageSource<T, Id = string, F = Record<string, unknown>>(source: PageSourceLike<T, F & {
|
|
14
|
+
q?: string | undefined;
|
|
15
|
+
}>, crud: PageSourceCrud<T, Id, F>): ResourceService<T, Id, F>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ServiceErrorKind } from "../core/constants.js";
|
|
2
|
+
import { ServiceFailure, serviceError } from "../core/errors.js";
|
|
3
|
+
import { searchKey } from "../resource/resource.js";
|
|
4
|
+
function unsupported(operation) {
|
|
5
|
+
throw new ServiceFailure(serviceError(ServiceErrorKind.forbidden, `This service has no ${operation}.`));
|
|
6
|
+
}
|
|
7
|
+
export function fromPageSource(source, crud) {
|
|
8
|
+
return {
|
|
9
|
+
capabilities: { ...source.capabilities, ...(crud.restore ? { restore: true } : {}) },
|
|
10
|
+
list(query, { signal }) {
|
|
11
|
+
const filter = { ...query.filter, [searchKey]: query.search };
|
|
12
|
+
return source.fetchPage({ filter, sort: query.sort, size: query.size, position: query.position }, { signal });
|
|
13
|
+
},
|
|
14
|
+
...(source.fetchTotal
|
|
15
|
+
? {
|
|
16
|
+
count: (filter, { signal }) => source.fetchTotal({ ...filter, [searchKey]: undefined }, { signal }),
|
|
17
|
+
}
|
|
18
|
+
: {}),
|
|
19
|
+
get: (id, context) => crud.get(id, context),
|
|
20
|
+
create: (input, context) => (crud.create ? crud.create(input, context) : unsupported("create")),
|
|
21
|
+
update: (id, patch, context) => (crud.update ? crud.update(id, patch, context) : unsupported("update")),
|
|
22
|
+
remove: (target, context) => (crud.remove ? crud.remove(target, context) : unsupported("remove")),
|
|
23
|
+
...(crud.restore ? { restore: crud.restore } : {}),
|
|
24
|
+
...(crud.getMany ? { getMany: crud.getMany } : {}),
|
|
25
|
+
...(crud.versionOf ? { versionOf: crud.versionOf } : {}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Page, type Query, type SortSpec } from "../core/contracts.js";
|
|
2
|
+
export declare function valueAt(row: unknown, path: string): unknown;
|
|
3
|
+
export declare function sortRows<T>(rows: readonly T[], sort: readonly SortSpec[]): T[];
|
|
4
|
+
export declare function isActive(value: unknown): boolean;
|
|
5
|
+
export declare function matchesValue(actual: unknown, expected: unknown): boolean;
|
|
6
|
+
export declare function matchesFilter(row: unknown, filter: unknown): boolean;
|
|
7
|
+
export declare function matchesSearch(row: unknown, search: string | undefined): boolean;
|
|
8
|
+
export declare function applyQuery<T>(rows: readonly T[], query: Query<unknown>): Page<T>;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { CursorDirection, SortDirection } from "../core/constants.js";
|
|
2
|
+
import { isPagePosition } from "../core/contracts.js";
|
|
3
|
+
import { isBoolean, isNumber, isRecord, isString } from "../core/guards.js";
|
|
4
|
+
export function valueAt(row, path) {
|
|
5
|
+
let current = row;
|
|
6
|
+
for (const segment of path.split(".")) {
|
|
7
|
+
if (!isRecord(current))
|
|
8
|
+
return undefined;
|
|
9
|
+
current = current[segment];
|
|
10
|
+
}
|
|
11
|
+
return current;
|
|
12
|
+
}
|
|
13
|
+
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
|
14
|
+
function comparable(value) {
|
|
15
|
+
if (isNumber(value))
|
|
16
|
+
return value;
|
|
17
|
+
if (isBoolean(value))
|
|
18
|
+
return value ? 1 : 0;
|
|
19
|
+
if (isString(value))
|
|
20
|
+
return value;
|
|
21
|
+
if (value instanceof Date)
|
|
22
|
+
return value.getTime();
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
function compare(left, right) {
|
|
26
|
+
const a = comparable(left);
|
|
27
|
+
const b = comparable(right);
|
|
28
|
+
if (a === undefined)
|
|
29
|
+
return b === undefined ? 0 : 1;
|
|
30
|
+
if (b === undefined)
|
|
31
|
+
return -1;
|
|
32
|
+
if (isNumber(a) && isNumber(b))
|
|
33
|
+
return a - b;
|
|
34
|
+
return collator.compare(String(a), String(b));
|
|
35
|
+
}
|
|
36
|
+
export function sortRows(rows, sort) {
|
|
37
|
+
if (sort.length === 0)
|
|
38
|
+
return [...rows];
|
|
39
|
+
return rows.toSorted((left, right) => {
|
|
40
|
+
for (const spec of sort) {
|
|
41
|
+
const order = compare(valueAt(left, spec.field), valueAt(right, spec.field));
|
|
42
|
+
if (order !== 0)
|
|
43
|
+
return spec.direction === SortDirection.desc ? -order : order;
|
|
44
|
+
}
|
|
45
|
+
return 0;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export function isActive(value) {
|
|
49
|
+
if (value === undefined || value === null)
|
|
50
|
+
return false;
|
|
51
|
+
if (isString(value))
|
|
52
|
+
return value.trim() !== "";
|
|
53
|
+
if (Array.isArray(value))
|
|
54
|
+
return value.length > 0;
|
|
55
|
+
if (isRecord(value) && ("from" in value || "to" in value))
|
|
56
|
+
return isActive(value.from) || isActive(value.to);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
function timeOf(value) {
|
|
60
|
+
if (value instanceof Date)
|
|
61
|
+
return value.getTime();
|
|
62
|
+
if (isString(value) || isNumber(value)) {
|
|
63
|
+
const time = new Date(value).getTime();
|
|
64
|
+
return Number.isNaN(time) ? undefined : time;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
function dayEnd(value) {
|
|
69
|
+
const time = timeOf(value);
|
|
70
|
+
if (time === undefined)
|
|
71
|
+
return undefined;
|
|
72
|
+
return isString(value) && /^\d{4}-\d{2}-\d{2}$/u.test(value) ? time + 86_399_999 : time;
|
|
73
|
+
}
|
|
74
|
+
const loose = (value) => (isString(value) || isNumber(value) || isBoolean(value) ? String(value) : value);
|
|
75
|
+
export function matchesValue(actual, expected) {
|
|
76
|
+
if (!isActive(expected))
|
|
77
|
+
return true;
|
|
78
|
+
if (Array.isArray(expected))
|
|
79
|
+
return expected.some((entry) => matchesValue(actual, entry));
|
|
80
|
+
if (Array.isArray(actual))
|
|
81
|
+
return actual.some((entry) => matchesValue(entry, expected));
|
|
82
|
+
if (isRecord(expected) && ("from" in expected || "to" in expected)) {
|
|
83
|
+
const time = timeOf(actual);
|
|
84
|
+
if (time === undefined)
|
|
85
|
+
return false;
|
|
86
|
+
const from = timeOf(expected.from);
|
|
87
|
+
const to = dayEnd(expected.to);
|
|
88
|
+
return (from === undefined || time >= from) && (to === undefined || time <= to);
|
|
89
|
+
}
|
|
90
|
+
if (isRecord(expected) && isString(expected.op)) {
|
|
91
|
+
if (!isNumber(actual))
|
|
92
|
+
return false;
|
|
93
|
+
if (expected.op === "between")
|
|
94
|
+
return isNumber(expected.min) && isNumber(expected.max) ? actual >= expected.min && actual <= expected.max : true;
|
|
95
|
+
return isNumber(expected.value) ? compareNumber(expected.op, actual, expected.value) : true;
|
|
96
|
+
}
|
|
97
|
+
return loose(actual) === loose(expected);
|
|
98
|
+
}
|
|
99
|
+
function compareNumber(op, actual, value) {
|
|
100
|
+
switch (op) {
|
|
101
|
+
case "neq":
|
|
102
|
+
return actual !== value;
|
|
103
|
+
case "lt":
|
|
104
|
+
return actual < value;
|
|
105
|
+
case "lte":
|
|
106
|
+
return actual <= value;
|
|
107
|
+
case "gt":
|
|
108
|
+
return actual > value;
|
|
109
|
+
case "gte":
|
|
110
|
+
return actual >= value;
|
|
111
|
+
default:
|
|
112
|
+
return actual === value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export function matchesFilter(row, filter) {
|
|
116
|
+
if (!isRecord(filter))
|
|
117
|
+
return true;
|
|
118
|
+
return Object.entries(filter).every(([key, expected]) => key.startsWith("$") || matchesValue(valueAt(row, key), expected));
|
|
119
|
+
}
|
|
120
|
+
function textOf(value, depth = 0) {
|
|
121
|
+
if (isString(value))
|
|
122
|
+
return value;
|
|
123
|
+
if (isNumber(value))
|
|
124
|
+
return String(value);
|
|
125
|
+
if (depth > 1)
|
|
126
|
+
return "";
|
|
127
|
+
if (Array.isArray(value))
|
|
128
|
+
return value.map((entry) => textOf(entry, depth + 1)).join(" ");
|
|
129
|
+
if (isRecord(value))
|
|
130
|
+
return Object.values(value)
|
|
131
|
+
.map((entry) => textOf(entry, depth + 1))
|
|
132
|
+
.join(" ");
|
|
133
|
+
return "";
|
|
134
|
+
}
|
|
135
|
+
export function matchesSearch(row, search) {
|
|
136
|
+
if (search === undefined || search.trim() === "")
|
|
137
|
+
return true;
|
|
138
|
+
const haystack = textOf(row).toLocaleLowerCase();
|
|
139
|
+
return search
|
|
140
|
+
.toLocaleLowerCase()
|
|
141
|
+
.split(/\s+/u)
|
|
142
|
+
.filter(Boolean)
|
|
143
|
+
.every((word) => haystack.includes(word));
|
|
144
|
+
}
|
|
145
|
+
export function applyQuery(rows, query) {
|
|
146
|
+
const matched = sortRows(rows.filter((row) => matchesFilter(row, query.filter) && matchesSearch(row, query.search)), query.sort);
|
|
147
|
+
const size = Math.max(1, Math.trunc(query.size));
|
|
148
|
+
if (isPagePosition(query.position)) {
|
|
149
|
+
const page = Math.max(1, Math.trunc(query.position.page));
|
|
150
|
+
const start = (page - 1) * size;
|
|
151
|
+
const items = matched.slice(start, start + size);
|
|
152
|
+
return { items, total: matched.length, hasMore: start + size < matched.length, hasPrevious: page > 1 };
|
|
153
|
+
}
|
|
154
|
+
const offset = query.position.cursor === null ? 0 : Number(query.position.cursor) || 0;
|
|
155
|
+
const start = query.position.direction === CursorDirection.backward ? Math.max(0, offset - size) : offset;
|
|
156
|
+
const items = matched.slice(start, start + size);
|
|
157
|
+
return {
|
|
158
|
+
items,
|
|
159
|
+
total: matched.length,
|
|
160
|
+
hasMore: start + size < matched.length,
|
|
161
|
+
hasPrevious: start > 0,
|
|
162
|
+
nextCursor: start + size < matched.length ? String(start + size) : null,
|
|
163
|
+
prevCursor: start > 0 ? String(start) : null,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ResourceService } from "../core/contracts.js";
|
|
2
|
+
import { type Rng } from "./rng.js";
|
|
3
|
+
import { type Introspect, type StandardSchema } from "../resource/schema.js";
|
|
4
|
+
type IdOf<T> = T extends {
|
|
5
|
+
id: infer Id;
|
|
6
|
+
} ? Id : string;
|
|
7
|
+
export type MemoryServiceOptions<T> = {
|
|
8
|
+
/** The same seed gives the same rows. Default 1. */
|
|
9
|
+
seed?: number;
|
|
10
|
+
/** How many rows to generate. Default 50. */
|
|
11
|
+
count?: number;
|
|
12
|
+
rows?: readonly T[];
|
|
13
|
+
generate?: Partial<Record<string, (rng: Rng, index: number) => unknown>>;
|
|
14
|
+
/** A delay per call, in ms, or a range: to see the loading states. */
|
|
15
|
+
latency?: number | readonly [min: number, max: number];
|
|
16
|
+
/** The share of calls that fail with a retryable server error: to see the error states. */
|
|
17
|
+
failRate?: number;
|
|
18
|
+
versioned?: boolean;
|
|
19
|
+
softDelete?: boolean;
|
|
20
|
+
/** Adds what a server would join on read, like `country` from `countryId`. Never stored. */
|
|
21
|
+
expand?: (row: T) => T;
|
|
22
|
+
introspect?: Introspect;
|
|
23
|
+
/** The time generated dates count back from. A fixed date by default, so fixtures don't drift. */
|
|
24
|
+
now?: Date;
|
|
25
|
+
};
|
|
26
|
+
export type MemoryService<T, Id> = ResourceService<T, Id> & {
|
|
27
|
+
rows(): T[];
|
|
28
|
+
reset(): void;
|
|
29
|
+
};
|
|
30
|
+
export declare function memoryService<T, Id = IdOf<T>>(resource: StandardSchema<unknown, T> | {
|
|
31
|
+
schema: StandardSchema<unknown, T>;
|
|
32
|
+
}, options?: MemoryServiceOptions<T>): MemoryService<T, Id>;
|
|
33
|
+
export {};
|