@usebacklit/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/dist/main.d.ts +319 -0
- package/dist/main.js +256 -0
- package/package.json +49 -3
- package/README.md +0 -3
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import Macroable from "@poppinss/macroable";
|
|
2
|
+
import { BooleanField, Field, FieldBase, FieldRole, GetFieldKindsForRole, GetFieldRolesForKind, GetFieldVariantsFor } from "@usebacklit/protocol";
|
|
3
|
+
import { Prettify } from "@poppinss/types";
|
|
4
|
+
//#region src/errors.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Thrown at emit when a field kind has no remapper in the role that the block
|
|
7
|
+
* asks for, nor in the fallback role of the block. That is a configuration
|
|
8
|
+
* gap, not bad data, so it fails loud. Args fill the message: field role,
|
|
9
|
+
* field kind, then field name.
|
|
10
|
+
*/
|
|
11
|
+
declare const E_MISSING_FIELD_REMAPPER: new (args: [role: string, kind: string, name: string], options?: ErrorOptions) => import("@poppinss/exception").Exception;
|
|
12
|
+
/** Thrown when remappers are added to a role that has not been registered. */
|
|
13
|
+
declare const E_UNKNOWN_FIELD_REMAPPER_ROLE: new (args: [role: string], options?: ErrorOptions) => import("@poppinss/exception").Exception;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/symbols.d.ts
|
|
16
|
+
/**
|
|
17
|
+
* Builds the node of a field in the role that a block asks for. The block
|
|
18
|
+
* calls it once, then calls `VALUE_FOR` with the node for each record.
|
|
19
|
+
*/
|
|
20
|
+
declare const NODE_FOR: unique symbol;
|
|
21
|
+
/**
|
|
22
|
+
* Builds the value of a field for one record, in the role of the node that
|
|
23
|
+
* `NODE_FOR` returned. The block calls it once for each record.
|
|
24
|
+
*/
|
|
25
|
+
declare const VALUE_FOR: unique symbol;
|
|
26
|
+
/** Carries a field's own name, so a resource can look the field up by it. */
|
|
27
|
+
declare const FIELD_NAME: unique symbol;
|
|
28
|
+
/**
|
|
29
|
+
* Carries the value a field's kind puts on the wire, at the type level only.
|
|
30
|
+
* Nothing is ever assigned under it — it exists so the kind declares its
|
|
31
|
+
* remapped shape once and everything downstream reads it: a lookup's pick, a
|
|
32
|
+
* form's binding, a renderer's component.
|
|
33
|
+
*/
|
|
34
|
+
declare const FIELD_VALUE: unique symbol;
|
|
35
|
+
/**
|
|
36
|
+
* Carries the row a field reads, at the type level only — the same record
|
|
37
|
+
* `VALUE_FOR` is called with at emit. Nothing is ever assigned under it —
|
|
38
|
+
* it exists so the place that knows the row can state it (a block holding
|
|
39
|
+
* the data, a relation naming its target) and the field's own callbacks
|
|
40
|
+
* are typed by it, with a wrong match refused where field meets data.
|
|
41
|
+
*/
|
|
42
|
+
declare const FIELD_ROW: unique symbol;
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/types/common.d.ts
|
|
45
|
+
/**
|
|
46
|
+
* Reports a value that could not be sent, and returns `undefined` so the
|
|
47
|
+
* caller can return it in the same statement. Where the defect goes, and
|
|
48
|
+
* what to do about it — throw, warn, or collect — are the reporter's.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const report: ReportDefect = (reason) => {
|
|
53
|
+
* console.warn(reason)
|
|
54
|
+
* return undefined
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
type ReportDefect = (reason: string) => undefined;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/field_remappers/builtin.d.ts
|
|
61
|
+
/**
|
|
62
|
+
* The remappers that Backlit gives, for each built-in role and kind, with the
|
|
63
|
+
* default variant of the kind in the role. Each key is present, so an app that
|
|
64
|
+
* changes a conversion can handle its own stored values and hand each other
|
|
65
|
+
* value to the built-in remapper.
|
|
66
|
+
*/
|
|
67
|
+
declare const builtinRemappers: {
|
|
68
|
+
summary: Pick<FieldRemapperMap<'summary'>, 'boolean'>;
|
|
69
|
+
detail: Pick<FieldRemapperMap<'detail'>, 'boolean'>;
|
|
70
|
+
input: Pick<FieldRemapperMap<'input'>, 'boolean'>;
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/field_remappers/main.d.ts
|
|
74
|
+
/** Owns the registered field remappers and runs them for SDK data. */
|
|
75
|
+
declare class FieldRemappers {
|
|
76
|
+
#private;
|
|
77
|
+
/** Registers the complete built-in role maps. */
|
|
78
|
+
constructor();
|
|
79
|
+
/**
|
|
80
|
+
* Registers a complete role map. Calling it again for the same role replaces
|
|
81
|
+
* the complete map.
|
|
82
|
+
*/
|
|
83
|
+
registerRole<Role extends FieldRole>(role: Role, remappers: FieldRemapperMap<Role>): void;
|
|
84
|
+
/** Adds or replaces selected remappers on an existing role. */
|
|
85
|
+
registerRemappers<Role extends FieldRole>(role: Role, remappers: Partial<FieldRemapperMap<Role>>): void;
|
|
86
|
+
/**
|
|
87
|
+
* Returns what the kind registered in the role: its remapper and its
|
|
88
|
+
* default variant. Returns `undefined` when the kind has no remapper in the
|
|
89
|
+
* role.
|
|
90
|
+
*/
|
|
91
|
+
get(role: FieldRole, kind: string): RegisteredFieldRemapper | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* Remaps a field from its complete record and selected value, with the
|
|
94
|
+
* remapper of the kind in the role of the field node.
|
|
95
|
+
*/
|
|
96
|
+
remapField(field: Field, record: Record<string, unknown>, value: unknown, report: ReportDefect): unknown;
|
|
97
|
+
}
|
|
98
|
+
/** The process-wide field remapper registry used by SDK builders and extensions. */
|
|
99
|
+
declare const remappers: FieldRemappers;
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/types/fields.d.ts
|
|
102
|
+
/**
|
|
103
|
+
* The role that a block asks for when it emits a field. A block that can also
|
|
104
|
+
* render a second role names it in `fallbackRole`. When the kind of the field
|
|
105
|
+
* has no remapper in `role`, the field is emitted in `fallbackRole`. When it
|
|
106
|
+
* has no remapper in either role, emitting the field throws.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const options: FieldNodeOptions = { role: 'print', fallbackRole: 'summary' }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
type FieldNodeOptions = {
|
|
114
|
+
role: FieldRole;
|
|
115
|
+
fallbackRole?: FieldRole | undefined;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* A field builder. A block emits it with `NODE_FOR`, and gets the value of each
|
|
119
|
+
* record with `VALUE_FOR`. It carries its own name at the type level. The name
|
|
120
|
+
* is on a symbol so it cannot clash with anything on the field node.
|
|
121
|
+
*
|
|
122
|
+
* `Value` is the value the field puts on the wire once the remapper has run —
|
|
123
|
+
* declared by each kind where the kind is written, never assigned, and read by
|
|
124
|
+
* everything downstream: a lookup's pick claims it, a form's binding checks it,
|
|
125
|
+
* a renderer's component types against it. A kind that leaves it off says
|
|
126
|
+
* `unknown`, which claims nothing and is checked nowhere.
|
|
127
|
+
*
|
|
128
|
+
* `FieldBuilderContract<string>` is any field builder, whatever it is called.
|
|
129
|
+
*/
|
|
130
|
+
interface FieldBuilderContract<Name extends string, Value = unknown, in Row = unknown> {
|
|
131
|
+
readonly [FIELD_NAME]: Name;
|
|
132
|
+
readonly [FIELD_VALUE]?: Value;
|
|
133
|
+
/**
|
|
134
|
+
* The row this field reads — the record `VALUE_FOR` is called with at
|
|
135
|
+
* emit, carried at the type level. Function-position so the check runs
|
|
136
|
+
* the right way around: a field reading a subset of the row passes, a
|
|
137
|
+
* field built for different rows is refused where field meets data.
|
|
138
|
+
* Never assigned.
|
|
139
|
+
*/
|
|
140
|
+
readonly [FIELD_ROW]?: (row: Row) => void;
|
|
141
|
+
/**
|
|
142
|
+
* Builds the node of the field in the role that the block asks for, or in
|
|
143
|
+
* its fallback role. The node carries the role that was used, and its
|
|
144
|
+
* variant. It throws `E_MISSING_FIELD_REMAPPER` when the kind of the field
|
|
145
|
+
* has a remapper in neither role.
|
|
146
|
+
*/
|
|
147
|
+
[NODE_FOR](options: FieldNodeOptions): Field;
|
|
148
|
+
/**
|
|
149
|
+
* Builds the value for one record, in the role of `node`. The record is the
|
|
150
|
+
* row that the field reads, so a block calls it with its own rows and no
|
|
151
|
+
* cast, and a field built for other rows is refused at the call.
|
|
152
|
+
*/
|
|
153
|
+
[VALUE_FOR](record: Row, node: Field, report: ReportDefect): unknown;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Turns whatever the adapter hands back into the shape the protocol expects for
|
|
157
|
+
* a field kind. It never decides how a value looks — that is the renderer's job,
|
|
158
|
+
* driven by the selected role and the field variant.
|
|
159
|
+
*
|
|
160
|
+
* A remapper is given the selected value, its field, the complete source
|
|
161
|
+
* record, its parent `FieldRemappers`, and the defect reporter. It can use the
|
|
162
|
+
* parent to remap nested fields. It also decides whether a missing key, `null`,
|
|
163
|
+
* or `undefined` means "no data" or "something is wrong" for its field kind.
|
|
164
|
+
*
|
|
165
|
+
* When a value cannot be remapped, call `report` with the reason and return
|
|
166
|
+
* what it gives back. The caller then leaves the key out and records where it
|
|
167
|
+
* went wrong.
|
|
168
|
+
*
|
|
169
|
+
* The role is a type-level association. It is not passed to the remapper at
|
|
170
|
+
* runtime because the role registry has already selected the function. The
|
|
171
|
+
* field carries the role too, as `field.role`.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const remapBoolean: FieldRemapper<'summary', 'boolean'> = (
|
|
176
|
+
* value,
|
|
177
|
+
* _field,
|
|
178
|
+
* _record,
|
|
179
|
+
* _parent,
|
|
180
|
+
* report
|
|
181
|
+
* ) => {
|
|
182
|
+
* return typeof value === 'boolean' ? value : report('expected a boolean')
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
type FieldRemapper<Role extends FieldRole, Kind extends GetFieldKindsForRole<Role>> = (value: unknown, field: Extract<Field, {
|
|
187
|
+
kind: Kind;
|
|
188
|
+
}>, record: Record<string, unknown>, parent: FieldRemappers, report: ReportDefect) => unknown;
|
|
189
|
+
/**
|
|
190
|
+
* What one field kind registers in one role: the remapper that normalizes its
|
|
191
|
+
* value, and the variant that a field of the kind uses when its declaration
|
|
192
|
+
* names no variant for the role. A kind whose variants are `never` in the role
|
|
193
|
+
* has no default variant.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* const entry: FieldRemapperEntry<'summary', 'boolean'> = {
|
|
198
|
+
* remap: (value, _field, _record, _parent, report) =>
|
|
199
|
+
* typeof value === 'boolean' ? value : report('expected a boolean'),
|
|
200
|
+
* defaultVariant: 'check',
|
|
201
|
+
* }
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
type FieldRemapperEntry<Role extends FieldRole, Kind extends GetFieldKindsForRole<Role>> = {
|
|
205
|
+
remap: FieldRemapper<Role, Kind>;
|
|
206
|
+
} & ([GetFieldVariantsFor<Role, Kind>] extends [never] ? {
|
|
207
|
+
defaultVariant?: never;
|
|
208
|
+
} : {
|
|
209
|
+
defaultVariant: GetFieldVariantsFor<Role, Kind>;
|
|
210
|
+
});
|
|
211
|
+
/**
|
|
212
|
+
* What one field kind registered in one role, as `remappers.get` returns it
|
|
213
|
+
* for any role and kind: the remapper, and the default variant when the kind
|
|
214
|
+
* has variants in the role.
|
|
215
|
+
*/
|
|
216
|
+
type RegisteredFieldRemapper = {
|
|
217
|
+
remap(value: unknown, field: Field, record: Record<string, unknown>, parent: FieldRemappers, report: ReportDefect): unknown;
|
|
218
|
+
defaultVariant?: string | undefined;
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Creates the complete field-kind remapper map for one role. Every field kind
|
|
222
|
+
* supported by the role is required, including a kind whose variant is `never`.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* type SummaryRemappers = FieldRemapperMap<'summary'>
|
|
227
|
+
* // {
|
|
228
|
+
* // boolean: FieldRemapperEntry<'summary', 'boolean'>
|
|
229
|
+
* // }
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
type FieldRemapperMap<Role extends FieldRole> = Prettify<{ [Kind in GetFieldKindsForRole<Role>]: FieldRemapperEntry<Role, Kind>; }>;
|
|
233
|
+
/**
|
|
234
|
+
* Creates the optional variant options of one field kind, one for each role
|
|
235
|
+
* that supports the kind. A role with a `never` variant creates no option.
|
|
236
|
+
* The options are SDK only. The node carries the variant of its own role.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* type BooleanRoleOptions = GetFieldRoleOptionsFor<'boolean'>
|
|
241
|
+
* // {
|
|
242
|
+
* // summaryVariant?: 'check' | 'badge'
|
|
243
|
+
* // detailVariant?: 'check' | 'badge'
|
|
244
|
+
* // inputVariant?: 'checkbox' | 'toggle'
|
|
245
|
+
* // }
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
type GetFieldRoleOptionsFor<Kind extends PropertyKey> = Prettify<{ [Role in GetFieldRolesForKind<Kind> as [GetFieldVariantsFor<Role, Kind>] extends [never] ? never : `${Role}Variant`]?: GetFieldVariantsFor<Role, Kind>; }>;
|
|
249
|
+
/** The configuration accepted by a boolean field. */
|
|
250
|
+
type BooleanFieldOptions = Prettify<Partial<Pick<FieldBase, 'label' | 'description'>> & GetFieldRoleOptionsFor<'boolean'>>;
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/fields/field_builder.d.ts
|
|
253
|
+
/**
|
|
254
|
+
* The base of every field builder, built-in or added by an app. It picks the
|
|
255
|
+
* role and the variant that a field node carries, so each kind picks them in
|
|
256
|
+
* the same way and its `NODE_FOR` adds only the keys of the kind.
|
|
257
|
+
*/
|
|
258
|
+
declare abstract class FieldBuilder {
|
|
259
|
+
/**
|
|
260
|
+
* Picks the role and the variant of a field node. The role is the role that
|
|
261
|
+
* the block asks for, when the kind has a remapper in it. Otherwise it is
|
|
262
|
+
* the fallback role of the block, when the kind has a remapper in it. The
|
|
263
|
+
* variant is the option of the declaration for that role, such as
|
|
264
|
+
* `summaryVariant`, or else the default variant of the kind in the role.
|
|
265
|
+
*
|
|
266
|
+
* It throws when the kind has a remapper in neither role, because the block
|
|
267
|
+
* cannot emit the field.
|
|
268
|
+
*/
|
|
269
|
+
protected resolveRoleAndVariant(kind: string, name: string, options: FieldNodeOptions, variantOptions: Record<string, unknown>): {
|
|
270
|
+
role: FieldRole;
|
|
271
|
+
variant?: string;
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/fields/boolean.d.ts
|
|
276
|
+
/** Builds a boolean field: a value that is either true or false. */
|
|
277
|
+
declare class BooleanFieldBuilder<Name extends string = string, Row = unknown> extends FieldBuilder implements FieldBuilderContract<Name, boolean, Row> {
|
|
278
|
+
#private;
|
|
279
|
+
readonly [FIELD_VALUE]?: boolean;
|
|
280
|
+
readonly [FIELD_ROW]?: (row: Row) => void;
|
|
281
|
+
constructor(name: Name, options?: BooleanFieldOptions);
|
|
282
|
+
get [FIELD_NAME](): Name;
|
|
283
|
+
/** Returns a configured copy and leaves this field unchanged. */
|
|
284
|
+
configure<R = Row>(options: BooleanFieldOptions): BooleanFieldBuilder<Name, R>;
|
|
285
|
+
/** Returns an independent copy with the same configuration. */
|
|
286
|
+
clone(): BooleanFieldBuilder<Name, Row>;
|
|
287
|
+
/**
|
|
288
|
+
* Builds the node in the role that the block asks for, or in its fallback
|
|
289
|
+
* role. The node carries the role that was used, and the variant of the
|
|
290
|
+
* field for that role.
|
|
291
|
+
*/
|
|
292
|
+
[NODE_FOR](options: FieldNodeOptions): BooleanField;
|
|
293
|
+
/**
|
|
294
|
+
* Builds the value for one record: the raw value converted through the
|
|
295
|
+
* remapper registry, for the role of the node.
|
|
296
|
+
*/
|
|
297
|
+
[VALUE_FOR](row: Row, node: BooleanField, report: ReportDefect): unknown;
|
|
298
|
+
}
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/fields/factory.d.ts
|
|
301
|
+
/**
|
|
302
|
+
* The factory behind the `fields` export. The built-in kinds are the methods
|
|
303
|
+
* below.
|
|
304
|
+
*
|
|
305
|
+
* It is macroable, so a host can add a builder for its own field kind with
|
|
306
|
+
* `FieldsFactory.macro('image', ...)` and declare the method's type through
|
|
307
|
+
* `declare module '@usebacklit/sdk'`. Whatever it adds still has to satisfy
|
|
308
|
+
* `FieldBuilderContract<Name>`.
|
|
309
|
+
*/
|
|
310
|
+
declare class FieldsFactory extends Macroable {
|
|
311
|
+
/**
|
|
312
|
+
* Defines a boolean field.
|
|
313
|
+
*/
|
|
314
|
+
boolean<Name extends string>(name: Name, options?: BooleanFieldOptions): BooleanFieldBuilder<Name, unknown>;
|
|
315
|
+
}
|
|
316
|
+
/** The field builders, and where a host's own fields are added. */
|
|
317
|
+
declare const fields: FieldsFactory;
|
|
318
|
+
//#endregion
|
|
319
|
+
export { BooleanFieldBuilder, type BooleanFieldOptions, E_MISSING_FIELD_REMAPPER, E_UNKNOWN_FIELD_REMAPPER_ROLE, FIELD_NAME, FIELD_ROW, FIELD_VALUE, FieldBuilder, type FieldBuilderContract, type FieldNodeOptions, type FieldRemapper, type FieldRemapperEntry, type FieldRemapperMap, FieldRemappers, FieldsFactory, type GetFieldRoleOptionsFor, NODE_FOR, type RegisteredFieldRemapper, type ReportDefect, VALUE_FOR, builtinRemappers, fields, remappers };
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { createError } from "@poppinss/exception";
|
|
2
|
+
import Macroable from "@poppinss/macroable";
|
|
3
|
+
//#region src/errors.ts
|
|
4
|
+
/**
|
|
5
|
+
* Thrown at emit when a field kind has no remapper in the role that the block
|
|
6
|
+
* asks for, nor in the fallback role of the block. That is a configuration
|
|
7
|
+
* gap, not bad data, so it fails loud. Args fill the message: field role,
|
|
8
|
+
* field kind, then field name.
|
|
9
|
+
*/
|
|
10
|
+
const E_MISSING_FIELD_REMAPPER = createError("No remapper for role \"%s\" and field kind \"%s\" (field \"%s\"). Register one with remappers.registerRole() or remappers.registerRemappers().", "E_MISSING_FIELD_REMAPPER");
|
|
11
|
+
/** Thrown when remappers are added to a role that has not been registered. */
|
|
12
|
+
const E_UNKNOWN_FIELD_REMAPPER_ROLE = createError("Cannot register remappers for field role \"%s\". Register the complete role first with remappers.registerRole().", "E_UNKNOWN_FIELD_REMAPPER_ROLE");
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/symbols.ts
|
|
15
|
+
/**
|
|
16
|
+
* Builds the node of a field in the role that a block asks for. The block
|
|
17
|
+
* calls it once, then calls `VALUE_FOR` with the node for each record.
|
|
18
|
+
*/
|
|
19
|
+
const NODE_FOR = Symbol.for("nodeFor");
|
|
20
|
+
/**
|
|
21
|
+
* Builds the value of a field for one record, in the role of the node that
|
|
22
|
+
* `NODE_FOR` returned. The block calls it once for each record.
|
|
23
|
+
*/
|
|
24
|
+
const VALUE_FOR = Symbol.for("valueFor");
|
|
25
|
+
/** Carries a field's own name, so a resource can look the field up by it. */
|
|
26
|
+
const FIELD_NAME = Symbol.for("fieldName");
|
|
27
|
+
/**
|
|
28
|
+
* Carries the value a field's kind puts on the wire, at the type level only.
|
|
29
|
+
* Nothing is ever assigned under it — it exists so the kind declares its
|
|
30
|
+
* remapped shape once and everything downstream reads it: a lookup's pick, a
|
|
31
|
+
* form's binding, a renderer's component.
|
|
32
|
+
*/
|
|
33
|
+
const FIELD_VALUE = Symbol.for("fieldValue");
|
|
34
|
+
/**
|
|
35
|
+
* Carries the row a field reads, at the type level only — the same record
|
|
36
|
+
* `VALUE_FOR` is called with at emit. Nothing is ever assigned under it —
|
|
37
|
+
* it exists so the place that knows the row can state it (a block holding
|
|
38
|
+
* the data, a relation naming its target) and the field's own callbacks
|
|
39
|
+
* are typed by it, with a wrong match refused where field meets data.
|
|
40
|
+
*/
|
|
41
|
+
const FIELD_ROW = Symbol.for("fieldRow");
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/field_remappers/boolean.ts
|
|
44
|
+
/** Converts the stored value of a boolean field into the boolean of the protocol. */
|
|
45
|
+
var BooleanRemapper = class {
|
|
46
|
+
/**
|
|
47
|
+
* Converts a boolean for a role that shows the value: `summary` and
|
|
48
|
+
* `detail`. A key missing from the record is a defect.
|
|
49
|
+
*/
|
|
50
|
+
remapShown(value, field, record, report) {
|
|
51
|
+
if (!Object.hasOwn(record, field.name)) return report("the field is missing from the record");
|
|
52
|
+
return this.read(value, report);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Reads a raw value as a boolean: `true`, `false`, `1`, `0`, and the strings
|
|
56
|
+
* `"true"`, `"false"`, `"1"` and `"0"` in any case. A missing value is
|
|
57
|
+
* `null`. Each other value is a defect. The `input` role uses it alone,
|
|
58
|
+
* because a new form has no record.
|
|
59
|
+
*/
|
|
60
|
+
read(value, report) {
|
|
61
|
+
if (value === null || value === void 0) return null;
|
|
62
|
+
if (typeof value === "boolean") return value;
|
|
63
|
+
if (value === 0 || value === 1) return value === 1;
|
|
64
|
+
if (typeof value === "string") {
|
|
65
|
+
const normalized = value.trim().toLowerCase();
|
|
66
|
+
if (normalized === "true" || normalized === "1") return true;
|
|
67
|
+
if (normalized === "false" || normalized === "0") return false;
|
|
68
|
+
}
|
|
69
|
+
return report(`expected a boolean, got ${JSON.stringify(value)}`);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/field_remappers/builtin.ts
|
|
74
|
+
const boolean = new BooleanRemapper();
|
|
75
|
+
/**
|
|
76
|
+
* The remappers that Backlit gives, for each built-in role and kind, with the
|
|
77
|
+
* default variant of the kind in the role. Each key is present, so an app that
|
|
78
|
+
* changes a conversion can handle its own stored values and hand each other
|
|
79
|
+
* value to the built-in remapper.
|
|
80
|
+
*/
|
|
81
|
+
const builtinRemappers = {
|
|
82
|
+
summary: { boolean: {
|
|
83
|
+
remap: (value, field, record, _parent, report) => boolean.remapShown(value, field, record, report),
|
|
84
|
+
defaultVariant: "check"
|
|
85
|
+
} },
|
|
86
|
+
detail: { boolean: {
|
|
87
|
+
remap: (value, field, record, _parent, report) => boolean.remapShown(value, field, record, report),
|
|
88
|
+
defaultVariant: "check"
|
|
89
|
+
} },
|
|
90
|
+
input: { boolean: {
|
|
91
|
+
remap: (value, _field, _record, _parent, report) => boolean.read(value, report),
|
|
92
|
+
defaultVariant: "checkbox"
|
|
93
|
+
} }
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/field_remappers/main.ts
|
|
97
|
+
/** Owns the registered field remappers and runs them for SDK data. */
|
|
98
|
+
var FieldRemappers = class {
|
|
99
|
+
/** The complete remapper map registered for each field role. */
|
|
100
|
+
#roleRemappers = /* @__PURE__ */ new Map();
|
|
101
|
+
/** Registers the complete built-in role maps. */
|
|
102
|
+
constructor() {
|
|
103
|
+
this.registerRole("summary", builtinRemappers.summary);
|
|
104
|
+
this.registerRole("detail", builtinRemappers.detail);
|
|
105
|
+
this.registerRole("input", builtinRemappers.input);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Registers a complete role map. Calling it again for the same role replaces
|
|
109
|
+
* the complete map.
|
|
110
|
+
*/
|
|
111
|
+
registerRole(role, remappers) {
|
|
112
|
+
this.#roleRemappers.set(role, remappers);
|
|
113
|
+
}
|
|
114
|
+
/** Adds or replaces selected remappers on an existing role. */
|
|
115
|
+
registerRemappers(role, remappers) {
|
|
116
|
+
const registered = this.#roleRemappers.get(role);
|
|
117
|
+
if (registered === void 0) throw new E_UNKNOWN_FIELD_REMAPPER_ROLE([role]);
|
|
118
|
+
this.#roleRemappers.set(role, {
|
|
119
|
+
...registered,
|
|
120
|
+
...remappers
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Returns what the kind registered in the role: its remapper and its
|
|
125
|
+
* default variant. Returns `undefined` when the kind has no remapper in the
|
|
126
|
+
* role.
|
|
127
|
+
*/
|
|
128
|
+
get(role, kind) {
|
|
129
|
+
return this.#roleRemappers.get(role)?.[kind];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Remaps a field from its complete record and selected value, with the
|
|
133
|
+
* remapper of the kind in the role of the field node.
|
|
134
|
+
*/
|
|
135
|
+
remapField(field, record, value, report) {
|
|
136
|
+
const entry = this.get(field.role, field.kind);
|
|
137
|
+
if (entry === void 0) throw new E_MISSING_FIELD_REMAPPER([
|
|
138
|
+
field.role,
|
|
139
|
+
field.kind,
|
|
140
|
+
field.name
|
|
141
|
+
]);
|
|
142
|
+
return entry.remap(value, field, record, this, report);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
/** The process-wide field remapper registry used by SDK builders and extensions. */
|
|
146
|
+
const remappers = new FieldRemappers();
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/fields/field_builder.ts
|
|
149
|
+
/**
|
|
150
|
+
* The base of every field builder, built-in or added by an app. It picks the
|
|
151
|
+
* role and the variant that a field node carries, so each kind picks them in
|
|
152
|
+
* the same way and its `NODE_FOR` adds only the keys of the kind.
|
|
153
|
+
*/
|
|
154
|
+
var FieldBuilder = class {
|
|
155
|
+
/**
|
|
156
|
+
* Picks the role and the variant of a field node. The role is the role that
|
|
157
|
+
* the block asks for, when the kind has a remapper in it. Otherwise it is
|
|
158
|
+
* the fallback role of the block, when the kind has a remapper in it. The
|
|
159
|
+
* variant is the option of the declaration for that role, such as
|
|
160
|
+
* `summaryVariant`, or else the default variant of the kind in the role.
|
|
161
|
+
*
|
|
162
|
+
* It throws when the kind has a remapper in neither role, because the block
|
|
163
|
+
* cannot emit the field.
|
|
164
|
+
*/
|
|
165
|
+
resolveRoleAndVariant(kind, name, options, variantOptions) {
|
|
166
|
+
const roles = options.fallbackRole === void 0 ? [options.role] : [options.role, options.fallbackRole];
|
|
167
|
+
for (const role of roles) {
|
|
168
|
+
const entry = remappers.get(role, kind);
|
|
169
|
+
if (entry === void 0) continue;
|
|
170
|
+
const chosen = variantOptions[`${role}Variant`];
|
|
171
|
+
const variant = typeof chosen === "string" ? chosen : entry.defaultVariant;
|
|
172
|
+
return variant === void 0 ? { role } : {
|
|
173
|
+
role,
|
|
174
|
+
variant
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
throw new E_MISSING_FIELD_REMAPPER([
|
|
178
|
+
options.role,
|
|
179
|
+
kind,
|
|
180
|
+
name
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
//#endregion
|
|
185
|
+
//#region src/fields/boolean.ts
|
|
186
|
+
/** Builds a boolean field: a value that is either true or false. */
|
|
187
|
+
var BooleanFieldBuilder = class BooleanFieldBuilder extends FieldBuilder {
|
|
188
|
+
#name;
|
|
189
|
+
#options;
|
|
190
|
+
constructor(name, options = {}) {
|
|
191
|
+
super();
|
|
192
|
+
this.#name = name;
|
|
193
|
+
this.#options = options;
|
|
194
|
+
}
|
|
195
|
+
get [FIELD_NAME]() {
|
|
196
|
+
return this.#name;
|
|
197
|
+
}
|
|
198
|
+
/** Returns a configured copy and leaves this field unchanged. */
|
|
199
|
+
configure(options) {
|
|
200
|
+
return new BooleanFieldBuilder(this.#name, {
|
|
201
|
+
...this.#options,
|
|
202
|
+
...options
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/** Returns an independent copy with the same configuration. */
|
|
206
|
+
clone() {
|
|
207
|
+
return new BooleanFieldBuilder(this.#name, { ...this.#options });
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Builds the node in the role that the block asks for, or in its fallback
|
|
211
|
+
* role. The node carries the role that was used, and the variant of the
|
|
212
|
+
* field for that role.
|
|
213
|
+
*/
|
|
214
|
+
[NODE_FOR](options) {
|
|
215
|
+
const { label, description } = this.#options;
|
|
216
|
+
return {
|
|
217
|
+
kind: "boolean",
|
|
218
|
+
name: this.#name,
|
|
219
|
+
label: label ?? "",
|
|
220
|
+
...description !== void 0 && { description },
|
|
221
|
+
...this.resolveRoleAndVariant("boolean", this.#name, options, this.#options)
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Builds the value for one record: the raw value converted through the
|
|
226
|
+
* remapper registry, for the role of the node.
|
|
227
|
+
*/
|
|
228
|
+
[VALUE_FOR](row, node, report) {
|
|
229
|
+
const record = row;
|
|
230
|
+
if (!Object.hasOwn(record, this.#name)) return report("the field is missing from the record");
|
|
231
|
+
return remappers.remapField(node, record, record[this.#name], report);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/fields/factory.ts
|
|
236
|
+
/**
|
|
237
|
+
* The factory behind the `fields` export. The built-in kinds are the methods
|
|
238
|
+
* below.
|
|
239
|
+
*
|
|
240
|
+
* It is macroable, so a host can add a builder for its own field kind with
|
|
241
|
+
* `FieldsFactory.macro('image', ...)` and declare the method's type through
|
|
242
|
+
* `declare module '@usebacklit/sdk'`. Whatever it adds still has to satisfy
|
|
243
|
+
* `FieldBuilderContract<Name>`.
|
|
244
|
+
*/
|
|
245
|
+
var FieldsFactory = class extends Macroable {
|
|
246
|
+
/**
|
|
247
|
+
* Defines a boolean field.
|
|
248
|
+
*/
|
|
249
|
+
boolean(name, options = {}) {
|
|
250
|
+
return new BooleanFieldBuilder(name, options);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
/** The field builders, and where a host's own fields are added. */
|
|
254
|
+
const fields = new FieldsFactory();
|
|
255
|
+
//#endregion
|
|
256
|
+
export { BooleanFieldBuilder, E_MISSING_FIELD_REMAPPER, E_UNKNOWN_FIELD_REMAPPER_ROLE, FIELD_NAME, FIELD_ROW, FIELD_VALUE, FieldBuilder, FieldRemappers, FieldsFactory, NODE_FOR, VALUE_FOR, builtinRemappers, fields, remappers };
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usebacklit/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"homepage": "https://usebacklit.com",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/crossoverlabs/backlit.git",
|
|
8
|
+
"directory": "packages/sdk"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=26"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/main.d.ts",
|
|
21
|
+
"default": "./dist/main.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@poppinss/exception": "1.2.3",
|
|
29
|
+
"@poppinss/macroable": "1.1.2",
|
|
30
|
+
"@poppinss/types": "1.2.1",
|
|
31
|
+
"@usebacklit/protocol": "0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@japa/assert": "4.2.0",
|
|
35
|
+
"@japa/expect-type": "2.0.4",
|
|
36
|
+
"@japa/runner": "5.4.0",
|
|
37
|
+
"@types/node": "26.6.11",
|
|
38
|
+
"oxlint": "1.96.0",
|
|
39
|
+
"oxlint-tsgolint": "7.0.2009",
|
|
40
|
+
"tsdown": "0.22.7",
|
|
41
|
+
"typescript": "6.0.3",
|
|
42
|
+
"@usebacklit/oxlint-config": "0.0.0",
|
|
43
|
+
"@usebacklit/tsconfig": "0.0.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"dev": "tsdown --watch",
|
|
48
|
+
"lint": "oxlint",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "node bin/test.ts"
|
|
51
|
+
}
|
|
6
52
|
}
|
package/README.md
DELETED