cap-ui5-ts-utils
v0.0.5
Published
Utilities for CAP and UI5 development with TypeScript.
Readme
cap-ui5-ts-utils
A collection of TypeScript utility functions for SAP UI5 and Fiori Elements development, designed to provide end-to-end type safety in interaction with OData services, common UI5 controls and CAP service calls.
Note: This package is designed to work with cds-typer as the basis for types. You can use your own types for cds services and entites if they match the signatures of those in cds-typer.
Features
- Action Invoker – Invoke actions with dynamically typed parameters in CAP services.
- Typed Event Parameters – Extract typed parameters from CDS functions.
- PickProperties – Pick properties of a base type, narrowing them if needed.
Installation
npm i cap-ui5-ts-utilsUsage
Import Utilities
import { genActionInvoker } from "cap-ui5-ts-utils";genActionInvoker - Invoking a CAP Service Action
In a normal controller:
// MyController.controller.ts
import { genActionInvoker } from "cap-ui5-ts-utils";
import MyService from "#cds-models/MyService";
const invokeAction = genActionInvoker<MyService>();
async function myHandler(api: ExtensionAPI) {
// Shows only actions available to `MyService`
await invokeAction(api, "myServiceAction", {
foo: "foo",
bar: "bar",
});
}In an extended ObjectPage controller:
// MyObjectPage.controller.ts
import { genActionInvoker } from "cap-ui5-ts-utils";
import MyService from "#cds-models/MyService";
export default class MyObjectPage extends ControllerExtension<ExtensionAPI> {
private invokeAction = genActionInvoker<MyService>();
public onMyEvent(this: TicketStatus) {
const extensionAPI = this.base.getExtensionAPI();
this.invokeAction(extensionAPI, "myServiceAction", {
foo: "foo",
bar: "bar",
}).then(() => {
// Do more stuff
});
}
}EventParams - Typed Event Parameters
// ./srv/myService.ts
import { EventParams } from "cap-ui5-ts-utils";
import { myFunction } from "#cds-models/MyService";
// Type check is done on the data obejct
const data: EventParams<typeof myFunction> = {
foo: "foo",
bar: "bar",
};
// ⚠️ No type check is done in .send()
await this.send(myFunction, data);PickProperties - Pick and Narrow Properties
// Pick parameters and narrow them
type MyTypeParams = PickParams< MyType,
"foo", // Inherit from MyType
"bar" | "baz", // Required but can be null
"qux" // Required and can't be null
>;
const myObject: MyTypeParams;
// Narrow properties and inherit all others
type MyOtherTypeParams = PickParams<
MyOtherType,
never,
"bar"
> & MyOtherType;
const myOtherObject: MyOtherType;License
ISC
