@xrmforge/helpers 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 XrmForge Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,396 @@
1
+ /**
2
+ * @xrmforge/helpers - Web API Helper Functions
3
+ *
4
+ * Lightweight utility functions for building OData query strings
5
+ * with type-safe field names from generated Fields enums.
6
+ *
7
+ * Zero runtime overhead when used with const enums (values are inlined).
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { select } from '@xrmforge/helpers';
12
+ *
13
+ * Xrm.WebApi.retrieveRecord(ref.entityType, ref.id, select(
14
+ * AccountFields.Name,
15
+ * AccountFields.WebsiteUrl,
16
+ * AccountFields.Address1Line1,
17
+ * ));
18
+ * ```
19
+ */
20
+ /**
21
+ * Build an OData $select query string from field names.
22
+ *
23
+ * @param fields - Field names (use generated Fields enum for type safety)
24
+ * @returns OData query string (e.g. "?$select=name,websiteurl,address1_line1")
25
+ */
26
+ declare function select(...fields: string[]): string;
27
+ /**
28
+ * Parse a lookup field from a Dataverse Web API response into a LookupValue.
29
+ *
30
+ * Dataverse returns lookups as `_fieldname_value` with OData annotations:
31
+ * - `_fieldname_value` (GUID)
32
+ * - `_fieldname_value@OData.Community.Display.V1.FormattedValue` (display name)
33
+ * - `_fieldname_value@Microsoft.Dynamics.CRM.lookuplogicalname` (entity type)
34
+ *
35
+ * This function extracts all three into an `Xrm.LookupValue` object.
36
+ *
37
+ * @param response - The raw Web API response object
38
+ * @param navigationProperty - Navigation property name (use NavigationProperties enum for type safety)
39
+ * @returns Xrm.LookupValue or null if the lookup is empty
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * // With NavigationProperties enum (recommended):
44
+ * parseLookup(result, AccountNav.Country);
45
+ *
46
+ * // Or with navigation property name directly:
47
+ * parseLookup(result, 'markant_address1_countryid');
48
+ * ```
49
+ */
50
+ declare function parseLookup(response: Record<string, unknown>, navigationProperty: string): {
51
+ id: string;
52
+ name: string;
53
+ entityType: string;
54
+ } | null;
55
+ /**
56
+ * Parse multiple lookup fields from a Dataverse Web API response at once.
57
+ *
58
+ * @param response - The raw Web API response object
59
+ * @param navigationProperties - Navigation property names to parse
60
+ * @returns Map of navigation property name to LookupValue (null entries omitted)
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const lookups = parseLookups(result, ['markant_address1_countryid', 'parentaccountid']);
65
+ * formContext.getAttribute(Fields.Country).setValue(
66
+ * lookups.markant_address1_countryid ? [lookups.markant_address1_countryid] : null
67
+ * );
68
+ * ```
69
+ */
70
+ declare function parseLookups(response: Record<string, unknown>, navigationProperties: string[]): Record<string, {
71
+ id: string;
72
+ name: string;
73
+ entityType: string;
74
+ } | null>;
75
+ /**
76
+ * Get the formatted (display) value of any field from a Web API response.
77
+ *
78
+ * Works for OptionSets, Lookups, DateTimes, Money, and other formatted fields.
79
+ *
80
+ * @param response - The raw Web API response object
81
+ * @param fieldName - The field logical name (e.g. "statecode", "createdon")
82
+ * @returns The formatted string value, or null if not available
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const status = parseFormattedValue(result, 'statecode');
87
+ * // "Active" (instead of 0)
88
+ * ```
89
+ */
90
+ declare function parseFormattedValue(response: Record<string, unknown>, fieldName: string): string | null;
91
+ /**
92
+ * Build an OData $select and $expand query string.
93
+ *
94
+ * @param fields - Field names to select
95
+ * @param expand - Navigation property to expand (optional)
96
+ * @returns OData query string
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * Xrm.WebApi.retrieveRecord("account", id, selectExpand(
101
+ * [AccountFields.Name, AccountFields.WebsiteUrl],
102
+ * "primarycontactid($select=fullname,emailaddress1)"
103
+ * ));
104
+ * ```
105
+ */
106
+ declare function selectExpand(fields: string[], expand: string): string;
107
+
108
+ /**
109
+ * @xrmforge/helpers - Xrm API Constants
110
+ *
111
+ * Const enums for all common Xrm string/number constants.
112
+ * Eliminates raw strings in D365 form scripts.
113
+ *
114
+ * @types/xrm defines these as string literal types for compile-time checking,
115
+ * but does NOT provide runtime constants (XrmEnum is not available at runtime).
116
+ * These const enums are erased at compile time (zero runtime overhead).
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * import { DisplayState } from '@xrmforge/helpers';
121
+ *
122
+ * if (tab.getDisplayState() === DisplayState.Expanded) { ... }
123
+ * ```
124
+ */
125
+ /** Tab/Section display state */
126
+ declare const enum DisplayState {
127
+ Expanded = "expanded",
128
+ Collapsed = "collapsed"
129
+ }
130
+ /** Form notification level (formContext.ui.setFormNotification) */
131
+ declare const enum FormNotificationLevel {
132
+ Error = "ERROR",
133
+ Warning = "WARNING",
134
+ Info = "INFO"
135
+ }
136
+ /** Attribute required level (attribute.setRequiredLevel) */
137
+ declare const enum RequiredLevel {
138
+ None = "none",
139
+ Required = "required",
140
+ Recommended = "recommended"
141
+ }
142
+ /** Attribute submit mode (attribute.setSubmitMode) */
143
+ declare const enum SubmitMode {
144
+ Always = "always",
145
+ Never = "never",
146
+ Dirty = "dirty"
147
+ }
148
+ /** Save mode (eventArgs.getSaveMode()) */
149
+ declare const enum SaveMode {
150
+ Save = 1,
151
+ SaveAndClose = 2,
152
+ Deactivate = 5,
153
+ Reactivate = 6,
154
+ Send = 7,
155
+ Disqualify = 15,
156
+ Qualify = 16,
157
+ Assign = 47,
158
+ SaveAsCompleted = 58,
159
+ SaveAndNew = 59,
160
+ AutoSave = 70
161
+ }
162
+ /** Client type (Xrm.Utility.getGlobalContext().client.getClient()) */
163
+ declare const enum ClientType {
164
+ Web = "Web",
165
+ Outlook = "Outlook",
166
+ Mobile = "Mobile"
167
+ }
168
+ /** Client state (Xrm.Utility.getGlobalContext().client.getClientState()) */
169
+ declare const enum ClientState {
170
+ Online = "Online",
171
+ Offline = "Offline"
172
+ }
173
+ /** Operation type for Xrm.WebApi.execute getMetadata().operationType */
174
+ declare const enum OperationType {
175
+ /** Custom Action or OOB Action (POST) */
176
+ Action = 0,
177
+ /** Custom Function or OOB Function (GET) */
178
+ Function = 1,
179
+ /** CRUD operation (Create, Retrieve, Update, Delete) */
180
+ CRUD = 2
181
+ }
182
+ /** Structural property for getMetadata().parameterTypes[].structuralProperty */
183
+ declare const enum StructuralProperty {
184
+ Unknown = 0,
185
+ PrimitiveType = 1,
186
+ ComplexType = 2,
187
+ EnumerationType = 3,
188
+ Collection = 4,
189
+ EntityType = 5
190
+ }
191
+ /** Binding type for Custom API definitions */
192
+ declare const enum BindingType {
193
+ /** Not bound to an entity (globally callable) */
194
+ Global = 0,
195
+ /** Bound to a single entity record */
196
+ Entity = 1,
197
+ /** Bound to an entity collection */
198
+ EntityCollection = 2
199
+ }
200
+
201
+ /**
202
+ * @xrmforge/helpers - Action/Function Runtime Helpers
203
+ *
204
+ * Factory functions for type-safe Custom API execution.
205
+ * These are imported by generated action/function modules.
206
+ *
207
+ * Design:
208
+ * - `createBoundAction` / `createUnboundAction`: Produce executor objects
209
+ * with `.execute()` (calls Xrm.WebApi) and `.request()` (for executeMultiple)
210
+ * - `executeRequest`: Central execute wrapper (single place for the `as any` cast)
211
+ * - `withProgress`: Convenience wrapper with progress indicator + error dialog
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * // Generated code (in generated/actions/quote.ts):
216
+ * import { createBoundAction } from '@xrmforge/helpers';
217
+ * export const WinQuote = createBoundAction('markant_winquote', 'quote');
218
+ *
219
+ * // Developer code (in quote-form.ts):
220
+ * import { WinQuote } from '../generated/actions/quote';
221
+ * const response = await WinQuote.execute(recordId);
222
+ * ```
223
+ */
224
+ /** Parameter metadata for getMetadata().parameterTypes */
225
+ interface ParameterMeta {
226
+ typeName: string;
227
+ structuralProperty: number;
228
+ }
229
+ /** Map of parameter names to their OData metadata */
230
+ type ParameterMetaMap = Record<string, ParameterMeta>;
231
+ /** Executor for a bound action without additional parameters */
232
+ interface BoundActionExecutor {
233
+ execute(recordId: string): Promise<Response>;
234
+ request(recordId: string): Record<string, unknown>;
235
+ }
236
+ /** Executor for a bound action with typed parameters */
237
+ interface BoundActionWithParamsExecutor<TParams> {
238
+ execute(recordId: string, params: TParams): Promise<Response>;
239
+ request(recordId: string, params: TParams): Record<string, unknown>;
240
+ }
241
+ /** Executor for an unbound action without parameters */
242
+ interface UnboundActionExecutor {
243
+ execute(): Promise<Response>;
244
+ request(): Record<string, unknown>;
245
+ }
246
+ /** Executor for an unbound action with typed parameters and optional typed response */
247
+ interface UnboundActionWithParamsExecutor<TParams, TResult = void> {
248
+ execute(params: TParams): Promise<TResult extends void ? Response : TResult>;
249
+ request(params: TParams): Record<string, unknown>;
250
+ }
251
+ /** Executor for an unbound function with typed response */
252
+ interface UnboundFunctionExecutor<TResult> {
253
+ execute(): Promise<TResult>;
254
+ request(): Record<string, unknown>;
255
+ }
256
+ /** Executor for a bound function with typed response */
257
+ interface BoundFunctionExecutor<TResult> {
258
+ execute(recordId: string): Promise<TResult>;
259
+ request(recordId: string): Record<string, unknown>;
260
+ }
261
+ /**
262
+ * Execute a single request via Xrm.WebApi.online.execute().
263
+ *
264
+ * This is the ONLY place in the entire framework where the `as any` cast happens.
265
+ * All generated executors call this function internally.
266
+ */
267
+ declare function executeRequest(request: Record<string, unknown>): Promise<Response>;
268
+ /**
269
+ * Execute multiple requests via Xrm.WebApi.online.executeMultiple().
270
+ *
271
+ * @param requests - Array of request objects (from `.request()` factories).
272
+ * Wrap a subset in an inner array for transactional changeset execution.
273
+ */
274
+ declare function executeMultiple(requests: Array<Record<string, unknown> | Array<Record<string, unknown>>>): Promise<Response[]>;
275
+ /**
276
+ * Create an executor for a bound action (entity-bound).
277
+ *
278
+ * @param operationName - Custom API unique name (e.g. "markant_winquote")
279
+ * @param entityLogicalName - Entity logical name (e.g. "quote")
280
+ */
281
+ declare function createBoundAction(operationName: string, entityLogicalName: string): BoundActionExecutor;
282
+ /**
283
+ * Create an executor for a bound action with typed parameters.
284
+ *
285
+ * @param operationName - Custom API unique name
286
+ * @param entityLogicalName - Entity logical name
287
+ * @param paramMeta - Parameter metadata map (parameter name to OData type info)
288
+ */
289
+ declare function createBoundAction<TParams extends Record<string, unknown>>(operationName: string, entityLogicalName: string, paramMeta: ParameterMetaMap): BoundActionWithParamsExecutor<TParams>;
290
+ /**
291
+ * Create an executor for an unbound (global) action without parameters.
292
+ *
293
+ * @param operationName - Custom API unique name
294
+ */
295
+ declare function createUnboundAction(operationName: string): UnboundActionExecutor;
296
+ /**
297
+ * Create an executor for an unbound action with typed parameters and response.
298
+ *
299
+ * @param operationName - Custom API unique name
300
+ * @param paramMeta - Parameter metadata map
301
+ */
302
+ declare function createUnboundAction<TParams extends Record<string, unknown>, TResult = void>(operationName: string, paramMeta: ParameterMetaMap): UnboundActionWithParamsExecutor<TParams, TResult>;
303
+ /**
304
+ * Create an executor for an unbound (global) function with typed response.
305
+ *
306
+ * @param operationName - Function name (e.g. "WhoAmI")
307
+ */
308
+ declare function createUnboundFunction<TResult>(operationName: string): UnboundFunctionExecutor<TResult>;
309
+ /**
310
+ * Create an executor for a bound function with typed response.
311
+ *
312
+ * @param operationName - Function name
313
+ * @param entityLogicalName - Entity logical name
314
+ */
315
+ declare function createBoundFunction<TResult>(operationName: string, entityLogicalName: string): BoundFunctionExecutor<TResult>;
316
+ /**
317
+ * Execute an async operation with Xrm progress indicator.
318
+ *
319
+ * Shows a progress spinner before the operation, closes it after,
320
+ * and shows an error dialog on failure.
321
+ *
322
+ * @param message - Progress indicator message (e.g. "Processing quote...")
323
+ * @param operation - Async function to execute
324
+ * @returns The result of the operation
325
+ *
326
+ * @example
327
+ * ```typescript
328
+ * await withProgress('Processing quote...', () => WinQuote.execute(recordId));
329
+ * ```
330
+ */
331
+ declare function withProgress<T>(message: string, operation: () => Promise<T>): Promise<T>;
332
+
333
+ /**
334
+ * @xrmforge/helpers - TypedForm Proxy
335
+ *
336
+ * Creates a proxy around Xrm.FormContext that allows direct property access
337
+ * to form fields. Instead of formContext.getAttribute("name").setValue("X"),
338
+ * write form.name.setValue("X").
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * import { typedForm } from '@xrmforge/helpers';
343
+ *
344
+ * type LeadForm = XrmForge.Forms.Lead.LeadMarkantLeadForm;
345
+ *
346
+ * const form = typedForm<LeadForm>(formContext);
347
+ * form.companyname.setValue("Contoso"); // StringAttribute
348
+ * form.parentaccountid.getValue(); // LookupValue[] | null
349
+ * form.$context.ui.setFormNotification(...); // Full FormContext access
350
+ * form.$control("companyname").setDisabled(true);
351
+ * ```
352
+ */
353
+ /**
354
+ * Extracts the Fields union type from a generated Form interface.
355
+ * Works with the XrmForge-generated pattern:
356
+ * getAttribute<K extends Fields>(name: K): AttrMap[K]
357
+ *
358
+ * For overloaded getAttribute, TypeScript resolves to the last overload.
359
+ * Our generated interfaces have the generic overload first, so we use
360
+ * a mapped approach with the AttrMap instead.
361
+ */
362
+ type FormFields<TForm> = TForm extends {
363
+ getAttribute(name: infer K): unknown;
364
+ } ? K extends string ? K : string : string;
365
+ /**
366
+ * TypedForm: proxy type that maps field names to their attribute types.
367
+ *
368
+ * Since extracting Fields + AttrMap from overloaded getAttribute is unreliable
369
+ * in TypeScript, we use a two-parameter approach internally but expose a
370
+ * single-parameter API via a helper type generated by typegen (FormTypedInfo).
371
+ *
372
+ * For v0.1.0 we support both signatures:
373
+ * typedForm<TForm>(fc) -- works when TForm has proper generic getAttribute
374
+ * typedForm<TForm, TFields, TAttrMap>(fc) -- explicit, always works
375
+ */
376
+ type TypedForm<_TForm, TFields extends string = string, TAttrMap extends Record<string, Xrm.Attributes.Attribute> = Record<string, Xrm.Attributes.Attribute>> = {
377
+ readonly [K in TFields]: K extends keyof TAttrMap ? TAttrMap[K] : Xrm.Attributes.Attribute;
378
+ } & {
379
+ /** Access the underlying FormContext for ui, data, tabs, etc. */
380
+ readonly $context: Xrm.FormContext;
381
+ /** Access a control by field name */
382
+ $control(name: TFields): Xrm.Controls.Control;
383
+ };
384
+ /**
385
+ * Create a typed form proxy around a FormContext.
386
+ *
387
+ * The proxy intercepts property access and delegates to getAttribute().
388
+ * Special properties $context and $control provide access to the full
389
+ * FormContext and controls respectively.
390
+ *
391
+ * @param formContext - The Xrm.FormContext from executionContext.getFormContext()
392
+ * @returns A proxy with direct property access to form fields
393
+ */
394
+ declare function typedForm<TForm, TFields extends string = string, TAttrMap extends Record<string, Xrm.Attributes.Attribute> = Record<string, Xrm.Attributes.Attribute>>(formContext: Xrm.FormContext): TypedForm<TForm, TFields, TAttrMap>;
395
+
396
+ export { BindingType, type BoundActionExecutor, type BoundActionWithParamsExecutor, type BoundFunctionExecutor, ClientState, ClientType, DisplayState, type FormFields, FormNotificationLevel, OperationType, type ParameterMeta, type ParameterMetaMap, RequiredLevel, SaveMode, StructuralProperty, SubmitMode, type TypedForm, type UnboundActionExecutor, type UnboundActionWithParamsExecutor, type UnboundFunctionExecutor, createBoundAction, createBoundFunction, createUnboundAction, createUnboundFunction, executeMultiple, executeRequest, parseFormattedValue, parseLookup, parseLookups, select, selectExpand, typedForm, withProgress };
package/dist/index.js ADDED
@@ -0,0 +1,329 @@
1
+ // src/webapi-helpers.ts
2
+ function select(...fields) {
3
+ if (fields.length === 0) return "";
4
+ return `?$select=${fields.join(",")}`;
5
+ }
6
+ function parseLookup(response, navigationProperty) {
7
+ const key = `_${navigationProperty}_value`;
8
+ const id = response[key];
9
+ if (!id) return null;
10
+ return {
11
+ id,
12
+ name: response[`${key}@OData.Community.Display.V1.FormattedValue`] ?? "",
13
+ entityType: response[`${key}@Microsoft.Dynamics.CRM.lookuplogicalname`] ?? ""
14
+ };
15
+ }
16
+ function parseLookups(response, navigationProperties) {
17
+ const result = {};
18
+ for (const prop of navigationProperties) {
19
+ result[prop] = parseLookup(response, prop);
20
+ }
21
+ return result;
22
+ }
23
+ function parseFormattedValue(response, fieldName) {
24
+ return response[`${fieldName}@OData.Community.Display.V1.FormattedValue`] ?? null;
25
+ }
26
+ function selectExpand(fields, expand) {
27
+ const parts = [];
28
+ if (fields.length > 0) parts.push(`$select=${fields.join(",")}`);
29
+ if (expand) parts.push(`$expand=${expand}`);
30
+ return parts.length > 0 ? `?${parts.join("&")}` : "";
31
+ }
32
+
33
+ // src/xrm-constants.ts
34
+ var DisplayState = /* @__PURE__ */ ((DisplayState2) => {
35
+ DisplayState2["Expanded"] = "expanded";
36
+ DisplayState2["Collapsed"] = "collapsed";
37
+ return DisplayState2;
38
+ })(DisplayState || {});
39
+ var FormNotificationLevel = /* @__PURE__ */ ((FormNotificationLevel2) => {
40
+ FormNotificationLevel2["Error"] = "ERROR";
41
+ FormNotificationLevel2["Warning"] = "WARNING";
42
+ FormNotificationLevel2["Info"] = "INFO";
43
+ return FormNotificationLevel2;
44
+ })(FormNotificationLevel || {});
45
+ var RequiredLevel = /* @__PURE__ */ ((RequiredLevel2) => {
46
+ RequiredLevel2["None"] = "none";
47
+ RequiredLevel2["Required"] = "required";
48
+ RequiredLevel2["Recommended"] = "recommended";
49
+ return RequiredLevel2;
50
+ })(RequiredLevel || {});
51
+ var SubmitMode = /* @__PURE__ */ ((SubmitMode2) => {
52
+ SubmitMode2["Always"] = "always";
53
+ SubmitMode2["Never"] = "never";
54
+ SubmitMode2["Dirty"] = "dirty";
55
+ return SubmitMode2;
56
+ })(SubmitMode || {});
57
+ var SaveMode = /* @__PURE__ */ ((SaveMode2) => {
58
+ SaveMode2[SaveMode2["Save"] = 1] = "Save";
59
+ SaveMode2[SaveMode2["SaveAndClose"] = 2] = "SaveAndClose";
60
+ SaveMode2[SaveMode2["Deactivate"] = 5] = "Deactivate";
61
+ SaveMode2[SaveMode2["Reactivate"] = 6] = "Reactivate";
62
+ SaveMode2[SaveMode2["Send"] = 7] = "Send";
63
+ SaveMode2[SaveMode2["Disqualify"] = 15] = "Disqualify";
64
+ SaveMode2[SaveMode2["Qualify"] = 16] = "Qualify";
65
+ SaveMode2[SaveMode2["Assign"] = 47] = "Assign";
66
+ SaveMode2[SaveMode2["SaveAsCompleted"] = 58] = "SaveAsCompleted";
67
+ SaveMode2[SaveMode2["SaveAndNew"] = 59] = "SaveAndNew";
68
+ SaveMode2[SaveMode2["AutoSave"] = 70] = "AutoSave";
69
+ return SaveMode2;
70
+ })(SaveMode || {});
71
+ var ClientType = /* @__PURE__ */ ((ClientType2) => {
72
+ ClientType2["Web"] = "Web";
73
+ ClientType2["Outlook"] = "Outlook";
74
+ ClientType2["Mobile"] = "Mobile";
75
+ return ClientType2;
76
+ })(ClientType || {});
77
+ var ClientState = /* @__PURE__ */ ((ClientState2) => {
78
+ ClientState2["Online"] = "Online";
79
+ ClientState2["Offline"] = "Offline";
80
+ return ClientState2;
81
+ })(ClientState || {});
82
+ var OperationType = /* @__PURE__ */ ((OperationType2) => {
83
+ OperationType2[OperationType2["Action"] = 0] = "Action";
84
+ OperationType2[OperationType2["Function"] = 1] = "Function";
85
+ OperationType2[OperationType2["CRUD"] = 2] = "CRUD";
86
+ return OperationType2;
87
+ })(OperationType || {});
88
+ var StructuralProperty = /* @__PURE__ */ ((StructuralProperty2) => {
89
+ StructuralProperty2[StructuralProperty2["Unknown"] = 0] = "Unknown";
90
+ StructuralProperty2[StructuralProperty2["PrimitiveType"] = 1] = "PrimitiveType";
91
+ StructuralProperty2[StructuralProperty2["ComplexType"] = 2] = "ComplexType";
92
+ StructuralProperty2[StructuralProperty2["EnumerationType"] = 3] = "EnumerationType";
93
+ StructuralProperty2[StructuralProperty2["Collection"] = 4] = "Collection";
94
+ StructuralProperty2[StructuralProperty2["EntityType"] = 5] = "EntityType";
95
+ return StructuralProperty2;
96
+ })(StructuralProperty || {});
97
+ var BindingType = /* @__PURE__ */ ((BindingType2) => {
98
+ BindingType2[BindingType2["Global"] = 0] = "Global";
99
+ BindingType2[BindingType2["Entity"] = 1] = "Entity";
100
+ BindingType2[BindingType2["EntityCollection"] = 2] = "EntityCollection";
101
+ return BindingType2;
102
+ })(BindingType || {});
103
+
104
+ // src/action-runtime.ts
105
+ function executeRequest(request) {
106
+ return Xrm.WebApi.online.execute(request);
107
+ }
108
+ function executeMultiple(requests) {
109
+ return Xrm.WebApi.online.executeMultiple(requests);
110
+ }
111
+ function cleanRecordId(id) {
112
+ return id.replace(/[{}]/g, "");
113
+ }
114
+ function buildBoundRequest(operationName, entityLogicalName, operationType, recordId, paramMeta, params) {
115
+ const parameterTypes = {
116
+ entity: {
117
+ typeName: `mscrm.${entityLogicalName}`,
118
+ structuralProperty: 5 /* EntityType */
119
+ }
120
+ };
121
+ if (paramMeta) {
122
+ for (const [key, meta] of Object.entries(paramMeta)) {
123
+ parameterTypes[key] = meta;
124
+ }
125
+ }
126
+ const request = {
127
+ getMetadata: () => ({
128
+ boundParameter: "entity",
129
+ parameterTypes,
130
+ operationName,
131
+ operationType
132
+ }),
133
+ entity: {
134
+ id: cleanRecordId(recordId),
135
+ entityType: entityLogicalName
136
+ }
137
+ };
138
+ if (params) {
139
+ for (const [key, value] of Object.entries(params)) {
140
+ request[key] = value;
141
+ }
142
+ }
143
+ return request;
144
+ }
145
+ function buildUnboundRequest(operationName, operationType, paramMeta, params) {
146
+ const parameterTypes = {};
147
+ if (paramMeta) {
148
+ for (const [key, meta] of Object.entries(paramMeta)) {
149
+ parameterTypes[key] = meta;
150
+ }
151
+ }
152
+ const request = {
153
+ getMetadata: () => ({
154
+ boundParameter: null,
155
+ parameterTypes,
156
+ operationName,
157
+ operationType
158
+ })
159
+ };
160
+ if (params) {
161
+ for (const [key, value] of Object.entries(params)) {
162
+ request[key] = value;
163
+ }
164
+ }
165
+ return request;
166
+ }
167
+ function createBoundAction(operationName, entityLogicalName, paramMeta) {
168
+ return {
169
+ execute(recordId, params) {
170
+ const req = buildBoundRequest(
171
+ operationName,
172
+ entityLogicalName,
173
+ 0 /* Action */,
174
+ recordId,
175
+ paramMeta,
176
+ params
177
+ );
178
+ return executeRequest(req);
179
+ },
180
+ request(recordId, params) {
181
+ return buildBoundRequest(
182
+ operationName,
183
+ entityLogicalName,
184
+ 0 /* Action */,
185
+ recordId,
186
+ paramMeta,
187
+ params
188
+ );
189
+ }
190
+ };
191
+ }
192
+ function createUnboundAction(operationName, paramMeta) {
193
+ return {
194
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- return type varies (Response or parsed JSON)
195
+ async execute(params) {
196
+ const req = buildUnboundRequest(
197
+ operationName,
198
+ 0 /* Action */,
199
+ paramMeta,
200
+ params
201
+ );
202
+ const response = await executeRequest(req);
203
+ if (!response.ok) {
204
+ const errorText = await response.text();
205
+ throw new Error(errorText);
206
+ }
207
+ if (response.status !== 204) {
208
+ return response.json();
209
+ }
210
+ return response;
211
+ },
212
+ request(params) {
213
+ return buildUnboundRequest(
214
+ operationName,
215
+ 0 /* Action */,
216
+ paramMeta,
217
+ params
218
+ );
219
+ }
220
+ };
221
+ }
222
+ function createUnboundFunction(operationName) {
223
+ return {
224
+ async execute() {
225
+ const req = buildUnboundRequest(operationName, 1 /* Function */);
226
+ const response = await executeRequest(req);
227
+ if (!response.ok) {
228
+ const errorText = await response.text();
229
+ throw new Error(errorText);
230
+ }
231
+ return response.json();
232
+ },
233
+ request() {
234
+ return buildUnboundRequest(operationName, 1 /* Function */);
235
+ }
236
+ };
237
+ }
238
+ function createBoundFunction(operationName, entityLogicalName) {
239
+ return {
240
+ async execute(recordId) {
241
+ const req = buildBoundRequest(
242
+ operationName,
243
+ entityLogicalName,
244
+ 1 /* Function */,
245
+ recordId
246
+ );
247
+ const response = await executeRequest(req);
248
+ if (!response.ok) {
249
+ const errorText = await response.text();
250
+ throw new Error(errorText);
251
+ }
252
+ return response.json();
253
+ },
254
+ request(recordId) {
255
+ return buildBoundRequest(
256
+ operationName,
257
+ entityLogicalName,
258
+ 1 /* Function */,
259
+ recordId
260
+ );
261
+ }
262
+ };
263
+ }
264
+ async function withProgress(message, operation) {
265
+ Xrm.Utility.showProgressIndicator(message);
266
+ try {
267
+ return await operation();
268
+ } catch (error) {
269
+ const msg = error instanceof Error ? error.message : String(error);
270
+ Xrm.Navigation.openErrorDialog({ message: msg });
271
+ throw error;
272
+ } finally {
273
+ Xrm.Utility.closeProgressIndicator();
274
+ }
275
+ }
276
+
277
+ // src/typed-form.ts
278
+ function typedForm(formContext) {
279
+ return new Proxy(formContext, {
280
+ get(_target, prop) {
281
+ if (typeof prop !== "string") {
282
+ return formContext[prop];
283
+ }
284
+ if (prop === "$context") return formContext;
285
+ if (prop === "$control") {
286
+ return (name) => formContext.getControl(name);
287
+ }
288
+ const attr = formContext.getAttribute(prop);
289
+ if (attr) return attr;
290
+ return formContext[prop];
291
+ },
292
+ set(_target, prop, _value) {
293
+ throw new TypeError(
294
+ `Cannot assign to '${String(prop)}'. Use form.${String(prop)}.setValue() instead.`
295
+ );
296
+ },
297
+ has(_target, prop) {
298
+ if (typeof prop !== "string") return false;
299
+ if (prop === "$context" || prop === "$control") return true;
300
+ return formContext.getAttribute(prop) !== null;
301
+ }
302
+ });
303
+ }
304
+ export {
305
+ BindingType,
306
+ ClientState,
307
+ ClientType,
308
+ DisplayState,
309
+ FormNotificationLevel,
310
+ OperationType,
311
+ RequiredLevel,
312
+ SaveMode,
313
+ StructuralProperty,
314
+ SubmitMode,
315
+ createBoundAction,
316
+ createBoundFunction,
317
+ createUnboundAction,
318
+ createUnboundFunction,
319
+ executeMultiple,
320
+ executeRequest,
321
+ parseFormattedValue,
322
+ parseLookup,
323
+ parseLookups,
324
+ select,
325
+ selectExpand,
326
+ typedForm,
327
+ withProgress
328
+ };
329
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/webapi-helpers.ts","../src/xrm-constants.ts","../src/action-runtime.ts","../src/typed-form.ts"],"sourcesContent":["/**\n * @xrmforge/helpers - Web API Helper Functions\n *\n * Lightweight utility functions for building OData query strings\n * with type-safe field names from generated Fields enums.\n *\n * Zero runtime overhead when used with const enums (values are inlined).\n *\n * @example\n * ```typescript\n * import { select } from '@xrmforge/helpers';\n *\n * Xrm.WebApi.retrieveRecord(ref.entityType, ref.id, select(\n * AccountFields.Name,\n * AccountFields.WebsiteUrl,\n * AccountFields.Address1Line1,\n * ));\n * ```\n */\n\n/**\n * Build an OData $select query string from field names.\n *\n * @param fields - Field names (use generated Fields enum for type safety)\n * @returns OData query string (e.g. \"?$select=name,websiteurl,address1_line1\")\n */\nexport function select(...fields: string[]): string {\n if (fields.length === 0) return '';\n return `?$select=${fields.join(',')}`;\n}\n\n/**\n * Parse a lookup field from a Dataverse Web API response into a LookupValue.\n *\n * Dataverse returns lookups as `_fieldname_value` with OData annotations:\n * - `_fieldname_value` (GUID)\n * - `_fieldname_value@OData.Community.Display.V1.FormattedValue` (display name)\n * - `_fieldname_value@Microsoft.Dynamics.CRM.lookuplogicalname` (entity type)\n *\n * This function extracts all three into an `Xrm.LookupValue` object.\n *\n * @param response - The raw Web API response object\n * @param navigationProperty - Navigation property name (use NavigationProperties enum for type safety)\n * @returns Xrm.LookupValue or null if the lookup is empty\n *\n * @example\n * ```typescript\n * // With NavigationProperties enum (recommended):\n * parseLookup(result, AccountNav.Country);\n *\n * // Or with navigation property name directly:\n * parseLookup(result, 'markant_address1_countryid');\n * ```\n */\nexport function parseLookup(\n response: Record<string, unknown>,\n navigationProperty: string,\n): { id: string; name: string; entityType: string } | null {\n const key = `_${navigationProperty}_value`;\n const id = response[key] as string | undefined;\n if (!id) return null;\n\n return {\n id,\n name: (response[`${key}@OData.Community.Display.V1.FormattedValue`] as string) ?? '',\n entityType: (response[`${key}@Microsoft.Dynamics.CRM.lookuplogicalname`] as string) ?? '',\n };\n}\n\n/**\n * Parse multiple lookup fields from a Dataverse Web API response at once.\n *\n * @param response - The raw Web API response object\n * @param navigationProperties - Navigation property names to parse\n * @returns Map of navigation property name to LookupValue (null entries omitted)\n *\n * @example\n * ```typescript\n * const lookups = parseLookups(result, ['markant_address1_countryid', 'parentaccountid']);\n * formContext.getAttribute(Fields.Country).setValue(\n * lookups.markant_address1_countryid ? [lookups.markant_address1_countryid] : null\n * );\n * ```\n */\nexport function parseLookups(\n response: Record<string, unknown>,\n navigationProperties: string[],\n): Record<string, { id: string; name: string; entityType: string } | null> {\n const result: Record<string, { id: string; name: string; entityType: string } | null> = {};\n for (const prop of navigationProperties) {\n result[prop] = parseLookup(response, prop);\n }\n return result;\n}\n\n/**\n * Get the formatted (display) value of any field from a Web API response.\n *\n * Works for OptionSets, Lookups, DateTimes, Money, and other formatted fields.\n *\n * @param response - The raw Web API response object\n * @param fieldName - The field logical name (e.g. \"statecode\", \"createdon\")\n * @returns The formatted string value, or null if not available\n *\n * @example\n * ```typescript\n * const status = parseFormattedValue(result, 'statecode');\n * // \"Active\" (instead of 0)\n * ```\n */\nexport function parseFormattedValue(\n response: Record<string, unknown>,\n fieldName: string,\n): string | null {\n return (response[`${fieldName}@OData.Community.Display.V1.FormattedValue`] as string) ?? null;\n}\n\n/**\n * Build an OData $select and $expand query string.\n *\n * @param fields - Field names to select\n * @param expand - Navigation property to expand (optional)\n * @returns OData query string\n *\n * @example\n * ```typescript\n * Xrm.WebApi.retrieveRecord(\"account\", id, selectExpand(\n * [AccountFields.Name, AccountFields.WebsiteUrl],\n * \"primarycontactid($select=fullname,emailaddress1)\"\n * ));\n * ```\n */\nexport function selectExpand(fields: string[], expand: string): string {\n const parts: string[] = [];\n if (fields.length > 0) parts.push(`$select=${fields.join(',')}`);\n if (expand) parts.push(`$expand=${expand}`);\n return parts.length > 0 ? `?${parts.join('&')}` : '';\n}\n","/**\n * @xrmforge/helpers - Xrm API Constants\n *\n * Const enums for all common Xrm string/number constants.\n * Eliminates raw strings in D365 form scripts.\n *\n * @types/xrm defines these as string literal types for compile-time checking,\n * but does NOT provide runtime constants (XrmEnum is not available at runtime).\n * These const enums are erased at compile time (zero runtime overhead).\n *\n * @example\n * ```typescript\n * import { DisplayState } from '@xrmforge/helpers';\n *\n * if (tab.getDisplayState() === DisplayState.Expanded) { ... }\n * ```\n */\n\n/** Tab/Section display state */\nexport const enum DisplayState {\n Expanded = 'expanded',\n Collapsed = 'collapsed',\n}\n\n// FormType: use XrmEnum.FormType from @types/xrm (already defined as const enum there)\n\n/** Form notification level (formContext.ui.setFormNotification) */\nexport const enum FormNotificationLevel {\n Error = 'ERROR',\n Warning = 'WARNING',\n Info = 'INFO',\n}\n\n/** Attribute required level (attribute.setRequiredLevel) */\nexport const enum RequiredLevel {\n None = 'none',\n Required = 'required',\n Recommended = 'recommended',\n}\n\n/** Attribute submit mode (attribute.setSubmitMode) */\nexport const enum SubmitMode {\n Always = 'always',\n Never = 'never',\n Dirty = 'dirty',\n}\n\n/** Save mode (eventArgs.getSaveMode()) */\nexport const enum SaveMode {\n Save = 1,\n SaveAndClose = 2,\n Deactivate = 5,\n Reactivate = 6,\n Send = 7,\n Disqualify = 15,\n Qualify = 16,\n Assign = 47,\n SaveAsCompleted = 58,\n SaveAndNew = 59,\n AutoSave = 70,\n}\n\n/** Client type (Xrm.Utility.getGlobalContext().client.getClient()) */\nexport const enum ClientType {\n Web = 'Web',\n Outlook = 'Outlook',\n Mobile = 'Mobile',\n}\n\n/** Client state (Xrm.Utility.getGlobalContext().client.getClientState()) */\nexport const enum ClientState {\n Online = 'Online',\n Offline = 'Offline',\n}\n\n// WebApi Execute Constants\n\n/** Operation type for Xrm.WebApi.execute getMetadata().operationType */\nexport const enum OperationType {\n /** Custom Action or OOB Action (POST) */\n Action = 0,\n /** Custom Function or OOB Function (GET) */\n Function = 1,\n /** CRUD operation (Create, Retrieve, Update, Delete) */\n CRUD = 2,\n}\n\n/** Structural property for getMetadata().parameterTypes[].structuralProperty */\nexport const enum StructuralProperty {\n Unknown = 0,\n PrimitiveType = 1,\n ComplexType = 2,\n EnumerationType = 3,\n Collection = 4,\n EntityType = 5,\n}\n\n/** Binding type for Custom API definitions */\nexport const enum BindingType {\n /** Not bound to an entity (globally callable) */\n Global = 0,\n /** Bound to a single entity record */\n Entity = 1,\n /** Bound to an entity collection */\n EntityCollection = 2,\n}\n","/**\n * @xrmforge/helpers - Action/Function Runtime Helpers\n *\n * Factory functions for type-safe Custom API execution.\n * These are imported by generated action/function modules.\n *\n * Design:\n * - `createBoundAction` / `createUnboundAction`: Produce executor objects\n * with `.execute()` (calls Xrm.WebApi) and `.request()` (for executeMultiple)\n * - `executeRequest`: Central execute wrapper (single place for the `as any` cast)\n * - `withProgress`: Convenience wrapper with progress indicator + error dialog\n *\n * @example\n * ```typescript\n * // Generated code (in generated/actions/quote.ts):\n * import { createBoundAction } from '@xrmforge/helpers';\n * export const WinQuote = createBoundAction('markant_winquote', 'quote');\n *\n * // Developer code (in quote-form.ts):\n * import { WinQuote } from '../generated/actions/quote';\n * const response = await WinQuote.execute(recordId);\n * ```\n */\n\nimport { OperationType, StructuralProperty } from './xrm-constants.js';\n\n// Types\n\n/** Parameter metadata for getMetadata().parameterTypes */\nexport interface ParameterMeta {\n typeName: string;\n structuralProperty: number;\n}\n\n/** Map of parameter names to their OData metadata */\nexport type ParameterMetaMap = Record<string, ParameterMeta>;\n\n/** Executor for a bound action without additional parameters */\nexport interface BoundActionExecutor {\n execute(recordId: string): Promise<Response>;\n request(recordId: string): Record<string, unknown>;\n}\n\n/** Executor for a bound action with typed parameters */\nexport interface BoundActionWithParamsExecutor<TParams> {\n execute(recordId: string, params: TParams): Promise<Response>;\n request(recordId: string, params: TParams): Record<string, unknown>;\n}\n\n/** Executor for an unbound action without parameters */\nexport interface UnboundActionExecutor {\n execute(): Promise<Response>;\n request(): Record<string, unknown>;\n}\n\n/** Executor for an unbound action with typed parameters and optional typed response */\nexport interface UnboundActionWithParamsExecutor<TParams, TResult = void> {\n execute(params: TParams): Promise<TResult extends void ? Response : TResult>;\n request(params: TParams): Record<string, unknown>;\n}\n\n/** Executor for an unbound function with typed response */\nexport interface UnboundFunctionExecutor<TResult> {\n execute(): Promise<TResult>;\n request(): Record<string, unknown>;\n}\n\n/** Executor for a bound function with typed response */\nexport interface BoundFunctionExecutor<TResult> {\n execute(recordId: string): Promise<TResult>;\n request(recordId: string): Record<string, unknown>;\n}\n\n// Central Execute\n\n/**\n * Execute a single request via Xrm.WebApi.online.execute().\n *\n * This is the ONLY place in the entire framework where the `as any` cast happens.\n * All generated executors call this function internally.\n */\nexport function executeRequest(request: Record<string, unknown>): Promise<Response> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (Xrm.WebApi as any).online.execute(request) as Promise<Response>;\n}\n\n/**\n * Execute multiple requests via Xrm.WebApi.online.executeMultiple().\n *\n * @param requests - Array of request objects (from `.request()` factories).\n * Wrap a subset in an inner array for transactional changeset execution.\n */\nexport function executeMultiple(\n requests: Array<Record<string, unknown> | Array<Record<string, unknown>>>,\n): Promise<Response[]> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (Xrm.WebApi as any).online.executeMultiple(requests) as Promise<Response[]>;\n}\n\n// Request Builder (internal)\n\nfunction cleanRecordId(id: string): string {\n return id.replace(/[{}]/g, '');\n}\n\nfunction buildBoundRequest(\n operationName: string,\n entityLogicalName: string,\n operationType: OperationType,\n recordId: string,\n paramMeta?: ParameterMetaMap,\n params?: Record<string, unknown>,\n): Record<string, unknown> {\n const parameterTypes: Record<string, ParameterMeta> = {\n entity: {\n typeName: `mscrm.${entityLogicalName}`,\n structuralProperty: StructuralProperty.EntityType,\n },\n };\n\n if (paramMeta) {\n for (const [key, meta] of Object.entries(paramMeta)) {\n parameterTypes[key] = meta;\n }\n }\n\n const request: Record<string, unknown> = {\n getMetadata: () => ({\n boundParameter: 'entity',\n parameterTypes,\n operationName,\n operationType,\n }),\n entity: {\n id: cleanRecordId(recordId),\n entityType: entityLogicalName,\n },\n };\n\n if (params) {\n for (const [key, value] of Object.entries(params)) {\n request[key] = value;\n }\n }\n\n return request;\n}\n\nfunction buildUnboundRequest(\n operationName: string,\n operationType: OperationType,\n paramMeta?: ParameterMetaMap,\n params?: Record<string, unknown>,\n): Record<string, unknown> {\n const parameterTypes: Record<string, ParameterMeta> = {};\n\n if (paramMeta) {\n for (const [key, meta] of Object.entries(paramMeta)) {\n parameterTypes[key] = meta;\n }\n }\n\n const request: Record<string, unknown> = {\n getMetadata: () => ({\n boundParameter: null,\n parameterTypes,\n operationName,\n operationType,\n }),\n };\n\n if (params) {\n for (const [key, value] of Object.entries(params)) {\n request[key] = value;\n }\n }\n\n return request;\n}\n\n// Action Factories\n\n/**\n * Create an executor for a bound action (entity-bound).\n *\n * @param operationName - Custom API unique name (e.g. \"markant_winquote\")\n * @param entityLogicalName - Entity logical name (e.g. \"quote\")\n */\nexport function createBoundAction(\n operationName: string,\n entityLogicalName: string,\n): BoundActionExecutor;\n\n/**\n * Create an executor for a bound action with typed parameters.\n *\n * @param operationName - Custom API unique name\n * @param entityLogicalName - Entity logical name\n * @param paramMeta - Parameter metadata map (parameter name to OData type info)\n */\nexport function createBoundAction<TParams extends Record<string, unknown>>(\n operationName: string,\n entityLogicalName: string,\n paramMeta: ParameterMetaMap,\n): BoundActionWithParamsExecutor<TParams>;\n\nexport function createBoundAction<TParams extends Record<string, unknown>>(\n operationName: string,\n entityLogicalName: string,\n paramMeta?: ParameterMetaMap,\n): BoundActionExecutor | BoundActionWithParamsExecutor<TParams> {\n return {\n execute(recordId: string, params?: TParams): Promise<Response> {\n const req = buildBoundRequest(\n operationName, entityLogicalName, OperationType.Action,\n recordId, paramMeta, params,\n );\n return executeRequest(req);\n },\n request(recordId: string, params?: TParams): Record<string, unknown> {\n return buildBoundRequest(\n operationName, entityLogicalName, OperationType.Action,\n recordId, paramMeta, params,\n );\n },\n };\n}\n\n/**\n * Create an executor for an unbound (global) action without parameters.\n *\n * @param operationName - Custom API unique name\n */\nexport function createUnboundAction(\n operationName: string,\n): UnboundActionExecutor;\n\n/**\n * Create an executor for an unbound action with typed parameters and response.\n *\n * @param operationName - Custom API unique name\n * @param paramMeta - Parameter metadata map\n */\nexport function createUnboundAction<\n TParams extends Record<string, unknown>,\n TResult = void,\n>(\n operationName: string,\n paramMeta: ParameterMetaMap,\n): UnboundActionWithParamsExecutor<TParams, TResult>;\n\nexport function createUnboundAction<\n TParams extends Record<string, unknown>,\n TResult = void,\n>(\n operationName: string,\n paramMeta?: ParameterMetaMap,\n): UnboundActionExecutor | UnboundActionWithParamsExecutor<TParams, TResult> {\n return {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- return type varies (Response or parsed JSON)\n async execute(params?: TParams): Promise<any> {\n const req = buildUnboundRequest(\n operationName, OperationType.Action, paramMeta, params,\n );\n const response = await executeRequest(req);\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(errorText);\n }\n if (response.status !== 204) {\n return response.json();\n }\n return response;\n },\n request(params?: TParams): Record<string, unknown> {\n return buildUnboundRequest(\n operationName, OperationType.Action, paramMeta, params,\n );\n },\n };\n}\n\n// Function Factories\n\n/**\n * Create an executor for an unbound (global) function with typed response.\n *\n * @param operationName - Function name (e.g. \"WhoAmI\")\n */\nexport function createUnboundFunction<TResult>(\n operationName: string,\n): UnboundFunctionExecutor<TResult> {\n return {\n async execute(): Promise<TResult> {\n const req = buildUnboundRequest(operationName, OperationType.Function);\n const response = await executeRequest(req);\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(errorText);\n }\n return response.json() as Promise<TResult>;\n },\n request(): Record<string, unknown> {\n return buildUnboundRequest(operationName, OperationType.Function);\n },\n };\n}\n\n/**\n * Create an executor for a bound function with typed response.\n *\n * @param operationName - Function name\n * @param entityLogicalName - Entity logical name\n */\nexport function createBoundFunction<TResult>(\n operationName: string,\n entityLogicalName: string,\n): BoundFunctionExecutor<TResult> {\n return {\n async execute(recordId: string): Promise<TResult> {\n const req = buildBoundRequest(\n operationName, entityLogicalName, OperationType.Function, recordId,\n );\n const response = await executeRequest(req);\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(errorText);\n }\n return response.json() as Promise<TResult>;\n },\n request(recordId: string): Record<string, unknown> {\n return buildBoundRequest(\n operationName, entityLogicalName, OperationType.Function, recordId,\n );\n },\n };\n}\n\n// Convenience\n\n/**\n * Execute an async operation with Xrm progress indicator.\n *\n * Shows a progress spinner before the operation, closes it after,\n * and shows an error dialog on failure.\n *\n * @param message - Progress indicator message (e.g. \"Processing quote...\")\n * @param operation - Async function to execute\n * @returns The result of the operation\n *\n * @example\n * ```typescript\n * await withProgress('Processing quote...', () => WinQuote.execute(recordId));\n * ```\n */\nexport async function withProgress<T>(\n message: string,\n operation: () => Promise<T>,\n): Promise<T> {\n Xrm.Utility.showProgressIndicator(message);\n try {\n return await operation();\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : String(error);\n Xrm.Navigation.openErrorDialog({ message: msg });\n throw error;\n } finally {\n Xrm.Utility.closeProgressIndicator();\n }\n}\n","/**\n * @xrmforge/helpers - TypedForm Proxy\n *\n * Creates a proxy around Xrm.FormContext that allows direct property access\n * to form fields. Instead of formContext.getAttribute(\"name\").setValue(\"X\"),\n * write form.name.setValue(\"X\").\n *\n * @example\n * ```typescript\n * import { typedForm } from '@xrmforge/helpers';\n *\n * type LeadForm = XrmForge.Forms.Lead.LeadMarkantLeadForm;\n *\n * const form = typedForm<LeadForm>(formContext);\n * form.companyname.setValue(\"Contoso\"); // StringAttribute\n * form.parentaccountid.getValue(); // LookupValue[] | null\n * form.$context.ui.setFormNotification(...); // Full FormContext access\n * form.$control(\"companyname\").setDisabled(true);\n * ```\n */\n\n/**\n * Extracts the Fields union type from a generated Form interface.\n * Works with the XrmForge-generated pattern:\n * getAttribute<K extends Fields>(name: K): AttrMap[K]\n *\n * For overloaded getAttribute, TypeScript resolves to the last overload.\n * Our generated interfaces have the generic overload first, so we use\n * a mapped approach with the AttrMap instead.\n */\nexport type FormFields<TForm> = TForm extends {\n getAttribute(name: infer K): unknown;\n} ? K extends string ? K : string : string;\n\n/**\n * TypedForm: proxy type that maps field names to their attribute types.\n *\n * Since extracting Fields + AttrMap from overloaded getAttribute is unreliable\n * in TypeScript, we use a two-parameter approach internally but expose a\n * single-parameter API via a helper type generated by typegen (FormTypedInfo).\n *\n * For v0.1.0 we support both signatures:\n * typedForm<TForm>(fc) -- works when TForm has proper generic getAttribute\n * typedForm<TForm, TFields, TAttrMap>(fc) -- explicit, always works\n */\nexport type TypedForm<\n _TForm,\n TFields extends string = string,\n TAttrMap extends Record<string, Xrm.Attributes.Attribute> = Record<string, Xrm.Attributes.Attribute>,\n> = {\n readonly [K in TFields]: K extends keyof TAttrMap\n ? TAttrMap[K]\n : Xrm.Attributes.Attribute;\n} & {\n /** Access the underlying FormContext for ui, data, tabs, etc. */\n readonly $context: Xrm.FormContext;\n /** Access a control by field name */\n $control(name: TFields): Xrm.Controls.Control;\n};\n\n/**\n * Create a typed form proxy around a FormContext.\n *\n * The proxy intercepts property access and delegates to getAttribute().\n * Special properties $context and $control provide access to the full\n * FormContext and controls respectively.\n *\n * @param formContext - The Xrm.FormContext from executionContext.getFormContext()\n * @returns A proxy with direct property access to form fields\n */\nexport function typedForm<\n TForm,\n TFields extends string = string,\n TAttrMap extends Record<string, Xrm.Attributes.Attribute> = Record<string, Xrm.Attributes.Attribute>,\n>(formContext: Xrm.FormContext): TypedForm<TForm, TFields, TAttrMap> {\n return new Proxy(formContext as unknown as TypedForm<TForm, TFields, TAttrMap>, {\n get(_target, prop) {\n // Symbols and non-string keys: delegate to formContext\n if (typeof prop !== 'string') {\n return (formContext as unknown as Record<symbol, unknown>)[prop];\n }\n if (prop === '$context') return formContext;\n if (prop === '$control') {\n return (name: string) => formContext.getControl(name);\n }\n // Try getAttribute first (form field access)\n const attr = formContext.getAttribute(prop);\n if (attr) return attr;\n // Fallback to FormContext properties (data, ui, etc.)\n return (formContext as unknown as Record<string, unknown>)[prop];\n },\n\n set(_target, prop, _value) {\n throw new TypeError(\n `Cannot assign to '${String(prop)}'. Use form.${String(prop)}.setValue() instead.`,\n );\n },\n\n has(_target, prop) {\n if (typeof prop !== 'string') return false;\n if (prop === '$context' || prop === '$control') return true;\n return formContext.getAttribute(prop) !== null;\n },\n });\n}\n"],"mappings":";AA0BO,SAAS,UAAU,QAA0B;AAClD,MAAI,OAAO,WAAW,EAAG,QAAO;AAChC,SAAO,YAAY,OAAO,KAAK,GAAG,CAAC;AACrC;AAyBO,SAAS,YACd,UACA,oBACyD;AACzD,QAAM,MAAM,IAAI,kBAAkB;AAClC,QAAM,KAAK,SAAS,GAAG;AACvB,MAAI,CAAC,GAAI,QAAO;AAEhB,SAAO;AAAA,IACL;AAAA,IACA,MAAO,SAAS,GAAG,GAAG,4CAA4C,KAAgB;AAAA,IAClF,YAAa,SAAS,GAAG,GAAG,2CAA2C,KAAgB;AAAA,EACzF;AACF;AAiBO,SAAS,aACd,UACA,sBACyE;AACzE,QAAM,SAAkF,CAAC;AACzF,aAAW,QAAQ,sBAAsB;AACvC,WAAO,IAAI,IAAI,YAAY,UAAU,IAAI;AAAA,EAC3C;AACA,SAAO;AACT;AAiBO,SAAS,oBACd,UACA,WACe;AACf,SAAQ,SAAS,GAAG,SAAS,4CAA4C,KAAgB;AAC3F;AAiBO,SAAS,aAAa,QAAkB,QAAwB;AACrE,QAAM,QAAkB,CAAC;AACzB,MAAI,OAAO,SAAS,EAAG,OAAM,KAAK,WAAW,OAAO,KAAK,GAA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,WAAO,MAAM,UAAU;AAAA,EACzB,SAAS,OAAgB;AACvB,UAAM,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACjE,QAAI,WAAW,gBAAgB,EAAE,SAAS,IAAI,CAAC;AAC/C,UAAM;AAAA,EACR,UAAE;AACA,QAAI,QAAQ,uBAAuB;AAAA,EACrC;AACF;;;AC3SO,SAAS,UAId,aAAmE;AACnE,SAAO,IAAI,MAAM,aAA+D;AAAA,IAC9E,IAAI,SAAS,MAAM;AAEjB,UAAI,OAAO,SAAS,UAAU;AAC5B,eAAQ,YAAmD,IAAI;AAAA,MACjE;AACA,UAAI,SAAS,WAAY,QAAO;AAChC,UAAI,SAAS,YAAY;AACvB,eAAO,CAAC,SAAiB,YAAY,WAAW,IAAI;AAAA,MACtD;AAEA,YAAM,OAAO,YAAY,aAAa,IAAI;AAC1C,UAAI,KAAM,QAAO;AAEjB,aAAQ,YAAmD,IAAI;AAAA,IACjE;AAAA,IAEA,IAAI,SAAS,MAAM,QAAQ;AACzB,YAAM,IAAI;AAAA,QACR,qBAAqB,OAAO,IAAI,CAAC,eAAe,OAAO,IAAI,CAAC;AAAA,MAC9D;AAAA,IACF;AAAA,IAEA,IAAI,SAAS,MAAM;AACjB,UAAI,OAAO,SAAS,SAAU,QAAO;AACrC,UAAI,SAAS,cAAc,SAAS,WAAY,QAAO;AACvD,aAAO,YAAY,aAAa,IAAI,MAAM;AAAA,IAC5C;AAAA,EACF,CAAC;AACH;","names":["DisplayState","FormNotificationLevel","RequiredLevel","SubmitMode","SaveMode","ClientType","ClientState","OperationType","StructuralProperty","BindingType"]}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@xrmforge/helpers",
3
+ "version": "0.1.0",
4
+ "description": "Browser-safe runtime helpers for Dynamics 365 form scripts: select(), parseLookup(), typedForm(), Xrm constants, Action/Function executors",
5
+ "keywords": [
6
+ "dynamics-365",
7
+ "typescript",
8
+ "xrm",
9
+ "dataverse",
10
+ "web-resources",
11
+ "helpers"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "XrmForge Contributors",
15
+ "homepage": "https://github.com/juergenbeck/XrmForge/tree/main/packages/helpers",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/juergenbeck/XrmForge.git",
19
+ "directory": "packages/helpers"
20
+ },
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "main": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "sideEffects": false,
35
+ "peerDependencies": {
36
+ "@types/xrm": ">=9.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^22.0.0",
40
+ "@vitest/coverage-v8": "^3.2.4",
41
+ "tsup": "^8.3.0",
42
+ "typescript": "^5.7.0",
43
+ "vitest": "^3.0.0"
44
+ },
45
+ "engines": {
46
+ "node": ">=20.0.0"
47
+ },
48
+ "scripts": {
49
+ "build": "tsup",
50
+ "dev": "tsup --watch",
51
+ "test": "vitest run",
52
+ "test:watch": "vitest",
53
+ "typecheck": "tsc --noEmit",
54
+ "lint": "eslint src/",
55
+ "clean": "rm -rf dist"
56
+ }
57
+ }