
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
enonic-types
Advanced tools
[!NOTE] There now exists official TypeScript-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.
You can install individual packages with support for Enonic libraries like this:
You can find the complete list of supported packages on npm.
npm i --save-dev @item-enonic-types/global
npm i --save-dev @item-enonic-types/lib-menu
We recommend using starter-tsup as the base of your XP-project.
To add the TypeScript-types you need to update your tsconfig.json with the following:
{
"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:
{
"compilerOptions": {
"paths": {
+ "/lib/tineikt/freemarker": ["./node_modules/@item-enonic-types/lib-freemarker"]
}
}
}
We recommend using this library together with the 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.
We have an Enonic service that returns an article by id.
import type { Article } from "../../site/content-types"; // 1
import { get as getOne, type Content } from "/lib/xp/content"; // 2
type ContentArticle = Content<Article, "my.awesome.project:Article">;
export function get(req: XP.Request): XP.Response { // 3
const content = getOne<ContentArticle>({
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<T>(value: T): asserts value is NonNullable<T> {
if (value === undefined || value === null) {
throw new Error(`${value} is not defined`);
}
}
type Article = { ... }
generated by xp-codegen-plugin.XP.Request
and XP.Response
to control the shape of our controller.content
is of the type Content<Article> | null
, but we can "assert" that it is not nullable (or throw an exception here if it is).FAQs
TypeScript types for Enonic XP
The npm package enonic-types receives a total of 70 weekly downloads. As such, enonic-types popularity was classified as not popular.
We found that enonic-types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.