Browser Client App
The page talks OData v4 to the service itself, with the browser client of Atscript OData. There is no server of your own. The screens are Atscript UI tables and forms over the generated types.
Open the app · Source: atscript-odata-demo-browser
Run it
git clone https://github.com/moostjs/atscript-odata-demo-browser
cd atscript-odata-demo-browser
pnpm install
pnpm devIn development, Vite proxies the service path /odata/v4/roastery to the public service. To use a local service, set ODATA_URL in the shell or in .env.local:
ODATA_URL=http://localhost:4010/odata/v4/roastery pnpm devIt is the same service, so nothing is regenerated.
The layout
atscript.config.ts plugins; the OData service
vite.config.ts the proxy to ODATA_URL
src/
generated/ GENERATED: $metadata, types, model
model/roastery.ui.as YOURS: annotate blocks
odata.ts YOURS: the client factory
main.ts YOURS: live $metadata, then mount
routes.ts, screens/ YOURS: the screens and their typed configs
components/, lib/ YOURS: toolbar, form, cells, toasts, the dialog
env.d.ts YOURS: declare type decimal = stringNever edit src/generated/: pnpm generate and the build write it. Your changes go into src/model/. env.d.ts declares the decimal of the generated types as a string (see Type Safety).
From $metadata to types
The generate script saves the service's $metadata:
atscript-odata fetch https://demo.odata.atscript.dev/odata/v4/roastery \
--out src/generated/roastery.xml --language ENThe OData plugin in atscript.config.ts turns it into types on every build:
odataPlugin({
services: [{ name: "roastery", metadata: "generated/roastery.xml" }],
}),src/generated/ then holds roastery.xml, the language it was read in (roastery.value-helps.json), roastery.as (an interface per entity set and code list, with labels, value helps and texts) and roastery.model.gen.ts (the service model and the operation handles). See Generating Models.
Your UI hints
What the service does not say, the app adds in src/model/roastery.ui.as: column widths, the form grid, hidden fields and custom cells. The coffee's ID column draws the bag:
annotate Coffee {
@ui.table.component 'coffee'
@ui.table.selectWith 'origin_code'
@ui.table.selectWith 'process_code'
@ui.table.selectWith 'roastLevel'
ID
}'coffee' names CoffeeCell.vue, and selectWith reads the fields the bag is drawn from, even when their columns are hidden. The blocks change the generated types in place, so a regeneration keeps them. See Enrich the generated types and Atscript UI: custom cells.
The wiring
src/odata.ts makes the OData client factory Atscript UI's source of data:
import "./model/roastery.ui.as"; // the app's annotate blocks, before the types are used
export const odata = createODataClientFactory({
services: [{ baseUrl: "/odata/v4/roastery", model, types: roastery, fetch: serviceFetch }],
});
setDefaultClientFactory(odata);A table or form is given odata.urlOf(roastery.Coffee), /odata/roastery/Coffees: a name the factory answers, not a URL that is fetched. The calls go to baseUrl. In code, odata.client(roastery.Order) is the set's client, typed by the type. src/main.ts reads the live $metadata before the first screen renders:
await odata.connect().catch(() => (service.down = true));See Browser Client, Typed clients and Live Metadata.
Calls made together go out together
The Overview makes three aggregate() calls and calls the service's bestsellers function at once, so the four leave as one $batch: open the network tab to see it. From OverviewScreen.vue:
const orders = odata.client(roastery.Order);
const overview = Promise.all([
orders.aggregate({
controls: { $groupBy: ["status_code", "statusName"], $select: ["status_code", "statusName", { $fn: "count", $field: "*", $as: "orders" }] },
}),
orders.aggregate({ … }), // revenue by month
odata.client(roastery.Coffee).aggregate({ … }), // stock by origin
odata.call(ops.bestsellers, { params: { weeks: 12, count: 5 } }),
]);Each result is typed: the first call's rows have status_code, statusName and orders, and bestsellers answers roastery.Coffee[]. See Batching and Aggregation.
Typed by the generated types
The app passes generated types where it can, so a misspelled field is a compile error. src/routes.ts configures each list screen with a ListConfig<R>, its field names checked against the rows' type:
const coffees: ListConfig<roastery.Coffee> = {
path: "/coffees",
type: roastery.Coffee,
title: "Coffees",
name: "name",
columns: ["ID", "origin_code", "process_code", "roastLevel", "score", "price", "stockKg"],
filters: ["origin_code", "process_code", "active"],
// …
};ListScreen.vue gets the table's URL from the type, odata.urlOf(props.screen.type). The form dialog of src/lib/dialog.ts is typed by the form's type: its submit gets that type's values. See Type Safety.
Operations
The service's bound operations appear with no code: Retire and Stock value in a coffee's row menu, Reprice all in the Coffees toolbar, and Confirm, Ship and Cancel order where an order's status allows them. Several selected rows go out as one $batch, each row in a changeset of its own, so a refused row fails alone.
Receive beans… is a row action of the app's own over the unbound action receiveBeans, in src/routes.ts. Its dialog is a form of the generated receiveBeansParams, and its submit calls the action by its handle, which checks the parameters:
openDialog({
title: `Receive beans · ${row.name}`,
submitText: "Receive",
type: roastery.receiveBeansParams,
data: { coffee: Number(row.ID) },
async submit(params) {
toast("good", "Receive beans", (await odata.call(ops.receiveBeans, { params })).message);
},
});See Operations in the browser and Several rows.
On your own service
- Put its URL in the
generatescript ofpackage.json, and runpnpm generate. - Set
ODATA_URLto it. - If its path is not
/odata/v4/roastery(for example/sap/opu/odata/sap/…), changebaseUrlinsrc/odata.tsand the proxied path invite.config.ts. - Write
annotateblocks and screens for its types.
The page calls the service on its own origin, with the user's session: see Reaching the service. The Quick Start builds the same setup from scratch.