Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/itemconsulting/enonic-types
TypeScript types for Enonic XP
https://github.com/itemconsulting/enonic-types
enonic-xp typescript typings
Last synced: 2 months ago
JSON representation
TypeScript types for Enonic XP
- Host: GitHub
- URL: https://github.com/itemconsulting/enonic-types
- Owner: ItemConsulting
- License: mit
- Created: 2019-11-04T09:24:53.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-02T07:06:34.000Z (9 months ago)
- Last Synced: 2024-11-06T23:03:05.365Z (3 months ago)
- Topics: enonic-xp, typescript, typings
- Homepage:
- Size: 868 KB
- Stars: 8
- Watchers: 15
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeScript types for Enonic XP
> [!NOTE]
> There now exists [official TypeScript-types](https://www.npmjs.com/org/enonic-types) from Enonic.
> The new purpose of this library will be to provide types for all the libraries that doesn't have official support yet.## Installing individual packages
You can install individual packages with support for Enonic libraries like this:
You can find the [complete list of supported packages on npm](https://www.npmjs.com/org/item-enonic-types).
```bash
npm i --save-dev @item-enonic-types/global
npm i --save-dev @item-enonic-types/lib-menu
```## Update tsconfig.json
We recommend using [starter-tsup](https://github.com/enonic/starter-tsup) as the base of your XP-project.
To add the TypeScript-types you need to update your *tsconfig.json* with the following:
```json
{
"compilerOptions": {
"target": "es5",
"strict": true,
"sourceMap": true,
"allowJs": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"module": "commonjs",
"moduleResolution": "node",
"types": ["@item-enonic-types/global"],
"rootDirs": [
"./src/main/resources",
"./.xp-codegen"
],
"paths": {
"/lib/xp/*": ["./node_modules/@enonic-types/lib-*"],
"/lib/*": [ "./node_modules/@item-enonic-types/lib-*" ,"./src/main/resources/lib/*"],
"/site/*": ["./src/main/resources/site/*" ,"./.xp-codegen/site/*"]
}
},
"include": [
"./.xp-codegen/**/*",
"./src/main/resources/**/*"
],
"exclude": ["./build/*"]
}
```Note that individual packages that are not directly under `"/lib/..."` needs to be mapped separately.
An example is Freemarker:
```diff
{
"compilerOptions": {
"paths": {
+ "/lib/tineikt/freemarker": ["./node_modules/@item-enonic-types/lib-freemarker"]
}
}
}
```## Code generation
We recommend using this library together with the [xp-codegen-plugin](https://github.com/ItemConsulting/xp-codegen-plugin) Gradle plugin. *xp-codegen-plugin* will create a TypeScript `type` for your content-types. Those interfaces will be very useful together with this library.
## Example
We have an Enonic service that returns an article by id.
```typescript
import type { Article } from "../../site/content-types"; // 1
import { get as getOne, type Content } from "/lib/xp/content"; // 2type ContentArticle = Content;
export function get(req: XP.Request): XP.Response { // 3
const content = getOne({
key: req.params.id!
});assertIsDefined(content); // 4
const article: Article = content.data;
return {
status: 200,
body: article
}
}/**
* Create this function in a utilities file of your choice...
*/
function assertIsDefined(value: T): asserts value is NonNullable {
if (value === undefined || value === null) {
throw new Error(`${value} is not defined`);
}
}
```1. We import an `type Article = { ... }` generated by [xp-codegen-plugin](https://github.com/ItemConsulting/xp-codegen-plugin).
2. The standard XP-libraries are mapped to their paths by the change to *tsconfig.json*.
3. We use `XP.Request` and `XP.Response` to control the shape of our controller.
4. `content` is of the type `Content | null`, but we can "assert" that it is not nullable (or throw an exception here if it is).
## Supported libraries* [CacheLibrary](./packages/cache)
* [CronLibrary](./packages/cron)
* [ExplorerLibrary](./packages/explorer)
* [FreeMarkerLibrary](./packages/freemarker)
* [GraphQLLibrary](./packages/graphql)
* [GraphQLPlaygroundLibrary](./packages/graphql-playground)
* [HttpClientLibrary](./packages/http-client)
* [MenuLibrary](./packages/menu)
* [MustacheLibrary](./packages/mustache)
* [NotificationsLibrary](./packages/notifications)
* [QRCodeLibrary](./packages/qrcode)
* [RecaptchaLibrary](./packages/recaptcha)
* [RouterLibrary](./packages/router)
* [SqlLibrary](./packages/sql)
* [StaticLibrary](./packages/static)
* [TestingLibrary](./packages/testing)
* [TextEncodingLibrary](./packages/text-encoding)
* [ThymeleafLibrary](./packages/thymeleaf)
* [XsltLibrary](./packages/xslt)