@motifx-community/ui 0.1.0-beta.14 → 0.1.0-beta.16
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/CHANGELOG.md +113 -0
- package/README.md +2 -2
- package/dist/components/diagram/Diagram.d.ts +607 -0
- package/dist/components/diagram/DiagramAutoPan.d.ts +17 -0
- package/dist/components/diagram/DiagramBends.d.ts +70 -0
- package/dist/components/diagram/DiagramDragging.d.ts +66 -0
- package/dist/components/diagram/DiagramEdgeView.d.ts +78 -0
- package/dist/components/diagram/DiagramGrid.d.ts +13 -0
- package/dist/components/diagram/DiagramHistory.d.ts +16 -0
- package/dist/components/diagram/DiagramLinks.d.ts +177 -0
- package/dist/components/diagram/DiagramMap.d.ts +62 -0
- package/dist/components/diagram/DiagramNodeView.d.ts +143 -0
- package/dist/components/diagram/DiagramOverview.d.ts +20 -0
- package/dist/components/diagram/DiagramPanning.d.ts +34 -0
- package/dist/components/diagram/diagram-alignment.d.ts +2 -0
- package/dist/components/diagram/diagram-auto-pan-host.d.ts +8 -0
- package/dist/components/diagram/diagram-background.d.ts +2 -0
- package/dist/components/diagram/diagram-bends-host.d.ts +17 -0
- package/dist/components/diagram/diagram-card-kind.d.ts +42 -0
- package/dist/components/diagram/diagram-card-node.d.ts +11 -0
- package/dist/components/diagram/diagram-center-options.d.ts +6 -0
- package/dist/components/diagram/diagram-change.d.ts +11 -0
- package/dist/components/diagram/diagram-class-member.d.ts +13 -0
- package/dist/components/diagram/diagram-class-node.d.ts +12 -0
- package/dist/components/diagram/diagram-column-checked-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connected-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-connecting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connection.d.ts +7 -0
- package/dist/components/diagram/diagram-data.d.ts +12 -0
- package/dist/components/diagram/diagram-deleted-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-deleting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-dragging-host.d.ts +47 -0
- package/dist/components/diagram/diagram-drawn-node.d.ts +10 -0
- package/dist/components/diagram/diagram-edge-end.d.ts +2 -0
- package/dist/components/diagram/diagram-edge-path.d.ts +5 -0
- package/dist/components/diagram/diagram-edge-reshaped-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-edge-reshaping-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-edge.d.ts +49 -0
- package/dist/components/diagram/diagram-edit.d.ts +10 -0
- package/dist/components/diagram/diagram-export-options.d.ts +14 -0
- package/dist/components/diagram/diagram-export.d.ts +28 -0
- package/dist/components/diagram/diagram-fit-options.d.ts +12 -0
- package/dist/components/diagram/diagram-history-changed-event-args.d.ts +14 -0
- package/dist/components/diagram/diagram-history-step.d.ts +9 -0
- package/dist/components/diagram/diagram-kinds.d.ts +9 -0
- package/dist/components/diagram/diagram-layered.d.ts +39 -0
- package/dist/components/diagram/diagram-layout-direction.d.ts +2 -0
- package/dist/components/diagram/diagram-layout-options.d.ts +24 -0
- package/dist/components/diagram/diagram-layout-type.d.ts +5 -0
- package/dist/components/diagram/diagram-layout.d.ts +33 -0
- package/dist/components/diagram/diagram-line-look.d.ts +6 -0
- package/dist/components/diagram/diagram-line.d.ts +11 -0
- package/dist/components/diagram/diagram-links-host.d.ts +45 -0
- package/dist/components/diagram/diagram-links-options.d.ts +7 -0
- package/dist/components/diagram/diagram-map-props.d.ts +10 -0
- package/dist/components/diagram/diagram-map-target.d.ts +27 -0
- package/dist/components/diagram/diagram-marker.d.ts +6 -0
- package/dist/components/diagram/diagram-mini-map-options.d.ts +13 -0
- package/dist/components/diagram/diagram-mini-map-position.d.ts +2 -0
- package/dist/components/diagram/diagram-node-context.d.ts +26 -0
- package/dist/components/diagram/diagram-node-host.d.ts +23 -0
- package/dist/components/diagram/diagram-node-kind.d.ts +43 -0
- package/dist/components/diagram/diagram-node-move.d.ts +11 -0
- package/dist/components/diagram/diagram-node-resized-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-node-resizing-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-node-size.d.ts +8 -0
- package/dist/components/diagram/diagram-node-status.d.ts +2 -0
- package/dist/components/diagram/diagram-node-toggled-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-node.d.ts +56 -0
- package/dist/components/diagram/diagram-nodes-moved-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-nodes-moving-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-panning-host.d.ts +19 -0
- package/dist/components/diagram/diagram-pasted-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-pasting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-path-ends.d.ts +11 -0
- package/dist/components/diagram/diagram-path-result.d.ts +6 -0
- package/dist/components/diagram/diagram-paths.d.ts +35 -0
- package/dist/components/diagram/diagram-point.d.ts +5 -0
- package/dist/components/diagram/diagram-port-place.d.ts +9 -0
- package/dist/components/diagram/diagram-port.d.ts +27 -0
- package/dist/components/diagram/diagram-props.d.ts +159 -0
- package/dist/components/diagram/diagram-reconnected-event-args.d.ts +10 -0
- package/dist/components/diagram/diagram-reconnecting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-rect.d.ts +7 -0
- package/dist/components/diagram/diagram-resize-side.d.ts +2 -0
- package/dist/components/diagram/diagram-route.d.ts +10 -0
- package/dist/components/diagram/diagram-selection-changed-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-selection.d.ts +5 -0
- package/dist/components/diagram/diagram-side.d.ts +2 -0
- package/dist/components/diagram/diagram-table-column.d.ts +13 -0
- package/dist/components/diagram/diagram-table-node.d.ts +12 -0
- package/dist/components/diagram/diagram-tree.d.ts +14 -0
- package/dist/components/diagram/diagram-viewport-changed-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-viewport-options.d.ts +5 -0
- package/dist/components/diagram/diagram-viewport.d.ts +6 -0
- package/dist/components/diagram/index.d.ts +65 -0
- package/dist/components/diagram/swallow-next-click.d.ts +5 -0
- package/dist/components/expressioneditor/ExpressionEditor.d.ts +111 -0
- package/dist/components/expressioneditor/expression-editor-props.d.ts +24 -0
- package/dist/components/expressioneditor/index.d.ts +2 -0
- package/dist/components/formlayout/FormLayout.d.ts +10 -3
- package/dist/components/formlayout/form-editor-type.d.ts +1 -1
- package/dist/components/formlayout/form-item.d.ts +4 -1
- package/dist/components/formlayout/form-layout-props.d.ts +1 -1
- package/dist/components/formlayout/index.d.ts +1 -0
- package/dist/components/formlayout/schema-items.d.ts +26 -0
- package/dist/components/pivotgrid/model/PivotAxisWindow.d.ts +53 -1
- package/dist/components/pivotgrid/model/PivotCellWindow.d.ts +2 -0
- package/dist/components/pivotgrid/model/PivotWindows.d.ts +3 -6
- package/dist/components/toolbar/Toolbar.d.ts +5 -3
- package/dist/core/control.d.ts +2 -0
- package/dist/core/keyboard.d.ts +5 -0
- package/dist/core/measure/index.d.ts +2 -0
- package/dist/core/measure/picture.d.ts +27 -0
- package/dist/core/shortcut-options.d.ts +5 -2
- package/dist/core/ui-strings.d.ts +191 -0
- package/dist/generated/tokens.d.ts +1615 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11501 -705
- package/dist/index.js.map +1 -1
- package/dist/locales/en.json +180 -1
- package/dist/locales/tr.json +180 -1
- package/dist/styles.css +2262 -3
- package/dist/tokens/index.d.ts +1615 -0
- package/dist/tokens/index.js +1615 -0
- package/dist/tokens/index.js.map +1 -1
- package/dist/{tr-BGbF9uCs.js → tr-4GNbFdQh.js} +361 -3
- package/dist/{tr-BGbF9uCs.js.map → tr-4GNbFdQh.js.map} +1 -1
- package/docs/accordion.md +1 -1
- package/docs/actionsheet.md +1 -1
- package/docs/appbar.md +1 -1
- package/docs/appmenu-api.md +1 -1
- package/docs/appmenu-programmatic.md +1 -1
- package/docs/appmenu-search.md +1 -1
- package/docs/appmenu.md +1 -1
- package/docs/avatar.md +1 -1
- package/docs/badge.md +1 -1
- package/docs/bar-gauge-angles.md +1 -1
- package/docs/bar-gauge-data.md +1 -1
- package/docs/bar-gauge-labels.md +1 -1
- package/docs/bar-gauge.md +1 -1
- package/docs/barcode.md +1 -1
- package/docs/bottomnavigation.md +1 -1
- package/docs/bottomsheet.md +1 -1
- package/docs/breadcrumb.md +1 -1
- package/docs/button.md +1 -1
- package/docs/buttongroup.md +1 -1
- package/docs/buttons.md +1 -1
- package/docs/calendar.md +1 -1
- package/docs/card.md +1 -1
- package/docs/chart-bars.md +1 -1
- package/docs/chart-candles.md +1 -1
- package/docs/chart-funnel.md +1 -1
- package/docs/chart-heatmap.md +1 -1
- package/docs/chart-lines.md +1 -1
- package/docs/chart-live.md +1 -1
- package/docs/chart-pie.md +1 -1
- package/docs/chart-radar.md +1 -1
- package/docs/chart-ranges.md +1 -1
- package/docs/chart-references.md +1 -1
- package/docs/chart-scatter.md +1 -1
- package/docs/chart-waterfall.md +1 -1
- package/docs/chart-zoom.md +1 -1
- package/docs/chart.md +1 -1
- package/docs/chat.md +1 -1
- package/docs/checkbox.md +1 -1
- package/docs/chip.md +1 -1
- package/docs/choice.md +1 -1
- package/docs/colorbox.md +1 -1
- package/docs/combobox.md +1 -1
- package/docs/commands.md +1 -1
- package/docs/control.md +1 -1
- package/docs/data.md +2 -2
- package/docs/datagrid-api.md +1 -1
- package/docs/datagrid-bands.md +1 -1
- package/docs/datagrid-drag-select.md +1 -1
- package/docs/datagrid-drag.md +1 -1
- package/docs/datagrid-editing.md +1 -1
- package/docs/datagrid-filtering.md +1 -1
- package/docs/datagrid-forms.md +1 -1
- package/docs/datagrid-grouping.md +1 -1
- package/docs/datagrid-layout.md +1 -1
- package/docs/datagrid-lookups.md +1 -1
- package/docs/datagrid-master-detail.md +1 -1
- package/docs/datagrid-scrolling.md +1 -1
- package/docs/datagrid-selection.md +1 -1
- package/docs/datagrid.md +1 -1
- package/docs/datalayer-api.md +491 -0
- package/docs/datalayer-changes.md +501 -0
- package/docs/datalayer-odata.md +998 -0
- package/docs/datalayer-pivot.md +850 -0
- package/docs/datalayer-queries.md +516 -0
- package/docs/datalayer-rest.md +529 -0
- package/docs/datalayer-soap.md +576 -0
- package/docs/datalayer-source.md +464 -0
- package/docs/datalayer.md +264 -0
- package/docs/datebox.md +1 -1
- package/docs/daterangebox.md +1 -1
- package/docs/diagram-api.md +200 -0
- package/docs/diagram-commands.md +79 -0
- package/docs/diagram-connections.md +157 -0
- package/docs/diagram-custom-nodes.md +325 -0
- package/docs/diagram-designer.md +816 -0
- package/docs/diagram-editing.md +62 -0
- package/docs/diagram-flow.md +157 -0
- package/docs/diagram-keyboard.md +58 -0
- package/docs/diagram-layout.md +354 -0
- package/docs/diagram-nodes.md +206 -0
- package/docs/diagram-tables.md +212 -0
- package/docs/diagram.md +217 -0
- package/docs/display.md +1 -1
- package/docs/draggable.md +1 -1
- package/docs/drawer.md +1 -1
- package/docs/dropdown.md +1 -1
- package/docs/editors.md +1 -1
- package/docs/expressioneditor.md +469 -0
- package/docs/expressions.md +431 -0
- package/docs/feedback.md +1 -1
- package/docs/fileuploader.md +1 -1
- package/docs/filterbuilder.md +1 -1
- package/docs/formlayout-schema.md +143 -0
- package/docs/formlayout.md +16 -4
- package/docs/foundations.md +1 -1
- package/docs/gallery.md +1 -1
- package/docs/gauge-base.md +1 -1
- package/docs/gauge-circular.md +1 -1
- package/docs/gauge-dashboards.md +1 -1
- package/docs/gauge-linear.md +1 -1
- package/docs/gauge-markers.md +1 -1
- package/docs/gauge-marks.md +1 -1
- package/docs/gauge-ranges.md +1 -1
- package/docs/gauge-scales.md +1 -1
- package/docs/gauge.md +1 -1
- package/docs/home.md +1 -1
- package/docs/keyboard.md +2 -2
- package/docs/language.md +1 -1
- package/docs/layout.md +1 -1
- package/docs/listbox.md +1 -1
- package/docs/loadpanel.md +1 -1
- package/docs/lookup.md +1 -1
- package/docs/markdown.md +1 -1
- package/docs/menus-api.md +1 -1
- package/docs/menus-context.md +1 -1
- package/docs/menus-customization.md +1 -1
- package/docs/menus-merging.md +1 -1
- package/docs/menus-programmatic.md +1 -1
- package/docs/menus.md +1 -1
- package/docs/multiview.md +1 -1
- package/docs/navigation.md +1 -1
- package/docs/numberbox.md +1 -1
- package/docs/otpinput.md +1 -1
- package/docs/pagination.md +1 -1
- package/docs/pivotgrid-api.md +1 -1
- package/docs/pivotgrid-chart.md +1 -1
- package/docs/pivotgrid-drilldown.md +1 -1
- package/docs/pivotgrid-export.md +1 -1
- package/docs/pivotgrid-fields.md +1 -1
- package/docs/pivotgrid-keyboard.md +1 -1
- package/docs/pivotgrid-layout.md +1 -1
- package/docs/pivotgrid-panel.md +1 -1
- package/docs/pivotgrid-remote.md +122 -44
- package/docs/pivotgrid-sorting.md +3 -3
- package/docs/pivotgrid-state.md +1 -1
- package/docs/pivotgrid-summaries.md +1 -1
- package/docs/pivotgrid.md +1 -1
- package/docs/popover.md +1 -1
- package/docs/progress.md +1 -1
- package/docs/qrcode.md +1 -1
- package/docs/radiogroup.md +1 -1
- package/docs/rating.md +1 -1
- package/docs/resizable.md +1 -1
- package/docs/ribbonmenu-api.md +1 -1
- package/docs/ribbonmenu-commands.md +1 -1
- package/docs/ribbonmenu-customization.md +1 -1
- package/docs/ribbonmenu-display.md +1 -1
- package/docs/ribbonmenu-layouts.md +1 -1
- package/docs/ribbonmenu-programmatic.md +1 -1
- package/docs/ribbonmenu.md +1 -1
- package/docs/scheduler-agenda.md +1 -1
- package/docs/scheduler-big-data.md +1 -1
- package/docs/scheduler-editing.md +1 -1
- package/docs/scheduler-form.md +1 -1
- package/docs/scheduler-icalendar.md +1 -1
- package/docs/scheduler-mobile.md +1 -1
- package/docs/scheduler-recurrence.md +1 -1
- package/docs/scheduler-resources.md +1 -1
- package/docs/scheduler-timeline.md +1 -1
- package/docs/scheduler.md +1 -1
- package/docs/scrollview.md +1 -1
- package/docs/shortcuteditor.md +1 -1
- package/docs/showcase-flowstudio.md +61 -0
- package/docs/showcase-home.md +1 -1
- package/docs/showcase-mail.md +1 -1
- package/docs/showcase.md +1 -1
- package/docs/signature.md +1 -1
- package/docs/skeleton.md +1 -1
- package/docs/slider.md +1 -1
- package/docs/sortable.md +1 -1
- package/docs/sparkline.md +1 -1
- package/docs/speeddial.md +1 -1
- package/docs/splitter.md +1 -1
- package/docs/stepper.md +1 -1
- package/docs/switch.md +1 -1
- package/docs/tabdocs-dock.md +1 -1
- package/docs/tabdocs-drag.md +1 -1
- package/docs/tabdocs-menus.md +1 -1
- package/docs/tabdocs-mobile.md +1 -1
- package/docs/tabdocs-tabs.md +1 -1
- package/docs/tabdocs-windows.md +1 -1
- package/docs/tabdocs-workspace.md +1 -1
- package/docs/tabdocs.md +1 -1
- package/docs/tabs.md +1 -1
- package/docs/tagbox.md +1 -1
- package/docs/textarea.md +1 -1
- package/docs/textbox.md +1 -1
- package/docs/tileview.md +1 -1
- package/docs/timeline.md +1 -1
- package/docs/toast.md +1 -1
- package/docs/tokens.md +324 -1
- package/docs/toolbar-api.md +1 -1
- package/docs/toolbar-customization.md +1 -1
- package/docs/toolbar.md +1 -1
- package/docs/tooltip.md +1 -1
- package/docs/treeview.md +1 -1
- package/docs/validationsummary.md +1 -1
- package/docs/window.md +1 -1
- package/docs/wizard.md +1 -1
- package/llms-full.txt +10142 -462
- package/llms.txt +32 -1
- package/package.json +2 -2
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
# Data layer: SOAP and WCF
|
|
2
|
+
|
|
3
|
+
> Data layer · @motifx-community/data 0.1.0-beta.3
|
|
4
|
+
|
|
5
|
+
`SoapAdapter` loads and saves rows through a SOAP service, such as a WCF `.svc` endpoint. Each part of the adapter contract calls a service operation, and what the service does not do itself runs in memory.
|
|
6
|
+
|
|
7
|
+
## Creating a SOAP adapter
|
|
8
|
+
|
|
9
|
+
The adapter takes the service address, the namespaces its envelopes are written with, and the operations each part of the contract calls. `key` and `fields` are the options [every adapter takes](datalayer-api.md); the declared `number` and `boolean` fields matter here, because XML carries no types.
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { data } from '@motifx-community/data';
|
|
13
|
+
|
|
14
|
+
const orders = new data.SoapAdapter<Order, number>({
|
|
15
|
+
url: 'https://host/OrderService.svc',
|
|
16
|
+
contract: 'IOrderService',
|
|
17
|
+
dataNamespace: 'http://schemas.datacontract.org/2004/07/Shop',
|
|
18
|
+
key: 'Id',
|
|
19
|
+
fields: { Id: 'number', Version: 'number', Amount: 'number', Shipped: 'boolean', 'Lines.Quantity': 'number' },
|
|
20
|
+
firstFields: ['Id', 'Version'],
|
|
21
|
+
lists: { Lines: 'OrderLine', Tags: 'string' },
|
|
22
|
+
operations: {
|
|
23
|
+
load: { name: 'GetAllOrders' },
|
|
24
|
+
byKey: { name: 'GetOrder', args: (id) => ({ id }) },
|
|
25
|
+
insert: { name: 'AddOrder', args: (order) => ({ order }) },
|
|
26
|
+
update: { name: 'UpdateOrder', args: (id, changes, original) => ({ order: { ...original, ...changes, Id: id } }) },
|
|
27
|
+
remove: { name: 'DeleteOrder', args: (id) => ({ id }) },
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`namespace` is the service namespace and defaults to `http://tempuri.org/`, the WCF default. `contract` is the service contract name; the default SOAP action of an operation is `namespace` + `contract/` + the operation name. `dataNamespace` is the namespace object fields are written in, which WCF derives from the CLR namespace of the data contract as `http://schemas.datacontract.org/2004/07/<CLR namespace>`; it defaults to `namespace`. `firstFields` and `lists` describe how objects are written and read ([How objects are written](#serialization)).
|
|
33
|
+
|
|
34
|
+
## Operations
|
|
35
|
+
|
|
36
|
+
SOAP has no query language: each part of the adapter contract (`load`, `byKey`, `insert`, `update`, `remove`, `save`) calls the operation it names in `operations`, with the arguments its `args` function returns, keyed by parameter name and in the operation's parameter order. A part without an operation is provided another way where one exists: without `byKey` the row is asked from `load` with a filter on the key, and a change that `insert`, `update` or `remove` does not cover is sent through `save`; a write without either operation fails as unsupported (`MJD301`).
|
|
37
|
+
|
|
38
|
+
The adapter reads the response envelope the way WCF writes it: the body holds a `NameResponse` element with a `NameResult` element inside, whose content is handed to the operation's `read` as plain values, objects and arrays. Without `read`, each part has a default: `load` takes the result as the list of rows; `byKey` takes the result object as the row and a `nil` result as no row; `insert` takes the saved row, or, when the operation returns only the new key, puts it into the key field; `update` takes the saved row; the result of `remove` is ignored.
|
|
39
|
+
|
|
40
|
+
`load.handles` names what the load operation does itself: `filter`, `sort`, `paging`, `count`, `select`, `group` and `summary`. Everything `handles` does not name runs in memory: the adapter loads what the operation returns, then filters, sorts and pages it in the browser ([queries](datalayer-queries.md)). With `handles`, `args` receives the load options and sends what the operation needs, and `read` returns `{ rows, total }`:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
operations: {
|
|
44
|
+
load: {
|
|
45
|
+
name: 'GetOrders',
|
|
46
|
+
handles: ['paging', 'sort', 'count'],
|
|
47
|
+
args: (o) => ({ skip: o.skip ?? 0, take: o.take ?? 0, sortBy: o.sort?.[0]?.field ?? null, descending: o.sort?.[0]?.dir === 'desc' }),
|
|
48
|
+
read: (r) => {
|
|
49
|
+
const page = r as { Items: Order[]; Total: number };
|
|
50
|
+
return { rows: page.Items, total: page.Total };
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
lists: { Items: 'Order' },
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
A `save` operation sends many changes in one call ([save modes](datalayer-changes.md)), and the `params` of a call are appended to `url` as query parameters.
|
|
58
|
+
|
|
59
|
+
## A WCF service in the page
|
|
60
|
+
|
|
61
|
+
The service below runs in the page: the adapter's `fetch` option hands it each request envelope, and the service reads it with `DOMParser`, runs the operation against orders in memory and answers as a WCF service would, including a fault for an invalid change. The panes show the request envelope the adapter sent and the response envelope the service answered, pretty-printed.
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
/** An XML document with one element per line, indented, for reading; text stays escaped. */
|
|
65
|
+
export function prettyXml(xml: string): string {
|
|
66
|
+
const declaration = xml.startsWith('<?xml') ? `${xml.slice(0, xml.indexOf('?>') + 2)}\n` : '';
|
|
67
|
+
const root = new DOMParser().parseFromString(xml, 'text/xml').documentElement;
|
|
68
|
+
return declaration + writeElement(root, 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function writeElement(element: Element, depth: number): string {
|
|
72
|
+
const pad = ' '.repeat(depth);
|
|
73
|
+
const attributes = Array.from(element.attributes)
|
|
74
|
+
.map((a) => ` ${a.name}="${escapeXml(a.value)}"`)
|
|
75
|
+
.join('');
|
|
76
|
+
const name = element.tagName;
|
|
77
|
+
if (element.firstElementChild) {
|
|
78
|
+
const children = Array.from(element.children)
|
|
79
|
+
.map((c) => writeElement(c, depth + 1))
|
|
80
|
+
.join('\n');
|
|
81
|
+
return `${pad}<${name}${attributes}>\n${children}\n${pad}</${name}>`;
|
|
82
|
+
}
|
|
83
|
+
if (!element.childNodes.length) return `${pad}<${name}${attributes}/>`;
|
|
84
|
+
return `${pad}<${name}${attributes}>${escapeXml(element.textContent ?? '')}</${name}>`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function escapeXml(text: string): string {
|
|
88
|
+
return text.replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The insert envelope shows the serialization rules at work: `Id` and `Version` first (`firstFields`), the other fields in alphabetical order, and the order lines as `OrderLine` items of the `Lines` element (`lists`). The invalid change comes back as a SOAP fault, which the adapter turns into an `MJD202` error.
|
|
93
|
+
|
|
94
|
+
## How objects are written
|
|
95
|
+
|
|
96
|
+
Objects in `args` are written the way WCF's `DataContractSerializer` reads them: every field is an element in `dataNamespace`, the fields come in alphabetical order, and the fields of a base class come first; `firstFields` names them, in their order. The order matters: `DataContractSerializer` reads fields in order and silently skips one that arrives out of order, so a value is lost without an error when `firstFields` is missing or wrong.
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
<AddOrder xmlns="http://tempuri.org/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
|
|
100
|
+
xmlns:d="http://schemas.datacontract.org/2004/07/Shop"
|
|
101
|
+
xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
|
102
|
+
<order>
|
|
103
|
+
<d:Id>0</d:Id>
|
|
104
|
+
<d:Version>0</d:Version>
|
|
105
|
+
<d:Amount>62.5</d:Amount>
|
|
106
|
+
<d:Customer>Nora Flint</d:Customer>
|
|
107
|
+
<d:Lines>
|
|
108
|
+
<d:OrderLine>
|
|
109
|
+
<d:Product>Lamp</d:Product>
|
|
110
|
+
<d:Quantity>1</d:Quantity>
|
|
111
|
+
</d:OrderLine>
|
|
112
|
+
</d:Lines>
|
|
113
|
+
<d:Note i:nil="true"/>
|
|
114
|
+
<d:Tags>
|
|
115
|
+
<arr:string>new</arr:string>
|
|
116
|
+
</d:Tags>
|
|
117
|
+
</order>
|
|
118
|
+
</AddOrder>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`lists` gives the element name of the items of every field that is a list: `{ Lines: 'OrderLine', Tags: 'string' }`. Writing a list requires its entry (`MJD302` without one); reading uses it so that a list of one item, or of none, stays a list, because XML alone cannot tell them apart. Items that are plain values (`string`, `int`, `boolean`, …) are written in the WCF arrays namespace.
|
|
122
|
+
|
|
123
|
+
`null` is written as `i:nil="true"` and read back as `null`; a field that is `undefined` is left out. A `Date` is written in ISO format. Reading converts the fields declared `number` and `boolean` from text, and a path such as `Lines.Quantity` reaches into every item of a list; `readRow` then changes each row where more is needed.
|
|
124
|
+
|
|
125
|
+
`serializer: 'xml'` is for ASMX and other `XmlSerializer` services: everything is written in the service namespace, without prefixes, and the fields keep the order given.
|
|
126
|
+
|
|
127
|
+
## Bindings
|
|
128
|
+
|
|
129
|
+
The adapter sends SOAP 1.1 by default, as WCF's `basicHttpBinding` expects it: the envelope goes as `text/xml` with the action in the `SOAPAction` header. `version: '1.2'` sends `application/soap+xml` with the action in the content type, and `addressing: true` adds the WS-Addressing headers (`Action`, `MessageID`, `ReplyTo`, `To`); together they match `wsHttpBinding` without security.
|
|
130
|
+
|
|
131
|
+
`soapHeader` returns XML for the envelope's `Header`, such as a security token, and is called with the operation name:
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
soapHeader: (operation) => `<Token xmlns="urn:example:auth">${token}</Token>`,
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Faults
|
|
138
|
+
|
|
139
|
+
A `Fault` in the response body becomes the error of the call, whatever the HTTP status; some services answer faults with status 200. A fault whose code is `Client` (SOAP 1.1) or `Sender` (SOAP 1.2) says the request was rejected and becomes `MJD202`; every other code becomes `MJD201`. The error's `details` holds the fault as a [`SoapFault`](#api): its code without the namespace prefix, the most specific SOAP 1.2 subcode, the message, and the detail read like a result. The [shared reference](datalayer-api.md) describes the `DataError` codes.
|
|
140
|
+
|
|
141
|
+
`readFault` maps a fault to an error of your own; returning nothing keeps the default. An answer that is no SOAP envelope fails as `MJD104`.
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
readFault: (fault, operation) => {
|
|
145
|
+
const kind = (fault.detail as { OrderFault?: { Kind?: string } } | undefined)?.OrderFault?.Kind;
|
|
146
|
+
if (kind === 'NotFound') return data.DataError.create('MJD204', { operation, message: fault.message });
|
|
147
|
+
},
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Versions and concurrency
|
|
151
|
+
|
|
152
|
+
`update` and `remove` receive `original`, the row as it was read; [saving through a change tracker](datalayer-changes.md) passes it with each change. Most WCF services take the whole row, so the `update` arguments merge `original` and the changes; a version field the service checks (`Version` above) then travels with them, and a row changed elsewhere since it was read comes back as a fault. The example's service rejects a stale `Version` this way and answers every saved row with the version it wrote.
|
|
153
|
+
|
|
154
|
+
## The service side
|
|
155
|
+
|
|
156
|
+
The adapter configuration on this page matches the WCF service below: the CLR namespace `Shop` gives `dataNamespace`, the base class `Entity` gives `firstFields`, and the collection members give `lists`. `[FaultContract]` declares the detail type faults carry, which `readFault` can read.
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
using System.Runtime.Serialization;
|
|
160
|
+
using System.ServiceModel;
|
|
161
|
+
|
|
162
|
+
namespace Shop;
|
|
163
|
+
|
|
164
|
+
[ServiceContract]
|
|
165
|
+
public interface IOrderService
|
|
166
|
+
{
|
|
167
|
+
[OperationContract]
|
|
168
|
+
List<Order> GetAllOrders();
|
|
169
|
+
|
|
170
|
+
[OperationContract]
|
|
171
|
+
Order? GetOrder(int id);
|
|
172
|
+
|
|
173
|
+
[OperationContract]
|
|
174
|
+
[FaultContract(typeof(OrderFault))]
|
|
175
|
+
Order AddOrder(Order order);
|
|
176
|
+
|
|
177
|
+
[OperationContract]
|
|
178
|
+
[FaultContract(typeof(OrderFault))]
|
|
179
|
+
Order UpdateOrder(Order order);
|
|
180
|
+
|
|
181
|
+
[OperationContract]
|
|
182
|
+
[FaultContract(typeof(OrderFault))]
|
|
183
|
+
void DeleteOrder(int id);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
[DataContract]
|
|
187
|
+
public class Entity
|
|
188
|
+
{
|
|
189
|
+
[DataMember] public int Id { get; set; }
|
|
190
|
+
[DataMember] public int Version { get; set; }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
[DataContract]
|
|
194
|
+
public class Order : Entity
|
|
195
|
+
{
|
|
196
|
+
[DataMember] public string Customer { get; set; } = "";
|
|
197
|
+
[DataMember] public decimal Amount { get; set; }
|
|
198
|
+
[DataMember] public bool Shipped { get; set; }
|
|
199
|
+
[DataMember] public string? Note { get; set; }
|
|
200
|
+
[DataMember] public List<string> Tags { get; set; } = [];
|
|
201
|
+
[DataMember] public List<OrderLine> Lines { get; set; } = [];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
[DataContract]
|
|
205
|
+
public class OrderLine
|
|
206
|
+
{
|
|
207
|
+
[DataMember] public string Product { get; set; } = "";
|
|
208
|
+
[DataMember] public int Quantity { get; set; }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
[DataContract]
|
|
212
|
+
public class OrderFault
|
|
213
|
+
{
|
|
214
|
+
[DataMember] public string Kind { get; set; } = "";
|
|
215
|
+
[DataMember] public string? Field { get; set; }
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Pivots
|
|
220
|
+
|
|
221
|
+
A `pivot` operation computes pivots, whole and in parts, with the same bodies and answers as a REST pivot endpoint. It is called with two parameters, `kind` and `body`: `body` is the JSON text of the body REST would send for that kind, and the operation answers the JSON text of the REST answer; JSON goes as plain text because XML carries no types. `args` and `read` change both sides; `window: false` is for an operation that answers whole pivots only, and `blocks: true` says it answers block counts and descendants too. Without a `pivot` operation, pivots are grouped in memory from the loaded rows. [Pivots and loading in parts](datalayer-pivot.md#soap) gives the bodies and the answers.
|
|
222
|
+
|
|
223
|
+
## Requests
|
|
224
|
+
|
|
225
|
+
`SoapAdapter` is an HTTP adapter: `fetch`, `headers`, `credentials`, `beforeSend`, `afterReceive`, `timeoutMs`, `retry`, `resilience`, `readError` and `errorTextLength` work as for every HTTP adapter and are described with [the REST adapter](datalayer-rest.md#requests).
|
|
226
|
+
|
|
227
|
+
## API reference
|
|
228
|
+
|
|
229
|
+
The options of `SoapAdapterOptions` beyond [the shared adapter options](datalayer-api.md) and [the HTTP options](datalayer-rest.md#requests):
|
|
230
|
+
|
|
231
|
+
| Option | Default | Description |
|
|
232
|
+
| --- | --- | --- |
|
|
233
|
+
| `url` | — | The service address the envelopes are posted to: `https://host/OrderService.svc`. |
|
|
234
|
+
| `operations` | — | The service operation each part of the adapter contract calls ([`SoapOperations`](#api)). |
|
|
235
|
+
| `namespace` | `'http://tempuri.org/'` | The service namespace; the default is what WCF uses when the contract sets none. |
|
|
236
|
+
| `contract` | — | The service contract name (`IOrderService`), part of the default SOAP action. |
|
|
237
|
+
| `version` | `'1.1'` | The SOAP version: `'1.1'` (`basicHttpBinding`) or `'1.2'`. |
|
|
238
|
+
| `addressing` | `false` | Adds WS-Addressing headers, for bindings that need them (`wsHttpBinding` without security). |
|
|
239
|
+
| `serializer` | `'datacontract'` | How the service reads objects: `'datacontract'` (WCF) or `'xml'` (ASMX, `XmlSerializer`). See [How objects are written](#serialization). |
|
|
240
|
+
| `dataNamespace` | `namespace` | The namespace of object fields: `http://schemas.datacontract.org/2004/07/<CLR namespace>`. |
|
|
241
|
+
| `firstFields` | `[]` | The fields written before the others, in this order: the fields of a base class, which WCF reads first. |
|
|
242
|
+
| `lists` | `{}` | The fields that are lists, with the element name of their items: `{ Lines: 'OrderLine', Tags: 'string' }`. |
|
|
243
|
+
| `soapHeader` | — | Returns XML for the envelope's `Header`, such as a security token; called with the operation name. |
|
|
244
|
+
| `readFault` | — | Turns a fault into an error of your own; returning nothing keeps the default (see [Faults](#faults)). |
|
|
245
|
+
| `readRow` | — | Changes each row read from the service, after declared `number` and `boolean` fields are converted. |
|
|
246
|
+
|
|
247
|
+
### SoapOperations
|
|
248
|
+
|
|
249
|
+
The second column says what the adapter does when the operation is not given.
|
|
250
|
+
|
|
251
|
+
| Operation | Without it | Description |
|
|
252
|
+
| --- | --- | --- |
|
|
253
|
+
| `load` | — | Loads rows ([`SoapLoadOperation`](#api)). Required. |
|
|
254
|
+
| `pivot` | Pivots are grouped in memory. | Computes pivots, whole and in parts ([`SoapPivotOperation`](#api), [Pivots](#pivots)). |
|
|
255
|
+
| `byKey` | The row is asked from `load` with a key filter. | `args(key)`. Default read: the result object as the row; a `nil` result is no row. |
|
|
256
|
+
| `insert` | The change is sent through `save`. | `args(values)`. Default read: the saved row, or the new key alone when the operation returns only that. |
|
|
257
|
+
| `update` | The change is sent through `save`. | `args(key, changes, original)`; `original` is the row as it was read. Default read: the saved row. |
|
|
258
|
+
| `remove` | The change is sent through `save`. | `args(key, original)`. The result is ignored. |
|
|
259
|
+
| `save` | Each change is sent through the operation above that covers it. | `args(changes)` sends many changes in one call; `read` returns one `{ ok, row?, error? }` result per change. Without `read`, every change counts as saved. |
|
|
260
|
+
|
|
261
|
+
### SoapOperation (byKey, insert, update, remove, save)
|
|
262
|
+
|
|
263
|
+
| Member | Default | Description |
|
|
264
|
+
| --- | --- | --- |
|
|
265
|
+
| `name` | — | The name of the service operation; the request body holds one `<name>` element with the arguments. |
|
|
266
|
+
| `action` | `namespace` + `contract/` + `name` | The SOAP action sent with the request. |
|
|
267
|
+
| `args` | — | The arguments by parameter name, in the operation's parameter order. |
|
|
268
|
+
| `read` | Per part; see [`SoapOperations`](#api). | Reads the result, given as plain values, objects and arrays. |
|
|
269
|
+
|
|
270
|
+
### SoapLoadOperation
|
|
271
|
+
|
|
272
|
+
| Member | Default | Description |
|
|
273
|
+
| --- | --- | --- |
|
|
274
|
+
| `name` | — | The name of the service operation. |
|
|
275
|
+
| `action` | `namespace` + `contract/` + `name` | The SOAP action sent with the request. |
|
|
276
|
+
| `args` | No arguments. | Builds the arguments from the load options; it receives only what `handles` names. |
|
|
277
|
+
| `read` | The result is the list of rows. | Returns `{ rows, total }`; with `group` or `summary` in `handles`, also `groups` or `summary`. |
|
|
278
|
+
| `handles` | `[]` | What the operation does itself: `'filter'`, `'sort'`, `'paging'`, `'count'`, `'select'`, `'group'`, `'summary'`. The rest runs in memory. |
|
|
279
|
+
|
|
280
|
+
### SoapPivotOperation
|
|
281
|
+
|
|
282
|
+
| Member | Default | Description |
|
|
283
|
+
| --- | --- | --- |
|
|
284
|
+
| `name` | — | The name of the service operation. |
|
|
285
|
+
| `action` | `namespace` + `contract/` + `name` | The SOAP action sent with the request. |
|
|
286
|
+
| `args` | `{ kind, body }` | The arguments; `body` is the JSON text of the body a REST pivot endpoint would receive. |
|
|
287
|
+
| `read` | The result parsed as JSON when it is text. | Reads the result as the REST defaults read an answer body. |
|
|
288
|
+
| `window` | `true` | `false` for an operation that answers whole pivots only: a view then loads the pivot whole. |
|
|
289
|
+
| `blocks` | `false` | The operation answers `kind: 'blocks'` and members' `descendants` (`capabilities.pivotBlocks`). |
|
|
290
|
+
|
|
291
|
+
### SoapFault
|
|
292
|
+
|
|
293
|
+
What `readFault` receives, and what the error's `details` holds when a fault is read the built-in way.
|
|
294
|
+
|
|
295
|
+
| Member | Description |
|
|
296
|
+
| --- | --- |
|
|
297
|
+
| `code` | The fault code without its namespace prefix: `Client` / `Sender` (the request was rejected), `Server` / `Receiver`, … |
|
|
298
|
+
| `subcode` | SOAP 1.2: the most specific subcode. |
|
|
299
|
+
| `message` | The fault reason, as the service wrote it. |
|
|
300
|
+
| `detail` | The fault detail as plain values, read like a result. |
|
|
301
|
+
|
|
302
|
+
## Feedback
|
|
303
|
+
|
|
304
|
+
Loading issues…
|
|
305
|
+
|
|
306
|
+
## Example files
|
|
307
|
+
|
|
308
|
+
`Envelopes.tsx`
|
|
309
|
+
|
|
310
|
+
```tsx
|
|
311
|
+
import { reactive } from '@motifx/core';
|
|
312
|
+
import { data } from '@motifx-community/data';
|
|
313
|
+
import { Button, Stack } from '@motifx-community/ui';
|
|
314
|
+
import { prettyXml } from './pretty-xml';
|
|
315
|
+
import { OrderService, SHOP, type Order } from './wcf-order-service';
|
|
316
|
+
|
|
317
|
+
const CUSTOMERS = ['Nora Flint', 'Omar Sayan', 'Petra Lund'];
|
|
318
|
+
|
|
319
|
+
/** Loads, inserts and updates orders through a WCF-style service in the page, showing each envelope pair. */
|
|
320
|
+
export default function Envelopes() {
|
|
321
|
+
const service = new OrderService();
|
|
322
|
+
const s = reactive({ rows: [] as Order[], sent: '', received: '', note: 'Loading…' });
|
|
323
|
+
let added = 0;
|
|
324
|
+
|
|
325
|
+
const orders = new data.SoapAdapter<Order, number>({
|
|
326
|
+
url: 'https://host/OrderService.svc',
|
|
327
|
+
key: 'Id',
|
|
328
|
+
contract: 'IOrderService',
|
|
329
|
+
dataNamespace: SHOP,
|
|
330
|
+
fields: { Id: 'number', Version: 'number', Amount: 'number', Shipped: 'boolean', 'Lines.Quantity': 'number' },
|
|
331
|
+
firstFields: ['Id', 'Version'],
|
|
332
|
+
lists: { Lines: 'OrderLine' },
|
|
333
|
+
operations: {
|
|
334
|
+
load: { name: 'GetAllOrders' },
|
|
335
|
+
insert: { name: 'AddOrder', args: (order) => ({ order }) },
|
|
336
|
+
update: { name: 'UpdateOrder', args: (id, changes, original) => ({ order: { ...original, ...changes, Id: id } }) },
|
|
337
|
+
},
|
|
338
|
+
// The service runs in the page: this fetch hands it the envelope and notes both sides.
|
|
339
|
+
fetch: async (url, init) => {
|
|
340
|
+
const headers = new Headers(init.headers);
|
|
341
|
+
s.sent = `POST ${url}\nContent-Type: ${headers.get('Content-Type')}\nSOAPAction: ${headers.get('SOAPAction')}\n\n${prettyXml(String(init.body))}`;
|
|
342
|
+
const { status, body } = service.answer(String(init.body));
|
|
343
|
+
s.received = `HTTP ${status}\nContent-Type: text/xml; charset=utf-8\n\n${prettyXml(body)}`;
|
|
344
|
+
return new Response(body, { status, headers: { 'content-type': 'text/xml; charset=utf-8' } });
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
const run = async (operation: () => Promise<string>) => {
|
|
349
|
+
try {
|
|
350
|
+
s.note = await operation();
|
|
351
|
+
} catch (error) {
|
|
352
|
+
s.note = error instanceof data.DataError ? `${error.code}: ${error.plainMessage}` : String(error);
|
|
353
|
+
}
|
|
354
|
+
s.rows = service.orders.map((o) => ({ ...o, Lines: o.Lines.map((l) => ({ ...l })) }));
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const load = () =>
|
|
358
|
+
run(async () => {
|
|
359
|
+
const { rows } = await orders.load();
|
|
360
|
+
return `Loaded ${rows.length} orders.`;
|
|
361
|
+
});
|
|
362
|
+
const insert = () =>
|
|
363
|
+
run(async () => {
|
|
364
|
+
const row = await orders.insert({ Id: 0, Version: 0, Customer: CUSTOMERS[added++ % CUSTOMERS.length], Amount: 60, Shipped: false, Lines: [{ Product: 'Lamp', Quantity: 1 }] });
|
|
365
|
+
return `Added order ${row.Id}.`;
|
|
366
|
+
});
|
|
367
|
+
const update = () =>
|
|
368
|
+
run(async () => {
|
|
369
|
+
const row = s.rows[0];
|
|
370
|
+
const saved = await orders.update(row.Id, { Amount: row.Amount + 25, Shipped: true }, { original: row });
|
|
371
|
+
return `Updated order ${row.Id}; the service wrote version ${saved.Version}.`;
|
|
372
|
+
});
|
|
373
|
+
const invalid = () =>
|
|
374
|
+
run(async () => {
|
|
375
|
+
const row = s.rows[0];
|
|
376
|
+
await orders.update(row.Id, { Customer: '' }, { original: row });
|
|
377
|
+
return 'Saved.';
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
void load();
|
|
381
|
+
|
|
382
|
+
return (
|
|
383
|
+
<>
|
|
384
|
+
<Stack orientation="horizontal" wrap align="center">
|
|
385
|
+
<Button text="Load" variant="outline" onclick={load} />
|
|
386
|
+
<Button text="Add an order" variant="outline" onclick={insert} />
|
|
387
|
+
<Button text="Ship the first order" variant="outline" onclick={update} enabled={() => s.rows.length > 0} />
|
|
388
|
+
<Button text="Send an empty customer" variant="outline" onclick={invalid} enabled={() => s.rows.length > 0} />
|
|
389
|
+
<output class="soap-note">{() => s.note}</output>
|
|
390
|
+
</Stack>
|
|
391
|
+
<table class="token-table soap-rows">
|
|
392
|
+
<thead>
|
|
393
|
+
<tr>
|
|
394
|
+
<th>Id</th>
|
|
395
|
+
<th>Version</th>
|
|
396
|
+
<th>Customer</th>
|
|
397
|
+
<th>Amount</th>
|
|
398
|
+
<th>Shipped</th>
|
|
399
|
+
<th>Lines</th>
|
|
400
|
+
</tr>
|
|
401
|
+
</thead>
|
|
402
|
+
<tbody>
|
|
403
|
+
{s.rows.map((o) => (
|
|
404
|
+
<tr key={o.Id}>
|
|
405
|
+
<td>{o.Id}</td>
|
|
406
|
+
<td>{o.Version}</td>
|
|
407
|
+
<td>{o.Customer}</td>
|
|
408
|
+
<td>{o.Amount}</td>
|
|
409
|
+
<td>{String(o.Shipped)}</td>
|
|
410
|
+
<td>{o.Lines.map((l) => `${l.Product} × ${l.Quantity}`).join(', ')}</td>
|
|
411
|
+
</tr>
|
|
412
|
+
))}
|
|
413
|
+
</tbody>
|
|
414
|
+
</table>
|
|
415
|
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(22rem, 1fr)); gap: var(--mx-space-3); inline-size: 100%">
|
|
416
|
+
<pre tabindex="0" aria-label="The request" style="max-block-size: 20rem; overflow: auto; margin: 0">{() => s.sent}</pre>
|
|
417
|
+
<pre tabindex="0" aria-label="The answer" style="max-block-size: 20rem; overflow: auto; margin: 0">{() => s.received}</pre>
|
|
418
|
+
</div>
|
|
419
|
+
</>
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
`wcf-order-service.ts`
|
|
425
|
+
|
|
426
|
+
```tsx
|
|
427
|
+
// A WCF-style order service that runs in the page: it reads each request envelope with DOMParser,
|
|
428
|
+
// runs the operation against orders in memory and answers as a WCF `.svc` endpoint would.
|
|
429
|
+
|
|
430
|
+
export interface OrderLine {
|
|
431
|
+
Product: string;
|
|
432
|
+
Quantity: number;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export interface Order {
|
|
436
|
+
Id: number;
|
|
437
|
+
Version: number;
|
|
438
|
+
Customer: string;
|
|
439
|
+
Amount: number;
|
|
440
|
+
Shipped: boolean;
|
|
441
|
+
Lines: OrderLine[];
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export const SHOP = 'http://schemas.datacontract.org/2004/07/Shop';
|
|
445
|
+
const XSI = 'http://www.w3.org/2001/XMLSchema-instance';
|
|
446
|
+
|
|
447
|
+
/** A rejected change: it becomes the fault the service answers. */
|
|
448
|
+
class Rejection {
|
|
449
|
+
constructor(
|
|
450
|
+
readonly kind: 'Invalid' | 'Conflict' | 'NotFound',
|
|
451
|
+
readonly field: string | null,
|
|
452
|
+
readonly message: string,
|
|
453
|
+
) {}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
export class OrderService {
|
|
457
|
+
orders: Order[] = [
|
|
458
|
+
{ Id: 1, Version: 1, Customer: 'Alva Reed', Amount: 120, Shipped: true, Lines: [{ Product: 'Notebook', Quantity: 4 }] },
|
|
459
|
+
{ Id: 2, Version: 1, Customer: 'Mete Aksoy', Amount: 80.5, Shipped: false, Lines: [{ Product: 'Pen', Quantity: 10 }, { Product: 'Folder', Quantity: 2 }] },
|
|
460
|
+
{ Id: 3, Version: 2, Customer: 'Iris Vane', Amount: 45, Shipped: false, Lines: [] },
|
|
461
|
+
];
|
|
462
|
+
private nextId = 4;
|
|
463
|
+
|
|
464
|
+
/** Answers one request envelope, as WCF would: a response envelope, or a fault with status 500. */
|
|
465
|
+
answer(request: string): { status: number; body: string } {
|
|
466
|
+
const root = new DOMParser().parseFromString(request, 'text/xml').documentElement;
|
|
467
|
+
const call = child(root, 'Body')?.firstElementChild;
|
|
468
|
+
try {
|
|
469
|
+
if (!call) throw new Rejection('Invalid', null, 'The request is not a SOAP envelope.');
|
|
470
|
+
if (call.localName === 'GetAllOrders') return respond('GetAllOrders', this.orders.map((o) => `<a:Order>${writeOrder(o)}</a:Order>`).join(''));
|
|
471
|
+
if (call.localName === 'AddOrder') return respond('AddOrder', writeOrder(this.add(readOrder(child(call, 'order')))));
|
|
472
|
+
if (call.localName === 'UpdateOrder') return respond('UpdateOrder', writeOrder(this.update(readOrder(child(call, 'order')))));
|
|
473
|
+
throw new Rejection('Invalid', null, `No operation ${call.localName}.`);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if (error instanceof Rejection) return fault(error);
|
|
476
|
+
throw error;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
private add(order: Order): Order {
|
|
481
|
+
checkCustomer(order);
|
|
482
|
+
const saved = { ...order, Id: this.nextId++, Version: 1 };
|
|
483
|
+
this.orders.push(saved);
|
|
484
|
+
return saved;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private update(order: Order): Order {
|
|
488
|
+
const found = this.orders.find((o) => o.Id === order.Id);
|
|
489
|
+
if (!found) throw new Rejection('NotFound', null, `No order ${order.Id}.`);
|
|
490
|
+
if (order.Version !== found.Version) throw new Rejection('Conflict', 'Version', 'The order was changed since it was read.');
|
|
491
|
+
checkCustomer(order);
|
|
492
|
+
Object.assign(found, order, { Version: found.Version + 1 });
|
|
493
|
+
return found;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function checkCustomer(order: Order): void {
|
|
498
|
+
if (!order.Customer.trim()) throw new Rejection('Invalid', 'Customer', 'Customer must not be empty.');
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function child(element: Element, name: string): Element | undefined {
|
|
502
|
+
return Array.from(element.children).find((c) => c.localName === name);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function readOrder(element: Element | undefined): Order {
|
|
506
|
+
if (!element) throw new Rejection('Invalid', null, 'The request holds no order.');
|
|
507
|
+
const text = (name: string) => child(element, name)?.textContent ?? '';
|
|
508
|
+
return {
|
|
509
|
+
Id: Number(text('Id')),
|
|
510
|
+
Version: Number(text('Version')),
|
|
511
|
+
Customer: text('Customer'),
|
|
512
|
+
Amount: Number(text('Amount')),
|
|
513
|
+
Shipped: text('Shipped') === 'true',
|
|
514
|
+
Lines: Array.from(child(element, 'Lines')?.children ?? []).map((l) => ({
|
|
515
|
+
Product: child(l, 'Product')?.textContent ?? '',
|
|
516
|
+
Quantity: Number(child(l, 'Quantity')?.textContent ?? '0'),
|
|
517
|
+
})),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// DataContractSerializer order: the fields of the base class first, the rest alphabetical.
|
|
522
|
+
function writeOrder(o: Order): string {
|
|
523
|
+
const lines = o.Lines.map((l) => `<a:OrderLine><a:Product>${escapeXml(l.Product)}</a:Product><a:Quantity>${l.Quantity}</a:Quantity></a:OrderLine>`).join('');
|
|
524
|
+
return `<a:Id>${o.Id}</a:Id><a:Version>${o.Version}</a:Version><a:Amount>${o.Amount}</a:Amount><a:Customer>${escapeXml(o.Customer)}</a:Customer><a:Lines>${lines}</a:Lines><a:Shipped>${o.Shipped}</a:Shipped>`;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function respond(name: string, inner: string): { status: number; body: string } {
|
|
528
|
+
const result = `<${name}Result xmlns:a="${SHOP}" xmlns:i="${XSI}">${inner}</${name}Result>`;
|
|
529
|
+
return { status: 200, body: envelope(`<${name}Response xmlns="http://tempuri.org/">${result}</${name}Response>`) };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function fault(rejection: Rejection): { status: number; body: string } {
|
|
533
|
+
const field = rejection.field ? `<Field>${escapeXml(rejection.field)}</Field>` : '<Field i:nil="true"/>';
|
|
534
|
+
const detail = `<detail><OrderFault xmlns="${SHOP}" xmlns:i="${XSI}">${field}<Kind>${rejection.kind}</Kind></OrderFault></detail>`;
|
|
535
|
+
return { status: 500, body: envelope(`<s:Fault><faultcode>s:Client</faultcode><faultstring>${escapeXml(rejection.message)}</faultstring>${detail}</s:Fault>`) };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function envelope(content: string): string {
|
|
539
|
+
return `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body>${content}</s:Body></s:Envelope>`;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function escapeXml(text: string): string {
|
|
543
|
+
return text.replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!);
|
|
544
|
+
}
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
`pretty-xml.ts`
|
|
548
|
+
|
|
549
|
+
```tsx
|
|
550
|
+
/** An XML document with one element per line, indented, for reading; text stays escaped. */
|
|
551
|
+
export function prettyXml(xml: string): string {
|
|
552
|
+
const declaration = xml.startsWith('<?xml') ? `${xml.slice(0, xml.indexOf('?>') + 2)}\n` : '';
|
|
553
|
+
const root = new DOMParser().parseFromString(xml, 'text/xml').documentElement;
|
|
554
|
+
return declaration + writeElement(root, 0);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function writeElement(element: Element, depth: number): string {
|
|
558
|
+
const pad = ' '.repeat(depth);
|
|
559
|
+
const attributes = Array.from(element.attributes)
|
|
560
|
+
.map((a) => ` ${a.name}="${escapeXml(a.value)}"`)
|
|
561
|
+
.join('');
|
|
562
|
+
const name = element.tagName;
|
|
563
|
+
if (element.firstElementChild) {
|
|
564
|
+
const children = Array.from(element.children)
|
|
565
|
+
.map((c) => writeElement(c, depth + 1))
|
|
566
|
+
.join('\n');
|
|
567
|
+
return `${pad}<${name}${attributes}>\n${children}\n${pad}</${name}>`;
|
|
568
|
+
}
|
|
569
|
+
if (!element.childNodes.length) return `${pad}<${name}${attributes}/>`;
|
|
570
|
+
return `${pad}<${name}${attributes}>${escapeXml(element.textContent ?? '')}</${name}>`;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function escapeXml(text: string): string {
|
|
574
|
+
return text.replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!);
|
|
575
|
+
}
|
|
576
|
+
```
|